Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
- Fix issue microsoft#95: from ... import shows incorrect completions
Browse files Browse the repository at this point in the history
- Fix issue microsoft#109: Reloading modules leaks memory
- Fix issue microsoft#278: Huge memory usage when analysis 9 lines python code
- Some code clean-up
  • Loading branch information
AlexanderSher committed Oct 18, 2018
1 parent 6c92792 commit 10a82bf
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 174 deletions.
4 changes: 2 additions & 2 deletions src/Analysis/Engine/Impl/Analyzer/DDG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,14 @@ private bool TryImportModule(string modName, bool forceAbsolute, out ModuleRefer
if (gotAllParents && ProjectState.Modules.TryImport(name, out modRef)) {
moduleRef = modRef;
(lastParent as BuiltinModule)?.AddChildModule(remainingParts[0], moduleRef.AnalysisModule);
_unit.DeclaringModule.AddModuleReference(moduleRef);
_unit.DeclaringModule.AddModuleReference(modName, moduleRef);
remainingParts = null;
return true;
}
}

if (moduleRef?.Module != null) {
_unit.DeclaringModule.AddModuleReference(moduleRef);
_unit.DeclaringModule.AddModuleReference(modName, moduleRef);
return true;
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/Analysis/Engine/Impl/Infrastructure/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ public static void FieldType<T>(string fieldName, object fieldValue) {

[DebuggerStepThrough]
public static void ArgumentOfType<T>(string argumentName, object argument, [CallerMemberName] string callerName = null) {
ArgumentNull(argumentName, argument);
ArgumentNotNull(argumentName, argument);

if (!(argument is T)) {
throw new ArgumentException($"Argument {argumentName} of method {callerName} must be of type {typeof(T)}");
}
}

[DebuggerStepThrough]
public static void ArgumentNull(string argumentName, object argument) {
public static void ArgumentNotNull(string argumentName, object argument) {
if (argument is null) {
throw new ArgumentNullException(argumentName);
}
}

[DebuggerStepThrough]
public static void ArgumentNotNullOrEmpty(string argumentName, string argument) {
ArgumentNull(argumentName, argument);
ArgumentNotNull(argumentName, argument);

if (string.IsNullOrEmpty(argument)) {
throw new ArgumentException(argumentName);
Expand Down
4 changes: 2 additions & 2 deletions src/Analysis/Engine/Impl/Infrastructure/Disposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class Disposable {
/// <returns>The disposable object that runs the given action upon disposal.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="dispose" /> is null.</exception>
public static IDisposable Create(Action dispose) {
Check.ArgumentNull(nameof(dispose), dispose);
Check.ArgumentNotNull(nameof(dispose), dispose);
return new AnonymousDisposable(dispose);
}

Expand All @@ -41,7 +41,7 @@ public static IDisposable Create(Action dispose) {
/// <returns>The disposable object that disposes wrapped object.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="disposable" /> is null.</exception>
public static IDisposable Create(IDisposable disposable) {
Check.ArgumentNull(nameof(disposable), disposable);
Check.ArgumentNotNull(nameof(disposable), disposable);
return new DisposableWrapper(disposable);
}

Expand Down
16 changes: 9 additions & 7 deletions src/Analysis/Engine/Impl/MemberResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,17 @@ private PythonMemberType GetMemberType() {
return result;
}

public override bool Equals(object obj) {
if (!(obj is MemberResult)) {
return false;
public bool Equals(MemberResult other) => string.Equals(Name, other.Name) && Equals(Scope, other.Scope);

public override bool Equals(object obj) => obj is MemberResult other && Equals(other);

public override int GetHashCode() {
unchecked {
return ((Name != null ? Name.GetHashCode() : 0) * 397) ^ (Scope != null ? Scope.GetHashCode() : 0);
}
return Name == ((MemberResult)obj).Name;
}

public static bool operator ==(MemberResult x, MemberResult y) => x.Name == y.Name;
public static bool operator !=(MemberResult x, MemberResult y) => x.Name != y.Name;
public override int GetHashCode() => Name.GetHashCode();
public static bool operator ==(MemberResult left, MemberResult right) => left.Equals(right);
public static bool operator !=(MemberResult left, MemberResult right) => !left.Equals(right);
}
}
8 changes: 3 additions & 5 deletions src/Analysis/Engine/Impl/Parsing/Ast/DottedName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ namespace Microsoft.PythonTools.Parsing.Ast {
public class DottedName : Node {
private readonly NameExpression[] _names;

public DottedName(NameExpression[] names) {
public DottedName(NameExpression[]/*!*/ names) {
_names = names;
}

public IList<NameExpression> Names {
get { return _names; }
}
public IList<NameExpression> Names => _names;

public virtual string MakeString() {
if (_names.Length == 0) return String.Empty;
if (_names.Length == 0) return string.Empty;

StringBuilder ret = new StringBuilder(_names[0].Name);
for (int i = 1; i < _names.Length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class FromImportStatement : Statement {
private static readonly string[] _star = new[] { "*" };
private PythonVariable[] _variables;

public FromImportStatement(ModuleName root, NameExpression/*!*/[] names, NameExpression[] asNames, bool fromFuture, bool forceAbsolute, int importIndex) {
public FromImportStatement(ModuleName/*!*/ root, NameExpression/*!*/[] names, NameExpression[] asNames, bool fromFuture, bool forceAbsolute, int importIndex) {
Root = root;
Names = names;
AsNames = asNames;
Expand Down
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/Parsing/Ast/ModuleName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Microsoft.PythonTools.Parsing.Ast {
public class ModuleName : DottedName {
public ModuleName(NameExpression[] names)
public ModuleName(NameExpression[]/*!*/ names)
: base(names) {
}
}
Expand Down
18 changes: 5 additions & 13 deletions src/Analysis/Engine/Impl/Parsing/Ast/RelativeModuleName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,18 @@

namespace Microsoft.PythonTools.Parsing.Ast {
public class RelativeModuleName : ModuleName {
private readonly int _dotCount;

public RelativeModuleName(NameExpression[] names, int dotCount)
public RelativeModuleName(NameExpression[]/*!*/ names, int dotCount)
: base(names) {
_dotCount = dotCount;
DotCount = dotCount;
}

public override string MakeString() {
return new string('.', DotCount) + base.MakeString();
}
public override string MakeString() => new string('.', DotCount) + base.MakeString();

public int DotCount {
get {
return _dotCount;
}
}
public int DotCount { get; }

internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
var whitespace = this.GetListWhiteSpace(ast);
for (int i = 0; i < _dotCount; i++) {
for (int i = 0; i < DotCount; i++) {
if (whitespace != null) {
res.Append(whitespace[i]);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Analysis/Engine/Impl/ProjectEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ private void Parse(bool enqueueOnly, CancellationToken cancel) {
MyScope.Scope.ClearNodeValues();
MyScope.Scope.ClearLinkedVariables();
MyScope.Scope.ClearVariables();
MyScope.ClearReferencedModules();
MyScope.ClearUnresolvedModules();
_unit.State.ClearDiagnostics(this);

Expand Down Expand Up @@ -366,10 +367,7 @@ public void Dispose() {
}
}

foreach (var moduleReference in MyScope.ModuleReferences.ToList()) {
MyScope.RemoveModuleReference(moduleReference);
}

MyScope.ClearReferencedModules();
NewParseTree -= NewParseTree;
NewAnalysis -= NewAnalysis;
}
Expand Down
Loading

0 comments on commit 10a82bf

Please sign in to comment.