Пример #1
0
        private IPythonModule ImportFromSearchPaths(string name)
        {
            var mmp = FindModuleInSearchPath(_userSearchPaths, GetUserSearchPathPackages(), name);

            if (mmp.HasValue)
            {
                lock (_userSearchPathsLock) {
                    if (_userSearchPathImported == null)
                    {
                        _userSearchPathImported = new HashSet <string>();
                    }
                    _userSearchPathImported.Add(name);
                }
            }
            else
            {
                mmp = FindModuleInSearchPath(_factory.GetSearchPaths(), _factory.GetImportableModules(), name);
            }

            if (!mmp.HasValue)
            {
                return(null);
            }

            var mp = mmp.Value;

            if (mp.IsCompiled)
            {
                return(new AstScrapedPythonModule(mp.FullName, mp.SourceFile));
            }

            return(AstPythonModule.FromFile(this, mp.SourceFile, _factory.LanguageVersion, mp.FullName));
        }
Пример #2
0
        private IPythonModule ImportFromSearchPaths(string name)
        {
            var mmp = FindModuleInSearchPath(_userSearchPaths, GetUserSearchPathPackages(), name);

            if (mmp.HasValue)
            {
                lock (_userSearchPathsLock) {
                    if (_userSearchPathImported == null)
                    {
                        _userSearchPathImported = new HashSet <string>();
                    }
                    _userSearchPathImported.Add(name);
                }
            }
            else
            {
                mmp = FindModuleInSearchPath(_factory.GetSearchPaths(), _factory.GetImportableModules(), name);
            }

            if (!mmp.HasValue)
            {
                return(null);
            }

            var mp = mmp.Value;

#if USE_TYPESHED
            lock (_typeShedPathsLock) {
                if (_typeShedPaths == null)
                {
                    var typeshed = FindModuleInSearchPath(_factory.GetSearchPaths(), _factory.GetImportableModules(), "typeshed");
                    if (typeshed.HasValue)
                    {
                        _typeShedPaths = GetTypeShedPaths(PathUtils.GetParent(typeshed.Value.SourceFile)).ToArray();
                    }
                    else
                    {
                        _typeShedPaths = Array.Empty <string>();
                    }
                }
                if (_typeShedPaths.Any())
                {
                    var mtsp = FindModuleInSearchPath(_typeShedPaths, null, mp.FullName);
                    if (mtsp.HasValue)
                    {
                        mp = mtsp.Value;
                    }
                }
            }
#endif

            if (mp.IsCompiled)
            {
                _log?.Log(TraceLevel.Verbose, "ImportScraped", mp.FullName, _factory.FastRelativePath(mp.SourceFile));
                return(new AstScrapedPythonModule(mp.FullName, mp.SourceFile));
            }

            _log?.Log(TraceLevel.Verbose, "Import", mp.FullName, _factory.FastRelativePath(mp.SourceFile));
            return(AstPythonModule.FromFile(this, mp.SourceFile, _factory.LanguageVersion, mp.FullName));
        }
Пример #3
0
        public static IPythonModule FromStream(
            IPythonInterpreter interpreter,
            Stream sourceFile,
            string fileName,
            PythonLanguageVersion langVersion,
            string moduleFullName
            )
        {
            var sink   = KeepParseErrors ? new CollectingErrorSink() : ErrorSink.Null;
            var parser = Parser.CreateParser(sourceFile, langVersion, new ParserOptions {
                StubFile  = fileName.EndsWithOrdinal(".pyi", ignoreCase: true),
                ErrorSink = sink
            });
            var ast          = parser.ParseFile();
            var pathResolver = interpreter is AstPythonInterpreter astPythonInterpreter ? astPythonInterpreter.CurrentPathResolver : new PathResolverSnapshot(langVersion);

            var module = new AstPythonModule(
                moduleFullName ?? ModulePath.FromFullPath(fileName, isPackage: IsPackageCheck).FullName,
                interpreter,
                ast.Documentation,
                fileName,
                (sink as CollectingErrorSink)?.Errors.Select(e => "{0} ({1}): {2}".FormatUI(fileName ?? "(builtins)", e.Span, e.Message))
                );

            module.Analyze(ast, pathResolver);
            return(module);
        }
