diff options
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/cmdline.py | 8 | ||||
-rw-r--r-- | coverage/execfile.py | 16 | ||||
-rw-r--r-- | coverage/misc.py | 8 |
3 files changed, 28 insertions, 4 deletions
diff --git a/coverage/cmdline.py b/coverage/cmdline.py index 60f9cdd3..f7cb28c3 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -1,10 +1,10 @@ """Command-line support for Coverage.""" -import optparse, re, sys +import optparse, re, sys, traceback from coverage.backward import sorted # pylint: disable-msg=W0622 from coverage.execfile import run_python_file -from coverage.misc import CoverageException +from coverage.misc import CoverageException, ExceptionDuringRun class Opts(object): @@ -596,6 +596,10 @@ def main(): """ try: status = CoverageScript().command_line(sys.argv[1:]) + except ExceptionDuringRun: + _, err, _ = sys.exc_info() + traceback.print_exception(*err.args) + status = ERR except CoverageException: _, err, _ = sys.exc_info() print(err) diff --git a/coverage/execfile.py b/coverage/execfile.py index 15f0a5f8..f04840b5 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -3,7 +3,7 @@ import imp, os, sys from coverage.backward import exec_function -from coverage.misc import NoSource +from coverage.misc import NoSource, ExceptionDuringRun try: @@ -36,11 +36,23 @@ def run_python_file(filename, args): sys.path[0] = os.path.dirname(filename) try: + # Open the source file. try: source = open(filename, 'rU').read() except IOError: raise NoSource("No file to run: %r" % filename) - exec_function(source, filename, main_mod.__dict__) + + # Execute the source file. + try: + exec_function(source, filename, main_mod.__dict__) + except: + # Something went wrong while executing the user code. + # Get the exc_info, and pack them into an exception that we can + # throw up to the outer loop. We peel two layers off the traceback + # so that the coverage.py code doesn't appear in the final printed + # traceback. + typ, err, tb = sys.exc_info() + raise ExceptionDuringRun(typ, err, tb.tb_next.tb_next) finally: # Restore the old __main__ sys.modules['__main__'] = old_main_mod diff --git a/coverage/misc.py b/coverage/misc.py index 4959efe0..4218536d 100644 --- a/coverage/misc.py +++ b/coverage/misc.py @@ -75,3 +75,11 @@ class CoverageException(Exception): class NoSource(CoverageException): """Used to indicate we couldn't find the source for a module.""" pass + +class ExceptionDuringRun(CoverageException): + """An exception happened while running customer code. + + Construct it with three arguments, the values from `sys.exc_info`. + + """ + pass |