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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 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 @@ -169,17 +170,22 @@ def main():
else:
progname = args[0]
sys.path.insert(0, os.path.dirname(progname))
with io.open_code(progname) as fp:
code = compile(fp.read(), progname, 'exec')
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
origin=progname)
loader = importlib.machinery.SourceFileLoader("__main__", progname)
spec.loader = loader
module = importlib.util.module_from_spec(spec)
globs = {
'__spec__': spec,
'__file__': spec.origin,
'__name__': spec.name,
'__package__': None,
'__cached__': None,
'module': module
}

sys.modules["__main__"] = module
code = "__spec__.loader.exec_module(module)"
try:
runctx(code, globs, None, options.outfile, options.sort)
except BrokenPipeError as exc:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support profiling modules that import __main___, such as modules that use to pickle. The github issue has an example repro that throws an exception without this change, and succeeds with it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Support profiling modules that import __main___, such as modules that use to pickle. The github issue has an example repro that throws an exception without this change, and succeeds with it.
Support profiling modules that import ``__main__``, such as modules that use to pickle. The github issue has an example repro that throws an exception without this change, and succeeds with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Support profiling modules that import __main___, such as modules that use to pickle. The github issue has an example repro that throws an exception without this change, and succeeds with it.
Support profiling modules that import __main___, such as modules that use to pickle.

NEWS does not need this

Loading