Skip to content

Commit 75a542e

Browse files
committed
Add regression test
1 parent 923cb6e commit 75a542e

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

Lib/cProfile.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,8 @@ def main():
175175
if len(args) > 0:
176176
if options.module:
177177
code = "run_module(modname, run_name='__main__')"
178-
globs = {
179-
'run_module': runpy.run_module,
180-
'modname': args[0]
181-
}
178+
globs = globals().copy()
179+
globs.update({"run_module": runpy.run_module, "modname": args[0]})
182180
else:
183181
progname = args[0]
184182
sys.path.insert(0, os.path.dirname(progname))

Lib/test/test_cprofile.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
# rip off all interesting stuff from test_profile
77
import cProfile
8+
import tempfile
9+
import textwrap
810
from test.test_profile import ProfileTest, regenerate_expected_output
9-
from test.support.script_helper import assert_python_failure
11+
from test.support.script_helper import assert_python_failure, assert_python_ok
1012
from test import support
1113

1214

@@ -155,6 +157,22 @@ def test_sort(self):
155157
self.assertIn(b"option -s: invalid choice: 'demo'", err)
156158

157159

160+
class TestProfilingScript(unittest.TestCase):
161+
def test_profile_script_importing_main(self):
162+
"""Check that scripts that reference __main__ see their own namespace
163+
when being profiled."""
164+
with tempfile.NamedTemporaryFile("w+") as f:
165+
f.write(textwrap.dedent("""\
166+
class Foo:
167+
pass
168+
169+
import __main__
170+
assert Foo == __main__.Foo
171+
"""))
172+
f.flush()
173+
assert_python_ok('-m', "cProfile", f.name)
174+
175+
158176
def main():
159177
if '-r' not in sys.argv:
160178
unittest.main()

0 commit comments

Comments
 (0)