示例#1
0
        public PythonAPI(PythonVersion pyVer)
        {
            string dllName = pyVer.ToString().ToLower();
            _pythonLibrary = new UnmanagedLibrary(dllName);

            Py_Initialize = _pythonLibrary.GetUnmanagedFunction<Action>("Py_Initialize");
            Py_IsInitialized = _pythonLibrary.GetUnmanagedFunction<FuncInt>("Py_IsInitialized");
            Py_Finalize = _pythonLibrary.GetUnmanagedFunction<Action>("Py_Finalize");
            PyEval_InitThreads = _pythonLibrary.GetUnmanagedFunction<Action>("PyEval_InitThreads");
            PyGILState_Ensure = _pythonLibrary.GetUnmanagedFunction<FuncIntPtr>("PyGILState_Ensure");
            PyGILState_Release = _pythonLibrary.GetUnmanagedFunction<ActionIntPtr>("PyGILState_Release");
            PyThreadState_Get = _pythonLibrary.GetUnmanagedFunction<FuncIntPtr>("PyThreadState_Get");

            PyRun_String = _pythonLibrary.GetUnmanagedFunction<FuncStringIntIntPtrIntPtrIntPtr>("PyRun_String");

            PyErr_Print = _pythonLibrary.GetUnmanagedFunction<Action>("PyErr_Print");
            PyErr_Occurred = _pythonLibrary.GetUnmanagedFunction<FuncIntPtr>("PyErr_Occurred");
            PyErr_Clear = _pythonLibrary.GetUnmanagedFunction<Action>("PyErr_Clear");
            PyErr_Fetch = _pythonLibrary.GetUnmanagedFunction<ActionRIntPtrRIntPtrRIntPtr>("PyErr_Fetch");

            PyImport_AddModule = _pythonLibrary.GetUnmanagedFunction<FuncStringIntPtr>("PyImport_AddModule");
            PyModule_GetDict = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyModule_GetDict");

            Py_IncRef = _pythonLibrary.GetUnmanagedFunction<ActionIntPtr>("Py_IncRef");
            Py_DecRef = _pythonLibrary.GetUnmanagedFunction<ActionIntPtr>("Py_DecRef");

            PyObject_HasAttrString = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrStringInt>("PyObject_HasAttrString");
            PyObject_GetAttrString = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrStringIntPtr>("PyObject_GetAttrString");
            PyObject_SetAttrString = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrStringIntPtrInt>("PyObject_SetAttrString");
            PyObject_Str = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyObject_Str");
            PyObject_Compare = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtrInt>("PyObject_Compare");
            PyCallable_Check = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrInt>("PyCallable_Check");
            PyObject_CallObject = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtrIntPtr>("PyObject_CallObject");
            PyObject_Length = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrInt>("PyObject_Length");
            PyObject_Dir = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyObject_Dir");
            PyObject_Type = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyObject_Type");
            PyObject_IsInstance = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtrInt>("PyObject_IsInstance");

            PyString_FromString = _pythonLibrary.GetUnmanagedFunction<FuncStringIntPtr>("PyString_FromString");
            PyString_AsString = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntPtr>("PyString_AsString");

            PySys_GetObject = _pythonLibrary.GetUnmanagedFunction<FuncStringIntPtr>("PySys_GetObject");

            PyInt_AsLong = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrInt>("PyInt_AsLong");
            PyLong_AsLongLong = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrLong>("PyLong_AsLongLong");

            PyFloat_AsDouble = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrDouble>("PyFloat_AsDouble");

            PyTuple_New = _pythonLibrary.GetUnmanagedFunction<FuncIntIntPtr>("PyTuple_New");
            PyTuple_SetItem = _pythonLibrary.GetUnmanagedFunction<FuncIntPtrIntIntPtrInt>("PyTuple_SetItem");

            PyType_Type = _pythonLibrary.GetGlobalVar("PyType_Type");
            PyBaseObject_Type = _pythonLibrary.GetGlobalVar("PyBaseObject_Type");
            PyString_Type = _pythonLibrary.GetGlobalVar("PyString_Type");
            PyUnicode_Type = _pythonLibrary.GetGlobalVar("PyUnicode_Type");
            PyInt_Type = _pythonLibrary.GetGlobalVar("PyInt_Type");
            PyLong_Type = _pythonLibrary.GetGlobalVar("PyLong_Type");
            PyBool_Type = _pythonLibrary.GetGlobalVar("PyBool_Type");
            PyFloat_Type = _pythonLibrary.GetGlobalVar("PyFloat_Type");
        }
