summaryrefslogtreecommitdiff
path: root/coverage/execfile.py
diff options
context:
space:
mode:
Diffstat (limited to 'coverage/execfile.py')
-rw-r--r--coverage/execfile.py42
1 files changed, 34 insertions, 8 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 3e20a527..693f54fb 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -10,7 +10,7 @@ import types
from coverage.backward import BUILTINS
from coverage.backward import PYC_MAGIC_NUMBER, imp, importlib_util_find_spec
-from coverage.misc import ExceptionDuringRun, NoCode, NoSource, isolate_module
+from coverage.misc import CoverageException, ExceptionDuringRun, NoCode, NoSource, isolate_module
from coverage.phystokens import compile_unicode
from coverage.python import get_python_source
@@ -166,11 +166,17 @@ def run_python_file(filename, args, package=None, modulename=None, path0=None):
sys.path[0] = path0 if path0 is not None else my_path0
try:
- # Make a code object somehow.
- if filename.endswith((".pyc", ".pyo")):
- code = make_code_from_pyc(filename)
- else:
- code = make_code_from_py(filename)
+ try:
+ # Make a code object somehow.
+ if filename.endswith((".pyc", ".pyo")):
+ code = make_code_from_pyc(filename)
+ else:
+ code = make_code_from_py(filename)
+ except CoverageException:
+ raise
+ except Exception as exc:
+ msg = "Couldn't run {filename!r} as Python code: {exc.__class__.__name__}: {exc}"
+ raise CoverageException(msg.format(filename=filename, exc=exc))
# Execute the code object.
try:
@@ -179,7 +185,7 @@ def run_python_file(filename, args, package=None, modulename=None, path0=None):
# The user called sys.exit(). Just pass it along to the upper
# layers, where it will be handled.
raise
- except:
+ except Exception:
# 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 one layer off the traceback
@@ -193,7 +199,27 @@ def run_python_file(filename, args, package=None, modulename=None, path0=None):
# it somehow? https://bitbucket.org/pypy/pypy/issue/1903
getattr(err, '__context__', None)
- raise ExceptionDuringRun(typ, err, tb.tb_next)
+ # Call the excepthook.
+ try:
+ if hasattr(err, "__traceback__"):
+ err.__traceback__ = err.__traceback__.tb_next
+ sys.excepthook(typ, err, tb.tb_next)
+ except SystemExit:
+ raise
+ except Exception:
+ # Getting the output right in the case of excepthook
+ # shenanigans is kind of involved.
+ sys.stderr.write("Error in sys.excepthook:\n")
+ typ2, err2, tb2 = sys.exc_info()
+ err2.__suppress_context__ = True
+ if hasattr(err2, "__traceback__"):
+ err2.__traceback__ = err2.__traceback__.tb_next
+ sys.__excepthook__(typ2, err2, tb2.tb_next)
+ sys.stderr.write("\nOriginal exception was:\n")
+ raise ExceptionDuringRun(typ, err, tb.tb_next)
+ else:
+ sys.exit(1)
+
finally:
# Restore the old __main__, argv, and path.
sys.modules['__main__'] = old_main_mod