Example #1
0
        public void LoadAndUnloadModule()
        {
            var factories = new[] { InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(3, 3)) };
            var services  = PythonToolsTestUtilities.CreateMockServiceProvider().GetEditorServices();

            using (var analyzer = new VsProjectAnalyzer(services, factories[0])) {
                var m1Path = TestData.GetPath("TestData\\SimpleImport\\module1.py");
                var m2Path = TestData.GetPath("TestData\\SimpleImport\\module2.py");

                var taskEntry1 = analyzer.AnalyzeFileAsync(m1Path);
                var taskEntry2 = analyzer.AnalyzeFileAsync(m2Path);
                taskEntry1.Wait(CancellationTokens.After5s);
                taskEntry2.Wait(CancellationTokens.After5s);
                var entry1 = taskEntry1.Result;
                var entry2 = taskEntry2.Result;

                var cancel = CancellationTokens.After60s;
                analyzer.WaitForCompleteAnalysis(_ => !cancel.IsCancellationRequested);
                cancel.ThrowIfCancellationRequested();

                var loc = new Microsoft.PythonTools.Parsing.SourceLocation(0, 1, 1);
                AssertUtil.ContainsExactly(
                    analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    analyzer.GetValueDescriptions(entry2, "x", loc),
                    "int"
                    );

                analyzer.UnloadFileAsync(entry1).Wait();
                analyzer.WaitForCompleteAnalysis(_ => true);

                // Even though module1 has been unloaded, we still know that
                // module2 imports it.
                AssertUtil.ContainsExactly(
                    analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    analyzer.GetValueDescriptions(entry2, "x", loc)
                    );

                analyzer.AnalyzeFileAsync(m1Path).Wait();
                analyzer.WaitForCompleteAnalysis(_ => true);

                AssertUtil.ContainsExactly(
                    analyzer.GetEntriesThatImportModuleAsync("module1", true).Result.Select(m => m.moduleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    analyzer.GetValueDescriptions(entry2, "x", loc),
                    "int"
                    );
            }
        }
        public async Task CrossThreadAnalysisCalls()
        {
            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));

            var interpreterFactory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(PythonLanguageVersion.V34.ToVersion());
            var state = await PythonAnalyzer.CreateAsync(interpreterFactory, cts.Token);

            var tasks = StartCrossThreadAnalysisCalls(state, cts.Token).ToArray();

            try {
                Task.WaitAny(tasks, cts.Token);
            } catch (OperationCanceledException) {
            }
            cts.Cancel();

            ExceptionDispatchInfo firstFail = null;
            bool multipleFail = false;

            foreach (var t in tasks)
            {
                if (t.IsCanceled || t.Exception == null)
                {
                    continue;
                }

                if (multipleFail)
                {
                    Console.WriteLine(t.Exception);
                }
                else if (firstFail != null)
                {
                    Console.WriteLine(firstFail);
                    Console.WriteLine(t.Exception);
                    firstFail    = null;
                    multipleFail = true;
                }
                else if (t.Exception.InnerExceptions.Count == 1)
                {
                    firstFail = ExceptionDispatchInfo.Capture(t.Exception.InnerException);
                }
                else
                {
                    foreach (var exc in t.Exception.InnerExceptions)
                    {
                        Console.WriteLine(exc);
                    }
                    multipleFail = true;
                }
            }

            if (multipleFail)
            {
                Assert.Fail("Errors occurred. See output for details.");
            }
            else if (firstFail != null)
            {
                firstFail.Throw();
            }
        }
Example #3
0
 internal Task <VsProjectAnalyzer> CreateAnalyzerAsync(IPythonInterpreterFactory factory)
 {
     if (factory == null)
     {
         return(VsProjectAnalyzer.CreateDefaultAsync(EditorServices, InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7))));
     }
     return(VsProjectAnalyzer.CreateDefaultAsync(EditorServices, factory));
 }
