3 from collections
import OrderedDict
7 def __init__(self
, traceback
):
11 return FrameFancy(self
.t
.tb_frame
)
13 def getLineNumber(self
):
14 return self
.t
.tb_lineno
if self
.t
is not None else None
17 return TracebackFancy(self
.t
.tb_next
)
22 str_self
= "%s @ %s" % (self
.getFrame().getName(), self
.getLineNumber())
23 return str_self
+ "\n" + self
.getNext().__str
__()
27 def __init__(self
, frame
):
28 self
.etraceback
= frame
.f_exc_traceback
29 self
.etype
= frame
.exc_type
30 self
.evalue
= frame
.f_exc_value
32 def __init__(self
, tb
, ty
, va
):
37 def getTraceback(self
):
38 return TracebackFancy(self
.etraceback
)
40 def __nonzero__(self
):
42 self
.etraceback
is not None
43 or self
.etype
is not None
44 or self
.evalue
is not None
48 return str(self
.etype
)
55 def __init__(self
, code
):
58 def getArgCount(self
):
59 return self
.c
.co_argcount
if self
.c
is not None else 0
61 def getFilename(self
):
62 return self
.c
.co_filename
if self
.c
is not None else ""
64 def getVariables(self
):
65 return self
.c
.co_varnames
if self
.c
is not None else []
68 return self
.c
.co_name
if self
.c
is not None else ""
70 def getFileName(self
):
71 return self
.c
.co_filename
if self
.c
is not None else ""
75 def __init__(self
, frame
, arginfo
):
80 args
, varargs
, kwargs
= self
.getArgs(), self
.getVarArgs(), self
.getKWArgs()
85 ret
= ret
+ ("%s = %s" % (arg
, args
[arg
]))
92 ret
= ret
+ "varargs are " + str(varargs
)
96 ret
= ret
+ "kwargs are " + str(kwargs
)
99 def getNumArgs(wantVarargs
=False, wantKWArgs
=False):
100 args
, varargs
, keywords
, values
= self
.a
102 if varargs
and wantVarargs
:
103 size
= size
+ len(self
.getVarArgs())
104 if keywords
and wantKWArgs
:
105 size
= size
+ len(self
.getKWArgs())
109 args
, _
, _
, values
= self
.a
110 argWValues
= OrderedDict()
112 argWValues
[arg
] = values
[arg
]
115 def getVarArgs(self
):
116 _
, vargs
, _
, _
= self
.a
118 return self
.f
.f_locals
[vargs
]
122 _
, _
, kwargs
, _
= self
.a
124 return self
.f
.f_locals
[kwargs
]
129 def __init__(self
, frame
):
133 return FrameFancy(self
.f
.f_back
)
135 def getLineNumber(self
):
136 return self
.f
.f_lineno
if self
.f
is not None else 0
138 def getCodeInformation(self
):
139 return CodeFancy(self
.f
.f_code
) if self
.f
is not None else None
141 def getExceptionInfo(self
):
142 return ExceptionFancy(self
.f
) if self
.f
is not None else None
145 return self
.getCodeInformation().getName() if self
.f
is not None else ""
147 def getFileName(self
):
148 return self
.getCodeInformation().getFileName() if self
.f
is not None else ""
151 return self
.f
.f_locals
if self
.f
is not None else {}
153 def getArgumentInfo(self
):
155 ArgsFancy(self
.f
, inspect
.getargvalues(self
.f
))
156 if self
.f
is not None
162 def callEvent(self
, frame
):
165 def lineEvent(self
, frame
):
168 def returnEvent(self
, frame
, retval
):
171 def exceptionEvent(self
, frame
, exception
, value
, traceback
):
174 def cCallEvent(self
, frame
, cfunct
):
177 def cReturnEvent(self
, frame
, cfunct
):
180 def cExceptionEvent(self
, frame
, cfunct
):
184 tracer_impl
= TracerClass()
187 def the_tracer_entrypoint(frame
, event
, args
):
188 if tracer_impl
is None:
191 call_retval
= tracer_impl
.callEvent(FrameFancy(frame
))
194 return the_tracer_entrypoint
195 elif event
== "line":
196 line_retval
= tracer_impl
.lineEvent(FrameFancy(frame
))
199 return the_tracer_entrypoint
200 elif event
== "return":
201 tracer_impl
.returnEvent(FrameFancy(frame
), args
)
202 elif event
== "exception":
203 exty
, exva
, extb
= args
204 exception_retval
= tracer_impl
.exceptionEvent(
205 FrameFancy(frame
), ExceptionFancy(extb
, exty
, exva
)
207 if not exception_retval
:
209 return the_tracer_entrypoint
210 elif event
== "c_call":
211 tracer_impl
.cCallEvent(FrameFancy(frame
), args
)
212 elif event
== "c_return":
213 tracer_impl
.cReturnEvent(FrameFancy(frame
), args
)
214 elif event
== "c_exception":
215 tracer_impl
.cExceptionEvent(FrameFancy(frame
), args
)
223 sys
.settrace(the_tracer_entrypoint
)
231 def callEvent(self
, frame
):
236 + frame
.getCaller().getName()
238 + str(frame
.getCaller().getLineNumber())
240 + str(frame
.getArgumentInfo())
243 def lineEvent(self
, frame
):
248 + str(frame
.getLineNumber())
250 + str(frame
.getLocals())
252 + frame
.getFileName()
255 def returnEvent(self
, frame
, retval
):
262 + str(frame
.getLocals())
265 def exceptionEvent(self
, frame
, exception
):
267 "exception %s %s raised from %s @ %s"
270 str(exception
.getValue()),
272 frame
.getLineNumber(),
275 print("tb: " + str(exception
.getTraceback()))
278 # the same functionality as LoggingTracer, but with a little more
279 # lldb-specific smarts
282 class LLDBAwareTracer
:
283 def callEvent(self
, frame
):
284 if frame
.getName() == "<module>":
286 if frame
.getName() == "run_one_line":
288 "call run_one_line(%s)"
289 % (frame
.getArgumentInfo().getArgs()["input_string"])
292 if "Python.framework" in frame
.getFileName():
293 print("call into Python at " + frame
.getName())
296 frame
.getName() == "__init__"
297 and frame
.getCaller().getName() == "run_one_line"
298 and frame
.getCaller().getLineNumber() == 101
301 strout
= "call " + frame
.getName()
302 if frame
.getCaller().getFileName() == "":
303 strout
+= " from LLDB - args are "
304 args
= frame
.getArgumentInfo().getArgs()
306 if arg
== "dict" or arg
== "internal_dict":
308 strout
= strout
+ ("%s = %s " % (arg
, args
[arg
]))
312 + frame
.getCaller().getName()
314 + str(frame
.getCaller().getLineNumber())
316 + str(frame
.getArgumentInfo())
320 def lineEvent(self
, frame
):
321 if frame
.getName() == "<module>":
323 if frame
.getName() == "run_one_line":
325 "running run_one_line(%s) @ %s"
327 frame
.getArgumentInfo().getArgs()["input_string"],
328 frame
.getLineNumber(),
332 if "Python.framework" in frame
.getFileName():
334 "running into Python at "
337 + str(frame
.getLineNumber())
344 + str(frame
.getLineNumber())
347 if frame
.getCaller().getFileName() == "":
348 locals = frame
.getLocals()
350 if local
== "dict" or local
== "internal_dict":
352 strout
= strout
+ ("%s = %s " % (local
, locals[local
]))
354 strout
= strout
+ str(frame
.getLocals())
355 strout
= strout
+ " in " + frame
.getFileName()
358 def returnEvent(self
, frame
, retval
):
359 if frame
.getName() == "<module>":
361 if frame
.getName() == "run_one_line":
363 "return from run_one_line(%s) return value is %s"
364 % (frame
.getArgumentInfo().getArgs()["input_string"], retval
)
367 if "Python.framework" in frame
.getFileName():
369 "return from Python at "
371 + " return value is "
378 + " return value is "
382 if frame
.getCaller().getFileName() == "":
383 locals = frame
.getLocals()
385 if local
== "dict" or local
== "internal_dict":
387 strout
= strout
+ ("%s = %s " % (local
, locals[local
]))
389 strout
= strout
+ str(frame
.getLocals())
390 strout
= strout
+ " in " + frame
.getFileName()
393 def exceptionEvent(self
, frame
, exception
):
394 if frame
.getName() == "<module>":
397 "exception %s %s raised from %s @ %s"
400 str(exception
.getValue()),
402 frame
.getLineNumber(),
405 print("tb: " + str(exception
.getTraceback()))
418 def print_keyword_args(**kwargs
):
419 # kwargs is a dict of the keyword args passed to the function
420 for key
, value
in kwargs
.items():
421 print("%s = %s" % (key
, value
))
424 def total(initial
=5, *numbers
, **keywords
):
426 for number
in numbers
:
429 count
+= keywords
[key
]
433 if __name__
== "__main__":
434 enable(LoggingTracer())
437 print_keyword_args(first_name
="John", last_name
="Doe")
438 total(10, 1, 2, 3, vegetables
=50, fruits
=100)