Пример #4
0
        private async Task <IPythonModule> ImportFromSearchPathsAsyncWorker(string name)
        {
            var mmp = FindModuleInSearchPath(
                _userSearchPaths,
                await GetUserSearchPathPackagesAsync().ConfigureAwait(false),
                name
                );

            if (mmp.HasValue)
            {
                lock (_userSearchPathsLock) {
                    if (_userSearchPathImported == null)
                    {
                        _userSearchPathImported = new HashSet <string>();
                    }
                    _userSearchPathImported.Add(name);
                }
            }
            else
            {
                _log?.Log(TraceLevel.Verbose, "FindModule", name, "system");
                var sp = await _factory.GetSearchPathsAsync().ConfigureAwait(false);

                _log?.Log(TraceLevel.Verbose, "FindModule", name, "system", string.Join(", ", sp));
                var mods = await _factory.GetImportableModulesAsync().ConfigureAwait(false);

                mmp = FindModuleInSearchPath(sp, mods, name);
            }

            if (!mmp.HasValue)
            {
                _log?.Log(TraceLevel.Verbose, "ImportNotFound", name);
                return(null);
            }

            var mp = mmp.Value;

#if USE_TYPESHED
            lock (_typeShedPathsLock) {
                if (_typeShedPaths == null)
                {
                    var typeshed = FindModuleInSearchPath(_factory.GetSearchPaths(), _factory.GetImportableModules(), "typeshed");
                    if (typeshed.HasValue)
                    {
                        _typeShedPaths = GetTypeShedPaths(PathUtils.GetParent(typeshed.Value.SourceFile)).ToArray();
                    }
                    else
                    {
                        _typeShedPaths = Array.Empty <string>();
                    }
                }
                if (_typeShedPaths.Any())
                {
                    var mtsp = FindModuleInSearchPath(_typeShedPaths, null, mp.FullName);
                    if (mtsp.HasValue)
                    {
                        mp = mtsp.Value;
                    }
                }
            }
#endif

            if (mp.IsCompiled)
            {
                _log?.Log(TraceLevel.Verbose, "ImportScraped", mp.FullName, _factory.FastRelativePath(mp.SourceFile));
                return(new AstScrapedPythonModule(mp.FullName, mp.SourceFile));
            }

            _log?.Log(TraceLevel.Verbose, "Import", mp.FullName, _factory.FastRelativePath(mp.SourceFile));
            return(AstPythonModule.FromFile(this, mp.SourceFile, _factory.LanguageVersion, mp.FullName));
        }
Пример #5
0
        public IPythonModule ImportModule(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }

            IPythonModule mod;

            if (_modules.TryGetValue(name, out mod) && mod != null)
            {
                if (mod is EmptyModule)
                {
                    Trace.TraceWarning($"Recursively importing {name}");
                }
                return(mod);
            }
            var sentinalValue = new EmptyModule();

            if (!_modules.TryAdd(name, sentinalValue))
            {
                return(_modules[name]);
            }

            var mmp = FindModuleInSearchPath(_userSearchPaths, GetUserSearchPathPackages(), name);

            if (mmp.HasValue)
            {
                lock (_userSearchPathsLock) {
                    if (_userSearchPathImported == null)
                    {
                        _userSearchPathImported = new HashSet <string>();
                    }
                    _userSearchPathImported.Add(name);
                }
            }
            else
            {
                mmp = FindModuleInSearchPath(_factory.GetSearchPaths(), _factory.GetImportableModules(), name);
            }

            if (!mmp.HasValue)
            {
                if (!_modules.TryUpdate(name, null, sentinalValue))
                {
                    return(_modules[name]);
                }
                return(null);
            }

            var mp = mmp.Value;

            if (mp.IsCompiled)
            {
                mod = new AstScrapedPythonModule(mp.FullName, mp.SourceFile);
            }
            else
            {
                mod = AstPythonModule.FromFile(this, mp.SourceFile, _factory.LanguageVersion, mp.FullName);
            }

            if (!_modules.TryUpdate(name, mod, sentinalValue))
            {
                mod = _modules[name];
            }

            return(mod);
        }