private IPythonModule ImportFromBuiltins(string name, AstBuiltinsPythonModule builtinModule) { if (builtinModule == null) { return(null); } var bmn = builtinModule.GetAnyMember("__builtin_module_names__") as AstPythonStringLiteral; var names = bmn?.Value ?? string.Empty; // Quick substring check if (!names.Contains(name)) { return(null); } // Proper split/trim check if (!names.Split(',').Select(n => n.Trim()).Contains(name)) { return(null); } _log?.Log(TraceLevel.Info, "ImportBuiltins", name, FastRelativePath(Configuration.InterpreterPath)); try { return(new AstBuiltinPythonModule(name, Configuration.InterpreterPath)); } catch (ArgumentNullException) { Debug.Fail("No factory means cannot import builtin modules"); return(null); } }
public async Task <IPythonModule> ImportModuleAsync(string name, CancellationToken token) { if (name == BuiltinModuleName) { if (_builtinModule == null) { _modules[BuiltinModuleName] = _builtinModule = new AstBuiltinsPythonModule(_factory.LanguageVersion); } return(_builtinModule); } Debug.Assert(_analyzer != null); var pathResolver = _analyzer.CurrentPathResolver; var typeStubPaths = _analyzer.Limits.UseTypeStubPackages ? _analyzer.GetTypeStubPaths() : null; var 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.) TryImportModuleResult result; try { result = await ModuleResolution.TryImportModuleAsync(name, pathResolver, typeStubPaths, mergeTypeStubPackages, token); } catch (OperationCanceledException) { Log.Log(TraceLevel.Error, "ImportTimeout", name); Debug.Fail("Import timeout"); return(null); } switch (result.Status) { case TryImportModuleResultCode.Success: return(result.Module); case TryImportModuleResultCode.ModuleNotFound: Log?.Log(TraceLevel.Info, "ImportNotFound", name); return(null); case TryImportModuleResultCode.NeedRetry: case TryImportModuleResultCode.Timeout: break; case 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); }