diff options
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/cmdline.py | 17 | ||||
-rw-r--r-- | coverage/execfile.py | 4 |
2 files changed, 15 insertions, 6 deletions
diff --git a/coverage/cmdline.py b/coverage/cmdline.py index c9383689..9e15074b 100644 --- a/coverage/cmdline.py +++ b/coverage/cmdline.py @@ -589,25 +589,30 @@ Coverage.py, version %(__version__)s. %(__url__)s """ -def main(): +def main(argv=None): """The main entrypoint to Coverage. This is installed as the script entrypoint. """ + if argv is None: + argv = sys.argv[1:] try: - status = CoverageScript().command_line(sys.argv[1:]) + status = CoverageScript().command_line(argv) except ExceptionDuringRun: # An exception was caught while running the product code. The # sys.exc_info() return tuple is packed into an ExceptionDuringRun - # exception. Note that the Python interpreter doesn't print SystemExit - # tracebacks, so it's important that we don't also. + # exception. _, err, _ = sys.exc_info() - if not isinstance(err.args[1], SystemExit): - traceback.print_exception(*err.args) + traceback.print_exception(*err.args) status = ERR except CoverageException: + # A controlled error inside coverage.py: print the message to the user. _, err, _ = sys.exc_info() print(err) status = ERR + except SystemExit: + # The user called `sys.exit()`. Exit with their status code. + _, err, _ = sys.exc_info() + status = err.args[0] return status diff --git a/coverage/execfile.py b/coverage/execfile.py index 1a2ffadd..333163f8 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 |