Skip to content

add post-mortem pdb flag #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 14 additions & 3 deletions fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def main(argv):
import inspect
import json
import os
import pdb
import pipes
import traceback
import shlex
import sys
import types
Expand Down Expand Up @@ -313,6 +315,7 @@ def _Fire(component, args, context, name=None):
show_completion = parsed_flag_args.completion
show_help = parsed_flag_args.help
show_trace = parsed_flag_args.trace
post_mortem = parsed_flag_args.debug

# component can be a module, class, routine, object, etc.
if component is None:
Expand Down Expand Up @@ -355,7 +358,7 @@ def _Fire(component, args, context, name=None):
filename, lineno = _GetFileAndLine(component)

component, consumed_args, remaining_args, capacity = _CallCallable(
component, remaining_args)
component, remaining_args, post_mortem)

# Update the trace.
if isclass:
Expand Down Expand Up @@ -543,12 +546,13 @@ def _GetMember(component, args):
raise FireError('Could not consume arg:', arg)


def _CallCallable(fn, args):
def _CallCallable(fn, args, mortem):
"""Calls the function fn by consuming args from args.

Args:
fn: The function to call or class to instantiate.
args: Args from which to consume for calling the function.
mortem: Boolean flag whether to enter a pdb post mortem session on err
Returns:
component: The object that is the result of the function call.
consumed_args: The args that were consumed for the function call.
Expand All @@ -558,7 +562,14 @@ def _CallCallable(fn, args):
parse = _MakeParseFn(fn)
(varargs, kwargs), consumed_args, remaining_args, capacity = parse(args)

result = fn(*varargs, **kwargs)
try:
result = fn(*varargs, **kwargs)
except Exception:
if mortem:
traceback.print_exc()
pdb.post_mortem(sys.exc_info()[-1])
raise

return result, consumed_args, remaining_args, capacity


Expand Down
2 changes: 2 additions & 0 deletions fire/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@


def CreateParser():
"""Create cli argv parser for special flags to Fire."""
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--verbose', '-v', action='store_true')
parser.add_argument('--interactive', '-i', action='store_true')
parser.add_argument('--separator', default='-')
parser.add_argument('--completion', action='store_true')
parser.add_argument('--help', '-h', action='store_true')
parser.add_argument('--trace', '-t', action='store_true')
parser.add_argument('--debug', '-d', action='store_true')
# TODO: Consider allowing name to be passed as an argument.
return parser

Expand Down