Example #4
0
        public void CheckObsoleteGenerateFunction()
        {
            var path = PythonPaths.Versions.LastOrDefault(p => p != null && p.IsCPython);

            path.AssertInstalled();

            var factory = InterpreterFactoryCreator.CreateInterpreterFactory(new InterpreterFactoryCreationOptions {
                Id = path.Id,
                LanguageVersion           = path.Version.ToVersion(),
                Description               = "Test Interpreter",
                Architecture              = path.Isx64 ? ProcessorArchitecture.Amd64 : ProcessorArchitecture.X86,
                LibraryPath               = path.LibPath,
                PrefixPath                = path.PrefixPath,
                InterpreterPath           = path.InterpreterPath,
                WatchLibraryForNewModules = false
            });

            var tcs        = new TaskCompletionSource <int>();
            var beforeProc = Process.GetProcessesByName("Microsoft.PythonTools.Analyzer");

            var request = new PythonTypeDatabaseCreationRequest {
                Factory       = factory,
                OutputPath    = TestData.GetTempPath(randomSubPath: true),
                SkipUnchanged = true,
                OnExit        = tcs.SetResult
            };

            Console.WriteLine("OutputPath: {0}", request.OutputPath);

#pragma warning disable 618
            PythonTypeDatabase.Generate(request);
#pragma warning restore 618

            int expected = 0;

            if (!tcs.Task.Wait(TimeSpan.FromMinutes(1.0)))
            {
                var proc = Process.GetProcessesByName("Microsoft.PythonTools.Analyzer")
                           .Except(beforeProc)
                           .ToArray();

                // Ensure we actually started running
                Assert.AreNotEqual(0, proc.Length, "Process is not running");

                expected = -1;

                // Kill the process
                foreach (var p in proc)
                {
                    Console.WriteLine("Killing process {0}", p.Id);
                    p.Kill();
                }

                Assert.IsTrue(tcs.Task.Wait(TimeSpan.FromMinutes(1.0)), "Process did not die");
            }

            Assert.AreEqual(expected, tcs.Task.Result, "Incorrect exit code");
        }
Example #5
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;
            }
Example #6
0
        internal Task <VsProjectAnalyzer> CreateAnalyzerAsync(IPythonInterpreterFactory factory)
        {
            if (factory == null)
            {
                var configuration = new VisualStudioInterpreterConfiguration("AnalysisOnly|2.7", "Analysis Only 2.7", version: new Version(2, 7));
                factory = InterpreterFactoryCreator.CreateInterpreterFactory(configuration);
            }

            return(VsProjectAnalyzer.CreateDefaultAsync(EditorServices, factory));
        }
Example #7
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));
        }
Example #8
0
 private TestAnalyzer MakeTestAnalyzer()
 {
     return(new TestAnalyzer(
                InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7)),
                // Not real files/directories, but not an entirely fake path
                TestData.GetPath("Fob.pyproj"),
                TestData.GetPath("Fob"),
                new Uri("executor://TestOnly/v1")
                ));
 }
 private static InterpreterFactoryCreationOptions GetCreationOptions(InterpreterArchitecture arch)
 {
     return(new InterpreterFactoryCreationOptions {
         PackageManager = BuiltInPackageManagers.PipXFrames,
         DatabasePath = Path.Combine(
             PythonTypeDatabase.CompletionDatabasePath,
             InterpreterFactoryCreator.GetRelativePathForConfigurationId(GetInterpreterId(arch))
             )
     });
 }
