Пример #1
0
        private IPythonModule GetModule(QualifiedNameParts parts)
        {
            if (parts.ModuleName == Module.Name)
            {
                return(Module);
            }
            using (_moduleReentrancy.Push(parts.ModuleName, out var reentered)) {
                if (reentered)
                {
                    return(null);
                }
                // Here we do not call GetOrLoad since modules references here must
                // either be loaded already since they were required to create
                // persistent state from analysis. Also, occasionally types come
                // from the stub and the main module was never loaded. This, for example,
                // happens with io which has member with mmap type coming from mmap
                // stub rather than the primary mmap module.
                var m = parts.IsStub
                    ? Module.Interpreter.TypeshedResolution.GetImportedModule(parts.ModuleName)
                    : Module.Interpreter.ModuleResolution.GetImportedModule(parts.ModuleName);

                if (m != null)
                {
                    return(parts.ObjectType == ObjectType.VariableModule ? new PythonVariableModule(m) : m);
                }
                return(null);
            }
        }
Пример #2
0
 private static void GetObjectTypeFromPrefix(string qualifiedName, ref QualifiedNameParts parts, out int prefixOffset)
 {
     prefixOffset = 2;
     if (qualifiedName.StartsWith("i:"))
     {
         parts.ObjectType = ObjectType.Instance;
     }
     else if (qualifiedName.StartsWith("m:"))
     {
         parts.ObjectType = ObjectType.Module;
     }
     else if (qualifiedName.StartsWith("p:"))
     {
         parts.ObjectType = ObjectType.VariableModule;
     }
     else if (qualifiedName.StartsWith("b:"))
     {
         parts.ObjectType = ObjectType.BuiltinModule;
     }
     else if (qualifiedName.StartsWith("t:"))
     {
         parts.ObjectType = ObjectType.Type;
     }
     else
     {
         // Unprefixed name is typically an argument to another type like Union[int, typing:Any]
         parts.ObjectType = ObjectType.Type;
         prefixOffset     = 0;
     }
 }
Пример #3
0
        private static void GetModuleNameAndMembers(string qualifiedName, ref QualifiedNameParts parts, int prefixOffset)
        {
            // Strip the prefix, turning i:module:A.B.C into module:A.B.C
            var typeName = qualifiedName.Substring(prefixOffset);

            var moduleSeparatorIndex = typeName.IndexOf(':');

            if (moduleSeparatorIndex < 0)
            {
                switch (parts.ObjectType)
                {
                case ObjectType.Type:
                case ObjectType.Instance:
                    // No module name means built-in type like 'int' or 'i:str'.
                    parts.ModuleName  = @"builtins";
                    parts.MemberNames = typeName == "..." ? new[] { "ellipsis" } : typeName.Split('.').ToArray();
                    break;

                default:
                    parts.ModuleName  = typeName;
                    parts.MemberNames = Array.Empty <string>();
                    DetermineModuleType(ref parts);
                    break;
                }
                return;
            }

            // Extract module name and member names, of any.
            parts.ModuleName = typeName.Substring(0, moduleSeparatorIndex);
            var memberNamesOffset = parts.ModuleName.Length + 1;

            parts.MemberNames = GetTypeNames(typeName.Substring(memberNamesOffset), '.');

            DetermineModuleType(ref parts);
        }
Пример #4
0
 private static void DetermineModuleType(ref QualifiedNameParts parts)
 {
     if (parts.ModuleName.EndsWith("(stub)"))
     {
         parts.ModuleName = parts.ModuleName.Substring(0, parts.ModuleName.Length - 6);
         parts.IsStub     = true;
     }
 }
Пример #5
0
        /// <summary>
        /// Splits qualified type name in form of i:A(3.6).B.C into parts. as well as determines if
        /// qualified name designates instance (prefixed with 'i:').
        /// </summary>
        /// <param name="qualifiedName">Qualified name to split. May include instance prefix.</param>
        /// <param name="parts">Qualified name parts.</param>
        public static bool DeconstructQualifiedName(string qualifiedName, out QualifiedNameParts parts)
        {
            parts = new QualifiedNameParts();
            if (string.IsNullOrEmpty(qualifiedName))
            {
                return(false);
            }

            GetObjectTypeFromPrefix(qualifiedName, ref parts, out var prefixOffset);
            GetModuleNameAndMembers(qualifiedName, ref parts, prefixOffset);

            return(!string.IsNullOrEmpty(parts.ModuleName));
        }
        /// <summary>
        /// Splits qualified type name in form of i:A(3.6).B.C into parts. as well as determines if
        /// qualified name designates instance (prefixed with 'i:').
        /// </summary>
        /// <param name="qualifiedName">Qualified name to split. May include instance prefix.</param>
        /// <param name="parts">Qualified name parts.</param>
        public static bool DeconstructQualifiedName(string qualifiedName, out QualifiedNameParts parts)
        {
            parts = new QualifiedNameParts();
            if (string.IsNullOrEmpty(qualifiedName))
            {
                return(false);
            }

            var index = qualifiedName.IndexOf('$');

            if (index > 0)
            {
                parts.ModuleId = qualifiedName.Substring(index + 1);
                qualifiedName  = qualifiedName.Substring(0, index);
            }

            GetObjectTypeFromPrefix(qualifiedName, ref parts, out var prefixOffset);
            GetModuleNameAndMembers(qualifiedName, ref parts, prefixOffset);

            return(!string.IsNullOrEmpty(parts.ModuleName));
        }