示例#2
0
        public void CustomCommandsRunProcessInConsole(PythonVisualStudioApp app, PythonVersion python)
        {
            PythonProjectNode node;

            EnvDTE.Project proj;
            OpenProject(app, "Commands3.sln", python, out node, out proj);

            var existingProcesses = new HashSet <int>(Process.GetProcessesByName("cmd").Select(p => p.Id));

            Execute(node, "Write to Console");

            Process newProcess = null;

            for (int retries = 100; retries > 0 && newProcess == null; --retries)
            {
                Thread.Sleep(100);
                newProcess = Process.GetProcessesByName("cmd").Where(p => !existingProcesses.Contains(p.Id)).FirstOrDefault();
            }
            Assert.IsNotNull(newProcess, "Process did not start");
            try {
                newProcess.CloseMainWindow();
                newProcess.WaitForExit(1000);
                if (newProcess.HasExited)
                {
                    newProcess = null;
                }
            } finally {
                if (newProcess != null)
                {
                    newProcess.Kill();
                }
            }
        }
示例#3
0
 private static void CreateVirtualEnvironment(PythonVersion pythonVersion, string envPath) {
     using (var output = ProcessOutput.RunHiddenAndCapture(pythonVersion.Configuration.InterpreterPath, "-m", "venv", envPath)) {
         Assert.IsTrue(output.Wait(TimeSpan.FromMinutes(3)));
         Assert.AreEqual(0, output.ExitCode);
         Assert.IsTrue(File.Exists(Path.Combine(envPath, "Scripts", "python.exe")));
     }
 }
示例#4
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();
                }
            }
        }
示例#5
0
        public void CustomCommandsSearchPath(PythonVisualStudioApp app, PythonVersion python)
        {
            var expectedSearchPath = string.Format("['{0}', '{1}', '{2}']",
                                                   // Includes CWD (ProjectHome) first
                                                   TestData.GetPath(@"TestData\Targets\Package\Subpackage").Replace("\\", "\\\\"),
                                                   // Specified as '..\..' from ProjectHome
                                                   TestData.GetPath(@"TestData\Targets").Replace("\\", "\\\\"),
                                                   // Specified as '..' from ProjectHome
                                                   TestData.GetPath(@"TestData\Targets\Package").Replace("\\", "\\\\")
                                                   );

            PythonProjectNode node;

            EnvDTE.Project proj;
            OpenProject(app, "CommandSearchPath.sln", python, out node, out proj);

            Execute(node, "Import From Search Path");

            var outputWindow = app.Element.FindFirst(TreeScope.Descendants,
                                                     new AndCondition(
                                                         new PropertyCondition(AutomationElement.ClassNameProperty, "GenericPane"),
                                                         new PropertyCondition(AutomationElement.NameProperty, "Output")
                                                         )
                                                     );

            Assert.IsNotNull(outputWindow, "Output Window was not opened");

            ExpectOutputWindowText(app, expectedSearchPath);
        }
示例#6
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();
                }
            }
        }
示例#7
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);
                        }
                    }
                }
            }
        }
示例#8
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);
                }
            }
        }
示例#9
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");
        }
示例#10
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");
        }
示例#11
0
 public static void AssertInstalled(this PythonVersion pyVersion, string customMessage)
 {
     if (pyVersion == null || !File.Exists(pyVersion.InterpreterPath))
     {
         Assert.Inconclusive(customMessage);
     }
 }
示例#12
0
 /// <summary>
 /// Selects the given interpreter as the default.
 /// </summary>
 /// <remarks>
 /// This method should always be called as a using block.
 /// </remarks>
 public DefaultInterpreterSetter SelectDefaultInterpreter(PythonVersion python)
 {
     return(new DefaultInterpreterSetter(
                InterpreterService.FindInterpreter(python.Id),
                ServiceProvider
                ));
 }
示例#13
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();
                }
            }
        }
示例#14
0
        internal static PythonProcess DebugProcess(this PythonDebugger debugger, PythonVersion version, string filename, Func <PythonProcess, PythonThread, Task> onLoaded = null, bool resumeOnProcessLoaded = true, string interpreterOptions = null, PythonDebugOptions debugOptions = PythonDebugOptions.RedirectOutput, string cwd = null, string arguments = "")
        {
            string fullPath = Path.GetFullPath(filename);
            string dir      = cwd ?? Path.GetFullPath(Path.GetDirectoryName(filename));

            if (!String.IsNullOrEmpty(arguments))
            {
                arguments = "\"" + fullPath + "\" " + arguments;
            }
            else
            {
                arguments = "\"" + fullPath + "\"";
            }
            var process = debugger.CreateProcess(version.Version, version.InterpreterPath, arguments, dir, "", interpreterOptions, debugOptions);

            process.DebuggerOutput += (sender, args) => {
                Console.WriteLine("{0}: {1}", args.Thread?.Id, args.Output);
            };
            process.ProcessLoaded += async(sender, args) => {
                if (onLoaded != null)
                {
                    await onLoaded(process, args.Thread);
                }
                if (resumeOnProcessLoaded)
                {
                    await process.ResumeAsync(default(CancellationToken));
                }
            };

            return(process);
        }
示例#15
0
        public static TestEnvironment GetOrCreate(PythonVersion pythonVersion, string testFramework, bool installFramework = true, bool installCoverage = false)
        {
            var testEnvironmentId = $"{pythonVersion.ToString().ToLower()}:{testFramework.ToLower()}:{installFramework.ToString()}:{installCoverage.ToString()}";

            if (_environmentsMap.TryGetValue(testEnvironmentId, out TestEnvironment foundEnv))
            {
                SetDirectories(foundEnv);
                return(foundEnv);
            }

            var env = new TestEnvironment();

            env.TestFramework = testFramework;

            SetDirectories(env);

            switch (testFramework)
            {
            case "Pytest": {
                var envDir   = TestData.GetTempPath();
                var packages = new List <string>();
                if (installFramework)
                {
                    packages.Add("pytest");
                }
                if (installCoverage)
                {
                    packages.Add("coverage");
                }
                pythonVersion.CreateVirtualEnv(envDir, packages);
                env.InterpreterPath = Path.Combine(envDir, "scripts", "python.exe");
            }
            break;

            default:
                if (HasPackage(pythonVersion.PrefixPath, "pytest") || installCoverage)
                {
                    // Create an empty virtual env to ensure we don't accidentally rely on pytest
                    // (which was bug https://github.com/microsoft/PTVS/issues/5454)
                    var envDir   = TestData.GetTempPath();
                    var packages = new List <string>();
                    if (installCoverage)
                    {
                        packages.Add("coverage");
                    }
                    pythonVersion.CreateVirtualEnv(envDir, packages);
                    env.InterpreterPath = Path.Combine(envDir, "scripts", "python.exe");
                }
                else
                {
                    env.InterpreterPath = pythonVersion.InterpreterPath;
                }
                break;
            }

            _environmentsMap.Add(testEnvironmentId, env);

            return(env);
        }
示例#16
0
        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);
        }
示例#17
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);
        }
示例#18
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);
                }
            }
        }
示例#19
0
        internal void OpenProject(VisualStudioApp app, string slnName, PythonVersion python, out PythonProjectNode projectNode, out EnvDTE.Project dteProject)
        {
            dteProject = app.OpenProject(app.CopyProjectForTest("TestData\\Targets\\" + slnName));
            var pn   = projectNode = dteProject.GetPythonProject();
            var fact = projectNode.InterpreterFactories.Where(x => x.Configuration.Id == python.Id).FirstOrDefault();

            Assert.IsNotNull(fact, "Project does not contain expected interpreter");
            app.ServiceProvider.GetUIThread().Invoke(() => pn.ActiveInterpreter = fact);
            dteProject.Save();
        }
示例#20
0
        private string CreateAndOpenWorkspace(PythonVisualStudioApp app, PythonVersion expectedFactory) {
            var workspaceFolder = TestData.GetTempPath();
            FileUtils.CopyDirectory(TestData.GetPath("TestData", "EnvironmentsSwitcherFolders", "WorkspaceWithoutSettings"), workspaceFolder);

            app.OpenFolder(workspaceFolder);
            app.OpenDocument(Path.Combine(workspaceFolder, "app1.py"));
            CheckSwitcherEnvironment(app, expectedFactory.Configuration.Description);

            return workspaceFolder;
        }
示例#21
0
            public NavigableHelper(string code, PythonVersion version)
            {
                var factory = InterpreterFactoryCreator.CreateInterpreterFactory(version.Configuration, new InterpreterFactoryCreationOptions()
                {
                    WatchFileSystem = false
                });

                _view      = new PythonEditor("", version.Version, factory: factory);
                _view.Text = code;
            }
示例#22
0
        private static string CreateWorkspaceFolder(
            string reqsFileContents      = null,
            string envFileContents       = null,
            string settingsFileContents  = null,
            PythonVersion virtualEnvBase = null,
            string[] virtualEnvPackages  = null
            )
        {
            var workspaceFolder = TestData.GetTempPath();

            File.WriteAllText(
                Path.Combine(workspaceFolder, "main.py"),
                string.Empty,
                Encoding.UTF8
                );

            if (reqsFileContents != null)
            {
                File.WriteAllText(
                    Path.Combine(workspaceFolder, "requirements.txt"),
                    reqsFileContents,
                    Encoding.UTF8
                    );
            }

            if (envFileContents != null)
            {
                File.WriteAllText(
                    Path.Combine(workspaceFolder, "environment.yml"),
                    envFileContents,
                    Encoding.UTF8
                    );
            }

            if (settingsFileContents != null)
            {
                File.WriteAllText(
                    Path.Combine(workspaceFolder, "PythonSettings.json"),
                    settingsFileContents,
                    Encoding.UTF8
                    );
            }

            if (virtualEnvBase != null)
            {
                CreateVirtualEnvironmentWithPackages(
                    virtualEnvBase,
                    Path.Combine(workspaceFolder, "env"),
                    virtualEnvPackages
                    );
            }

            return(workspaceFolder);
        }
