Пример #1
0
        private static IEnumerable <TestCaseInfo> GetTestCasesFromAst(string code, PythonAnalyzer analyzer)
        {
            var codeStream = new MemoryStream(Encoding.UTF8.GetBytes(code));
            var m          = AstPythonModule.FromStream(analyzer.Interpreter, codeStream, "<string>", analyzer.LanguageVersion, "__main__");

            return(TestAnalyzer.GetTestCasesFromAst(m, null));
        }
Пример #2
0
        private static IPythonModule Parse(string path, PythonLanguageVersion version)
        {
            var interpreter = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion()).CreateInterpreter();

            if (!Path.IsPathRooted(path))
            {
                path = TestData.GetPath(Path.Combine("TestData", "AstAnalysis", path));
            }
            return(AstPythonModule.FromFile(interpreter, path, version));
        }
Пример #3
0
        private IPythonModule LoadModuleFromDirectory(string searchPath, string moduleName)
        {
            Func <string, bool> isPackage = null;

            if (!ModulePath.PythonVersionRequiresInitPyFiles(_langVersion))
            {
                isPackage = Directory.Exists;
            }

            ModulePath package;

            try {
                package = ModulePath.FromBasePathAndName(searchPath, moduleName, isPackage);
            } catch (ArgumentException) {
                return(null);
            }

            EnsureSearchPathDB();
            if (package.IsNativeExtension || package.IsCompiled)
            {
                _searchPathDb.LoadExtensionModule(package);
            }
            else
            {
                _searchPathDb.AddModule(package.FullName, AstPythonModule.FromFile(
                                            this,
                                            package.SourceFile,
                                            _factory.GetLanguageVersion(),
                                            package.FullName
                                            ));
            }

            var mod = _searchPathDb.GetModule(package.FullName);

            if (!package.IsSpecialName)
            {
                int i = package.FullName.LastIndexOf('.');
                if (i >= 1)
                {
                    var parent    = package.FullName.Remove(i);
                    var parentMod = _searchPathDb.GetModule(parent) as AstPythonModule;
                    if (parentMod != null)
                    {
                        parentMod.AddChildModule(package.Name, mod);
                    }
                }
            }

            return(mod);
        }
Пример #4
0
        public IEnumerable <TestCaseInfo> GetTestCasesFromAst(string path)
        {
            IPythonModule module;

            try {
                module = AstPythonModule.FromFile(_analyzer.Interpreter, path, _analyzer.LanguageVersion);
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                return(Enumerable.Empty <TestCaseInfo>());
            }

            var ctxt = _analyzer.Interpreter.CreateModuleContext();

            return(GetTestCasesFromAst(module, ctxt));
        }
Пример #5
0
        private IPythonModule LoadModuleFromZipFile(string zipFile, string moduleName)
        {
            ModulePath       name;
            HashSet <string> packages = null;

            var cache = _zipPackageCache;

            if (cache == null)
            {
                cache = _zipPackageCache = new Dictionary <string, HashSet <string> >();
            }

            if (!cache.TryGetValue(zipFile, out packages) || packages == null)
            {
                using (var stream = new FileStream(zipFile, FileMode.Open, FileAccess.Read))
                    using (var zip = new ZipArchive(stream, ZipArchiveMode.Read, true)) {
                        cache[zipFile] = packages = new HashSet <string>(
                            zip.Entries.Select(e => e.FullName.Replace('/', '\\'))
                            );
                    }
            }

            try {
                name = ModulePath.FromBasePathAndName(
                    "",
                    moduleName,
                    packageName => packages.Contains(packageName + '\\'),
                    new GetModuleCallable(packages).GetModule
                    );
            } catch (ArgumentException) {
                return(null);
            }

            using (var stream = new FileStream(zipFile, FileMode.Open, FileAccess.Read))
                using (var zip = new ZipArchive(stream, ZipArchiveMode.Read, true))
                    using (var sourceStream = zip.GetEntry(name.SourceFile.Replace('\\', '/'))?.Open()) {
                        if (sourceStream == null)
                        {
                            return(null);
                        }
                        return(AstPythonModule.FromStream(
                                   this,
                                   sourceStream,
                                   PathUtils.GetAbsoluteFilePath(zipFile, name.SourceFile),
                                   _factory.GetLanguageVersion()
                                   ));
                    }
        }
Пример #6
0
        private IPythonModule LoadModuleFromDirectory(string searchPath, string moduleName)
        {
            Func <string, bool> isPackage = null;

            if (!ModulePath.PythonVersionRequiresInitPyFiles(_langVersion))
            {
                isPackage = Directory.Exists;
            }

            ModulePath package;

            try {
                package = ModulePath.FromBasePathAndName(searchPath, moduleName, isPackage);
            } catch (ArgumentException) {
                return(null);
            }

            var db = EnsureSearchPathDB();

            if (package.IsNativeExtension || package.IsCompiled)
            {
                db.LoadExtensionModule(package);
            }
            else
            {
                db.AddModule(package.FullName, AstPythonModule.FromFile(
                                 this,
                                 package.SourceFile,
                                 _factory.GetLanguageVersion(),
                                 package.FullName
                                 ));
            }

            if (db != _searchPathDb)
            {
                // Racing with the DB being invalidated.
                // It's okay if we miss it here, so don't worry
                // about taking the lock.
                return(null);
            }

            var mod = db.GetModule(package.FullName);

            if (!package.IsSpecialName)
            {
                int i = package.FullName.LastIndexOf('.');
                if (i >= 1)
                {
                    var parent    = package.FullName.Remove(i);
                    var parentMod = db.GetModule(parent) as AstPythonModule;
                    if (parentMod != null)
                    {
                        parentMod.AddChildModule(package.Name, mod);
                    }
                }
            }

            lock (_searchPathDbLock) {
                if (db != _searchPathDb)
                {
                    // Raced with the DB being invalidated
                    return(null);
                }
            }

            return(mod);
        }