Example #10
0
        public void CheckObsoleteGenerateFunction()
        {
            var path = PythonPaths.Versions.LastOrDefault(p => p != null && p.IsCPython);

            path.AssertInstalled();

            var factory = InterpreterFactoryCreator.CreateInterpreterFactory(path.Configuration) as PythonInterpreterFactoryWithDatabase;

            if (factory == null)
            {
                Assert.Inconclusive("Test requires PythonInterpreterFactoryWithDatabase");
            }

            var tcs        = new TaskCompletionSource <int>();
            var beforeProc = Process.GetProcessesByName("Microsoft.PythonTools.Analyzer");

            var request = new PythonTypeDatabaseCreationRequest {
                Factory       = factory,
                OutputPath    = TestData.GetTempPath(),
                SkipUnchanged = true,
                OnExit        = tcs.SetResult
            };

            Console.WriteLine("OutputPath: {0}", request.OutputPath);

#pragma warning disable 618
            PythonTypeDatabase.Generate(request);
#pragma warning restore 618

            int expected = 0;

            if (!tcs.Task.Wait(TimeSpan.FromMinutes(1.0)))
            {
                var proc = Process.GetProcessesByName("Microsoft.PythonTools.Analyzer")
                           .Except(beforeProc)
                           .ToArray();

                // Ensure we actually started running
                Assert.AreNotEqual(0, proc.Length, "Process is not running");

                expected = -1;

                // Kill the process
                foreach (var p in proc)
                {
                    Console.WriteLine("Killing process {0}", p.Id);
                    p.Kill();
                }

                Assert.IsTrue(tcs.Task.Wait(TimeSpan.FromMinutes(1.0)), "Process did not die");
            }

            Assert.AreEqual(expected, tcs.Task.Result, "Incorrect exit code");
        }
Example #11
0
        private static async Task CodeFormattingTest(string input, object selection, string expected, object expectedSelection, CodeFormattingOptions options, bool formatSelected = true)
        {
            var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7));
            var editorTestToolset = new EditorTestToolset().WithPythonToolsService();

            var services = editorTestToolset.GetPythonEditorServices();

            editorTestToolset.GetService <IPythonToolsOptionsService>().ImportFrom(options);

            using (var analyzer = await VsProjectAnalyzer.CreateForTestsAsync(services, fact))
            {
                var   analysisStartedTask = EventTaskSources.VsProjectAnalyzer.AnalysisStarted.Create(analyzer);
                var   buffer = editorTestToolset.CreatePythonTextBuffer(input, analyzer);
                var   view   = editorTestToolset.CreateTextView(buffer);
                await analysisStartedTask;

                var bi    = services.GetBufferInfo(buffer);
                var entry = await analyzer.AnalyzeFileAsync(bi.Filename);

                Assert.AreEqual(entry, bi.TrySetAnalysisEntry(entry, null), "Failed to set analysis entry");
                entry.GetOrCreateBufferParser(services).AddBuffer(buffer);

                if (formatSelected)
                {
                    var selectionSpan = new SnapshotSpan(
                        buffer.CurrentSnapshot,
                        ExtractMethodTests.GetSelectionSpan(input, selection)
                        );

                    await editorTestToolset.UIThread.InvokeTask(async() =>
                    {
                        view.Selection.Select(selectionSpan, false);
                        await EditFilter.GetOrCreate(services, view).FormatSelectionAsync();
                    });
                }
                else
                {
                    await editorTestToolset.UIThread.InvokeTask(async() =>
                    {
                        await EditFilter.GetOrCreate(services, view).FormatDocumentAsync();
                    });
                }

                Assert.AreEqual(expected, view.TextBuffer.CurrentSnapshot.GetText());
                if (expectedSelection != null)
                {
                    Assert.AreEqual(
                        ExtractMethodTests.GetSelectionSpan(expected, expectedSelection),
                        view.Selection.StreamSelectionSpan.SnapshotSpan.Span
                        );
                }
            }
        }
