Пример #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 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()
                                   ));
                    }
        }