mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
gh-132737: Support profiling modules that require __main___ (#132738)
This commit is contained in:
parent
e1c09fff05
commit
c7a7aa9a57
3 changed files with 29 additions and 3 deletions
|
@ -6,6 +6,7 @@ __all__ = ["run", "runctx", "Profile"]
|
|||
|
||||
import _lsprof
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
import io
|
||||
import profile as _pyprofile
|
||||
|
||||
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue