Example #1
0
        public void CloseRunningIisExpress()
        {
            PythonVersion.AssertInstalled();

            IEnumerable <Process> running;

            while ((running = Process.GetProcessesByName("iisexpress")).Any())
            {
                foreach (var p in running)
                {
                    try {
                        p.CloseMainWindow();
                    } catch (Exception ex) {
                        Console.WriteLine("Failed to CloseMainWindow on iisexpress: {0}", ex);
                    }
                }

                Thread.Sleep(1000);

                foreach (var p in running)
                {
                    if (!p.HasExited)
                    {
                        try {
                            p.Kill();
                        } catch (Exception ex) {
                            Console.WriteLine("Failed to Kill iisexpress: {0}", ex);
                        }
                    }
                }
            }
        }
Example #2
0
        public DefaultInterpreterSetter SelectDefaultInterpreter(PythonVersion interp, string installPackages)
        {
            interp.AssertInstalled();
            if (interp.IsIronPython && !string.IsNullOrEmpty(installPackages))
            {
                Assert.Inconclusive("Package installation not supported on IronPython");
            }

            var interpreterService       = InterpreterService;
            var factory                  = interpreterService.FindInterpreter(interp.Id);
            var defaultInterpreterSetter = new DefaultInterpreterSetter(factory);

            try {
                if (!string.IsNullOrEmpty(installPackages))
                {
                    factory.InstallPip();
                    foreach (var package in installPackages.Split(' ', ',', ';').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)))
                    {
                        factory.PipInstall("-U " + package);
                    }
                }

                Assert.AreEqual(factory.Configuration.Id, OptionsService.DefaultInterpreterId);

                var result = defaultInterpreterSetter;
                defaultInterpreterSetter = null;
                return(result);
            } finally {
                if (defaultInterpreterSetter != null)
                {
                    defaultInterpreterSetter.Dispose();
                }
            }
        }
Example #3
0
        private static void FullStdLibTest(PythonVersion v)
        {
            v.AssertInstalled();
            var modules = ModulePath.GetModulesInLib(v.PrefixPath).ToList();
            var paths   = modules.Select(m => m.LibraryPath).Distinct().ToArray();

            bool anySuccess = false;

            using (var analyzer = new PythonAnalysis(v.Version)) {
                analyzer.SetSearchPaths(paths);

                foreach (var modName in modules)
                {
                    if (modName.IsCompiled || modName.IsNativeExtension)
                    {
                        continue;
                    }
                    var mod = analyzer.Analyzer.Interpreter.ImportModule(modName.ModuleName);
                    if (mod == null)
                    {
                        Trace.TraceWarning("failed to import {0} from {1}".FormatInvariant(modName.ModuleName, modName.SourceFile));
                    }
                    else
                    {
                        anySuccess = true;
                        mod.GetMemberNames(analyzer.ModuleContext).ToList();
                    }
                }
            }
            Assert.IsTrue(anySuccess, "failed to import any modules at all");
        }
Example #4
0
        private static void FullStdLibTest(PythonVersion v)
        {
            v.AssertInstalled();
            var factory = new AstPythonInterpreterFactory(v.Configuration, null);
            var modules = ModulePath.GetModulesInLib(v.PrefixPath).ToList();

            bool anySuccess = false;
            bool anyExtensionSuccess = false, anyExtensionSeen = false;

            using (var analyzer = new PythonAnalysis(factory)) {
                foreach (var modName in modules)
                {
                    anyExtensionSeen |= modName.IsNativeExtension;
                    var mod = analyzer.Analyzer.Interpreter.ImportModule(modName.ModuleName);
                    if (mod == null)
                    {
                        Trace.TraceWarning("failed to import {0} from {1}".FormatInvariant(modName.ModuleName, modName.SourceFile));
                    }
                    else
                    {
                        anySuccess           = true;
                        anyExtensionSuccess |= modName.IsNativeExtension;
                        mod.GetMemberNames(analyzer.ModuleContext).ToList();
                    }
                }
            }
            Assert.IsTrue(anySuccess, "failed to import any modules at all");
            Assert.IsTrue(anyExtensionSuccess || !anyExtensionSeen, "failed to import all extension modules");
        }
