tags:

views:

88

answers:

1

Hi everyone!
Just a curiosity: is there an already-coded way to convert a printed traceback back to the exception that generated it? :) Or to a sys.exc_info-like structure?

+2  A: 

Converting a traceback to the exception object wouldn't be too hard, given common exception classes (parse the last line for the exception class and the arguments given to it at instantiation.) The traceback object (the third argument returned by sys.exc_info()) is an entirely different matter, though. The traceback object actually contains the chain of frame objects that constituted the stack at the time of the exception. Including local variables, global variables, et cetera. It is impossible to recreate that just from the displayed traceback.

The best you could do would be to parse each 'File "X", line N, in Y:' line and create fake frame objects that are almost entirely empty. There would be very little value in it, as basically the only thing you would be able to do with it would be to print it. What are you trying to accomplish?

Thomas Wouters
Yes, I know I could parse the printed traceback myself, I was looking for something that already does it :) Anyway, I was just playing with the idea to re-build a CherryPy exception (sent as an HTML page) client-side.. Thanks for your answer :)
Joril
Yes, sorry, I forgot to mention that I haven't seen anything like it, and since there is such little point to it I doubt it exists :-)
Thomas Wouters
I agree :D Thanks again!
Joril