示例#23
0
        private static bool HasSymbols(PythonVersion interpreter)
        {
            if (interpreter == null)
            {
                return(false);
            }

            // For simplicity, require the test machine to have the .pdb files
            // next to their binaries.
            return(File.Exists(Path.ChangeExtension(interpreter.Configuration.InterpreterPath, "pdb")));
        }
示例#24
0
        private static void CreateVirtualEnvironmentWithPackages(PythonVersion baseEnv, string envFolderPath, string[] packages)
        {
            EnvironmentUITests.CreateVirtualEnvironment(baseEnv, envFolderPath);

            var envPythonExePath = Path.Combine(envFolderPath, "scripts", "python.exe");

            foreach (var package in packages.MaybeEnumerate())
            {
                using (var output = ProcessOutput.RunHiddenAndCapture(envPythonExePath, "-m", "pip", "install", package)) {
                    Assert.IsTrue(output.Wait(TimeSpan.FromSeconds(30)));
                    Assert.AreEqual(0, output.ExitCode);
                }
            }
        }
示例#25
0
 public static void AssertInstalled(this PythonVersion pyVersion)
 {
     if (pyVersion == null || !File.Exists(pyVersion.InterpreterPath))
     {
         if (pyVersion == null)
         {
             Assert.Inconclusive("Python interpreter is not installed. pyVersion is null. ");
         }
         else
         {
             Assert.Inconclusive(string.Format("Python version {0} is not installed.", pyVersion.Configuration.Version.ToString()));
         }
     }
 }
示例#26
0
        /// <summary>
        /// Creates a Python virtual environment in specified directory.
        /// </summary>
        public static void CreateVirtualEnv(this PythonVersion pyVersion, string envPath)
        {
            var virtualEnvModule = (pyVersion.Version < PythonLanguageVersion.V30) ? "virtualenv" : "venv";

            using (var p = ProcessOutput.RunHiddenAndCapture(pyVersion.InterpreterPath, "-m", virtualEnvModule, envPath))
            {
                Console.WriteLine(p.Arguments);
                Assert.IsTrue(p.Wait(TimeSpan.FromMinutes(3)));
                Console.WriteLine(string.Join(Environment.NewLine, p.StandardOutputLines.Concat(p.StandardErrorLines)));
                Assert.AreEqual(0, p.ExitCode);
            }

            Assert.IsTrue(File.Exists(Path.Combine(envPath, "scripts", "python.exe")));
        }
示例#27
0
            public NavigableHelper(string code, PythonVersion version)
            {
                var factory = InterpreterFactoryCreator.CreateInterpreterFactory(version.Configuration, new InterpreterFactoryCreationOptions()
                {
                    WatchFileSystem = false
                });

                _view = new PythonEditor("", version.Version, factory: factory);
                if (_view.Analyzer.IsAnalyzing)
                {
                    _view.Analyzer.WaitForCompleteAnalysis(_ => true);
                }
                _view.Text = code;
            }
示例#28
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
            ));
        }
示例#29
0
        /// <summary>
        /// Creates a Python virtual environment in specified directory and installs the specified packages.
        /// </summary>
        public static void CreateVirtualEnv(this PythonVersion pyVersion, string envPath, IEnumerable <string> packages)
        {
            pyVersion.CreateVirtualEnv(envPath);

            var envPythonExePath = Path.Combine(envPath, "scripts", "python.exe");

            foreach (var package in packages.MaybeEnumerate())
            {
                using (var output = ProcessOutput.RunHiddenAndCapture(envPythonExePath, "-m", "pip", "install", package))
                {
                    Assert.IsTrue(output.Wait(TimeSpan.FromSeconds(30)));
                    Assert.AreEqual(0, output.ExitCode);
                }
            }
        }
示例#30
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);
        }
示例#31
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
                       ));
        }
示例#32
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();
                }
            }
        }
示例#33
0
 /// <summary>
 /// Selects the given interpreter as the default.
 /// </summary>
 /// <remarks>
 /// This method should always be called as a using block.
 /// </remarks>
 public DefaultInterpreterSetter SelectDefaultInterpreter(PythonVersion python) {
     return new DefaultInterpreterSetter(
         InterpreterService.FindInterpreter(python.Id, python.Version.ToVersion()),
         ServiceProvider
     );
 }