Esempio n. 1
0
        public async Task <IPythonModule> ImportModuleAsync(string name, CancellationToken token)
        {
            if (name == BuiltinModuleName)
            {
                if (_builtinModule == null)
                {
                    _modules[BuiltinModuleName] = _builtinModule = new AstBuiltinsPythonModule(_factory.LanguageVersion);
                    _builtinModuleNames         = null;
                }
                return(_builtinModule);
            }

            var ctxt = new AstPythonInterpreterFactory.TryImportModuleContext {
                Interpreter   = this,
                ModuleCache   = _modules,
                BuiltinModule = _builtinModule,
                FindModuleInUserSearchPathAsync = FindModuleInUserSearchPathAsync,
                TypeStubPaths         = _analyzer.Limits.UseTypeStubPackages ? _analyzer.GetTypeStubPaths() : null,
                MergeTypeStubPackages = !_analyzer.Limits.UseTypeStubPackagesExclusively
            };

            for (var retries = 5; retries > 0; --retries)
            {
                // The call should be cancelled by the cancellation token, but since we
                // are blocking here we wait for slightly longer. Timeouts are handled
                // gracefully by TryImportModuleAsync(), so we want those to trigger if
                // possible, but if all else fails then we'll abort and treat it as an
                // error.
                // (And if we've got a debugger attached, don't time out at all.)
                AstPythonInterpreterFactory.TryImportModuleResult result;
                try {
                    result = await _factory.TryImportModuleAsync(name, ctxt, token);
                } catch (OperationCanceledException) {
                    _log.Log(TraceLevel.Error, "ImportTimeout", name);
                    Debug.Fail("Import timeout");
                    return(null);
                }

                switch (result.Status)
                {
                case AstPythonInterpreterFactory.TryImportModuleResultCode.Success:
                    return(result.Module);

                case AstPythonInterpreterFactory.TryImportModuleResultCode.ModuleNotFound:
                    _log?.Log(TraceLevel.Info, "ImportNotFound", name);
                    return(null);

                case AstPythonInterpreterFactory.TryImportModuleResultCode.NeedRetry:
                case AstPythonInterpreterFactory.TryImportModuleResultCode.Timeout:
                    break;

                case AstPythonInterpreterFactory.TryImportModuleResultCode.NotSupported:
                    _log?.Log(TraceLevel.Error, "ImportNotSupported", name);
                    return(null);
                }
            }
            // Never succeeded, so just log the error and fail
            _log?.Log(TraceLevel.Error, "RetryImport", name);
            return(null);
        }
Esempio n. 2
0
        public IPythonModule ImportModule(string name)
        {
            if (name == BuiltinModuleName)
            {
                if (_builtinModule == null)
                {
                    _modules[BuiltinModuleName] = _builtinModule = new AstBuiltinsPythonModule(_factory.LanguageVersion);
                    _builtinModuleNames         = null;
                }
                return(_builtinModule);
            }

            IPythonModule module = null;
            var           ctxt   = new AstPythonInterpreterFactory.TryImportModuleContext {
                Interpreter   = this,
                ModuleCache   = _modules,
                BuiltinModule = _builtinModule,
                FindModuleInUserSearchPathAsync = FindModuleInUserSearchPathAsync,
                TypeStubPaths         = _analyzer.Limits.UseTypeStubPackages ? _analyzer.GetTypeStubPaths() : null,
                MergeTypeStubPackages = !_analyzer.Limits.UseTypeStubPackagesExclusively
            };

            for (int retries = 5; retries > 0; --retries)
            {
                switch (_factory.TryImportModule(name, out module, ctxt))
                {
                case AstPythonInterpreterFactory.TryImportModuleResult.Success:
                    return(module);

                case AstPythonInterpreterFactory.TryImportModuleResult.ModuleNotFound:
                    _log?.Log(TraceLevel.Info, "ImportNotFound", name);
                    return(null);

                case AstPythonInterpreterFactory.TryImportModuleResult.NeedRetry:
                case AstPythonInterpreterFactory.TryImportModuleResult.Timeout:
                    break;

                case AstPythonInterpreterFactory.TryImportModuleResult.NotSupported:
                    _log?.Log(TraceLevel.Error, "ImportNotSupported", name);
                    return(null);
                }
            }
            // Never succeeded, so just log the error and fail
            _log?.Log(TraceLevel.Error, "RetryImport", name);
            return(null);
        }