public void SetInScope(string name, IMember value, bool mergeWithExisting = true, Dictionary <string, IMember> scope = null)
        {
            Debug.Assert(_scopes.Count > 0);
            if (value == null && _scopes.Count == 0)
            {
                return;
            }
            var s = scope ?? _scopes.Peek();

            if (value == null)
            {
                s.Remove(name);
                return;
            }
            if (mergeWithExisting && s.TryGetValue(name, out IMember existing) && existing != null)
            {
                if (IsUnknown(existing))
                {
                    s[name] = value;
                }
                else if (!IsUnknown(value))
                {
                    s[name] = AstPythonMultipleMembers.Combine(existing, value);
                }
            }
            else
            {
                s[name] = value;
            }
        }
Example #2
0
        public void SetInScope(string name, IMember value, bool mergeWithExisting = true)
        {
            var s = _scopes.Peek();

            if (value == null)
            {
                s.Remove(name);
                return;
            }
            if (mergeWithExisting && s.TryGetValue(name, out IMember existing) && existing != null)
            {
                if (IsUnknown(existing))
                {
                    s[name] = value;
                }
                else if (!IsUnknown(value))
                {
                    s[name] = AstPythonMultipleMembers.Combine(existing, value);
                }
            }
            else
            {
                s[name] = value;
            }
        }
        private IMember GetValueFromConditional(ConditionalExpression expr, LookupOptions options)
        {
            if (expr == null)
            {
                return(null);
            }

            return(AstPythonMultipleMembers.Combine(
                       GetValueFromExpression(expr.TrueExpression),
                       GetValueFromExpression(expr.FalseExpression)
                       ));
        }
Example #4
0
        public void SetInScope(string name, IMember value, bool mergeWithExisting = true)
        {
            var s = _scopes.Peek();

            if (mergeWithExisting && s.TryGetValue(name, out IMember existing) && existing != null)
            {
                s[name] = AstPythonMultipleMembers.Combine(existing, value);
            }
            else
            {
                s[name] = value;
            }
        }
        private IMember GetValueFromConditional(ConditionalExpression expr, LookupOptions options)
        {
            if (expr == null)
            {
                return(null);
            }

            var trueValue  = GetValueFromExpression(expr.TrueExpression);
            var falseValue = GetValueFromExpression(expr.FalseExpression);

            return(trueValue != null || falseValue != null
                ? AstPythonMultipleMembers.Combine(trueValue, falseValue)
                : null);
        }
Example #6
0
        /// <summary>
        /// Soft-casts a member to a type, extracting the type from
        /// a multi-member object if possible.
        /// </summary>
        private static IPythonType AsIPythonType(IMember m)
        {
            if (m is IPythonType t)
            {
                return(t);
            }

            if (m is IPythonMultipleMembers mm)
            {
                return(AstPythonMultipleMembers.CreateAs <IPythonType>(mm.Members));
            }

            return(null);
        }
Example #7
0
        public override IPythonType LookupName(string name)
        {
            var m = _scope.LookupNameInScopes(name, NameLookupContext.LookupOptions.Global | NameLookupContext.LookupOptions.Builtins);

            if (m is IPythonMultipleMembers mm)
            {
                m = (IMember)AstPythonMultipleMembers.CreateAs <IPythonType>(mm.Members) ??
                    AstPythonMultipleMembers.CreateAs <IPythonModule>(mm.Members);
            }
            if (m is IPythonModule mod)
            {
                // Wrap the module in an IPythonType interface
                return(new ModuleType(mod));
            }
            return(m as IPythonType);
        }
        public IMember GetMember(IModuleContext context, string name)
        {
            IMember res = null;

            foreach (var m in _modules)
            {
                var r = m.GetMember(context, name);
                if (res == null)
                {
                    res = r;
                }
                else
                {
                    res = AstPythonMultipleMembers.Combine(res, r);
                }
            }
            return(res);
        }
        public IPythonType GetTypeFromValue(IMember value)
        {
            if (value == null)
            {
                return(null);
            }

            var type = (value as IPythonConstant)?.Type;

            if (type != null)
            {
                return(type);
            }

            switch (value.MemberType)
            {
            case PythonMemberType.Class:
                return(Interpreter.GetBuiltinType(BuiltinTypeId.Type));

            case PythonMemberType.Delegate:
            case PythonMemberType.DelegateInstance:
            case PythonMemberType.Function:
                return(Interpreter.GetBuiltinType(BuiltinTypeId.Function));

            case PythonMemberType.Method:
                return(Interpreter.GetBuiltinType(BuiltinTypeId.BuiltinMethodDescriptor));

            case PythonMemberType.Enum:
            case PythonMemberType.EnumInstance:
                break;

            case PythonMemberType.Module:
                return(Interpreter.GetBuiltinType(BuiltinTypeId.Module));

            case PythonMemberType.Namespace:
                return(Interpreter.GetBuiltinType(BuiltinTypeId.Object));

            case PythonMemberType.Event:
                break;
            }

            IPythonFunction f;

            if ((f = value as IPythonFunction ?? (value as IPythonBoundFunction)?.Function) != null)
            {
                if (f.IsStatic)
                {
                    return(Interpreter.GetBuiltinType(BuiltinTypeId.StaticMethod));
                }
                else if (f.IsClassMethod)
                {
                    return(Interpreter.GetBuiltinType(BuiltinTypeId.ClassMethod));
                }
                return(Interpreter.GetBuiltinType(BuiltinTypeId.Function));
            }

            if (value is IBuiltinProperty prop)
            {
                return(prop.Type ?? Interpreter.GetBuiltinType(BuiltinTypeId.Property));
            }
            else if (value.MemberType == PythonMemberType.Property)
            {
                return(Interpreter.GetBuiltinType(BuiltinTypeId.Property));
            }

            if (value is IPythonMethodDescriptor)
            {
                return(Interpreter.GetBuiltinType(BuiltinTypeId.BuiltinMethodDescriptor));
            }

            if (value is IPythonMultipleMembers mm)
            {
                return(AstPythonMultipleMembers.CreateAs <IPythonType>(mm.Members));
            }

            if (value is IPythonType)
            {
                return(Interpreter.GetBuiltinType(BuiltinTypeId.Type));
            }

#if DEBUG
            var implements = string.Join(", ", new[] { value.GetType().FullName }.Concat(value.GetType().GetInterfaces().Select(i => i.Name)));
            Debug.Fail("Unhandled type() value: " + implements);
#endif
            return(null);
        }