Example #5
0
        public DefaultInterpreterSetter SelectDefaultInterpreter(PythonVersion interp, string installPackages)
        {
            interp.AssertInstalled();
            if (interp.IsIronPython && !string.IsNullOrEmpty(installPackages))
            {
                Assert.Inconclusive("Package installation not supported on IronPython");
            }

            var interpreterService       = InterpreterService;
            var factory                  = interpreterService.FindInterpreter(interp.Id);
            var defaultInterpreterSetter = new DefaultInterpreterSetter(factory);

            try {
                if (!string.IsNullOrEmpty(installPackages))
                {
                    Pip.InstallPip(ServiceProvider, factory, false).Wait();
                    foreach (var package in installPackages.Split(' ', ',', ';').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)))
                    {
                        Pip.Install(ServiceProvider, factory, package, false).Wait();
                    }
                }

                var result = defaultInterpreterSetter;
                defaultInterpreterSetter = null;
                return(result);
            } finally {
                if (defaultInterpreterSetter != null)
                {
                    defaultInterpreterSetter.Dispose();
                }
            }
        }
Example #6
0
        private void AstBuiltinScrape(PythonVersion version)
        {
            version.AssertInstalled();
            using (var analysis = CreateAnalysis(version)) {
                var fact   = (AstPythonInterpreterFactory)analysis.Analyzer.InterpreterFactory;
                var interp = (AstPythonInterpreter)analysis.Analyzer.Interpreter;

                var mod = interp.ImportModule(interp.BuiltinModuleName);
                Assert.IsInstanceOfType(mod, typeof(AstBuiltinsPythonModule));

                // Ensure we can get all the builtin types
                foreach (BuiltinTypeId v in Enum.GetValues(typeof(BuiltinTypeId)))
                {
                    var type = interp.GetBuiltinType(v);
                    Assert.IsNotNull(type, v.ToString());
                    Assert.IsInstanceOfType(type, typeof(AstPythonBuiltinType), $"Did not find {v}");
                }

                // Ensure we cannot see or get builtin types directly
                AssertUtil.DoesntContain(
                    mod.GetMemberNames(null),
                    Enum.GetNames(typeof(BuiltinTypeId)).Select(n => $"__{n}")
                    );

                foreach (var id in Enum.GetNames(typeof(BuiltinTypeId)))
                {
                    Assert.IsNull(mod.GetMember(null, $"__{id}"), id);
                }
            }
        }
Example #7
0
        public DefaultInterpreterSetter SelectDefaultInterpreter(PythonVersion interp, string installPackages)
        {
            interp.AssertInstalled();

            var interpreterService       = InterpreterService;
            var factory                  = interpreterService.FindInterpreter(interp.Id);
            var defaultInterpreterSetter = new DefaultInterpreterSetter(factory, ServiceProvider);

            try {
                if (!string.IsNullOrEmpty(installPackages))
                {
                    var pm = OptionsService.GetPackageManagers(factory).FirstOrDefault();
                    var ui = new TestPackageManagerUI();
                    pm.PrepareAsync(ui, CancellationTokens.After60s).WaitAndUnwrapExceptions();
                    foreach (var package in installPackages.Split(' ', ',', ';').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)))
                    {
                        pm.InstallAsync(new PackageSpec(package), ui, CancellationTokens.After60s).WaitAndUnwrapExceptions();
                    }
                }

                Assert.AreEqual(factory.Configuration.Id, OptionsService.DefaultInterpreterId);

                var result = defaultInterpreterSetter;
                defaultInterpreterSetter = null;
                return(result);
            } finally {
                if (defaultInterpreterSetter != null)
                {
                    defaultInterpreterSetter.Dispose();
                }
            }
        }
        public async Task DetectReqPkgMissingPython3Async()
        {
            PythonVersion pythonInterpreter = PythonPaths.LatestVersion;

            pythonInterpreter.AssertInstalled("Unable to run test because python 3.5, 3.6, or 3.7 must be installed");

            await DetectReqPkgMissingAsync(pythonInterpreter);
        }
