Question about the 'code' module

Thomas Heller theller at
Thu Dec 29 22:10:20 CET 2005

I'm using the code module to implement an interactive interpreter
console in a GUI application, the interpreter running in a separate
thread.  To provide clean shutdown of the application, I have to make
sure that objects used in the interpreter thread are deleted when the
thread ends.

I delete the sys.last_type, sys.last_value, and sys.last_traceback
attributes which are set when an exception occured in the interpreter
thread (*).  To clean up the '_' symbol that the interpreter maintains,
I have found no other solution than to execute 'console.runsource("0")'
at the end of the thread.  Where is this symbol stored? How can I delete
it without the .runsource() call?

(*) It seems to me that the code modules usage of the sys.last_type,
sys.last_value, and sys.last_traceback attributes is not thread safe.
The docs mention that this doesn't matter because there's only one
interactive thread - which does not need to be true when using the code
module.  You could easily run several interactive interpreters at the
same time, which is exactly the purpose of this module.

Why is the (non thread-safe) sys.last_traceback used at all?  Couldn't
it be replaced with the (thread-safe) sys.exc_info()[2]?


More information about the Python-list mailing list