示例#1
0
        public async Task <TryImportModuleResult> TryImportModuleAsync(
            string name,
            TryImportModuleContext context,
            CancellationToken cancellationToken
            )
        {
            IPythonModule module = null;

            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);
            }

            var            modules       = context?.ModuleCache;
            SentinelModule sentinalValue = null;

            if (modules != null)
            {
                // Return any existing module
                if (modules.TryGetValue(name, out module) && module != null)
                {
                    if (module 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.
                        try {
                            module = await smod.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
                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 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 = await ImportFromSearchPathsAsync(name, context, cancellationToken);
                } catch (OperationCanceledException) {
                    _log?.Log(TraceLevel.Error, "ImportTimeout", name, "ImportFromSearchPaths");
                    return(TryImportModuleResult.Timeout);
                }

                if (module == null)
                {
                    module = ImportFromBuiltins(name, context.BuiltinModule as AstBuiltinsPythonModule);
                }
            }
            if (module == null)
            {
                module = ImportFromCache(name, context);
            }

            if (modules != null)
            {
                if (sentinalValue == null)
                {
                    _log?.Log(TraceLevel.Error, "RetryImport", name, "sentinalValue==null");
                    Debug.Fail("Sentinal module was never created");
                    return(TryImportModuleResult.NeedRetry);
                }

                // Replace our sentinel, or if we raced, get the current
                // value and abandon the one we just created.
                if (!modules.TryUpdate(name, module, sentinalValue))
                {
                    // 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);
                }
                sentinalValue.Complete(module);
            }

            // Also search for type stub packages if enabled and we are not a blacklisted module
            if (module != null && context?.TypeStubPaths != null && module.Name != "typing")
            {
                var tsModule = await ImportFromTypeStubsAsync(module.Name, context, cancellationToken);

                if (tsModule != null)
                {
                    if (context.MergeTypeStubPackages)
                    {
                        module = AstPythonMultipleModules.Combine(module, tsModule);
                    }
                    else
                    {
                        module = tsModule;
                    }
                }
            }

            return(new TryImportModuleResult(module));
        }
        public TryImportModuleResult TryImportModule(
            string name,
            out IPythonModule module,
            TryImportModuleContext context
            )
        {
            module = null;
            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);
            }

            var            modules       = context?.ModuleCache;
            int            importTimeout = context?.Timeout ?? 5000;
            SentinelModule sentinalValue = null;

            if (modules != null)
            {
                // Return any existing module
                if (modules.TryGetValue(name, out module) && module != null)
                {
                    if (module 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(importTimeout);
                        if (newMod is SentinelModule)
                        {
                            _log?.Log(TraceLevel.Warning, "RecursiveImport", name);
                            module = newMod;
                        }
                        else if (newMod == null)
                        {
                            _log?.Log(TraceLevel.Warning, "ImportTimeout", name);
                            module = null;
                            return(TryImportModuleResult.Timeout);
                        }
                        else
                        {
                            module = newMod;
                        }
                    }
                    return(TryImportModuleResult.Success);
                }

                // Set up a sentinel so we can detect recursive imports
                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 module) && !(module is SentinelModule))
                    {
                        return(module == null ? TryImportModuleResult.ModuleNotFound : TryImportModuleResult.Success);
                    }
                    // 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))
            {
                var importTask = ImportFromSearchPathsAsync(name, context);
                if (importTask.Wait(importTimeout))
                {
                    module = importTask.Result;
                }
                else
                {
                    _log?.Log(TraceLevel.Error, "ImportTimeout", name, "ImportFromSearchPaths");
                    module = null;
                }
                module = module ?? ImportFromBuiltins(name, context.BuiltinModule as AstBuiltinsPythonModule);
            }
            if (module == null)
            {
                module = ImportFromCache(name);
            }

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

            // Also search for type stub packages if enabled and we are not a blacklisted module
            if (module != null && module.Name != "typing")
            {
                // Note that this currently only looks in the typeshed package, as type stub
                // packages are not yet standardised so we don't know where to look.
                // The details will be in PEP 561.
                if (context.TypeStubPaths?.Any() == true)
                {
                    var mtsp = FindModuleInSearchPath(context.TypeStubPaths, null, module.Name);
                    if (mtsp.HasValue)
                    {
                        var mp = mtsp.Value;
                        if (mp.IsCompiled)
                        {
                            Debug.Fail("Unsupported native module in typeshed");
                        }
                        else
                        {
                            _log?.Log(TraceLevel.Verbose, "ImportTypeShed", mp.FullName, FastRelativePath(mp.SourceFile));
                            var tsModule = PythonModuleLoader.FromFile(context.Interpreter, mp.SourceFile, LanguageVersion, mp.FullName);

                            if (tsModule != null)
                            {
                                if (context.MergeTypeStubPackages)
                                {
                                    module = AstPythonMultipleModules.Combine(module, tsModule);
                                }
                                else
                                {
                                    module = tsModule;
                                }
                            }
                        }
                    }
                }
            }

            return(module == null ? TryImportModuleResult.ModuleNotFound : TryImportModuleResult.Success);
        }