예제 #1
0
        private static bool BuiltinModuleContainsMember(IModuleContext context, string name, IPythonModule interpModule)
        {
            var mem = interpModule.GetMember(context, name);

            if (mem != null)
            {
                if (IsExcludedBuiltin(interpModule, mem))
                {
                    // if a module imports a builtin and exposes it don't report it for purposes of adding imports
                    return(false);
                }

                IPythonMultipleMembers multiMem = mem as IPythonMultipleMembers;
                if (multiMem != null)
                {
                    foreach (var innerMem in multiMem.Members)
                    {
                        if (IsExcludedBuiltin(interpModule, innerMem))
                        {
                            // if something non-excludable aliased w/ something excluable we probably
                            // only care about the excluable (for example a module and None - timeit.py
                            // does this in the std lib)
                            return(false);
                        }
                    }
                }

                return(true);
            }
            return(false);
        }
예제 #2
0
        private async Task HandleModuleImportStarAsync(IPythonModule module, CancellationToken cancellationToken = default)
        {
            foreach (var memberName in module.GetMemberNames())
            {
                cancellationToken.ThrowIfCancellationRequested();

                var member = module.GetMember(memberName);
                if (member == null)
                {
                    Log?.Log(TraceEventType.Verbose, $"Undefined import: {module.Name}, {memberName}");
                }
                else if (member.MemberType == PythonMemberType.Unknown)
                {
                    Log?.Log(TraceEventType.Verbose, $"Unknown import: {module.Name}, {memberName}");
                }

                member = member ?? Eval.UnknownType;
                if (member is IPythonModule m)
                {
                    await ModuleResolution.ImportModuleAsync(m.Name, cancellationToken);
                }

                Eval.DeclareVariable(memberName, member, module.Location);
            }
        }
예제 #3
0
        private void AssignMemberFromModule(IPythonModule module, string typeName, IMember memb, List <object> indexTypes, Action <IPythonType> assign, bool addFixups)
        {
            if (memb == null)
            {
                IBuiltinPythonModule builtin;
                if ((builtin = module as IBuiltinPythonModule) != null)
                {
                    memb = builtin.GetAnyMember(typeName);
                }
                else
                {
                    memb = module.GetMember(null, typeName);
                }
            }

            IPythonType            type;
            CPythonMultipleMembers mm;

            if (memb == null)
            {
                if (addFixups)
                {
                    AddFixup(() => {
                        // Fixup 2: Type was not found in module
                        AssignMemberFromModule(module, typeName, null, indexTypes, assign, false);
                    });
                    return;
                }
                else
                {
                    // TODO: Maybe skip this to reduce noise in loaded database
                    AddObjectTypeFixup(assign);
                    return;
                }
            }
            else if ((type = memb as IPythonType) != null)
            {
                if (indexTypes != null)
                {
                    assign(new CPythonSequenceType(type, this, indexTypes));
                }
                else
                {
                    assign(type);
                }
            }
            else if ((mm = memb as CPythonMultipleMembers) != null)
            {
                mm.AssignTypes(this, assign);
            }
        }
예제 #4
0
        private static IPythonModule GetModuleFromDottedName(ImmutableArray <NameExpression> names, int position, IExpressionEvaluator eval)
        {
            IPythonModule module = null;
            var           index  = position >= 0 ? names.IndexOf(n => n.StartIndex <= position && position <= n.EndIndex) : names.Count - 1;

            if (index >= 0)
            {
                module = eval.Interpreter.ModuleResolution.GetImportedModule(names[0].Name);
                for (var i = 1; module != null && i <= index; i++)
                {
                    module = module.GetMember(names[i].Name) as IPythonModule;
                }
            }
            return(module);
        }
예제 #5
0
 private static IEnumerable <IPythonType> GetTestCaseClasses(IPythonModule module, IModuleContext context)
 {
     foreach (var name in module.GetMemberNames(context))
     {
         if (module.GetMember(context, name) is IPythonType cls)
         {
             foreach (var baseCls in cls.Mro.MaybeEnumerate())
             {
                 if (baseCls.Name == "TestCase" ||
                     baseCls.Name.StartsWithOrdinal("unittest.") && baseCls.Name.EndsWithOrdinal(".TestCase"))
                 {
                     yield return(cls);
                 }
             }
         }
     }
 }
예제 #6
0
        private void AssignMemberFromModule(IPythonModule module, string typeName, IMember memb, List<object> indexTypes, Action<IPythonType> assign, bool addFixups) {
            if (memb == null) {
                IBuiltinPythonModule builtin;
                if ((builtin = module as IBuiltinPythonModule) != null) {
                    memb = builtin.GetAnyMember(typeName);
                } else {
                    memb = module.GetMember(null, typeName);
                }
            }

            IPythonType type;
            CPythonMultipleMembers mm;
            if (memb == null) {
                if (addFixups) {
                    AddFixup(() => {
                        // Fixup 2: Type was not found in module
                        AssignMemberFromModule(module, typeName, null, indexTypes, assign, false);
                    });
                    return;
                } else {
                    // TODO: Maybe skip this to reduce noise in loaded database
                    AddObjectTypeFixup(assign);
                    return;
                }
            } else if ((type = memb as IPythonType) != null) {
                if (indexTypes != null) {
                    assign(new CPythonSequenceType(type, this, indexTypes));
                } else {
                    assign(type);
                }
            } else if ((mm = memb as CPythonMultipleMembers) != null) {
                mm.AssignTypes(this, assign);
            }
        }