|
21 | 21 | using Microsoft.Python.Analysis.Values;
|
22 | 22 | using Microsoft.Python.Core;
|
23 | 23 | using Microsoft.Python.Core.Disposables;
|
24 |
| -using Microsoft.Python.Core.Text; |
25 | 24 | using Microsoft.Python.Parsing.Ast;
|
26 | 25 |
|
27 | 26 | namespace Microsoft.Python.Analysis.Analyzer.Evaluation {
|
@@ -55,12 +54,12 @@ public void DeclareVariable(string name, IMember value, VariableSource source, L
|
55 | 54 | if (member != null && !overwrite) {
|
56 | 55 | return;
|
57 | 56 | }
|
58 |
| - |
| 57 | + |
59 | 58 | if (source == VariableSource.Import && value is IVariable v) {
|
60 | 59 | CurrentScope.LinkVariable(name, v, location);
|
61 | 60 | return;
|
62 | 61 | }
|
63 |
| - |
| 62 | + |
64 | 63 | if (member != null) {
|
65 | 64 | if (!value.IsUnknown()) {
|
66 | 65 | CurrentScope.DeclareVariable(name, value, source, location);
|
@@ -193,10 +192,17 @@ public void Dispose() {
|
193 | 192 | // them better.
|
194 | 193 | // TODO: figure out threading/locking for the Open/Close pairs.
|
195 | 194 | // Debug.Assert(_eval._openScopes.Count > 0, "Attempt to close global scope");
|
196 |
| - if (_eval._openScopes.Count > 0) { |
197 |
| - _eval._openScopes.Pop(); |
| 195 | + try { |
| 196 | + if (_eval._openScopes.Count > 0) { |
| 197 | + _eval._openScopes.Pop(); |
| 198 | + } |
| 199 | + _eval.CurrentScope = _eval._openScopes.Count == 0 ? _eval.GlobalScope : _eval._openScopes.Peek(); |
| 200 | + } catch (InvalidOperationException) { |
| 201 | + // Per comment above this can happen occasionally. |
| 202 | + // The catch is tactical fix to prevent crashes since complete handling of open/close |
| 203 | + // in threaded cases would be much larger change. |
| 204 | + _eval.Log?.Log(TraceEventType.Verbose, "Error: Mismatched open/close in scope tracker - scope stack is empty on Dispose()"); |
198 | 205 | }
|
199 |
| - _eval.CurrentScope = _eval._openScopes.Count == 0 ? _eval.GlobalScope : _eval._openScopes.Peek(); |
200 | 206 | }
|
201 | 207 | }
|
202 | 208 | }
|
|
0 commit comments