Question about the 'code' module

T

Thomas Heller

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]?

Thomas
 
F

Fernando Perez

Thomas said:
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?

Wild guess: try defining your own sys.displayhook to manage output. This may be
done there, though I'm not really sure. Otherwise, it's done by the
interpreter internally when exec is called on a code object compiled in
'single' mode.

Cheers,

f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,241
Messages
2,571,220
Members
47,854
Latest member
RoboEx darge

Latest Threads

Top