Skip to content

Commit c5e9476

Browse files
committed
Move core profiling module into a package
This allows adding a new 'sample' submodule and enables invoking the sampling profiler through 'python -m profile.sample', while retaining backwards compatibility when using 'profile'.
1 parent 1ddfe59 commit c5e9476

File tree

3 files changed

+67
-63
lines changed

3 files changed

+67
-63
lines changed

Lib/profile/__init__.py

Whitespace-only changes.

Lib/profile/__main__.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import io
2+
import importlib.machinery
3+
import os
4+
import sys
5+
from optparse import OptionParser
6+
7+
from .profile import runctx
8+
9+
def main():
10+
11+
usage = "profile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
12+
parser = OptionParser(usage=usage)
13+
parser.allow_interspersed_args = False
14+
parser.add_option('-o', '--outfile', dest="outfile",
15+
help="Save stats to <outfile>", default=None)
16+
parser.add_option('-m', dest="module", action="store_true",
17+
help="Profile a library module.", default=False)
18+
parser.add_option('-s', '--sort', dest="sort",
19+
help="Sort order when printing to stdout, based on pstats.Stats class",
20+
default=-1)
21+
22+
if not sys.argv[1:]:
23+
parser.print_usage()
24+
sys.exit(2)
25+
26+
(options, args) = parser.parse_args()
27+
sys.argv[:] = args
28+
29+
# The script that we're profiling may chdir, so capture the absolute path
30+
# to the output file at startup.
31+
if options.outfile is not None:
32+
options.outfile = os.path.abspath(options.outfile)
33+
34+
if len(args) > 0 or options.pid:
35+
if options.module:
36+
import runpy
37+
code = "run_module(modname, run_name='__main__')"
38+
globs = {
39+
'run_module': runpy.run_module,
40+
'modname': args[0]
41+
}
42+
else:
43+
progname = args[0]
44+
sys.path.insert(0, os.path.dirname(progname))
45+
with io.open_code(progname) as fp:
46+
code = compile(fp.read(), progname, 'exec')
47+
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
48+
origin=progname)
49+
globs = {
50+
'__spec__': spec,
51+
'__file__': spec.origin,
52+
'__name__': spec.name,
53+
'__package__': None,
54+
'__cached__': None,
55+
}
56+
try:
57+
runctx(code, globs, None, options.outfile, options.sort)
58+
except BrokenPipeError as exc:
59+
# Prevent "Exception ignored" during interpreter shutdown.
60+
sys.stdout = None
61+
sys.exit(exc.errno)
62+
else:
63+
parser.print_usage()
64+
return parser
65+
66+
if __name__ == '__main__':
67+
main()

Lib/profile.py renamed to Lib/profile/profile.py

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -550,66 +550,3 @@ def f(m, f1=f1):
550550
return mean
551551

552552
#****************************************************************************
553-
554-
def main():
555-
import os
556-
from optparse import OptionParser
557-
558-
usage = "profile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ..."
559-
parser = OptionParser(usage=usage)
560-
parser.allow_interspersed_args = False
561-
parser.add_option('-o', '--outfile', dest="outfile",
562-
help="Save stats to <outfile>", default=None)
563-
parser.add_option('-m', dest="module", action="store_true",
564-
help="Profile a library module.", default=False)
565-
parser.add_option('-s', '--sort', dest="sort",
566-
help="Sort order when printing to stdout, based on pstats.Stats class",
567-
default=-1)
568-
569-
if not sys.argv[1:]:
570-
parser.print_usage()
571-
sys.exit(2)
572-
573-
(options, args) = parser.parse_args()
574-
sys.argv[:] = args
575-
576-
# The script that we're profiling may chdir, so capture the absolute path
577-
# to the output file at startup.
578-
if options.outfile is not None:
579-
options.outfile = os.path.abspath(options.outfile)
580-
581-
if len(args) > 0:
582-
if options.module:
583-
import runpy
584-
code = "run_module(modname, run_name='__main__')"
585-
globs = {
586-
'run_module': runpy.run_module,
587-
'modname': args[0]
588-
}
589-
else:
590-
progname = args[0]
591-
sys.path.insert(0, os.path.dirname(progname))
592-
with io.open_code(progname) as fp:
593-
code = compile(fp.read(), progname, 'exec')
594-
spec = importlib.machinery.ModuleSpec(name='__main__', loader=None,
595-
origin=progname)
596-
globs = {
597-
'__spec__': spec,
598-
'__file__': spec.origin,
599-
'__name__': spec.name,
600-
'__package__': None,
601-
'__cached__': None,
602-
}
603-
try:
604-
runctx(code, globs, None, options.outfile, options.sort)
605-
except BrokenPipeError as exc:
606-
# Prevent "Exception ignored" during interpreter shutdown.
607-
sys.stdout = None
608-
sys.exit(exc.errno)
609-
else:
610-
parser.print_usage()
611-
return parser
612-
613-
# When invoked as main program, invoke the profiler on a script
614-
if __name__ == '__main__':
615-
main()

0 commit comments

Comments
 (0)