示例#1
0
        private bool TryImportModule(string modName, bool forceAbsolute, out ModuleReference moduleRef)
        {
            if (ProjectState.Limits.CrossModule > 0 &&
                ProjectState.ModulesByFilename.Count > ProjectState.Limits.CrossModule)
            {
                // too many modules loaded, disable cross module analysis by blocking
                // scripts from seeing other modules.
                moduleRef = null;
                return(false);
            }

            foreach (var name in PythonAnalyzer.ResolvePotentialModuleNames(_unit.ProjectEntry, modName, forceAbsolute))
            {
                foreach (var part in ModulePath.GetParents(name, includeFullName: false))
                {
                    ModuleReference parentRef;
                    if (ProjectState.Modules.TryImport(part, out parentRef))
                    {
                        parentRef.Module?.Imported(_unit);
                    }
                }

                if (ProjectState.Modules.TryImport(name, out moduleRef))
                {
                    return(true);
                }
            }

            _unit.DeclaringModule.AddUnresolvedModule(modName, forceAbsolute);

            moduleRef = null;
            return(false);
        }
示例#2
0
        public override bool Walk(FromImportStatement node)
        {
            var modName = node.Root.MakeString();

            if (modName == "__future__")
            {
                return(false);
            }

            if (node.Names == null)
            {
                return(false);
            }

            bool onlyImportModules = modName.EndsWith(".");

            var mod = new AstNestedPythonModule(
                _interpreter,
                modName,
                PythonAnalyzer.ResolvePotentialModuleNames(_module.Name, _filePath, modName, true).ToArray()
                );

            mod.Imported(_scope.Context);
            // Ensure child modules have been loaded
            mod.GetChildrenModules();

            try {
                for (int i = 0; i < node.Names.Count; ++i)
                {
                    if (!onlyImportModules)
                    {
                        if (node.Names[i].Name == "*")
                        {
                            foreach (var member in mod.GetMemberNames(_scope.Context))
                            {
                                var mem = mod.GetMember(_scope.Context, member) ?? new AstPythonConstant(
                                    _interpreter.GetBuiltinType(BuiltinTypeId.Unknown),
                                    mod.Locations.ToArray()
                                    );
                                _scope.SetInScope(member, mem);
                                (mem as IPythonModule)?.Imported(_scope.Context);
                            }
                            continue;
                        }
                        var n = node.AsNames?[i] ?? node.Names[i];
                        if (n != null)
                        {
                            var mem = mod.GetMember(_scope.Context, node.Names[i].Name) ?? new AstPythonConstant(
                                _interpreter.GetBuiltinType(BuiltinTypeId.Unknown),
                                GetLoc(n)
                                );
                            _scope.SetInScope(n.Name, mem);
                            (mem as IPythonModule)?.Imported(_scope.Context);
                        }
                    }
                }
            } catch (IndexOutOfRangeException) {
            }
            return(false);
        }
