Skip to content

Commit b4f3934

Browse files
committed
Support profiling modules that import __main___
1 parent e7c5f60 commit b4f3934

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Lib/cProfile.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import _lsprof
88
import importlib.machinery
9+
import importlib.util
910
import io
1011
import profile as _pyprofile
1112

@@ -169,17 +170,22 @@ def main():
169170
else:
170171
progname = args[0]
171172
sys.path.insert(0, os.path.dirname(progname))
172-
with io.open_code(progname) as fp:
173-
code = compile(fp.read(), progname, 'exec')
174173
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
175174
origin=progname)
175+
loader = importlib.machinery.SourceFileLoader("__main__", progname)
176+
spec.loader = loader
177+
module = importlib.util.module_from_spec(spec)
176178
globs = {
177179
'__spec__': spec,
178180
'__file__': spec.origin,
179181
'__name__': spec.name,
180182
'__package__': None,
181183
'__cached__': None,
184+
'module': module
182185
}
186+
187+
sys.modules["__main__"] = module
188+
code = "__spec__.loader.exec_module(module)"
183189
try:
184190
runctx(code, globs, None, options.outfile, options.sort)
185191
except BrokenPipeError as exc:

0 commit comments

Comments
 (0)