@@ -150,6 +150,7 @@ def main():
150150 import runpy
151151 import pstats
152152 from optparse import OptionParser
153+ from pkgutil import get_importer
153154 usage = "cProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
154155 parser = OptionParser (usage = usage )
155156 parser .allow_interspersed_args = False
@@ -183,27 +184,35 @@ def main():
183184 }
184185 else :
185186 progname = args [0 ]
187+ importer = get_importer (progname )
186188 sys .path .insert (0 , os .path .dirname (progname ))
187- with io .open_code (progname ) as fp :
188- code = compile (fp .read (), progname , 'exec' , module = '__main__' )
189- spec = importlib .machinery .ModuleSpec (name = '__main__' , loader = None ,
190- origin = progname )
191- module = importlib .util .module_from_spec (spec )
192- # Set __main__ so that importing __main__ in the profiled code will
193- # return the same namespace that the code is executing under.
194- sys .modules ['__main__' ] = module
195- # Ensure that we're using the same __dict__ instance as the module
196- # for the global variables so that updates to globals are reflected
197- # in the module's namespace.
198- globs = module .__dict__
199- globs .update ({
200- # Set __spec__ to None so the profiled program behaves like a
201- # script run directly (gh-140729).
202- '__spec__' : None ,
203- '__file__' : spec .origin ,
204- '__name__' : spec .name ,
205- '__package__' : None ,
206- })
189+
190+ if importer is not None :
191+ code = "run_module_as_main('__main__', alter_argv=False)"
192+ globs = {
193+ "run_module_as_main" : runpy ._run_module_as_main ,
194+ }
195+ else :
196+ with io .open_code (progname ) as fp :
197+ code = compile (fp .read (), progname , 'exec' , module = '__main__' )
198+ spec = importlib .machinery .ModuleSpec (name = '__main__' , loader = None ,
199+ origin = progname )
200+ module = importlib .util .module_from_spec (spec )
201+ # Set __main__ so that importing __main__ in the profiled code will
202+ # return the same namespace that the code is executing under.
203+ sys .modules ['__main__' ] = module
204+ # Ensure that we're using the same __dict__ instance as the module
205+ # for the global variables so that updates to globals are reflected
206+ # in the module's namespace.
207+ globs = module .__dict__
208+ globs .update ({
209+ # Set __spec__ to None so the profiled program behaves like a
210+ # script run directly (gh-140729).
211+ '__spec__' : None ,
212+ '__file__' : spec .origin ,
213+ '__name__' : spec .name ,
214+ '__package__' : None ,
215+ })
207216
208217 try :
209218 runctx (code , globs , None , options .outfile , options .sort )
0 commit comments