8000 gh-132737: Support profiling modules that import __main___ by aneeshdurg · Pull Request #132738 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 15 commits into from
Apr 24, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
less hacky
  • Loading branch information
aneeshdurg committed Apr 23, 2025
commit 2fffb33541c7b15806d1c4dc136780dbc16c85a6
26 changes: 13 additions & 13 deletions Lib/cProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import _lsprof
import importlib.machinery
import importlib.util
import io
import profile as _pyprofile

Expand Down Expand Up @@ -173,22 +174,25 @@ def main():
code = compile(fp.read(), progname, 'exec')
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
origin=progname)
globs = {
module = importlib.util.module_from_spec(spec)
# Set __main__ so that importing __main__ in the profiled code will
# return the same namespace that the code is executing under.
sys.modules['__main__'] = module
# Ensure that we're using the same __dict__ instance as the module
# for the global variables so that updates to globals are reflected
# in the module's namespace.
globs = module.__dict__
globs.update({
'__spec__': spec,
'__file__': spec.origin,
'__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, __main__.__dict__, None, options.outfile, options.sort)
runctx(code, globs, None, options.outfile, options.sort)
except BrokenPipeError as exc:
# Prevent "Exception ignored" during interpreter shutdown.
sys.stdout = None
Expand All @@ -199,8 +203,4 @@ def main():

# When invoked as main program, invoke the profiler on a script
if __name__ == '__main__':
# Since the code we run might need to modify __main__, we need to ensure
# that cProfile's main function is run under some other namespace, so we
# reimport and execute the main function separately
import cProfile
cProfile.main()
main()
Loading
0