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
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
14 changes: 12 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 @@ -173,13 +174,22 @@ 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,
}
})

try:
runctx(code, globs, None, options.outfile, options.sort)
except BrokenPipeError as exc:
Expand Down
17 changes: 16 additions & 1 deletion Lib/test/test_cprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

# rip off all interesting stuff from test_profile
import cProfile
import tempfile
import textwrap
from test.test_profile import ProfileTest, regenerate_expected_output
from test.support.script_helper import assert_python_failure
from test.support.script_helper import assert_python_failure, assert_python_ok
from test import support


Expand Down Expand Up @@ -154,6 +156,19 @@ def test_sort(self):
self.assertGreater(rc, 0)
self.assertIn(b"option -s: invalid choice: 'demo'", err)

def test_profile_script_importing_main(self):
"""Check that scripts that reference __main__ see their own namespace
when being profiled."""
with tempfile.NamedTemporaryFile("w+", delete_on_close=False) as f:
f.write(textwrap.dedent("""\
class Foo:
pass
import __main__
assert Foo == __main__.Foo
"""))
f.close()
assert_python_ok('-m', "cProfile", f.name)


def main():
if '-r' not in sys.argv:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support profiling code that requires ``__main__``, such as :mod:`pickle`.
Loading
0