I've since found a work around, but still want to know the answer.
-
4Then you should explain what exactly you are trying to do. You can convert your traceback to a string using traceback.format_exec(). Strings are eminently pickleable. – dusktreader May 26 '11 at 00:41
-
I'm not necessarily complaining, but I am interested in why this was down-voted exactly. Out of 1) lack of research, 2) unclear, or 3) not useful, I guess I'd chose (1) if I had to, but does that mean I should list all the Google results and books that *didn't* contain the answer I was after? – Trindaz May 27 '11 at 05:51
-
1@Trindaz I haven't downvoted, but if I had to guess at the provenance of the three -1s, I'd say it was due to the lack of detail in your question. Why are you trying to pickle a traceback? What was the work-around you found? – Benjamin Hodgson Oct 08 '13 at 18:53
-
4What work around did you find? – coldfix Mar 10 '14 at 21:07
-
There is a library called [eliot](https://github.com/itamarst/eliot/) that does a very nice job at logging tracebacks for you. Here's how eliot does traceback logging: https://github.com/itamarst/eliot/blob/master/eliot/_traceback.py#L82 – Nathaniel Jones Aug 22 '20 at 00:41
-
related: https://stackoverflow.com/questions/141802/how-do-i-dump-an-entire-python-process-for-later-debugging-inspection – Wilem2 Jan 10 '21 at 17:54
3 Answers
The traceback holds references to the stack frames of each function/method that was called on the current thread, from the topmost-frame on down to the point where the error was raised. Each stack frame also holds references to the local and global variables in effect at the time each function in the stack was called.
Since there is no way for pickle to know what to serialize and what to ignore, if you were able to pickle a traceback you'd end up pickling a moving snapshot of the entire application state: as pickle runs, other threads may be modifying the values of shared variables.
One solution is to create a picklable object to walk the traceback and extract only the information you need to save.

- 37,113
- 6
- 107
- 103
-
1Thanks for the super clear answer.. makes total sense. My dream of shipping LogRecord transparently across the network is shattered, but now I understand why it can't be done... – little_birdie Jun 22 '16 at 21:07
You can use tblib
try:
1 / 0
except Exception as e:
raise Exception("foo") from e
except Exception as e:
s = pickle.dumps(e)
raise pickle.loads(s)

- 51
- 3
I guess you are interested in saving the complete call context (traceback + globals + locals of each frame).
That would be very useful to determine a difference of behavior of the same function in two different call contexts, or to build your own advanced tools to process, show or compare those tracebacks.
The problem is that pickl doesn't know how to serialize all type of objects that could be in the locals or globals.
I guess you can build your own object and save it, filtering out all those objects that are not picklabe. This code can serve as basis:
import sys, traceback
def print_exc_plus():
"""
Print the usual traceback information, followed by a listing of all the
local variables in each frame.
"""
tb = sys.exc_info()[2]
while 1:
if not tb.tb_next:
break
tb = tb.tb_next
stack = []
f = tb.tb_frame
while f:
stack.append(f)
f = f.f_back
stack.reverse()
traceback.print_exc()
print "Locals by frame, innermost last"
for frame in stack:
print
print "Frame %s in %s at line %s" % (frame.f_code.co_name,
frame.f_code.co_filename,
frame.f_lineno)
for key, value in frame.f_locals.items():
print "\t%20s = " % key,
#We have to be careful not to cause a new error in our error
#printer! Calling str() on an unknown object could cause an
#error we don't want.
try:
print value
except:
print "<ERROR WHILE PRINTING VALUE>"
but instead of printing the objects you can add them to a list with your own pickable representation ( a json or yml format might be better).
Maybe you want to load all this call context in order to reproduce the same situation for your function without run the complicated workflow that generate it. I don't know if this can be done (because of memory references), but in that case you would need to de-serialize it from your format.

- 4,431
- 3
- 34
- 42
-
More here: http://code.activestate.com/recipes/52215-get-more-information-from-tracebacks/ – yucer Dec 14 '17 at 12:49