Example #9
0
        public async Task DetectReqPkgMissingPython2Async()
        {
            PythonVersion pythonInterpreter = PythonPaths.Python27_x64 ??
                                              PythonPaths.Python27;

            pythonInterpreter.AssertInstalled("Unable to run test because python 2.7 must be installed");

            await DetectReqPkgMissingAsync(pythonInterpreter);
        }
Example #10
0
        private void AstBuiltinScrape(PythonVersion version) {
            AstScrapedPythonModule.KeepAst = true;
            version.AssertInstalled();
            using (var analysis = CreateAnalysis(version)) {
                try {
                    var fact = (AstPythonInterpreterFactory)analysis.Analyzer.InterpreterFactory;
                    var interp = (AstPythonInterpreter)analysis.Analyzer.Interpreter;
                    var ctxt = interp.CreateModuleContext();

                    var mod = interp.ImportModule(interp.BuiltinModuleName);
                    Assert.IsInstanceOfType(mod, typeof(AstBuiltinsPythonModule));
                    mod.Imported(ctxt);

                    var modPath = fact.GetCacheFilePath(fact.Configuration.InterpreterPath);
                    if (File.Exists(modPath)) {
                        _moduleCache = File.ReadAllText(modPath);
                    }

                    var errors = ((AstScrapedPythonModule)mod).ParseErrors ?? Enumerable.Empty<string>();
                    foreach (var err in errors) {
                        Console.WriteLine(err);
                    }
                    Assert.AreEqual(0, errors.Count(), "Parse errors occurred");

                    var seen = new HashSet<string>();
                    foreach (var stmt in ((Ast.SuiteStatement)((AstScrapedPythonModule)mod).Ast.Body).Statements) {
                        if (stmt is Ast.ClassDefinition cd) {
                            Assert.IsTrue(seen.Add(cd.Name), $"Repeated use of {cd.Name} at index {cd.StartIndex} in {modPath}");
                        } else if (stmt is Ast.FunctionDefinition fd) {
                            Assert.IsTrue(seen.Add(fd.Name), $"Repeated use of {fd.Name} at index {fd.StartIndex} in {modPath}");
                        } else if (stmt is Ast.AssignmentStatement assign && assign.Left.FirstOrDefault() is Ast.NameExpression n) {
                            Assert.IsTrue(seen.Add(n.Name), $"Repeated use of {n.Name} at index {n.StartIndex} in {modPath}");
                        }
                    }

                    // Ensure we can get all the builtin types
                    foreach (BuiltinTypeId v in Enum.GetValues(typeof(BuiltinTypeId))) {
                        var type = interp.GetBuiltinType(v);
                        Assert.IsNotNull(type, v.ToString());
                        Assert.IsInstanceOfType(type, typeof(AstPythonBuiltinType), $"Did not find {v}");
                    }

                    // Ensure we cannot see or get builtin types directly
                    AssertUtil.DoesntContain(
                        mod.GetMemberNames(null),
                        Enum.GetNames(typeof(BuiltinTypeId)).Select(n => $"__{n}")
                    );

                    foreach (var id in Enum.GetNames(typeof(BuiltinTypeId))) {
                        Assert.IsNull(mod.GetMember(null, $"__{id}"), id);
                    }
                } finally {
                    _analysisLog = analysis.GetLogContent(CultureInfo.InvariantCulture);
                }
            }
        }
Example #11
0
        private static PythonAnalysis CreateAnalysis(PythonVersion version) {
            version.AssertInstalled();
            var opts = new InterpreterFactoryCreationOptions {
                DatabasePath = TestData.GetTempPath("AstAnalysisCache"),
                UseExistingCache = false
            };

            Trace.TraceInformation("Cache Path: " + opts.DatabasePath);

            return new PythonAnalysis(() => new AstPythonInterpreterFactory(
                version.Configuration,
                opts
            ));
        }
