Skip to content

Commit

Permalink
Fixes #4679 Analyzer stuck in psutil
Browse files Browse the repository at this point in the history
Uses ephemeral variables for imported module members, and only clears out non-ephemeral variables.
  • Loading branch information
zooba committed Aug 22, 2018
1 parent aae0ae5 commit 708bac7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Python/Product/Analysis/AnalysisUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ internal virtual void AnalyzeWorker(DDG ddg, CancellationToken cancel) {

foreach (var variableInfo in DeclaringModule.Scope.AllVariables) {
variableInfo.Value.ClearOldValues(ProjectEntry);
if (!variableInfo.Value.HasTypes && !variableInfo.Value.IsAssigned) {
if (!variableInfo.Value.HasTypes && !variableInfo.Value.IsAssigned && !variableInfo.Value.IsEphemeral) {
toRemove = toRemove ?? new List<KeyValuePair<string, VariableDef>>();
toRemove.Add(variableInfo);
}
Expand Down
2 changes: 1 addition & 1 deletion Python/Product/Analysis/Values/ModuleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public IEnumerable<IReferenceable> GetDefinitions(string name) {
#endregion

public IAnalysisSet GetModuleMember(Node node, AnalysisUnit unit, string name, bool addRef = true, InterpreterScope linkedScope = null, string linkedName = null) {
var importedValue = Scope.CreateVariable(node, unit, name, addRef);
var importedValue = Scope.CreateEphemeralVariable(node, unit, name, addRef);
ModuleDefinition.AddDependency(unit);

if (linkedScope != null) {
Expand Down
17 changes: 17 additions & 0 deletions Python/Tests/Analysis/AnalysisTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7169,6 +7169,23 @@ class Employee(NamedTuple):
AssertUtil.ContainsAtLeast(entry.GetMemberNames("e"), "name", "id");
}

[TestMethod, Priority(0)]
public void CrossModuleUnassignedImport() {
var version = PythonPaths.Versions.LastOrDefault(v => v.IsCPython && File.Exists(v.InterpreterPath));
version.AssertInstalled();
var entry = CreateAnalyzer(new Microsoft.PythonTools.Interpreter.Ast.AstPythonInterpreterFactory(
version.Configuration,
new InterpreterFactoryCreationOptions { DatabasePath = TestData.GetTempPath(), WatchFileSystem = false }
));
var e1 = entry.AddModule("p", "from . import m; m.X; m.Z; W = 1", "__init__.py");
var e2 = entry.AddModule("p.m", "from . import Y, W; Z = 1", "m.py");
entry.WaitForAnalysis();
Assert.AreEqual(0, entry.Analyzer.Queue.Count, "Analysis did not complete");

entry.AssertNotHasAttr(e1, "m", 0, "X", "Y");
entry.AssertHasAttr(e1, "m", 0, "Z", "W");
}

#endregion

#region Helpers
Expand Down

0 comments on commit 708bac7

Please sign in to comment.