Example #12
0
        private DjangoAnalyzer AnalyzerTest(string path, out PythonLanguageVersion languageVersion)
        {
            var version = PythonPaths.Versions.LastOrDefault(v => Directory.Exists(Path.Combine(v.PrefixPath, "Lib", "site-packages", "django")));

            version.AssertInstalled();

            var testFact = InterpreterFactoryCreator.CreateInterpreterFactory(version.Configuration, new InterpreterFactoryCreationOptions {
                DatabasePath     = TestData.GetTempPath(),
                UseExistingCache = false,
                TraceLevel       = TraceLevel.Verbose,
                WatchFileSystem  = false
            });

            Debug.WriteLine("Testing with {0}".FormatInvariant(version.InterpreterPath));
            languageVersion = testFact.GetLanguageVersion();

            var analyzer       = PythonAnalyzer.CreateAsync(testFact).WaitAndUnwrapExceptions();
            var djangoAnalyzer = new DjangoAnalyzer();

            djangoAnalyzer.Register(analyzer);

            var entries = new List <IPythonProjectEntry>();

            foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories))
            {
                if (!ModulePath.FromBasePathAndFile_NoThrow(path, file, out var mp))
                {
                    Debug.WriteLine("Not parsing {0}".FormatInvariant(file));
                    continue;
                }
                Debug.WriteLine("Parsing {0} ({1})".FormatInvariant(mp.FullName, file));
                var entry  = analyzer.AddModule(mp.ModuleName, file);
                var parser = Parser.CreateParser(
                    new FileStream(file, FileMode.Open, FileAccess.Read),
                    testFact.GetLanguageVersion()
                    );
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                entries.Add(entry);
            }

            foreach (var entry in entries)
            {
                entry.Analyze(CancellationToken.None);
            }

            Debug.WriteLine((testFact as IPythonInterpreterFactoryWithLog)?.GetAnalysisLogContent(CultureInfo.CurrentUICulture) ?? "(no logs)");

            return(djangoAnalyzer);
        }
Example #13
0
 public IEnumerable <IPythonInterpreterFactory> GetInterpreterFactories()
 {
     yield return(InterpreterFactoryCreator.CreateInterpreterFactory(new InterpreterFactoryCreationOptions {
         LanguageVersion = new Version(2, 6),
         Description = "Python",
         InterpreterPath = _pythonExe,
         WindowInterpreterPath = _pythonWinExe,
         LibraryPath = _pythonLib,
         PathEnvironmentVariableName = "PYTHONPATH",
         Architecture = ProcessorArchitecture.X86,
         WatchLibraryForNewModules = false
     }));
 }
Example #14
0
        public void TestPthFiles()
        {
            var outputPath = TestData.GetTempPath();

            Console.WriteLine("Writing to: " + outputPath);

            // run the analyzer
            using (var output = ProcessOutput.RunHiddenAndCapture("Microsoft.PythonTools.Analyzer.exe",
                                                                  "/lib", TestData.GetPath("TestData", "PathStdLib"),
                                                                  "/version", "2.7",
                                                                  "/outdir", outputPath,
                                                                  "/indir", CompletionDB,
                                                                  "/unittest",
                                                                  "/log", "AnalysisLog.txt")) {
                output.Wait();
                Console.WriteLine("* Stdout *");
                foreach (var line in output.StandardOutputLines)
                {
                    Console.WriteLine(line);
                }
                Console.WriteLine("* Stderr *");
                foreach (var line in output.StandardErrorLines)
                {
                    Console.WriteLine(line);
                }
                Assert.AreEqual(0, output.ExitCode);
            }

            File.Copy(Path.Combine(CompletionDB, "__builtin__.idb"), Path.Combine(outputPath, "__builtin__.idb"));

            var fact  = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7));
            var paths = new List <string> {
                outputPath
            };

            paths.AddRange(Directory.EnumerateDirectories(outputPath));
            var typeDb = new PythonTypeDatabase(fact, paths);
            var module = typeDb.GetModule("SomeLib");

            Assert.IsNotNull(module, "Could not import SomeLib");
            var fobMod = typeDb.GetModule("SomeLib.fob");

            Assert.IsNotNull(fobMod, "Could not import SomeLib.fob");

            var cClass = ((IPythonModule)fobMod).GetMember(null, "C");

            Assert.IsNotNull(cClass, "Could not get SomeLib.fob.C");

            Assert.AreEqual(PythonMemberType.Class, cClass.MemberType);
        }