Example #12
0
        public async Task <Server> CreateServer(Uri rootUri, PythonVersion version = null, Dictionary <Uri, PublishDiagnosticsEventArgs> diagnosticEvents = null)
        {
            version = version ?? Default;
            version.AssertInstalled();
            var s = new Server();

            s.OnLogMessage += Server_OnLogMessage;
            var properties = new InterpreterFactoryCreationOptions {
                TraceLevel   = System.Diagnostics.TraceLevel.Verbose,
                DatabasePath = TestData.GetTempPath($"AstAnalysisCache{version.Version}")
            }.ToDictionary();

            version.Configuration.WriteToDictionary(properties);

            await s.Initialize(new InitializeParams {
                rootUri = rootUri,
                initializationOptions = new PythonInitializationOptions {
                    interpreter = new PythonInitializationOptions.Interpreter {
                        assembly   = typeof(AstPythonInterpreterFactory).Assembly.Location,
                        typeName   = typeof(AstPythonInterpreterFactory).FullName,
                        properties = properties
                    },
                    asyncStartup    = false,
                    testEnvironment = true
                },
                capabilities = new ClientCapabilities {
                    python = new PythonClientCapabilities {
                        analysisUpdates = true,
                        liveLinting     = true,
                        traceLogging    = true
                    }
                }
            });

            if (diagnosticEvents != null)
            {
                s.OnPublishDiagnostics += (sender, e) => { lock (diagnosticEvents) diagnosticEvents[e.uri] = e; };
            }

            if (rootUri != null)
            {
                await s.WaitForDirectoryScanAsync().ConfigureAwait(false);

                await s.WaitForCompleteAnalysisAsync().ConfigureAwait(false);
            }

            return(s);
        }
Example #13
0
        private static async Task <VsProjectAnalyzer> CreateAnalyzerAsync(PythonVersion version)
        {
            version.AssertInstalled();
            var factory      = new MockPythonInterpreterFactory(version.Configuration);
            var sp           = new MockServiceProvider();
            var services     = new Microsoft.PythonTools.Editor.PythonEditorServices(sp);
            var interpreters = new MockInterpreterOptionsService();

            interpreters.AddProvider(new MockPythonInterpreterFactoryProvider("Test Provider", factory));
            services.InterpreterRegistryService = interpreters;
            services.InterpreterOptionsService  = interpreters;
            return(await VsProjectAnalyzer.CreateForTestsAsync(
                       services,
                       factory
                       ));
        }
Example #14
0
        private void AstBuiltinScrape(PythonVersion version)
        {
            version.AssertInstalled();
            using (var analysis = CreateAnalysis(version)) {
                try {
                    var fact   = (AstPythonInterpreterFactory)analysis.Analyzer.InterpreterFactory;
                    var interp = (AstPythonInterpreter)analysis.Analyzer.Interpreter;

                    var mod     = interp.ImportModule(interp.BuiltinModuleName);
                    var modPath = fact.GetCacheFilePath(fact.Configuration.InterpreterPath);
                    if (File.Exists(modPath))
                    {
                        _moduleCache = File.ReadAllText(modPath);
                    }
                    Assert.IsInstanceOfType(mod, typeof(AstBuiltinsPythonModule));

                    var errors = ((AstScrapedPythonModule)mod).ParseErrors ?? Enumerable.Empty <string>();
                    foreach (var err in errors)
                    {
                        Console.WriteLine(err);
                    }
                    Assert.AreEqual(0, errors.Count(), "Parse errors occurred");

                    // Ensure we can get all the builtin types
                    foreach (BuiltinTypeId v in Enum.GetValues(typeof(BuiltinTypeId)))
                    {
                        var type = interp.GetBuiltinType(v);
                        Assert.IsNotNull(type, v.ToString());
                        Assert.IsInstanceOfType(type, typeof(AstPythonBuiltinType), $"Did not find {v}");
                    }

                    // Ensure we cannot see or get builtin types directly
                    AssertUtil.DoesntContain(
                        mod.GetMemberNames(null),
                        Enum.GetNames(typeof(BuiltinTypeId)).Select(n => $"__{n}")
                        );

                    foreach (var id in Enum.GetNames(typeof(BuiltinTypeId)))
                    {
                        Assert.IsNull(mod.GetMember(null, $"__{id}"), id);
                    }
                } finally {
                    _analysisLog = analysis.GetLogContent(CultureInfo.InvariantCulture);
                }
            }
        }
