Repository navigation
gh-158985: Make profile and cProfile -m run the module as __main__ - #158992
Open
sricursion wants to merge 1 commit into
Open
sricursion wants to merge 1 commit into
sricursion wants to merge 1 commit into
Conversation
Pass alter_sys=True to runpy.run_module() in the -m branch of profile and profiling.tracing, so that sys.modules['__main__'] and sys.argv[0] refer to the profiled module, as with python -m.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
python -m cProfile -m MODULEandpython -m profile -m MODULErun the module withrunpy.run_module()and its defaultalter_sys=False, sosys.modules['__main__']stays the profiler module andsys.argv[0]is the bare module name. Objects defined in the profiled module then cannot be pickled, which breaksProcessPoolExecutorandmultiprocessingwith thespawnstart method. Profiling a script path was fixed in gh-132737 and gh-140729, but the-mbranch was not changed. This passesalter_sys=Trueinprofile.main()andprofiling.tracing.main(), so the module runs as__main__withsys.argv[0]set to its path, as underpython -m. The regression test is added toProfileTest, so it also runs forprofiling.tracingthroughCProfileTest.