Esempio n. 1
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));
        }
Esempio n. 2
0
        private IPythonModule ImportModuleOrRetry(string name, out bool retry)
        {
            retry = false;
            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }

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

            // Handle builtins explicitly
            if (name == BuiltinModuleName)
            {
                if (_builtinModule == null)
                {
                    _modules[BuiltinModuleName] = _builtinModule = new AstBuiltinsPythonModule(_factory.LanguageVersion);
                    _builtinModuleNames         = null;
                    _builtinModule.Imported(this);
                    var bmn = ((AstBuiltinsPythonModule)_builtinModule).GetAnyMember("__builtin_module_names") as AstPythonStringLiteral;
                    _builtinModuleNames = bmn?.Value?.Split(',') ?? Array.Empty <string>();
                }
                return(_builtinModule);
            }

            IPythonModule mod;

            // Return any existing module
            if (_modules.TryGetValue(name, out mod) && mod != null)
            {
                if (mod is SentinelModule smod)
                {
                    // 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.
                    var newMod = smod.WaitForImport(5000);
                    if (newMod is SentinelModule)
                    {
                        _log?.Log(TraceLevel.Warning, "RecursiveImport", name);
                        mod = newMod;
                    }
                    else if (newMod == null)
                    {
                        _log?.Log(TraceLevel.Warning, "ImportTimeout", name);
                    }
                    else
                    {
                        mod = newMod;
                    }
                }
                return(mod);
            }

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

            if (!_modules.TryAdd(name, sentinalValue))
            {
                // Try to get the new module, in case we raced with a .Clear()
                if (_modules.TryGetValue(name, out mod) && !(mod is SentinelModule))
                {
                    return(mod);
                }
                // 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);
                retry = true;
                return(null);
            }

            // Do normal searches
            mod = ImportFromBuiltins(name) ?? ImportFromSearchPaths(name);

            // Replace our sentinel, or if we raced, get the current
            // value and abandon the one we just created.
            if (!_modules.TryUpdate(name, mod, sentinalValue))
            {
                // Try to get the new module, in case we raced
                if (_modules.TryGetValue(name, out mod) && !(mod is SentinelModule))
                {
                    return(mod);
                }
                // 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);
                retry = true;
                return(null);
            }

            sentinalValue.Complete(mod);
            sentinalValue.Dispose();
            return(mod);
        }