From f70090edc42512d8c50d9629867645fff552b775 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 19 Feb 2010 23:08:48 -0500 Subject: If the product code throws an exception, 'coverage run' now produces the same traceback as 'python' would, without the coverage-internal frames distracting from your code. --- coverage/execfile.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'coverage/execfile.py') diff --git a/coverage/execfile.py b/coverage/execfile.py index 15f0a5f..f04840b 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 -- cgit v1.2.1 From b007d17e5e2da220c57920b84c7f337bc8b378d0 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 22 Feb 2010 20:35:45 -0500 Subject: Python source files that don't end with a newline can now be executed, fixing #47. --- coverage/execfile.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'coverage/execfile.py') diff --git a/coverage/execfile.py b/coverage/execfile.py index f04840b..1a2ffad 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -2,7 +2,7 @@ import imp, os, sys -from coverage.backward import exec_function +from coverage.backward import exec_code_object from coverage.misc import NoSource, ExceptionDuringRun @@ -42,9 +42,15 @@ def run_python_file(filename, args): except IOError: raise NoSource("No file to run: %r" % filename) + # We have the source. `compile` still needs the last line to be clean, + # so make sure it is, then compile a code object from it. + if source[-1] != '\n': + source += '\n' + code = compile(source, filename, "exec") + # Execute the source file. try: - exec_function(source, filename, main_mod.__dict__) + exec_code_object(code, 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 -- cgit v1.2.1 From 99d5e7491a3b73a44de306b9735c26e4d63c0f20 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 28 Feb 2010 13:11:21 -0500 Subject: If the user's code calls sys.exit(), honor the request and exit with that status. Fixes issue #50. --- coverage/execfile.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'coverage/execfile.py') diff --git a/coverage/execfile.py b/coverage/execfile.py index 1a2ffad..333163f 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -51,6 +51,10 @@ def run_python_file(filename, args): # Execute the source file. try: exec_code_object(code, main_mod.__dict__) + except SystemExit: + # The user called sys.exit(). Just pass it along to the upper + # layers, where it will be handled. + raise except: # Something went wrong while executing the user code. # Get the exc_info, and pack them into an exception that we can -- cgit v1.2.1