public GeneratorInfo(PythonAnalyzer projectState, IPythonProjectEntry entry) : base(projectState.ClassInfos[BuiltinTypeId.Generator]) { _declaringModule = entry; _declaringVersion = entry.AnalysisVersion; Yields = new VariableDef(); Sends = new VariableDef(); Returns = new VariableDef(); }
public CoroutineInfo(PythonAnalyzer projectState, IPythonProjectEntry entry) : base(projectState.ClassInfos[BuiltinTypeId.Generator]) { // Internally, coroutines are represented by generators with a CO_* // flag on the code object. Here we represent it as a separate info, // but reuse the underlying class info. _declaringModule = entry; _declaringVersion = entry.AnalysisVersion; Returns = new VariableDef(); }
public DropDownBarClient(IWpfTextView textView, IPythonProjectEntry pythonProjectEntry) { Utilities.ArgumentNotNull("textView", textView); Utilities.ArgumentNotNull("pythonProjectEntry", pythonProjectEntry); _projectEntry = pythonProjectEntry; _projectEntry.OnNewParseTree += ParserOnNewParseTree; _textView = textView; _topLevelEntries = _nestedEntries = EmptyEntries; _dispatcher = Dispatcher.CurrentDispatcher; _textView.Caret.PositionChanged += CaretPositionChanged; }
private ReadOnlyCollection<DropDownEntryInfo> _topLevelEntries; // entries for top-level members of the file #endregion Fields #region Constructors public DropDownBarClient(IWpfTextView textView, IPythonProjectEntry classifier) { Debug.Assert(textView != null); Debug.Assert(classifier != null); _classifier = classifier; _classifier.OnNewParseTree += ParserOnNewParseTree; _textView = textView; _topLevelEntries = _nestedEntries = EmptyEntries; _dispatcher = Dispatcher.CurrentDispatcher; _textView.Caret.PositionChanged += CaretPositionChanged; }
public FunctionScope( FunctionInfo function, Node node, InterpreterScope declScope, IPythonProjectEntry declModule ) : base(function, node, declScope) { ReturnValue = new VariableDef(); if (Function.FunctionDefinition.IsCoroutine) { Coroutine = new CoroutineInfo(function.ProjectState, declModule); ReturnValue.AddTypes(function.ProjectEntry, Coroutine.SelfSet, false); } else if (Function.FunctionDefinition.IsGenerator) { Generator = new GeneratorInfo(function.ProjectState, declModule); ReturnValue.AddTypes(function.ProjectEntry, Generator.SelfSet, false); } }
internal FunctionAnalysisUnit( FunctionInfo function, AnalysisUnit declUnit, InterpreterScope declScope, IPythonProjectEntry declEntry ) : base(function.FunctionDefinition, null) { _declUnit = declUnit; Function = function; _decoratorCalls = new Dictionary<Node, Expression>(); var scope = new FunctionScope(Function, Function.FunctionDefinition, declScope, declEntry); scope.EnsureParameters(this); _scope = scope; AnalysisLog.NewUnit(this); }
public T GetValue <T>(IPythonProjectEntry module, string variable, int index = 0) where T : AnalysisValue { var rs = module.Analysis.GetValuesByIndex(variable, index).ToArray(); if (rs.Length == 0) { Assert.Fail("'{0}.{1}' had no variables".FormatInvariant(module.ModuleName, variable)); } else if (rs.Length > 1) { foreach (var r in rs) { Trace.TraceInformation(r.ToString()); } Assert.Fail("'{0}.{1}' had multiple values: {2}".FormatInvariant(module.ModuleName, variable, AnalysisSet.Create(rs))); } else { Assert.IsInstanceOfType(rs[0], typeof(T), "'{0}.{1}' was not expected type".FormatInvariant(module.ModuleName, variable)); return((T)rs[0]); } return(default(T)); }
internal FunctionAnalysisUnit( FunctionInfo function, AnalysisUnit declUnit, InterpreterScope declScope, IPythonProjectEntry declEntry, bool concreteParameters ) : base(function.FunctionDefinition, null) { _declUnit = declUnit; Function = function; _concreteParameters = concreteParameters; _decoratorCalls = new Dictionary <Node, Expression>(); var scope = new FunctionScope(Function, Function.FunctionDefinition, declScope, declEntry); _scope = scope; if (GetType() == typeof(FunctionAnalysisUnit)) { AnalysisLog.NewUnit(this); } }
internal AggregateProjectEntry AggregateWith(IPythonProjectEntry with) { if (_aggregating.Contains(with)) { // we're not adding any new types return(this); } if (_next == null) { _next = new Dictionary <IProjectEntry, AggregateProjectEntry>(); } AggregateProjectEntry entry; if (!_next.TryGetValue(with, out entry)) { // We don't yet have the next transition, create it now. _next[with] = entry = with.ProjectState.GetAggregate(_aggregating, with); } return(entry); }
private SaveLoadResult SaveLoad(PythonLanguageVersion version, params AnalysisModule[] modules) { IPythonProjectEntry[] entries = new IPythonProjectEntry[modules.Length]; var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion()); var interp = fact.CreateInterpreter(); var dbFolder = TestData.GetTempPath(randomSubPath: true); Directory.CreateDirectory(dbFolder); var state = new PythonAnalysis(fact, interp, SharedDatabaseState.BuiltinName2x); state.CreateProjectOnDisk = true; for (int i = 0; i < modules.Length; i++) { state.AddModule(modules[i].ModuleName, modules[i].Code, modules[i].Filename); } state.WaitForAnalysis(); new SaveAnalysis().Save(state.Analyzer, 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(CreateAnalyzer(loadFactory), state.CodeFolder)); }
private static IEnumerable <MemberResult> GetModuleVariables( IPythonProjectEntry entry, GetMemberOptions opts, string prefix ) { var analysis = entry?.Analysis; if (analysis == null) { yield break; } foreach (var m in analysis.GetAllAvailableMembers(SourceLocation.None, opts)) { if (m.Values.Any(v => v.DeclaringModule == entry)) { if (string.IsNullOrEmpty(prefix) || m.Name.StartsWithOrdinal(prefix, ignoreCase: true)) { yield return(m); } } } }
public void AssertAttrIsType(IPythonProjectEntry module, string variable, string memberName, params PythonMemberType[] types) { AssertAttrIsType(module, variable, memberName, 0, types); }
public void AssertIsInstance(IPythonProjectEntry module, string expr, params BuiltinTypeId[] types) { AssertIsInstance(module, expr, 0, types); }
public void AssertIsInstance(IPythonProjectEntry module, string expr) { AssertIsInstance(module, expr, 0); }
public void AssertHasAttrExact(IPythonProjectEntry module, string expr, int index, params string[] attrs) { AssertUtil.ContainsExactly(GetMemberNames(module, expr, index), attrs); }
public IOverloadResult[] GetOverrideable(IPythonProjectEntry module, int index = 0) { return(module.Analysis.GetOverrideableByIndex(index).ToArray()); }
public AnalysisValue[] GetValues(IPythonProjectEntry module, string variable, int index = 0) { return(module.Analysis.GetValuesByIndex(variable, index).ToArray()); }
public void AssertConstantEquals(IPythonProjectEntry module, string expr, int index, string value) { var val = GetValue <AnalysisValue>(module, expr, index); Assert.AreEqual(val?.GetConstantValueAsString(), value, "{0}.{1}".FormatInvariant(module.ModuleName, expr)); }
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); }
internal static bool TryGetPythonAnalysis(this ITextBuffer buffer, out IPythonProjectEntry analysis) { IProjectEntry entry; if (buffer.TryGetAnalysis(out entry) && (analysis = entry as IPythonProjectEntry) != null) { return true; } analysis = null; return false; }
public AstScopeNode(PythonAst pythonAst, IPythonProjectEntry projectEntry) { _ast = pythonAst; _projectEntry = projectEntry; }
public static void Prepare(IPythonProjectEntry entry, TextReader sourceUnit, PythonLanguageVersion version = PythonLanguageVersion.V27) { using (var parser = Parser.CreateParser(sourceUnit, version, new ParserOptions() { BindReferences = true })) { entry.UpdateTree(parser.ParseFile(), null); } }
/// <summary> /// Get Test Case Members for a class. If the class has 'test*' tests /// return those. If there aren't any 'test*' tests return (if one at /// all) the runTest overridden method /// </summary> private static IEnumerable<KeyValuePair<string, IAnalysisSet>> GetTestCaseMembers( IPythonProjectEntry entry, AnalysisValue classValue ) { var methodFunctions = classValue.GetAllMembers(entry.Analysis.InterpreterContext) .Where(v => v.Value.Any(m => m.MemberType == PythonMemberType.Function || m.MemberType == PythonMemberType.Method)); var tests = methodFunctions.Where(v => v.Key.StartsWith("test")); var runTest = methodFunctions.Where(v => v.Key.Equals("runTest")); if (tests.Any()) { return tests; } else { return runTest; } }
private static IEnumerable<AnalysisValue> GetTestCaseClasses(IPythonProjectEntry entry) { if (entry.IsAnalyzed) { return entry.Analysis.GetAllAvailableMembersByIndex(0) .SelectMany(m => entry.Analysis.GetValuesByIndex(m.Name, 0)) .Where(v => v.MemberType == PythonMemberType.Class) .Where(v => v.Mro.SelectMany(v2 => v2).Any(IsTestCaseClass)); } else { return Enumerable.Empty<AnalysisValue>(); } }
public void AssertDescription(IPythonProjectEntry module, string expr, string description) { AssertDescription(module, expr, 0, description); }
public void AssertDescriptionContains(IPythonProjectEntry module, string expr, int index, params string[] description) { var val = GetValue <AnalysisValue>(module, expr, index); AssertUtil.Contains(val?.Description, description); }
public TagInfo(string doc, IPythonProjectEntry entry) { Documentation = doc; Entry = entry; }
private static void AnalyzeCode( out PythonAnalyzer analyzer, out IPythonProjectEntry entry, string code, Version preferredVersion = null, InterpreterArchitecture preferredArch = null, string module = "test-module" ) { var provider = InterpFactory; var factory = provider.GetInterpreterFactories().OrderByDescending(f => f.Configuration.Version) .Where(f => preferredVersion == null || f.Configuration.Version == preferredVersion) .Where(f => preferredArch == null || f.Configuration.Architecture == preferredArch) .FirstOrDefault(); Assert.IsNotNull(factory, "no factory found"); analyzer = PythonAnalyzer.CreateSynchronously(factory); var path = Path.Combine(TestData.GetTempPath(randomSubPath: true), module.Replace('.', '\\')); Directory.CreateDirectory(PathUtils.GetParent(path)); File.WriteAllText(path, code); entry = analyzer.AddModule(module, path); PythonAst ast; using (var p = Parser.CreateParser(new StringReader(code), factory.GetLanguageVersion())) { ast = p.ParseFile(); entry.UpdateTree(ast, null); } entry.Analyze(CancellationToken.None, true); }
private void BufferContentTypeChanged(object sender, ContentTypeChangedEventArgs e) { _spanCache = null; _buffer.Changed -= BufferChanged; _buffer.ContentTypeChanged -= BufferContentTypeChanged; if (_entry != null) { _entry.OnNewAnalysis -= OnNewAnalysis; _entry = null; } _buffer.Properties.RemoveProperty(typeof(PythonAnalysisClassifier)); }
public IOverloadResult[] GetSignatures(IPythonProjectEntry module, string exprText, int index = 0) { return(module.Analysis.GetSignaturesByIndex(exprText, index).ToArray()); }
private static void UpdateAnalysisTree(IPythonProjectEntry pyEntry, SortedDictionary<int, ParseResult> parseResults) { IAnalysisCookie cookie = new VersionCookie( parseResults.ToDictionary( x => x.Key, x => new BufferVersion(x.Value.Version, x.Value.Ast) ) ); var asts = parseResults.Where(x => x.Value.Ast != null).Select(x => x.Value.Ast).ToArray(); PythonAst finalAst; if (asts.Length == 1) { finalAst = asts[0]; } else if (asts.Length > 0) { // multiple ASTs, merge them together finalAst = new PythonAst( new SuiteStatement( asts.Select(ast => ast.Body).ToArray() ), new NewLineLocation[0], asts[0].LanguageVersion ); } else { // we failed to get any sort of AST out, so we can't analyze... // But we need to balance the UpdateTree call, so just fetch the // last valid ast and cookie. pyEntry.GetTreeAndCookie(out finalAst, out cookie); } pyEntry.UpdateTree(finalAst, cookie); }
public void AssertNotHasAttr(IPythonProjectEntry module, string expr, int index, params string[] attrs) { AssertUtil.DoesntContain(GetMemberNames(module, expr, index), attrs); }
private async void SendParseComplete(IPythonProjectEntry entry, SortedDictionary<int, ParseResult> parseResults) { await _connection.SendEventAsync( new AP.FileParsedEvent() { fileId = ProjectEntryMap.GetId(entry), buffers = parseResults.Select( x => new AP.BufferParseInfo() { bufferId = x.Key, version = x.Value.Version, errors = x.Value.Errors.Errors.Select(MakeError).ToArray(), warnings = x.Value.Errors.Warnings.Select(MakeError).ToArray(), hasErrors = x.Value.Errors.Errors.Any(), tasks = x.Value.Tasks.ToArray() } ).ToArray() } ); }
public void AssertHasParameters(IPythonProjectEntry module, string expr, int index, params string[] paramNames) { AssertUtil.AreEqual(module.Analysis.GetSignaturesByIndex(expr, index).Single().Parameters.Select(p => p.Name), paramNames); }
public ImportStatementWalker(PythonAst ast, IPythonProjectEntry entry, PythonAnalyzer analyzer) { _ast = ast; _entry = entry; _analyzer = analyzer; }
public void AssertIsInstance(IPythonProjectEntry module, string expr, int index) { AssertIsInstance(module, expr, index, Array.Empty <BuiltinTypeId>()); }
public ImportStatementWalker(IPythonProjectEntry entry, PythonAnalyzer analyzer) { _entry = entry; _analyzer = analyzer; }
public void AssertIsInstance(IPythonProjectEntry module, string expr, params string[] className) { AssertIsInstance(module, expr, 0, className); }
public void ListenForNextNewAnalysis(IPythonProjectEntry entry) { if (entry != null && !string.IsNullOrEmpty(entry.FilePath)) { entry.OnNewAnalysis += OnNewAnalysis; } }
public void AssertAttrIsType(IPythonProjectEntry module, string variable, string memberName, int index, params PythonMemberType[] types) { AssertUtil.ContainsExactly(GetMember(module, variable, memberName, index).Select(m => m.MemberType), types); }
public SysModulesDictionaryInfo(SysModuleInfo owner, IPythonProjectEntry declaringModule, Node node) : base(declaringModule, node) { _owner = owner; }
public void AssertDescriptionContains(IPythonProjectEntry module, string expr, params string[] description) { AssertDescriptionContains(module, expr, 0, description); }
public SingleIteratorValue(VariableDef indexTypes, BuiltinClassInfo iterType, IPythonProjectEntry declModule) : base(iterType) { _indexTypes = indexTypes; _declModule = declModule; }
public void AssertConstantEquals(IPythonProjectEntry module, string expr, string value) { AssertConstantEquals(module, expr, 0, value); }
private void RegisterTag(IPythonProjectEntry entry, Dictionary<string, TagInfo> tags, string name, string documentation = null) { TagInfo tag; if (!tags.TryGetValue(name, out tag) || (String.IsNullOrWhiteSpace(tag.Documentation) && !String.IsNullOrEmpty(documentation))) { tags[name] = tag = new TagInfo(documentation, entry); if (entry != null && _hookedEntries.Add(entry)) { entry.OnNewParseTree += OnNewParseTree; } } }
public void AssertReferencesInclude(IPythonProjectEntry module, string expr, int index, params VariableLocation[] expectedVars) { AssertReferencesWorker(module, expr, index, false, expectedVars); }
private IEnumerable<IPythonProjectEntry[]> MakeModulePermutations(string prefix, string[] code) { foreach (var p in Permutations(code.Length)) { var result = new IPythonProjectEntry[code.Length]; using (var state = PythonAnalyzer.CreateSynchronously(InterpreterFactory, Interpreter)) { state.Limits = GetLimits(); for (int i = 0; i < code.Length; i++) { result[p[i]] = state.AddModule(prefix + (p[i] + 1).ToString(), "fob", null); } for (int i = 0; i < code.Length; i++) { Prepare(result[p[i]], GetSourceUnit(code[p[i]])); } for (int i = 0; i < code.Length; i++) { result[p[i]].Analyze(CancellationToken.None, true); } state.AnalyzeQueuedEntries(CancellationToken.None); yield return result; } } }
/// <summary> /// Returns a sequence of candidate absolute module names for the given /// modules. /// </summary> /// <param name="projectEntry"> /// The project entry that is importing the module. /// </param> /// <param name="relativeModuleName"> /// A dotted name identifying the path to the module. /// </param> /// <returns> /// A sequence of strings representing the absolute names of the module /// in order of precedence. /// </returns> internal static IEnumerable <string> ResolvePotentialModuleNames( IPythonProjectEntry projectEntry, string relativeModuleName, bool absoluteImports ) { string importingFrom = null; if (projectEntry != null) { importingFrom = projectEntry.ModuleName; if (ModulePath.IsInitPyFile(projectEntry.FilePath)) { if (string.IsNullOrEmpty(importingFrom)) { importingFrom = "__init__"; } else { importingFrom += ".__init__"; } } } if (string.IsNullOrEmpty(relativeModuleName)) { yield break; } // Handle relative module names if (relativeModuleName.FirstOrDefault() == '.') { if (string.IsNullOrEmpty(importingFrom)) { // No source to import relative to. yield break; } var prefix = importingFrom.Split('.'); if (relativeModuleName.LastOrDefault() == '.') { // Last part empty means the whole name is dots, so there's // nothing to concatenate. yield return(string.Join(".", prefix.Take(prefix.Length - relativeModuleName.Length))); } else { var suffix = relativeModuleName.Split('.'); var dotCount = suffix.TakeWhile(bit => string.IsNullOrEmpty(bit)).Count(); if (dotCount < prefix.Length) { // If we have as many dots as prefix parts, the entire // name will disappear. Despite what PEP 328 says, in // reality this means the import will fail. yield return(string.Join(".", prefix.Take(prefix.Length - dotCount).Concat(suffix.Skip(dotCount)))); } } yield break; } // The two possible names that can be imported here are: // * relativeModuleName // * importingFrom.relativeModuleName // and the order they are returned depends on whether // absolute_import is enabled or not. // With absolute_import, we treat the name as complete first. if (absoluteImports) { yield return(relativeModuleName); } if (!string.IsNullOrEmpty(importingFrom)) { var prefix = importingFrom.Split('.'); if (prefix.Length > 1) { var adjacentModuleName = string.Join(".", prefix.Take(prefix.Length - 1)) + "." + relativeModuleName; yield return(adjacentModuleName); } } // Without absolute_import, we treat the name as complete last. if (!absoluteImports) { yield return(relativeModuleName); } }
private SaveLoadResult SaveLoad(PythonLanguageVersion version, params AnalysisModule[] modules) { IPythonProjectEntry[] entries = new IPythonProjectEntry[modules.Length]; var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(version.ToVersion()); var interp = fact.CreateInterpreter(); var dbFolder = TestData.GetTempPath(randomSubPath: true); Directory.CreateDirectory(dbFolder); var state = new PythonAnalysis(fact, interp, SharedDatabaseState.BuiltinName2x); state.CreateProjectOnDisk = true; for (int i = 0; i < modules.Length; i++) { state.AddModule(modules[i].ModuleName, modules[i].Code, modules[i].Filename); } state.WaitForAnalysis(); new SaveAnalysis().Save(state.Analyzer, 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(CreateAnalyzer(loadFactory), state.CodeFolder); }
public void StopListening(IPythonProjectEntry entry) { if (entry != null) { entry.OnNewAnalysis -= OnNewAnalysis; _taskProvider.Clear(entry, VsProjectAnalyzer.UnresolvedImportMoniker); } }
public StarArgsDictionaryInfo(IPythonProjectEntry declaringModule, Node node) : base(declaringModule, node) { }
public ImportedModuleNameWalker(IPythonProjectEntry entry, int location, PythonAst ast) : this(entry.ModuleName, entry.FilePath, location, ast) { }
internal void UpdateProjectEntry(IProjectEntry newEntry) { if (newEntry is IPythonProjectEntry) { _projectEntry.OnNewParseTree -= ParserOnNewParseTree; _projectEntry = (IPythonProjectEntry)newEntry; _projectEntry.OnNewParseTree += ParserOnNewParseTree; } }
public IEnumerable <string> GetAllNames(IPythonProjectEntry module, int index = 0) { return(module.Analysis.GetAllAvailableMembers(SourceLocation.MinValue).Select(m => m.Name)); }
public PartialFunctionInfo(ProjectEntry declProjEntry, IAnalysisSet function, IAnalysisSet[] args, NameExpression[] keywordArgNames) { _declProjEntry = declProjEntry; _function = function; _args = args; _keywordArgNames = keywordArgNames; }
public IEnumerable <string> GetNamesNoBuiltins(IPythonProjectEntry module, int index = 0) { return(module.Analysis.GetVariablesNoBuiltinsByIndex(index)); }