Repository navigation
Expand file tree
/
Copy pathTraceBack.cs
More file actions
341 lines (290 loc) · 12.2 KB
/
Copy pathTraceBack.cs
File metadata and controls
341 lines (290 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* [email protected]. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Scripting;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime.Operations;
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Scope = "member", Target = "IronPython.Runtime.Exceptions.TraceBackFrame..ctor(System.Object,System.Object,System.Object)", MessageId = "0#globals")]
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Scope = "member", Target = "IronPython.Runtime.Exceptions.TraceBackFrame.Globals", MessageId = "Globals")]
namespace IronPython.Runtime.Exceptions {
[PythonType("traceback")]
[Serializable]
public class TraceBack {
private readonly TraceBack _next;
private readonly TraceBackFrame _frame;
private int _line;
public TraceBack(TraceBack nextTraceBack, TraceBackFrame fromFrame) {
_next = nextTraceBack;
_frame = fromFrame;
}
public TraceBack tb_next {
get {
return _next;
}
}
public TraceBackFrame tb_frame {
get {
return _frame;
}
}
public int tb_lineno {
get {
return _line;
}
}
public int tb_lasti {
get {
return 0; // not presently tracked
}
}
internal void SetLine(int lineNumber) {
_line = lineNumber;
}
/// <summary>
/// returns string containing human readable representation of traceback
/// </summary>
internal string Extract() {
var sb = new StringBuilder();
var tb = this;
while (tb != null) {
var f = tb._frame;
var lineno = tb._line;
var co = f.f_code;
var filename = co.co_filename;
var name = co.co_name;
sb.AppendFormat(" File \"{0}\", line {1}, in {2}{3}", filename, lineno, name, Environment.NewLine);
tb = tb._next;
}
return sb.ToString();
}
}
[PythonType("frame")]
[Serializable]
public class TraceBackFrame {
private readonly PythonTracebackListener _traceAdapter;
private TracebackDelegate _trace;
private object _traceObject;
internal int _lineNo;
private readonly PythonDebuggingPayload _debugProperties;
private readonly Func<IDictionary<object, object>> _scopeCallback;
private readonly PythonDictionary _globals;
private readonly object _locals;
private readonly FunctionCode _code;
private readonly CodeContext/*!*/ _context;
private readonly TraceBackFrame _back;
internal TraceBackFrame(CodeContext/*!*/ context, PythonDictionary globals, object locals, FunctionCode code) {
_globals = globals;
_locals = locals;
_code = code;
_context = context;
}
internal TraceBackFrame(CodeContext/*!*/ context, PythonDictionary globals, object locals, FunctionCode code, TraceBackFrame back) {
_globals = globals;
_locals = locals;
_code = code;
_context = context;
_back = back;
}
internal TraceBackFrame(PythonTracebackListener traceAdapter, FunctionCode code, TraceBackFrame back, PythonDebuggingPayload debugProperties, Func<IDictionary<object, object>> scopeCallback) {
_traceAdapter = traceAdapter;
_code = code;
_back = back;
_debugProperties = debugProperties;
_scopeCallback = scopeCallback;
}
[SpecialName, PropertyMethod]
public object Getf_trace() {
if (_traceAdapter != null) {
return _traceObject;
} else {
return null;
}
}
[SpecialName, PropertyMethod]
public void Setf_trace(object value) {
_traceObject = value;
_trace = (TracebackDelegate)Converter.ConvertToDelegate(value, typeof(TracebackDelegate));
}
[SpecialName, PropertyMethod]
public void Deletef_trace() {
Setf_trace(null);
}
internal CodeContext Context {
get {
return _context;
}
}
internal TracebackDelegate TraceDelegate {
get {
if (_traceAdapter != null) {
return _trace;
} else {
return null;
}
}
}
public PythonDictionary f_globals {
get {
object context;
if (_scopeCallback != null &&
_scopeCallback().TryGetValue(Compiler.Ast.PythonAst.GlobalContextName, out context) && context != null) {
return ((CodeContext)context).GlobalDict;
} else {
return _globals;
}
}
}
public object f_locals {
get {
if (_traceAdapter != null && _scopeCallback != null) {
if (_code.IsModule) {
// don't use the scope callback for locals in a module, use our globals dictionary instead
return f_globals;
}
return new PythonDictionary(new DebuggerDictionaryStorage(_scopeCallback()));
} else {
return _locals;
}
}
}
public FunctionCode f_code {
get {
return _code;
}
}
public object f_builtins {
get {
return PythonContext.GetContext(_context).BuiltinModuleDict;
}
}
public TraceBackFrame f_back {
get {
return _back;
}
}
public object f_exc_traceback {
get {
return null;
}
}
public object f_exc_type {
get {
return null;
}
}
public bool f_restricted {
get {
return false;
}
}
public object f_lineno {
get {
if (_traceAdapter != null) {
return _lineNo;
} else {
return 1;
}
}
set {
if (!(value is int)) {
throw PythonOps.ValueError("lineno must be an integer");
}
int newLineNum = (int)value;
if (_traceAdapter != null) {
SetLineNumber(newLineNum);
} else {
throw PythonOps.ValueError("f_lineno can only be set by a trace function");
}
}
}
private void SetLineNumber(int newLineNum) {
var pyThread = PythonOps.GetFunctionStackNoCreate();
if (!IsTopMostFrame(pyThread)) {
if (!TracingThisFrame(pyThread)) {
throw PythonOps.ValueError("f_lineno can only be set by a trace function");
} else {
return;
}
}
FunctionCode funcCode = _debugProperties.Code;
Dictionary<int, Dictionary<int, bool>> loopAndFinallyLocations = _debugProperties.LoopAndFinallyLocations;
Dictionary<int, bool> handlerLocations = _debugProperties.HandlerLocations;
Dictionary<int, bool> currentLoopIds = null;
bool inForLoopOrFinally = loopAndFinallyLocations != null && loopAndFinallyLocations.TryGetValue(_lineNo, out currentLoopIds);
int originalNewLine = newLineNum;
if (newLineNum < funcCode.Span.Start.Line) {
throw PythonOps.ValueError("line {0} comes before the current code block", newLineNum);
} else if (newLineNum > funcCode.Span.End.Line) {
throw PythonOps.ValueError("line {0} comes after the current code block", newLineNum);
}
while (newLineNum <= funcCode.Span.End.Line) {
var span = new SourceSpan(new SourceLocation(0, newLineNum, 1), new SourceLocation(0, newLineNum, Int32.MaxValue));
// Check if we're jumping onto a handler
bool handlerIsFinally;
if (handlerLocations != null && handlerLocations.TryGetValue(newLineNum, out handlerIsFinally)) {
throw PythonOps.ValueError("can't jump to 'except' line");
}
// Check if we're jumping into a for-loop
Dictionary<int, bool> jumpIntoLoopIds;
if (loopAndFinallyLocations != null && loopAndFinallyLocations.TryGetValue(newLineNum, out jumpIntoLoopIds)) {
// If we're not in any loop already - then we can't jump into a loop
if (!inForLoopOrFinally) {
throw BadForOrFinallyJump(newLineNum, jumpIntoLoopIds);
}
// If we're in loops - we can only jump if we're not entering a new loop
foreach (int jumpIntoLoopId in jumpIntoLoopIds.Keys) {
if (!currentLoopIds.ContainsKey(jumpIntoLoopId)) {
throw BadForOrFinallyJump(newLineNum, currentLoopIds);
}
}
} else if (currentLoopIds != null) {
foreach (bool isFinally in currentLoopIds.Values) {
if (isFinally) {
throw PythonOps.ValueError("can't jump out of 'finally block'");
}
}
}
if (_traceAdapter.PythonContext.TracePipeline.CanSetNextStatement((string)((FunctionCode)_code).co_filename, span)) {
_traceAdapter.PythonContext.TracePipeline.SetNextStatement((string)((FunctionCode)_code).co_filename, span);
_lineNo = newLineNum;
return;
}
++newLineNum;
}
throw PythonOps.ValueError("line {0} is invalid jump location ({1} - {2} are valid)", originalNewLine, funcCode.Span.Start.Line, funcCode.Span.End.Line);
}
private bool TracingThisFrame(List<FunctionStack> pyThread) {
return pyThread != null &&
pyThread.FindIndex(x => x.Frame == this) != -1;
}
private bool IsTopMostFrame(List<FunctionStack> pyThread) {
return pyThread != null && pyThread.Count != 0 && Type.ReferenceEquals(this, pyThread[pyThread.Count - 1].Frame);
}
private static Exception BadForOrFinallyJump(int newLineNum, Dictionary<int, bool> jumpIntoLoopIds) {
foreach (bool isFinally in jumpIntoLoopIds.Values) {
if (isFinally) {
return PythonOps.ValueError("can't jump into 'finally block'", newLineNum);
}
}
return PythonOps.ValueError("can't jump into 'for loop'", newLineNum);
}
}
public delegate TracebackDelegate TracebackDelegate(TraceBackFrame frame, string result, object payload);
}