Example #15
0
        private void TestMutateStdLib(PythonVersion version)
        {
            version.AssertInstalled();

            for (int i = 0; i < 100; i++)
            {
                int seed   = (int)DateTime.Now.Ticks;
                var random = new Random(seed);
                Console.WriteLine("Seed == " + seed);


                Console.WriteLine("Testing version {0} {1}", version.Version, Path.Combine(version.PrefixPath, "Lib"));
                int      ran = 0, succeeded = 0;
                string[] files;
                try {
                    files = Directory.GetFiles(Path.Combine(version.PrefixPath, "Lib"));
                } catch (DirectoryNotFoundException) {
                    continue;
                }

                foreach (var file in files)
                {
                    try {
                        if (file.EndsWith(".py"))
                        {
                            ran++;
                            TestOneFileMutated(file, version.Version, random);
                            succeeded++;
                        }
                    } catch (Exception e) {
                        Console.WriteLine(e);
                        Console.WriteLine("Failed: {0}", file);
                        break;
                    }
                }

                Assert.AreEqual(ran, succeeded);
            }
        }
Example #16
0
        public async Task <Server> CreateServer(Uri rootUri, PythonVersion version)
        {
            version.AssertInstalled();
            var s = new Server();

            s.OnLogMessage += Server_OnLogMessage;
            var properties = new InterpreterFactoryCreationOptions {
                TraceLevel   = System.Diagnostics.TraceLevel.Verbose,
                DatabasePath = TestData.GetTempPath($"AstAnalysisCache{version.Version}")
            }.ToDictionary();

            version.Configuration.WriteToDictionary(properties);

            await s.Initialize(new InitializeParams {
                rootUri = rootUri,
                initializationOptions = new PythonInitializationOptions {
                    interpreter = new PythonInitializationOptions.Interpreter {
                        assembly   = typeof(AstPythonInterpreterFactory).Assembly.Location,
                        typeName   = typeof(AstPythonInterpreterFactory).FullName,
                        properties = properties
                    }
                },
                capabilities = new ClientCapabilities {
                    python = new PythonClientCapabilities {
                        analysisUpdates = true,
                        traceLogging    = true
                    }
                }
            });

            if (rootUri != null)
            {
                await s.WaitForDirectoryScanAsync().ConfigureAwait(false);

                await s.WaitForCompleteAnalysisAsync().ConfigureAwait(false);
            }

            return(s);
        }
Example #17
0
        private async Task FullStdLibTest(PythonVersion v, params string[] skipModules)
        {
            v.AssertInstalled();
            var factory = new AstPythonInterpreterFactory(v.Configuration, new InterpreterFactoryCreationOptions {
                DatabasePath     = TestData.GetTempPath(),
                UseExistingCache = false
            });
            var modules = ModulePath.GetModulesInLib(v.PrefixPath).ToList();

            var skip = new HashSet <string>(skipModules);

            skip.UnionWith(new[] {
                "matplotlib.backends._backend_gdk",
                "matplotlib.backends._backend_gtkagg",
                "matplotlib.backends._gtkagg",
            });

            bool anySuccess = false;
            bool anyExtensionSuccess = false, anyExtensionSeen = false;
            bool anyParseError = false;

            using (var analyzer = new PythonAnalysis(factory)) {
                try {
                    var tasks = new List <Task <Tuple <ModulePath, IPythonModule> > >();

                    var interp = (AstPythonInterpreter)analyzer.Analyzer.Interpreter;
                    foreach (var m in skip)
                    {
                        interp.AddUnimportableModule(m);
                    }

                    foreach (var r in modules
                             .Where(m => !skip.Contains(m.ModuleName))
                             .GroupBy(m => {
                        int i = m.FullName.IndexOf('.');
                        return(i <= 0 ? m.FullName : m.FullName.Remove(i));
                    })
                             .AsParallel()
                             .SelectMany(g => g.Select(m => Tuple.Create(m, interp.ImportModule(m.ModuleName))))
                             )
                    {
                        var modName = r.Item1;
                        var mod     = r.Item2;

                        anyExtensionSeen |= modName.IsNativeExtension;
                        if (mod == null)
                        {
                            Trace.TraceWarning("failed to import {0} from {1}", modName.ModuleName, modName.SourceFile);
                        }
                        else if (mod is AstScrapedPythonModule smod)
                        {
                            if (smod.ParseErrors?.Any() ?? false)
                            {
                                anyParseError = true;
                                Trace.TraceError("Parse errors in {0}", modName.SourceFile);
                                foreach (var e in smod.ParseErrors)
                                {
                                    Trace.TraceError(e);
                                }
                            }
                            else
                            {
                                anySuccess           = true;
                                anyExtensionSuccess |= modName.IsNativeExtension;
                                mod.GetMemberNames(analyzer.ModuleContext).ToList();
                            }
                        }
                        else if (mod is AstPythonModule)
                        {
                            // pass
                        }
                        else
                        {
                            Trace.TraceError("imported {0} as type {1}", modName.ModuleName, mod.GetType().FullName);
                        }
                    }
                } finally {
                    _analysisLog = analyzer.GetLogContent(CultureInfo.InvariantCulture);
                }
            }
            Assert.IsTrue(anySuccess, "failed to import any modules at all");
            Assert.IsTrue(anyExtensionSuccess || !anyExtensionSeen, "failed to import all extension modules");
            Assert.IsFalse(anyParseError, "parse errors occurred");
        }
