Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 8 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 @@ -169,17 +170,22 @@ def main():
else:
progname = args[0]
sys.path.insert(0, os.path.dirname(progname))
with io.open_code(progname) as fp:
code = compile(fp.read(), progname, 'exec')
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
origin=progname)
loader = importlib.machinery.SourceFileLoader("__main__", progname)
spec.loader = loader
module = importlib.util.module_from_spec(spec)
globs = {
'__spec__': spec,
'__file__': spec.origin,
'__name__': spec.name,
'__package__': None,
'__cached__': None,
'module': module
}

sys.modules["__main__"] = module
code = "__spec__.loader.exec_module(module)"
try:
runctx(code, globs, None, options.outfile, options.sort)
except BrokenPipeError as exc:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support profiling modules that import __main___, such as modules that use to pickle. The github issue has an example repro that throws an exception without this change, and succeeds with it.
Loading