Esempio n. 1
0
 public CJInterpreterFactory(Version version, Guid id, string description, string jPath, string jvPath, string pathEnvVar, ProcessorArchitecture arch)
 {
     if (version == default(Version)) {
         version = new Version(6, 0, 2);
     }
     _description = description;
     _id = id;
     _config = new CJInterpreterConfiguration(jPath, jvPath, pathEnvVar, arch, version);
 }
 public PythonInterpreterInformation(
     InterpreterConfiguration configuration,
     string vendor,
     string vendorUrl,
     string supportUrl
 ) {
     Configuration = configuration;
     Vendor = vendor;
     VendorUrl = vendorUrl;
     SupportUrl = supportUrl;
 }
Esempio n. 3
0
        public void RemovedModule() {
            var id = Guid.NewGuid().ToString();
            var config = new InterpreterConfiguration(id, id, version: new Version(3, 5));
            var fact = new MockPythonInterpreterFactory(config);
            var interp = new MockPythonInterpreter(fact);
            var modules = new ModuleTable(null, interp);

            var orig = modules.Select(kv => kv.Key).ToSet();

            interp.AddModule("test", new MockPythonModule("test"));

            ModuleReference modref;
            Assert.IsTrue(modules.TryImport("test", out modref));

            interp.RemoveModule("test", retainName: true);

            modules.ReInit();
            Assert.IsFalse(modules.TryImport("test", out modref));
        }
        private async Task BuiltinScrape(InterpreterConfiguration configuration)
        {
            configuration.AssertInstalled();
            var moduleUri       = TestData.GetDefaultModuleUri();
            var moduleDirectory = Path.GetDirectoryName(moduleUri.LocalPath);

            var services = await CreateServicesAsync(moduleDirectory, configuration);

            var interpreter = services.GetService <IPythonInterpreter>();

            var mod = interpreter.ModuleResolution.GetOrLoadModule(interpreter.ModuleResolution.BuiltinModuleName);
            await services.GetService <IPythonAnalyzer>().WaitForCompleteAnalysisAsync();

            Assert.IsInstanceOfType(mod, typeof(BuiltinsPythonModule));
            var modPath = interpreter.ModuleResolution.ModuleCache.GetCacheFilePath(interpreter.Configuration.InterpreterPath);

            var doc    = mod as IDocument;
            var errors = doc.GetParseErrors().ToArray();

            foreach (var err in errors)
            {
                Console.WriteLine(err);
            }
            Assert.AreEqual(0, errors.Count(), "Parse errors occurred");

            var ast = await doc.GetAstAsync();

            var seen = new HashSet <string>();

            foreach (var stmt in ((SuiteStatement)ast.Body).Statements)
            {
                if (stmt is ClassDefinition cd)
                {
                    Assert.IsTrue(seen.Add(cd.Name), $"Repeated use of {cd.Name} at index {cd.StartIndex} in {modPath}");
                }
                else if (stmt is FunctionDefinition fd)
                {
                    Assert.IsTrue(seen.Add(fd.Name), $"Repeated use of {fd.Name} at index {fd.StartIndex} in {modPath}");
                }
                else if (stmt is AssignmentStatement assign && assign.Left.FirstOrDefault() is 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 = interpreter.GetBuiltinType(v);
                type.Should().NotBeNull().And.BeAssignableTo <IPythonType>($"Did not find {v}");
                type.IsBuiltin.Should().BeTrue();
            }

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

            foreach (var id in Enum.GetNames(typeof(BuiltinTypeId)))
            {
                mod.GetMember($"__{id}").Should().BeNull(id);
            }
        }
Esempio n. 5
0
 public PythonVersion(InterpreterConfiguration config, bool ironPython = false, bool cPython = false)
 {
     Configuration = config;
     IsCPython     = cPython;
     IsIronPython  = ironPython;
 }
Esempio n. 6
0
 public void InterpreterUnloaded(object context, InterpreterConfiguration factory)
 {
 }
Esempio n. 7
0
        private async Task <IReadOnlyList <DiagnosticsEntry> > LintAsync(string code, InterpreterConfiguration configuration = null)
        {
            var analysis = await GetAnalysisAsync(code, configuration ?? PythonVersions.LatestAvailable3X);

            var a = Services.GetService <IPythonAnalyzer>();

            return(a.LintModule(analysis.Document));
        }
Esempio n. 8
0
 /// <summary>
 /// Create a PythonInterpreterView with values from an IPythonInterpreterFactory.
 /// </summary>
 public PythonInterpreterView(InterpreterConfiguration config)
 {
     _name = config.Description;
     _id   = config.Id;
     _path = config.InterpreterPath;
 }
 public AstPythonInterpreterFactory(InterpreterConfiguration config, InterpreterFactoryCreationOptions options)
     : this(config, options, string.IsNullOrEmpty(options?.DatabasePath))
 {
 }
Esempio n. 10
0
 private EnvironmentView(string id, string localizedName)
 {
     Configuration = new InterpreterConfiguration(id, id);
     Description   = LocalizedDisplayName = localizedName;
     Extensions    = new ObservableCollection <object>();
 }
 public string AddConfigurableInterpreter(string name, InterpreterConfiguration config)
 {
     throw new NotImplementedException();
 }
Esempio n. 12
0
 public AstModuleResolution(IPythonInterpreter interpreter, ConcurrentDictionary <string, IPythonModule> modules, AstModuleCache astModuleCache, InterpreterConfiguration configuration, AnalysisLogWriter log)
 {
     _interpreter    = interpreter;
     _modules        = modules;
     _astModuleCache = astModuleCache;
     _configuration  = configuration;
     _log            = log;
     _requireInitPy  = ModulePath.PythonVersionRequiresInitPyFiles(_configuration.Version);
 }
 /// <summary>
 /// Returns true if the configuration can be run. This checks whether
 /// the configured InterpreterPath value is an actual file.
 /// </summary>
 public static bool IsRunnable(this InterpreterConfiguration config)
 {
     return(config != null &&
            !InterpreterRegistryConstants.IsNoInterpretersFactory(config.Id) &&
            File.Exists(config.InterpreterPath));
 }
 /// <summary>
 /// Returns <c>true</c> if the factory should appear in the UI.
 /// </summary>
 /// <remarks>New in 2.2</remarks>
 public static bool IsUIVisible(this InterpreterConfiguration config)
 {
     return(config is VisualStudioInterpreterConfiguration vsConfig &&
            !vsConfig.UIMode.HasFlag(InterpreterUIMode.Hidden));
 }
 public static string GetWindowsInterpreterPath(this InterpreterConfiguration config)
 {
     return(config != null ? ((VisualStudioInterpreterConfiguration)config).WindowsInterpreterPath : null);
 }
 /// <summary>
 /// Returns <c>true</c> if the factory can be automatically selected as
 /// the default interpreter.
 /// </summary>
 /// <remarks>New in 2.2</remarks>
 public static bool CanBeAutoDefault(this InterpreterConfiguration config)
 {
     return(config is VisualStudioInterpreterConfiguration vsConfig &&
            !vsConfig.UIMode.HasFlag(InterpreterUIMode.CannotBeDefault) &&
            !vsConfig.UIMode.HasFlag(InterpreterUIMode.CannotBeAutoDefault));
 }
Esempio n. 17
0
 public CPythonInterpreterFactory(InterpreterConfiguration configuration, InterpreterFactoryCreationOptions options) :
     base(configuration, options)
 {
 }
Esempio n. 18
0
 internal static InterpreterFactoryCreationOptions GetCreationOptions(IServiceProvider site, InterpreterConfiguration config)
 {
     return(new InterpreterFactoryCreationOptions {
     });
 }
Esempio n. 19
0
        internal static IVsInteractiveWindow /*!*/ EnsureReplWindow(IServiceProvider serviceProvider, InterpreterConfiguration config, PythonProjectNode project)
        {
            var compModel        = serviceProvider.GetComponentModel();
            var provider         = compModel.GetService <InteractiveWindowProvider>();
            var vsProjectContext = compModel.GetService <VsProjectContextProvider>();

            var projectId = project != null?PythonReplEvaluatorProvider.GetEvaluatorId(project) : null;

            var configId = config != null?PythonReplEvaluatorProvider.GetEvaluatorId(config) : null;

            if (config?.IsRunnable() == false)
            {
                throw new MissingInterpreterException(
                          Strings.MissingEnvironment.FormatUI(config.Description, config.Version)
                          );
            }

            IVsInteractiveWindow window;

            // If we find an open window for the project, prefer that to a per-config one
            if (!string.IsNullOrEmpty(projectId))
            {
                window = provider.Open(
                    projectId,
                    e => ((e as SelectableReplEvaluator)?.Evaluator as PythonCommonInteractiveEvaluator)?.AssociatedProjectHasChanged != true
                    );
                if (window != null)
                {
                    return(window);
                }
            }

            // If we find an open window for the configuration, return that
            if (!string.IsNullOrEmpty(configId))
            {
                window = provider.Open(configId);
                if (window != null)
                {
                    return(window);
                }
            }

            // No window found, so let's create one
            if (!string.IsNullOrEmpty(projectId))
            {
                window = provider.Create(projectId);
                project.AddActionOnClose(window, w => InteractiveWindowProvider.CloseIfEvaluatorMatches(w, projectId));
            }
            else if (!string.IsNullOrEmpty(configId))
            {
                window = provider.Create(configId);
            }
            else
            {
                var interpService = compModel.GetService <IInterpreterOptionsService>();
                window = provider.Create(PythonReplEvaluatorProvider.GetEvaluatorId(interpService.DefaultInterpreter.Configuration));
            }

            return(window);
        }
Esempio n. 20
0
 protected Task <IServiceManager> CreateServicesAsync(InterpreterConfiguration configuration, IServiceManager sm = null)
 => CreateServicesAsync(TestData.GetTestSpecificRootUri().AbsolutePath, configuration, sm);
 protected Task <IServiceManager> CreateServicesAsync(InterpreterConfiguration configuration, string[] searchPaths = null)
 => CreateServicesAsync(TestData.GetTestSpecificRootPath(), configuration, null, null, searchPaths);
Esempio n. 22
0
        private static bool HasPrefixName(InterpreterConfiguration config, string name)
        {
            var current = PathUtils.GetFileOrDirectoryName(config.PrefixPath);

            return(string.CompareOrdinal(current, name) == 0);
        }
Esempio n. 23
0
 internal AstPythonInterpreterFactory(Dictionary <string, object> properties) :
     this(InterpreterConfiguration.FromDictionary(properties), InterpreterFactoryCreationOptions.FromDictionary(properties))
 {
 }
Esempio n. 24
0
 public Task <Server> CreateServer(string rootPath, InterpreterConfiguration configuration = null, Dictionary <Uri, PublishDiagnosticsEventArgs> diagnosticEvents = null)
 {
     return(CreateServer(string.IsNullOrEmpty(rootPath) ? null : new Uri(rootPath), configuration ?? Default, diagnosticEvents));
 }
Esempio n. 25
0
        internal static void ShowOptionPage(System.IServiceProvider serviceProvider, Type dialogPage, InterpreterConfiguration interpreter) {
            if (dialogPage == typeof(PythonInterpreterOptionsPage)) {
                PythonInterpreterOptionsPage.NextOptionsSelection = interpreter;
            } else if (dialogPage == typeof(PythonInteractiveOptionsPage)) {
                PythonInteractiveOptionsPage.NextOptionsSelection = interpreter;
            } else {
                throw new InvalidOperationException();
            }

            serviceProvider.ShowOptionsPage(dialogPage);
        }
Esempio n. 26
0
 internal static bool IsIronPython(this InterpreterConfiguration config)
 {
     return(config.Id.StartsWithOrdinal("IronPython|", ignoreCase: true));
 }
Esempio n. 27
0
 public UnavailableFactory(string id, string version)
 {
     Id            = Guid.Parse(id);
     Configuration = new InterpreterConfiguration(Version.Parse(version));
 }
Esempio n. 28
0
        public async Task <InitializeResult> InitializeAsync(InitializeParams @params, CancellationToken cancellationToken)
        {
            _disposableBag.ThrowIfDisposed();
            _clientCaps = @params.capabilities;
            _log        = _services.GetService <ILogger>();

            _services.AddService(new DiagnosticsService(_services));

            var cacheFolderPath = @params.initializationOptions.cacheFolderPath;
            var fs = _services.GetService <IFileSystem>();

            if (cacheFolderPath != null && !fs.DirectoryExists(cacheFolderPath))
            {
                _log?.Log(TraceEventType.Warning, Resources.Error_InvalidCachePath);
                cacheFolderPath = null;
            }

            var analyzer = new PythonAnalyzer(_services, cacheFolderPath);

            _services.AddService(analyzer);

            analyzer.AnalysisComplete += OnAnalysisComplete;
            _disposableBag.Add(() => analyzer.AnalysisComplete -= OnAnalysisComplete);

            _services.AddService(new RunningDocumentTable(_services));
            _rdt = _services.GetService <IRunningDocumentTable>();

            _rootDir = @params.rootUri != null? @params.rootUri.ToAbsolutePath() : @params.rootPath;

            if (_rootDir != null)
            {
                _rootDir = PathUtils.NormalizePath(_rootDir);
                _rootDir = PathUtils.TrimEndSeparator(_rootDir);
            }

            Version.TryParse(@params.initializationOptions.interpreter.properties?.Version, out var version);

            var configuration = new InterpreterConfiguration(null, null,
                                                             interpreterPath: @params.initializationOptions.interpreter.properties?.InterpreterPath,
                                                             version: version
                                                             )
            {
                // 1) Split on ';' to support older VS Code extension versions which send paths as a single entry separated by ';'. TODO: Eventually remove.
                // 2) Normalize paths.
                // 3) If a path isn't rooted, then root it relative to the workspace root. If _rootDir is null, then accept the path as-is.
                // 4) Trim off any ending separator for a consistent style.
                // 5) Filter out any entries which are the same as the workspace root; they are redundant. Also ignore "/" to work around the extension (for now).
                // 6) Remove duplicates.
                SearchPaths = @params.initializationOptions.searchPaths
                              .Select(p => p.Split(';', StringSplitOptions.RemoveEmptyEntries)).SelectMany()
                              .Select(PathUtils.NormalizePath)
                              .Select(p => _rootDir == null || Path.IsPathRooted(p) ? p : Path.GetFullPath(p, _rootDir))
                              .Select(PathUtils.TrimEndSeparator)
                              .Where(p => !string.IsNullOrWhiteSpace(p) && p != "/" && !p.PathEquals(_rootDir))
                              .Distinct(PathEqualityComparer.Instance)
                              .ToList(),
                TypeshedPath = @params.initializationOptions.typeStubSearchPaths.FirstOrDefault()
            };

            _interpreter = await PythonInterpreter.CreateAsync(configuration, _rootDir, _services, cancellationToken);

            _services.AddService(_interpreter);

            var fileSystem = _services.GetService <IFileSystem>();

            _indexManager = new IndexManager(fileSystem, _interpreter.LanguageVersion, _rootDir,
                                             @params.initializationOptions.includeFiles,
                                             @params.initializationOptions.excludeFiles,
                                             _services.GetService <IIdleTimeService>());
            _indexManager.IndexWorkspace().DoNotWait();
            _services.AddService(_indexManager);
            _disposableBag.Add(_indexManager);

            DisplayStartupInfo();

            _completionSource = new CompletionSource(
                ChooseDocumentationSource(_clientCaps?.textDocument?.completion?.completionItem?.documentationFormat),
                Settings.completion
                );

            _hoverSource = new HoverSource(
                ChooseDocumentationSource(_clientCaps?.textDocument?.hover?.contentFormat)
                );

            var sigInfo = _clientCaps?.textDocument?.signatureHelp?.signatureInformation;

            _signatureSource = new SignatureSource(
                ChooseDocumentationSource(sigInfo?.documentationFormat),
                sigInfo?.parameterInformation?.labelOffsetSupport == true
                );

            return(GetInitializeResult());
        }
        private async Task FullStdLibTest(InterpreterConfiguration configuration, params string[] skipModules)
        {
            configuration.AssertInstalled();
            var moduleUri       = TestData.GetDefaultModuleUri();
            var moduleDirectory = Path.GetDirectoryName(moduleUri.LocalPath);

            var services = await CreateServicesAsync(moduleDirectory, configuration);

            var interpreter = services.GetService <IPythonInterpreter>();

            var modules = ModulePath.GetModulesInLib(configuration.LibraryPath, configuration.SitePackagesPath).ToList();

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

            skip.UnionWith(new[] {
                @"matplotlib.backends._backend_gdk",
                @"matplotlib.backends._backend_gtkagg",
                @"matplotlib.backends._gtkagg",
                "test.test_pep3131",
                "test.test_unicode_identifiers",
                "test.test_super" // nonlocal syntax error
            });
            skip.UnionWith(modules.Select(m => m.FullName)
                           .Where(n => n.StartsWith(@"test.badsyntax") || n.StartsWith("test.bad_coding")));

            var anySuccess          = false;
            var anyExtensionSuccess = false;
            var anyExtensionSeen    = false;
            var anyParseError       = false;

            foreach (var m in skip)
            {
                ((MainModuleResolution)interpreter.ModuleResolution).AddUnimportableModule(m);
            }

            var set = modules
                      .Where(m => !skip.Contains(m.ModuleName))
                      .GroupBy(m => {
                var i = m.FullName.IndexOf('.');
                return(i <= 0 ? m.FullName : m.FullName.Remove(i));
            })
                      .SelectMany(g => g.Select(m => Tuple.Create(m, m.ModuleName)))
                      .ToArray();

            set = set.Where(x => x.Item2 != null && x.Item2.Contains("grammar")).ToArray();

            foreach (var r in set)
            {
                interpreter.ModuleResolution.GetOrLoadModule(r.Item2);
            }

            await services.GetService <IPythonAnalyzer>().WaitForCompleteAnalysisAsync();

            foreach (var r in set)
            {
                var modName = r.Item1;
                var mod     = interpreter.ModuleResolution.GetOrLoadModule(r.Item2);

                anyExtensionSeen |= modName.IsNativeExtension;
                switch (mod)
                {
                case null:
                    Trace.TraceWarning("failed to import {0} from {1}", modName.ModuleName, modName.SourceFile);
                    break;

                case CompiledPythonModule compiledPythonModule:
                    var errors = compiledPythonModule.GetParseErrors().ToArray();
                    if (errors.Any())
                    {
                        anyParseError = true;
                        Trace.TraceError("Parse errors in {0}", modName.SourceFile);
                        foreach (var e in errors)
                        {
                            Trace.TraceError(e.Message);
                        }
                    }
                    else
                    {
                        anySuccess           = true;
                        anyExtensionSuccess |= modName.IsNativeExtension;
                    }

                    break;

                case IPythonModule _: {
                    var filteredErrors = ((IDocument)mod).GetParseErrors().Where(e => !e.Message.Contains("encoding problem")).ToArray();
                    if (filteredErrors.Any())
                    {
                        // Do not fail due to errors in installed packages
                        if (!mod.FilePath.Contains("site-packages"))
                        {
                            anyParseError = true;
                        }
                        Trace.TraceError("Parse errors in {0}", modName.SourceFile);
                        foreach (var e in filteredErrors)
                        {
                            Trace.TraceError(e.Message);
                        }
                    }
                    else
                    {
                        anySuccess           = true;
                        anyExtensionSuccess |= modName.IsNativeExtension;
                    }

                    break;
                }
                }
            }
            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");
        }
Esempio n. 30
0
 public InterpreterOptions(PythonToolsService pyService, InterpreterConfiguration config)
 {
     _pyService = pyService;
     _config    = config;
 }
Esempio n. 31
0
 private PythonInterpreter(InterpreterConfiguration configuration)
 {
     Configuration   = configuration ?? throw new ArgumentNullException(nameof(configuration));
     LanguageVersion = Configuration.Version.ToLanguageVersion();
     UnknownType     = _builtinTypes[BuiltinTypeId.Unknown];
 }
        public void LaunchDebugTarget(IWorkspace workspace, IServiceProvider serviceProvider, DebugLaunchActionContext debugLaunchActionContext)
        {
            var registry = serviceProvider.GetComponentModel().GetService <IInterpreterRegistryService>();

            var settings       = debugLaunchActionContext.LaunchConfiguration;
            var scriptName     = settings.GetValue(ScriptNameKey, string.Empty);
            var debug          = !settings.GetValue("noDebug", false);
            var interpreterVal = settings.GetValue(InterpreterKey, string.Empty);
            var path           = interpreterVal;
            InterpreterConfiguration config = null;

            if (string.IsNullOrEmpty(scriptName))
            {
                throw new InvalidOperationException(Strings.DebugLaunchScriptNameMissing);
            }

            if (!string.IsNullOrEmpty(path) && !DefaultInterpreterValue.Equals(path, StringComparison.OrdinalIgnoreCase))
            {
                if (PathUtils.IsValidPath(path) && !Path.IsPathRooted(path))
                {
                    path = workspace.MakeRooted(path);
                }

                if (File.Exists(path))
                {
                    config = registry.Configurations.FirstOrDefault(c => c.InterpreterPath.Equals(path, StringComparison.OrdinalIgnoreCase)) ??
                             new VisualStudioInterpreterConfiguration("Custom", path, PathUtils.GetParent(path), path);
                }
                else
                {
                    config = registry.FindConfiguration(interpreterVal);
                }
            }
            else
            {
                var options     = serviceProvider.GetComponentModel().GetService <IInterpreterOptionsService>();
                var interpreter = workspace.GetInterpreterFactory(registry, options);
                interpreter.ThrowIfNotRunnable();
                config = interpreter.Configuration;
                path   = config.InterpreterPath;
            }

            if (!File.Exists(path))
            {
                throw new InvalidOperationException(Strings.DebugLaunchInterpreterMissing_Path.FormatUI(path));
            }

            var searchPaths = workspace.GetAbsoluteSearchPaths().ToList();

            var environment = new Dictionary <string, string>();

            if (settings.TryGetValue <IPropertySettings>(EnvKey, out IPropertySettings envSettings))
            {
                foreach (var keyVal in envSettings)
                {
                    environment[keyVal.Key] = keyVal.Value.ToString();
                }
            }

            string workingDir = settings.GetValue(WorkingDirectoryKey, string.Empty);

            if (string.IsNullOrEmpty(workingDir))
            {
                workingDir = workspace.MakeRooted(".");
            }
            else if (PathUtils.IsValidPath(workingDir) && !Path.IsPathRooted(workingDir))
            {
                workingDir = workspace.MakeRooted(workingDir);
            }

            var launchConfig = new LaunchConfiguration(config)
            {
                InterpreterPath      = config == null ? path : null,
                InterpreterArguments = settings.GetValue(InterpreterArgumentsKey, string.Empty),
                ScriptName           = Path.IsPathRooted(scriptName) ? scriptName : Path.Combine(workingDir, scriptName),
                ScriptArguments      = settings.GetValue(ScriptArgumentsKey, string.Empty),
                WorkingDirectory     = workingDir,
                SearchPaths          = searchPaths,
                Environment          = environment,
            };

            launchConfig.LaunchOptions[PythonConstants.EnableNativeCodeDebugging] = settings.GetValue(NativeDebuggingKey, false).ToString();

            IProjectLauncher launcher = null;
            var browserUrl            = settings.GetValue(WebBrowserUrlKey, string.Empty);

            if (!string.IsNullOrEmpty(browserUrl))
            {
                launchConfig.LaunchOptions[PythonConstants.WebBrowserUrlSetting] = browserUrl;
                launcher = new PythonWebLauncher(serviceProvider, launchConfig, launchConfig, launchConfig);
            }

            (launcher ?? new DefaultPythonLauncher(serviceProvider, launchConfig)).LaunchProject(debug);
        }
Esempio n. 33
0
 public CPythonInterpreterFactory(InterpreterConfiguration configuration, InterpreterFactoryCreationOptions options)
     : base(configuration, options.WatchLibraryForNewModules)
 {
 }
Esempio n. 34
0
 internal static string GetTemporaryId(string key, InterpreterConfiguration config)
 {
     return(GetEvaluatorId(config) + ";" + key);
 }