Example #18
0
        private void AstNativeBuiltinScrape(PythonVersion version) {
            AstScrapedPythonModule.KeepAst = true;
            version.AssertInstalled();
            using (var analysis = CreateAnalysis(version)) {
                try {
                    var fact = (AstPythonInterpreterFactory)analysis.Analyzer.InterpreterFactory;
                    var interp = (AstPythonInterpreter)analysis.Analyzer.Interpreter;
                    var ctxt = interp.CreateModuleContext();

                    var dllsDir = PathUtils.GetAbsoluteDirectoryPath(fact.Configuration.PrefixPath, "DLLs");
                    if (!Directory.Exists(dllsDir)) {
                        Assert.Inconclusive("Configuration does not have DLLs");
                    }

                    var report = new List<string>();
                    var permittedImports = fact.GetLanguageVersion().Is2x() ?
                        new[] { interp.BuiltinModuleName, "exceptions" } :
                        new[] { interp.BuiltinModuleName };

                    foreach (var pyd in PathUtils.EnumerateFiles(dllsDir, "*", recurse: false).Where(ModulePath.IsPythonFile)) {
                        var mp = ModulePath.FromFullPath(pyd);
                        if (mp.IsDebug) {
                            continue;
                        }

                        Console.WriteLine("Importing {0} from {1}", mp.ModuleName, mp.SourceFile);
                        var mod = interp.ImportModule(mp.ModuleName);
                        Assert.IsInstanceOfType(mod, typeof(AstScrapedPythonModule));
                        mod.Imported(ctxt);

                        var modPath = fact.GetCacheFilePath(pyd);
                        Assert.IsTrue(File.Exists(modPath), "No cache file created");
                        _moduleCache = File.ReadAllText(modPath);

                        var errors = ((AstScrapedPythonModule)mod).ParseErrors ?? Enumerable.Empty<string>();
                        foreach (var err in errors) {
                            Console.WriteLine(err);
                        }
                        Assert.AreEqual(0, errors.Count(), "Parse errors occurred");

                        var ast = ((AstScrapedPythonModule)mod).Ast;

                        
                        var imports = ((Ast.SuiteStatement)ast.Body).Statements
                            .OfType<Ast.ImportStatement>()
                            .SelectMany(s => s.Names)
                            .Select(n => n.MakeString())
                            .Except(permittedImports)
                            .ToArray();

                        // We expect no imports (after excluding builtins)
                        report.AddRange(imports.Select(n => $"{mp.ModuleName} imported {n}"));

                        _moduleCache = null;
                    }

                    AssertUtil.ContainsExactly(report);
                } finally {
                    _analysisLog = analysis.GetLogContent(CultureInfo.InvariantCulture);
                }
            }
        }
Example #19
0
 private static PythonAnalysis CreateAnalysis(PythonVersion version)
 {
     version.AssertInstalled();
     return(new PythonAnalysis(() => new AstPythonInterpreterFactory(version.Configuration, null)));
 }
