Issue 9914: trace/profile conflict with the use of sys.modules[name] (original) (raw)

Created on 2010-09-21 18:17 by belopolsky, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
issue5758_trace_execute_other_modules_main_v0.patch tati_alchueyr,2012-07-20 14:00 review
issue24676.py serhiy.storchaka,2015-07-21 05:57
Messages (6)
msg117090 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-09-21 18:17
The main() method of trace and profile modules attempt to emulate the environment in which traced code runs when invoked directly, but it fails in several respects. The specific problem which is the subject of this issue is that while __name__ is set to '__main__' in code globals, sys.modules['__main__'] still point to the trace/profile module. Among other problems, this conflicts, with a popular idiom used in regression test scripts: support.run_unittest(__name__) For example, $ python -m trace -c -C trace.d Lib/test/test_optparse.py ---------------------------------------------------------------------- Ran 0 tests in 0.001s OK No tests are ran because run_unittests() looks for test case classes in the trace module and finds none. This is related to #9323, so I am merging in the nosy list. See also r83393.
msg165928 - (view) Author: Tatiana Al-Chueyr (tati_alchueyr) * Date: 2012-07-20 14:00
Yesterday I've studied this problem with flavio.ribeiro, and we've started "solving" it. The result of our progress is available at: issue5758_trace_execute_other_modules_main_v0.patch The problem of our approach is that any code outside the condition "if __name__ == '__main__'" will be run twice, as we used imp.load_source to obtain trace's analyzed code and redefine sys.modules['__main__']. In order to provide a better solution, we were considering using lazy module import, e.g: http://code.activestate.com/recipes/473888-lazy-module-imports/ Any thoughts on this?
msg166466 - (view) Author: Alyssa Coghlan (ncoghlan) * (Python committer) Date: 2012-07-26 08:09
This is a tricky one. Long term, the right approach is to migrate all the "scripts that run other scripts" over to runpy, but the runpy API needs work before we can do that (see #9325). For bug fix purposes though, these modules can borrow some of runpy's infrastructure in order to fake the system state correctly. Specifically, the runpy._TempModule and runpy._ModifiedArgv0 context managers. (_TempModule may need a tweak to allow the module to be used to be passed in rather than always being implicitly created) See runpy._run_module_code for an example of how to use them.
msg247014 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-07-21 05:57
Yet one problem (with pickle) was reported in .
msg286484 - (view) Author: (bli) Date: 2017-01-30 11:08
Just to report that this bug seems responsible for failures at cProfiling some code using multiprocessing: http://stackoverflow.com/q/41892297/1878788 http://stackoverflow.com/q/11512499/1878788
msg286491 - (view) Author: Davin Potts (davin) * (Python committer) Date: 2017-01-30 14:08
Though this issue is specifically concerned with runpy APIs and their impact especially in running unittest test scripts, it's worth commenting here for people who need a workaround in the short term: code such as that shared in http://stackoverflow.com/q/41892297/1878788 can be made to run happily by creating a second script which imports the first and simply runs the test(s) from there. In the specific case of the 'forkiter.py' from http://stackoverflow.com/q/41892297/1878788, one would create a 'run_my_tests.py' with the contents: from forkiter import main if __name__ == "__main__": exit(main()) Now this invocation of cProfile runs happily because pickle is able to see the module where all the needed classes/functions were defined: python3.6 -m cProfile -o forkiter.prof ./run_my_tests.py
History
Date User Action Args
2022-04-11 14:57:06 admin set github: 54123
2017-01-30 14:08:10 davin set nosy: + davinmessages: +
2017-01-30 11:08:16 bli set nosy: + blimessages: +
2015-07-21 05:57:54 serhiy.storchaka set files: + issue24676.pyversions: + Python 2.7, Python 3.4, Python 3.5, Python 3.6, - Python 3.2nosy: + serhiy.storchakamessages: +
2015-07-21 05:52:20 serhiy.storchaka link issue24676 superseder
2012-11-13 05:00:47 eric.snow set nosy: + eric.snow
2012-07-26 08:09:03 ncoghlan set messages: +
2012-07-20 18:29:09 brett.cannon set nosy: + ncoghlan
2012-07-20 14:00:28 tati_alchueyr set files: + issue5758_trace_execute_other_modules_main_v0.patchnosy: + tati_alchueyrmessages: + keywords: + patch
2012-07-15 03:53:59 eli.bendersky set nosy: - eli.bendersky
2010-09-21 18:17:43 belopolsky create