Nick Coghlan <ncogh...@gmail.com> added the comment: On Wed, Oct 6, 2010 at 2:59 AM, Alexander Belopolsky <rep...@bugs.python.org> wrote: > > Alexander Belopolsky <belopol...@users.sourceforge.net> added the comment: > > I am afraid, for ordinary scripts these modules effectively use option 3. I > think these modules should remove its own scaffolding from "real" __main__ > before loading any traced code. I am not sure how this can be achieved, > though.
If you use runpy.run_module or runpy.run_path, they will switch the existing __main__ out of sys.modules, replacing it with a temporary module. However, that approach is currently slightly broken, in that it leaves the temporary module namespace inaccessible if the module execution fails with an exception (hence the existence of run_module_as_main). I've thought of a few ways to fix that, but never explored any of them: - allow the module to be used for execution to be passed in to run_module and run_path as a new optional parameter - allow a list (or other mutable container) to be passed in as an output parameter, and stick the temporary module in there - define a thread-local variable for the runpy module that stores the last module namespace executed via runpy in the current thread (and a convenience API for retrieving it) Option 2 strikes me as rather clumsy, so we can probably skip that. I find option 3 to be quite elegant in a sys.exc_info() kind of way, but option 1 is probably simpler. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue9325> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com