Example #20
0
        public DefaultInterpreterSetter SelectDefaultInterpreter(PythonVersion interp, string installPackages) {
            interp.AssertInstalled();
            if (interp.IsIronPython && !string.IsNullOrEmpty(installPackages)) {
                Assert.Inconclusive("Package installation not supported on IronPython");
            }

            var interpreterService = InterpreterService;
            var factory = interpreterService.FindInterpreter(interp.Id, interp.Configuration.Version);
            var defaultInterpreterSetter = new DefaultInterpreterSetter(factory);

            try {
                if (!string.IsNullOrEmpty(installPackages)) {
                    Pip.InstallPip(ServiceProvider, factory, false).Wait();
                    foreach (var package in installPackages.Split(' ', ',', ';').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s))) {
                        Pip.Install(ServiceProvider, factory, package, false).Wait();
                    }
                }

                var result = defaultInterpreterSetter;
                defaultInterpreterSetter = null;
                return result;
            } finally {
                if (defaultInterpreterSetter != null) {
                    defaultInterpreterSetter.Dispose();
                }
            }
        }
Example #21
0
        private void TestOpen(PythonVersion path)
        {
            path.AssertInstalled();
            Console.WriteLine(path.InterpreterPath);

            Guid testId  = Guid.NewGuid();
            var  testDir = TestData.GetTempPath(testId.ToString());

            var scraper = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "PythonScraper.py");

            Assert.IsTrue(File.Exists(scraper), $"Did not find {scraper}");

            // run the scraper
            using (var proc = ProcessOutput.RunHiddenAndCapture(
                       path.InterpreterPath,
                       scraper,
                       testDir,
                       CompletionDB
                       )) {
                Console.WriteLine("Command: " + proc.Arguments);

                proc.Wait();

                // it should succeed
                Console.WriteLine("**Stdout**");
                foreach (var line in proc.StandardOutputLines)
                {
                    Console.WriteLine(line);
                }
                Console.WriteLine("");
                Console.WriteLine("**Stdout**");
                foreach (var line in proc.StandardErrorLines)
                {
                    Console.WriteLine(line);
                }

                Assert.AreEqual(0, proc.ExitCode, "Bad exit code: " + proc.ExitCode);
            }

            // perform some basic validation
            dynamic builtinDb = Unpickle.Load(new FileStream(Path.Combine(testDir, path.Version.Is3x() ? "builtins.idb" : "__builtin__.idb"), FileMode.Open, FileAccess.Read));

            if (path.Version.Is2x())   // no open in 3.x
            {
                foreach (var overload in builtinDb["members"]["open"]["value"]["overloads"])
                {
                    Assert.AreEqual("__builtin__", overload["ret_type"][0][0]);
                    Assert.AreEqual("file", overload["ret_type"][0][1]);
                }

                if (!path.InterpreterPath.Contains("Iron"))
                {
                    // http://pytools.codeplex.com/workitem/799
                    var arr = (IList <object>)builtinDb["members"]["list"]["value"]["members"]["__init__"]["value"]["overloads"];
                    Assert.AreEqual(
                        "args",
                        ((dynamic)(arr[0]))["args"][1]["name"]
                        );
                }
            }

            if (!path.InterpreterPath.Contains("Iron"))
            {
                dynamic itertoolsDb = Unpickle.Load(new FileStream(Path.Combine(testDir, "itertools.idb"), FileMode.Open, FileAccess.Read));
                var     tee         = itertoolsDb["members"]["tee"]["value"];
                var     overloads   = tee["overloads"];
                var     nArg        = overloads[0]["args"][1];
                Assert.AreEqual("n", nArg["name"]);
                Assert.AreEqual("2", nArg["default_value"]);

                dynamic sreDb   = Unpickle.Load(new FileStream(Path.Combine(testDir, "_sre.idb"), FileMode.Open, FileAccess.Read));
                var     members = sreDb["members"];
                Assert.IsTrue(members.ContainsKey("SRE_Pattern"));
                Assert.IsTrue(members.ContainsKey("SRE_Match"));
            }

            Console.WriteLine("Passed: {0}", path.InterpreterPath);
        }