summaryrefslogtreecommitdiff
path: root/coverage/execfile.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-07-20 06:54:22 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-07-20 05:32:28 -0700
commit5313297fe84c596f9222a4890dd45a53a6d4d632 (patch)
treeceacf73477e129786a8566fff146c18cd57c0edf /coverage/execfile.py
parentde38a0e74a8683a3d3381038aeee4d226cc5b714 (diff)
downloadpython-coveragepy-git-5313297fe84c596f9222a4890dd45a53a6d4d632.tar.gz
fix: raise chained errors with "from" #998
This makes exceptions report their causes correctly, as "The above exception was the direct cause of the following exception" instead of "During handling of the above exception, another exception occurred."
Diffstat (limited to 'coverage/execfile.py')
-rw-r--r--coverage/execfile.py16
1 files changed, 8 insertions, 8 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 2a3776bf..66020019 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -42,7 +42,7 @@ def find_module(modulename):
try:
spec = importlib.util.find_spec(modulename)
except ImportError as err:
- raise NoSource(str(err))
+ raise NoSource(str(err)) from err
if not spec:
raise NoSource(f"No module named {modulename!r}")
pathname = spec.origin
@@ -193,7 +193,7 @@ class PyRunner:
raise
except Exception as exc:
msg = "Couldn't run '{filename}' as Python code: {exc.__class__.__name__}: {exc}"
- raise CoverageException(msg.format(filename=self.arg0, exc=exc))
+ raise CoverageException(msg.format(filename=self.arg0, exc=exc)) from exc
# Execute the code object.
# Return to the original directory in case the test code exits in
@@ -226,7 +226,7 @@ class PyRunner:
sys.excepthook(typ, err, tb.tb_next)
except SystemExit: # pylint: disable=try-except-raise
raise
- except Exception:
+ except Exception as exc:
# Getting the output right in the case of excepthook
# shenanigans is kind of involved.
sys.stderr.write("Error in sys.excepthook:\n")
@@ -236,7 +236,7 @@ class PyRunner:
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)
+ raise ExceptionDuringRun(typ, err, tb.tb_next) from exc
else:
sys.exit(1)
finally:
@@ -277,8 +277,8 @@ def make_code_from_py(filename):
# Open the source file.
try:
source = get_python_source(filename)
- except (OSError, NoSource):
- raise NoSource("No file to run: '%s'" % filename)
+ except (OSError, NoSource) as exc:
+ raise NoSource("No file to run: '%s'" % filename) from exc
code = compile_unicode(source, filename, "exec")
return code
@@ -288,8 +288,8 @@ def make_code_from_pyc(filename):
"""Get a code object from a .pyc file."""
try:
fpyc = open(filename, "rb")
- except OSError:
- raise NoCode("No file to run: '%s'" % filename)
+ except OSError as exc:
+ raise NoCode("No file to run: '%s'" % filename) from exc
with fpyc:
# First four bytes are a version-specific magic number. It has to