-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-132737: Support profiling modules that import __main___ #132738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
b4f3934
e068bea
ca66a0e
1c621a0
923cb6e
75a542e
3665a7f
e6d50bd
5607cb8
8d6f37b
191445c
2fffb33
8f9b8a1
226d78d
15d8674
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -163,8 +163,8 @@ def main(): | |||
| if options.module: | ||||
| code = "run_module(modname, run_name='__main__')" | ||||
| globs = { | ||||
| 'run_module': runpy.run_module, | ||||
| 'modname': args[0] | ||||
| "run_module": runpy.run_module, | ||||
| "modname": args[0] | ||||
| } | ||||
| else: | ||||
| progname = args[0] | ||||
|
|
@@ -179,9 +179,16 @@ def main(): | |||
| '__name__': spec.name, | ||||
| '__package__': None, | ||||
| '__cached__': None, | ||||
| '__builtins__': __builtins__, | ||||
| } | ||||
| # cmd has to run in __main__ namespace (or imports from __main__ will | ||||
| # break). Clear __main__ and replace with the globals provided. | ||||
| import __main__ | ||||
| __main__.__dict__.clear() | ||||
| __main__.__dict__.update(globs) | ||||
|
|
||||
| try: | ||||
| runctx(code, globs, None, options.outfile, options.sort) | ||||
| runctx(code, __main__.__dict__, None, options.outfile, options.sort) | ||||
|
||||
| dictionary of the module __main__ is used (see the explanation of |
Ah, it's actually explicitly documented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vstinner @gaogaotiantian I did a bit more poking around and I think I managed to get rid of any of the hacky-ness. cProfile's main remains untouched, and in the case where we execute a file, I create a new module, set it as main, and ensure that the globals dict is the dict of the new module. The tests pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the current solution looks much less hacky.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Support profiling modules that import ``__main__``, such as modules that use pickle. | ||
aneeshdurg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.