Beispiel #1
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);
        }
Beispiel #2
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);
        }