Example #15
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;
            }
        public void LoadAndUnloadModule()
        {
            var factories = new[] { InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(3, 3)) };

            using (var analyzer = new VsProjectAnalyzer(PythonToolsTestUtilities.CreateMockServiceProvider(), factories[0], factories)) {
                var m1Path = TestData.GetPath("TestData\\SimpleImport\\module1.py");
                var m2Path = TestData.GetPath("TestData\\SimpleImport\\module2.py");

                var entry1 = analyzer.AnalyzeFile(m1Path) as IPythonProjectEntry;
                var entry2 = analyzer.AnalyzeFile(m2Path) as IPythonProjectEntry;
                analyzer.WaitForCompleteAnalysis(_ => true);

                AssertUtil.ContainsExactly(
                    analyzer.Project.GetEntriesThatImportModule("module1", true).Select(m => m.ModuleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    entry2.Analysis.GetValuesByIndex("x", 0).Select(v => v.TypeId),
                    BuiltinTypeId.Int
                    );

                analyzer.UnloadFile(entry1);
                analyzer.WaitForCompleteAnalysis(_ => true);

                // Even though module1 has been unloaded, we still know that
                // module2 imports it.
                AssertUtil.ContainsExactly(
                    analyzer.Project.GetEntriesThatImportModule("module1", true).Select(m => m.ModuleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    entry2.Analysis.GetValuesByIndex("x", 0).Select(v => v.TypeId)
                    );

                analyzer.AnalyzeFile(m1Path);
                analyzer.WaitForCompleteAnalysis(_ => true);

                AssertUtil.ContainsExactly(
                    analyzer.Project.GetEntriesThatImportModule("module1", true).Select(m => m.ModuleName),
                    "module2"
                    );

                AssertUtil.ContainsExactly(
                    entry2.Analysis.GetValuesByIndex("x", 0).Select(v => v.TypeId),
                    BuiltinTypeId.Int
                    );
            }
        }
Example #17
0
        private SaveLoadResult SaveLoad(PythonLanguageVersion version, params AnalysisModule[] modules)
        {
            IPythonProjectEntry[] entries = new IPythonProjectEntry[modules.Length];

            var fact   = InterpreterFactory;
            var interp = Interpreter;

            if (version != fact.GetLanguageVersion())
            {
                fact   = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());
                interp = fact.CreateInterpreter();
            }

            var codeFolder = TestData.GetTempPath(randomSubPath: true);
            var dbFolder   = Path.Combine(codeFolder, "DB");

            Directory.CreateDirectory(codeFolder);
            Directory.CreateDirectory(dbFolder);

            var state = PythonAnalyzer.CreateSynchronously(fact, interp, SharedDatabaseState.BuiltinName2x);

            for (int i = 0; i < modules.Length; i++)
            {
                var fullname = Path.Combine(codeFolder, modules[i].Filename);
                File.WriteAllText(fullname, modules[i].Code);
                entries[i] = state.AddModule(modules[i].ModuleName, fullname);
                Prepare(entries[i], new StringReader(modules[i].Code), version);
            }

            for (int i = 0; i < modules.Length; i++)
            {
                entries[i].Analyze(CancellationToken.None, false);
            }

            new SaveAnalysis().Save(state, dbFolder);

            File.Copy(
                Path.Combine(PythonTypeDatabase.BaselineDatabasePath, "__builtin__.idb"),
                Path.Combine(dbFolder, "__builtin__.idb"),
                true
                );

            var loadFactory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(
                version.ToVersion(),
                null,
                dbFolder
                );

            return(new SaveLoadResult(PythonAnalyzer.CreateSynchronously(loadFactory), codeFolder));
        }
