Question about the 'code' module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Heller

    #1

    Question about the 'code' module

    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_traceb ack
    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.runsou rce("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_traceb ack 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_traceb ack used at all? Couldn't
    it be replaced with the (thread-safe) sys.exc_info()[2]?

    Thomas
  • Fernando Perez

    #2
    Re: Question about the 'code' module

    Thomas Heller wrote:
    [color=blue]
    > 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_traceb ack
    > 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.runsou rce("0")'
    > at the end of the thread. Where is this symbol stored? How can I delete
    > it without the .runsource() call?[/color]

    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

    Comment

    Working...