Example #10
0
        public async Task <TryImportModuleResult> TryImportModuleAsync(string name, PathResolverSnapshot pathResolver, IReadOnlyList <string> typeStubPaths, bool mergeTypeStubPackages, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(TryImportModuleResult.ModuleNotFound);
            }

            Debug.Assert(!name.EndsWithOrdinal("."), $"{name} should not end with '.'");

            // Handle builtins explicitly
            if (name == BuiltinModuleName)
            {
                Debug.Fail($"Interpreters must handle import {name} explicitly");
                return(TryImportModuleResult.NotSupported);
            }

            // Return any existing module
            if (_modules.TryGetValue(name, out var module) && module != null)
            {
                if (module is SentinelModule sentinelModule)
                {
                    // If we are importing this module on another thread, allow
                    // time for it to complete. This does not block if we are
                    // importing on the current thread or the module is not
                    // really being imported.
                    try {
                        module = await sentinelModule.WaitForImportAsync(cancellationToken);
                    } catch (OperationCanceledException) {
                        _log?.Log(TraceLevel.Warning, "ImportTimeout", name);
                        return(TryImportModuleResult.Timeout);
                    }

                    if (module is SentinelModule)
                    {
                        _log?.Log(TraceLevel.Warning, "RecursiveImport", name);
                    }
                }
                return(new TryImportModuleResult(module));
            }

            // Set up a sentinel so we can detect recursive imports
            var sentinelValue = new SentinelModule(name, true);

            if (!_modules.TryAdd(name, sentinelValue))
            {
                // Try to get the new module, in case we raced with a .Clear()
                if (_modules.TryGetValue(name, out module) && !(module is SentinelModule))
                {
                    return(new TryImportModuleResult(module));
                }
                // If we reach here, the race is too complicated to recover
                // from. Signal the caller to try importing again.
                _log?.Log(TraceLevel.Warning, "RetryImport", name);
                return(TryImportModuleResult.NeedRetry);
            }

            // Do normal searches
            if (!string.IsNullOrEmpty(_configuration?.InterpreterPath))
            {
                try {
                    module = ImportFromSearchPaths(name, pathResolver);
                } catch (OperationCanceledException) {
                    _log?.Log(TraceLevel.Error, "ImportTimeout", name, "ImportFromSearchPaths");
                    return(TryImportModuleResult.Timeout);
                }
            }

            if (module == null)
            {
                module = _astModuleCache.ImportFromCache(name, _interpreter);
            }

            // Also search for type stub packages if enabled and we are not a blacklisted module
            if (module != null && typeStubPaths != null && module.Name != "typing")
            {
                var tsModule = ImportFromTypeStubs(module.Name, typeStubPaths, pathResolver);
                if (tsModule != null)
                {
                    module = mergeTypeStubPackages ? AstPythonMultipleMembers.CombineAs <IPythonModule>(module, tsModule) : tsModule;
                }
            }

            // Replace our sentinel, or if we raced, get the current
            // value and abandon the one we just created.
            if (!_modules.TryUpdate(name, module, sentinelValue))
            {
                // Try to get the new module, in case we raced
                if (_modules.TryGetValue(name, out module) && !(module is SentinelModule))
                {
                    return(new TryImportModuleResult(module));
                }
                // If we reach here, the race is too complicated to recover
                // from. Signal the caller to try importing again.
                _log?.Log(TraceLevel.Warning, "RetryImport", name);
                return(TryImportModuleResult.NeedRetry);
            }
            sentinelValue.Complete(module);

            return(new TryImportModuleResult(module));
        }