Example #18
0
        public virtual void ExecuteInReplSysArgvScriptArgs()
        {
            using (var app = new PythonVisualStudioApp())
                using (new DefaultInterpreterSetter(InterpreterFactoryCreator.CreateInterpreterFactory(Settings.Version.Configuration))) {
                    app.ServiceProvider.GetUIThread().Invoke(() => {
                        app.ServiceProvider.GetPythonToolsService().InteractiveBackendOverride = ReplWindowProxy.StandardBackend;
                    });

                    var project = app.OpenProject(@"TestData\SysArgvScriptArgsRepl.sln");

                    using (var interactive = app.ExecuteInInteractive(project, Settings)) {
                        interactive.WaitForTextEnd(@"Program.py', '-source', 'C:\\Projects\\BuildSuite', '-destination', 'C:\\Projects\\TestOut', '-pattern', '*.txt', '-recurse', 'true']", ">");
                    }
                }
        }
 public IEnumerable<IPythonInterpreterFactory> GetInterpreterFactories() {
     yield return InterpreterFactoryCreator.CreateInterpreterFactory(new VisualStudioInterpreterConfiguration(
         "Test Interpreter",
         "Python 2.6 32-bit",
         PathUtils.GetParent(_pythonExe),
         _pythonExe,
         _pythonWinExe,
         "PYTHONPATH",
         InterpreterArchitecture.x86,
         new Version(2, 6),
         InterpreterUIMode.CannotBeDefault
     ), new InterpreterFactoryCreationOptions {
         WatchFileSystem = false
     });
 }
Example #20
0
        public void AnalyzeBadEgg()
        {
            var factories = new[] { InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(3, 4)) };

            using (var analyzer = new VsProjectAnalyzer(PythonToolsTestUtilities.CreateMockServiceProvider(), factories[0], factories)) {
                analyzer.AnalyzeZipArchive(TestData.GetPath(@"TestData\BadEgg.egg"));
                analyzer.WaitForCompleteAnalysis(_ => true);

                // Analysis result must contain the module for the filename inside the egg that is a valid identifier,
                // and no entries for the other filename which is not.
                var moduleNames = analyzer.Project.Modules.Select(kv => kv.Key);
                AssertUtil.Contains(moduleNames, "module");
                AssertUtil.DoesntContain(moduleNames, "42");
            }
        }
Example #21
0
        public virtual void ExecuteInReplSysArgv()
        {
            using (var app = new PythonVisualStudioApp())
                using (new DefaultInterpreterSetter(InterpreterFactoryCreator.CreateInterpreterFactory(Settings.Version.Configuration))) {
                    app.ServiceProvider.GetUIThread().Invoke(() => {
                        app.ServiceProvider.GetPythonToolsService().InteractiveBackendOverride = ReplWindowProxy.StandardBackend;
                    });

                    var project = app.OpenProject(@"TestData\SysArgvRepl.sln");

                    using (var interactive = app.ExecuteInInteractive(project, Settings)) {
                        interactive.WaitForTextEnd("Program.py']", ">");
                    }
                }
        }
Example #22
0
        private Task <VsProjectAnalyzer> CreateAnalyzerAsync()
        {
            var interpreters = _interpreterOptionsService.Value;

            // may not available in some test cases
            if (interpreters == null)
            {
                return(VsProjectAnalyzer.CreateDefaultAsync(EditorServices, InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7))));
            }

            var defaultFactory = interpreters.DefaultInterpreter;

            EnsureCompletionDb(defaultFactory);
            return(VsProjectAnalyzer.CreateDefaultAsync(EditorServices, defaultFactory));
        }
Example #23
0
        private VsProjectAnalyzer CreateAnalyzer()
        {
            var interpreters = _interpreterOptionsService.Value;

            // may not available in some test cases
            if (interpreters == null)
            {
                return(new VsProjectAnalyzer(_container, InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7))));
            }

            var defaultFactory = interpreters.DefaultInterpreter;

            EnsureCompletionDb(defaultFactory);
            return(new VsProjectAnalyzer(_container, defaultFactory));
        }
Example #24
0
        public virtual void ExecuteInReplUnicodeFilename()
        {
            using (var app = new PythonVisualStudioApp())
                using (new DefaultInterpreterSetter(InterpreterFactoryCreator.CreateInterpreterFactory(Settings.Version.Configuration))) {
                    app.ServiceProvider.GetUIThread().Invoke(() => {
                        app.ServiceProvider.GetPythonToolsService().InteractiveBackendOverride = ReplWindowProxy.StandardBackend;
                    });

                    var project = app.OpenProject(PythonTestData.GetUnicodePathSolution());

                    using (var interactive = app.ExecuteInInteractive(project, Settings)) {
                        interactive.WaitForTextEnd("hello world from unicode path", ">");
                    }
                }
        }
