Skip to content

Commit 5d0f397

Browse files
authored
Merge pull request #259 from BarrensZeppelin/master
Add `-m` switch to vmprof cli
2 parents 1c8c053 + f3fcbe2 commit 5d0f397

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

docs/vmprof.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ After ``-m vmprof`` you can specify some options:
8484
* ``-n`` - enable all C frames, only useful if you have a debug build of
8585
PyPy or CPython.
8686

87+
* ``-m`` - indicate that the provided program should be executed as a module (like ``python -m``). Example: ``-m vmprof -m http.server``
88+
8789
* ``--lines`` - enable line profiling mode. This mode adds some overhead to profiling, but in addition to function calls it marks the execution of the specific lines inside functions.
8890

8991
* ``-o file`` - save logs for later

vmprof/__main__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ def main():
5656
_jitlog.enable(fd)
5757
# invoke the user program:
5858
try:
59-
sys.argv = [args.program] + args.args
60-
sys.path.insert(0, os.path.dirname(args.program))
61-
runpy.run_path(args.program, run_name='__main__')
59+
sys.argv[1:] = args.args
60+
if args.module:
61+
runpy.run_module(args.program, run_name='__main__', alter_sys=True)
62+
else:
63+
sys.path.insert(0, os.path.dirname(args.program))
64+
runpy.run_path(args.program, run_name='__main__')
6265
except BaseException as e:
6366
if not isinstance(e, (KeyboardInterrupt, SystemExit)):
6467
raise

vmprof/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ def build_argparser():
1010
description='VMprof',
1111
prog="vmprof"
1212
)
13+
parser.add_argument(
14+
'-m',
15+
dest='module',
16+
action='store_true',
17+
help='Run module as a script'
18+
)
1319
parser.add_argument(
1420
'program',
1521
help='program'

vmprof/test/test_config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,10 @@ def test_parser_without_section():
6161

6262
assert test_file == args.config.name
6363
assert args.no_native == True
64+
65+
66+
def test_parser_with_m_switch():
67+
args = cli.parse_args(['-m', 'example'])
68+
69+
assert args.module
70+
assert args.program == 'example'

0 commit comments

Comments
 (0)