示例#3
0
        private bool TryImportModule(string modName, bool forceAbsolute, out ModuleReference moduleRef, out IReadOnlyList <string> remainingParts)
        {
            moduleRef      = null;
            remainingParts = null;

            if (ProjectState.Limits.CrossModule > 0 &&
                ProjectState.ModulesByFilename.Count > ProjectState.Limits.CrossModule)
            {
                // too many modules loaded, disable cross module analysis by blocking
                // scripts from seeing other modules.
                return(false);
            }

            var candidates = PythonAnalyzer.ResolvePotentialModuleNames(_unit.ProjectEntry, modName, forceAbsolute).ToArray();

            foreach (var name in candidates)
            {
                if (ProjectState.Modules.TryImport(name, out moduleRef))
                {
                    return(true);
                }
            }

            foreach (var name in candidates)
            {
                moduleRef = null;
                foreach (var part in ModulePath.GetParents(name, includeFullName: true))
                {
                    if (ProjectState.Modules.TryImport(part, out var mref))
                    {
                        moduleRef = mref;
                        if (part.Length < name.Length)
                        {
                            moduleRef.Module?.Imported(_unit);
                        }
                    }
                    else if (moduleRef != null)
                    {
                        Debug.Assert(moduleRef.Name.Length + 1 < name.Length, $"Expected {name} to be a child of {moduleRef.Name}");
                        if (moduleRef.Name.Length + 1 < name.Length)
                        {
                            remainingParts = name.Substring(moduleRef.Name.Length + 1).Split('.');
                        }
                        return(true);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(moduleRef?.Module != null);
        }
示例#4
0
        public override bool Walk(ImportStatement node)
        {
            if (node.Names == null)
            {
                return(false);
            }

            try {
                for (int i = 0; i < node.Names.Count; ++i)
                {
                    var n = node.AsNames?[i] ?? node.Names[i].Names[0];
                    if (n != null)
                    {
                        _scope.SetInScope(n.Name, new AstNestedPythonModule(
                                              _interpreter,
                                              n.Name,
                                              PythonAnalyzer.ResolvePotentialModuleNames(_module.Name, _scope.FilePath, n.Name, true).ToArray()
                                              ));
                    }
                }
            } catch (IndexOutOfRangeException) {
            }
            return(false);
        }
示例#5
0
        private bool TryImportModule(string modName, bool forceAbsolute, out ModuleReference moduleRef)
        {
            if (ProjectState.Limits.CrossModule != null &&
                ProjectState.ModulesByFilename.Count > ProjectState.Limits.CrossModule)
            {
                // too many modules loaded, disable cross module analysis by blocking
                // scripts from seeing other modules.
                moduleRef = null;
                return(false);
            }

            foreach (var name in PythonAnalyzer.ResolvePotentialModuleNames(_unit.ProjectEntry, modName, forceAbsolute))
            {
                if (ProjectState.Modules.TryGetValue(name, out moduleRef))
                {
                    return(true);
                }
            }

            _unit.DeclaringModule.AddUnresolvedModule(modName, forceAbsolute);

            moduleRef = null;
            return(false);
        }
示例#6
0
        public override bool Walk(ImportStatement node)
        {
            var m = _scope.Peek();

            if (m != null && node.Names != null)
            {
                try {
                    for (int i = 0; i < node.Names.Count; ++i)
                    {
                        var n = node.AsNames?[i] ?? node.Names[i].Names[0];
                        if (n != null)
                        {
                            m[n.Name] = new AstNestedPythonModule(
                                _interpreter,
                                n.Name,
                                PythonAnalyzer.ResolvePotentialModuleNames(_module.Name, _filePath, n.Name, true).ToArray()
                                );
                        }
                    }
                } catch (IndexOutOfRangeException) {
                }
            }
            return(false);
        }
示例#7
0
        public override bool Walk(FromImportStatement node)
        {
            var modName = node.Root.MakeString();

            if (modName == "__future__")
            {
                return(false);
            }

            if (node.Names == null)
            {
                return(false);
            }

            var mod = new AstNestedPythonModule(
                _interpreter,
                modName,
                PythonAnalyzer.ResolvePotentialModuleNames(_module.Name, _scope.FilePath, modName, true).ToArray()
                );

            foreach (var name in GetImportNames(node.Names, node.AsNames))
            {
                if (name.Key == "*")
                {
                    mod.Imported(_scope.Context);
                    // Ensure child modules have been loaded
                    mod.GetChildrenModules();
                    foreach (var member in mod.GetMemberNames(_scope.Context))
                    {
                        var mem = mod.GetMember(_scope.Context, member) ??
                                  new AstPythonConstant(_unknownType, mod.Locations.ToArray());
                        if (mem.MemberType == PythonMemberType.Unknown && WarnAboutUndefinedValues)
                        {
                            _log?.Log(TraceLevel.Warning, "UndefinedImport", modName, name);
                        }
                        _scope.SetInScope(member, mem);
                        (mem as IPythonModule)?.Imported(_scope.Context);
                    }
                }
                else
                {
                    IMember mem;
                    if (mod.IsLoaded)
                    {
                        mem = mod.GetMember(_scope.Context, name.Key) ??
                              new AstPythonConstant(_unknownType, GetLoc(name.Value));
                        if (mem.MemberType == PythonMemberType.Unknown && WarnAboutUndefinedValues)
                        {
                            _log?.Log(TraceLevel.Warning, "UndefinedImport", modName, name);
                        }
                        (mem as IPythonModule)?.Imported(_scope.Context);
                    }
                    else
                    {
                        mem = new AstNestedPythonModuleMember(name.Key, mod, _scope.Context, GetLoc(name.Value));
                    }
                    _scope.SetInScope(name.Value.Name, mem);
                }
            }

            return(false);
        }
示例#8
0
 internal void AddUnresolvedModule(string relativeModuleName, bool absoluteImports) {
     _unresolvedModules.UnionWith(PythonAnalyzer.ResolvePotentialModuleNames(_projectEntry, relativeModuleName, absoluteImports));
     _projectEntry.ProjectState.ModuleHasUnresolvedImports(this, true);
 }