Example #25
0
        public void AnalyzeBadEgg()
        {
            var factories = new[] { InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(3, 4)) };
            var services  = PythonToolsTestUtilities.CreateMockServiceProvider().GetEditorServices();

            using (var analyzer = new VsProjectAnalyzer(services, factories[0])) {
                analyzer.SetSearchPathsAsync(new[] { TestData.GetPath(@"TestData\BadEgg.egg") }).Wait();
                analyzer.WaitForCompleteAnalysis(_ => true);

                // Analysis result must contain the module for the filename inside the egg that is a valid identifier,
                // and no entries for the other filename which is not.
                var moduleNames = analyzer.GetModulesResult(true).Result.Select(x => x.Name);
                AssertUtil.Contains(moduleNames, "module");
                AssertUtil.DoesntContain(moduleNames, "42");
            }
        }
Example #26
0
        private async Task <PythonAnalyzer> CreateAnalyzer(PythonInitializationOptions.Interpreter interpreter)
        {
            IPythonInterpreterFactory factory = null;

            if (!string.IsNullOrEmpty(interpreter.assembly) && !string.IsNullOrEmpty(interpreter.typeName))
            {
                try {
                    var assembly = File.Exists(interpreter.assembly) ? AssemblyName.GetAssemblyName(interpreter.assembly) : new AssemblyName(interpreter.assembly);
                    var type     = Assembly.Load(assembly).GetType(interpreter.typeName, true);

                    factory = (IPythonInterpreterFactory)Activator.CreateInstance(
                        type,
                        BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
                        null,
                        new object[] { interpreter.properties },
                        CultureInfo.CurrentCulture
                        );
                } catch (Exception ex) {
                    LogMessage(MessageType.Warning, ex.ToString());
                }
            }
            else
            {
                factory = new AstPythonInterpreterFactory(interpreter.properties);
            }

            if (factory == null)
            {
                Version v;
                if (!Version.TryParse(interpreter.version ?? "0.0", out v))
                {
                    v = new Version();
                }
                factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(v);
            }

            var interp = factory.CreateInterpreter();

            if (interp == null)
            {
                throw new InvalidOperationException("Failed to create interpreter");
            }

            LogMessage(MessageType.Info, $"Created {interp.GetType().FullName} instance from {factory.GetType().FullName}");

            return(await PythonAnalyzer.CreateAsync(factory, interp));
        }
Example #27
0
        public void ShouldWarnOnRun()
        {
            var sln = new ProjectDefinition(
                "HelloWorld",
                PythonProject,
                Compile("app", "print \"hello\"")
                ).Generate();

            using (var vs = sln.ToMockVs())
                using (var analyzerChanged = new AutoResetEvent(false)) {
                    var project = vs.GetProject("HelloWorld").GetPythonProject();
                    project.ProjectAnalyzerChanged += (s, e) => analyzerChanged.Set();

                    var v27 = InterpreterFactoryCreator.CreateInterpreterFactory(new InterpreterFactoryCreationOptions {
                        LanguageVersion = new Version(2, 7),
                        PrefixPath      = "C:\\Python27",
                        InterpreterPath = "C:\\Python27\\python.exe"
                    });
                    var v34 = InterpreterFactoryCreator.CreateInterpreterFactory(new InterpreterFactoryCreationOptions {
                        LanguageVersion = new Version(3, 4),
                        PrefixPath      = "C:\\Python34",
                        InterpreterPath = "C:\\Python34\\python.exe"
                    });

                    var uiThread = (UIThreadBase)project.GetService(typeof(UIThreadBase));

                    uiThread.Invoke(() => {
                        project.Interpreters.AddInterpreter(v27);
                        project.Interpreters.AddInterpreter(v34);
                    });

                    project.SetInterpreterFactory(v27);
                    Assert.IsTrue(analyzerChanged.WaitOne(10000), "Timed out waiting for analyzer change #1");
                    uiThread.Invoke(() => project.GetAnalyzer()).WaitForCompleteAnalysis(_ => true);
                    Assert.IsFalse(project.ShouldWarnOnLaunch, "Should not warn on 2.7");

                    project.SetInterpreterFactory(v34);
                    Assert.IsTrue(analyzerChanged.WaitOne(10000), "Timed out waiting for analyzer change #2");
                    uiThread.Invoke(() => project.GetAnalyzer()).WaitForCompleteAnalysis(_ => true);
                    Assert.IsTrue(project.ShouldWarnOnLaunch, "Expected warning on 3.4");

                    project.SetInterpreterFactory(v27);
                    Assert.IsTrue(analyzerChanged.WaitOne(10000), "Timed out waiting for analyzer change #3");
                    uiThread.Invoke(() => project.GetAnalyzer()).WaitForCompleteAnalysis(_ => true);
                    Assert.IsFalse(project.ShouldWarnOnLaunch, "Expected warning to go away on 2.7");
                }
        }
