Rechecking values ​​in Traceback

I am trying to conduct a thorough re-examination of the trace and get the actual values ​​from the objects that do not return more (better?) Information along with the trace.

A random script is a function that I import and execute that looks like this:

def foo(): a = True b = False assert a == b 

And it starts as:

 from foo import foo def re_inspect(): try: foo() except Exception, e: # re-inspect traceback and check `a` and `b` 

When an AssertionError occurs, if I try to evaluate the line in which the exception occurs, I (of course) cannot say that a or b ( NameError immediately occurs) because I lack the code context.

Note that I do not have access to a and b , as the above code is imported and then executed. Since foo does not live in the current namespace, my problem is getting the correct values ​​from the foo context.

What would be the right approach to be able to say that a and b so that y can safely say something like: "a - Truth is a lie"?

+4
source share
1 answer

You can use the validation module:

 try: foo() except AssertionError, e: import inspect previous_trace = inspect.trace()[1] frame = previous_trace[0] print 'value of a, b:', inspect.getargvalues(frame).locals 

See http://docs.python.org/library/inspect.html#inspect.getargvalues

+5
source

Source: https://habr.com/ru/post/1342132/


All Articles