File tree Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Expand file tree Collapse file tree 2 files changed +21
-5
lines changed Original file line number Diff line number Diff 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 ))
Original file line number Diff line number Diff line change 55
66# rip off all interesting stuff from test_profile
77import cProfile
8+ import tempfile
9+ import textwrap
810from 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
1012from 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+
158176def main ():
159177 if '-r' not in sys .argv :
160178 unittest .main ()
You can’t perform that action at this time.
0 commit comments