Example #28
0
        public PythonAnalysis ProcessText(
            string text,
            PythonLanguageVersion version = PythonLanguageVersion.None,
            bool allowParseErrors         = false
            )
        {
            // TODO: Analyze against multiple versions when the version is None
            if (version == PythonLanguageVersion.None)
            {
                return(ProcessTextV2(text, allowParseErrors));
            }

            var analysis = CreateAnalyzer(InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion()), allowParseErrors);

            analysis.AddModule("test-module", text).WaitForCurrentParse();
            analysis.WaitForAnalysis();
            return(analysis);
        }
Example #29
0
            public ClassifierHelper(string code, PythonLanguageVersion version)
            {
                _vs = new MockVs();

                var reg       = _vs.ContentTypeRegistry;
                var providers = _vs.ComponentModel.GetExtensions <IClassifierProvider>().ToArray();

                _provider1 = providers.OfType <PythonClassifierProvider>().Single();
                _provider2 = providers.OfType <PythonAnalysisClassifierProvider>().Single();

                var factory = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion());

                _analyzer = new VsProjectAnalyzer(_vs.ServiceProvider, factory, new[] { factory });

                _view = _vs.CreateTextView(PythonCoreConstants.ContentType, code, v => {
                    v.TextView.TextBuffer.Properties.AddProperty(typeof(VsProjectAnalyzer), _analyzer);
                });
            }
Example #30
0
        private DjangoAnalyzer AnalyzerTest(string path)
        {
            string djangoDbPath = TestData.GetPath("TestData\\DjangoDB");

            Assert.IsTrue(
                PythonTypeDatabase.IsDatabaseVersionCurrent(djangoDbPath),
                "TestData\\DjangoDB needs updating."
                );

            var testFact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(
                new Version(2, 7),
                "Django Test Interpreter",
                TestData.GetPath("CompletionDB"),
                djangoDbPath
                );

            var            serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
            PythonAnalyzer analyzer        = new PythonAnalyzer(testFact);
            DjangoAnalyzer djangoAnalyzer  = new DjangoAnalyzer(serviceProvider);

            djangoAnalyzer.OnNewAnalyzer(analyzer);

            analyzer.AddAnalysisDirectory(path);

            List <IPythonProjectEntry> entries = new List <IPythonProjectEntry>();

            foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories))
            {
                var entry  = analyzer.AddModule(ModulePath.FromFullPath(file).ModuleName, file);
                var parser = Parser.CreateParser(
                    new FileStream(file, FileMode.Open, FileAccess.Read),
                    PythonLanguageVersion.V27
                    );
                entry.UpdateTree(parser.ParseFile(), null);
                entries.Add(entry);
            }

            foreach (var entry in entries)
            {
                entry.Analyze(CancellationToken.None, false);
            }

            return(djangoAnalyzer);
        }