Skip to content

Commit

Permalink
Merge pull request #41 from vodik/better-run
Browse files Browse the repository at this point in the history
Add support to directly run a module
  • Loading branch information
carljm committed Jan 8, 2018
2 parents 2a35a0c + 875a1b4 commit 59cf4e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
15 changes: 9 additions & 6 deletions doc/tracing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ monkeytype run
~~~~~~~~~~~~~~

The simplest way to trace some function calls with MonkeyType is to run a Python
script under MonkeyType tracing using ``monkeytype run`` at the command line::
script under MonkeyType tracing using ``monkeytype run`` or
``monkeytype run -m`` at the command line::

$ monkeytype run myscript.py
$ monkeytype run -m mymodule

``monkeytype run`` accepts the same :option:`monkeytype -c` option as
``monkeytype stub`` and ``monkeytype apply``, to point MonkeyType to the config
it should use.

Because of the way Python treats scripts and modules differently, MonkeyType
will record usable traces for modules imported and used by ``myscript.py``; not
for ``myscript.py`` itself. If you want to annotate ``myscript.py``, treat it
as a module and write another short script that imports and calls its
function(s), and run that script with ``monkeytype run``.
Because of the way Python treats scripts and imported modules differently,
MonkeyType will not record traces for the entry point itself (that is, the script
passed to ``monkeytype run`` or the module passed to ``run -m``); traces are
recorded only for imported modules. If you want to annotate the entry point
script/module, write another short script that imports and calls its function(s),
and run that script with ``monkeytype run``.

.. module:: monkeytype

Expand Down
13 changes: 11 additions & 2 deletions monkeytype/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ def print_stub_handler(args: argparse.Namespace, stdout: IO, stderr: IO) -> None
def run_handler(args: argparse.Namespace, stdout: IO, stderr: IO) -> None:
# remove initial `monkeytype run`
old_argv = sys.argv.copy()
sys.argv = sys.argv[2:]
try:
with trace(args.config):
runpy.run_path(args.script_path, run_name='__main__')
if args.m:
sys.argv = sys.argv[3:]
runpy.run_module(args.script_path, run_name='__main__', alter_sys=True)
else:
sys.argv = sys.argv[2:]
runpy.run_path(args.script_path, run_name='__main__')
finally:
sys.argv = old_argv

Expand Down Expand Up @@ -195,6 +199,11 @@ def main(argv: List[str], stdout: IO, stderr: IO) -> int:
'script_path',
type=str,
help="""Filesystem path to a Python script file to run under tracing""")
run_parser.add_argument(
'-m',
action='store_true',
help="Run a library module as a script"
)
run_parser.add_argument(
'script_args',
nargs=argparse.REMAINDER,
Expand Down
1 change: 1 addition & 0 deletions monkeytype/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def __call__(self, frame: FrameType, event: str, arg: Any) -> 'CallTracer':
code = frame.f_code
if (
event not in SUPPORTED_EVENTS or
frame.f_globals.get('__name__') == '__main__' or
code.co_name == 'trace_types' or
self.should_trace and not self.should_trace(code)
):
Expand Down

0 comments on commit 59cf4e6

Please sign in to comment.