summaryrefslogtreecommitdiff
path: root/test/base/test_utils.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-02-29 14:40:45 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2020-03-02 17:24:19 -0500
commit57dc36a01b2b334a996f73f6a78b3bfbe4d9f2ec (patch)
tree77cbb0199ca91be3b0816e3a5bd4c217e36a7d1b /test/base/test_utils.py
parent649de79950dcf952d7a44069faf36925c23c4e63 (diff)
downloadsqlalchemy-57dc36a01b2b334a996f73f6a78b3bfbe4d9f2ec.tar.gz
Ensure all nested exception throws have a cause
Applied an explicit "cause" to most if not all internally raised exceptions that are raised from within an internal exception catch, to avoid misleading stacktraces that suggest an error within the handling of an exception. While it would be preferable to suppress the internally caught exception in the way that the ``__suppress_context__`` attribute would, there does not as yet seem to be a way to do this without suppressing an enclosing user constructed context, so for now it exposes the internally caught exception as the cause so that full information about the context of the error is maintained. Fixes: #4849 Change-Id: I55a86b29023675d9e5e49bc7edc5a2dc0bcd4751
Diffstat (limited to 'test/base/test_utils.py')
-rw-r--r--test/base/test_utils.py28
1 files changed, 18 insertions, 10 deletions
diff --git a/test/base/test_utils.py b/test/base/test_utils.py
index 48e464a01..183e157e5 100644
--- a/test/base/test_utils.py
+++ b/test/base/test_utils.py
@@ -3,7 +3,6 @@
import copy
import datetime
import inspect
-import sys
from sqlalchemy import exc
from sqlalchemy import sql
@@ -2899,20 +2898,29 @@ class ReraiseTest(fixtures.TestBase):
except MyException as err:
is_(err.__cause__, None)
- def test_reraise_disallow_same_cause(self):
+ def test_raise_from_cause_legacy(self):
class MyException(Exception):
pass
+ class MyOtherException(Exception):
+ pass
+
+ me = MyException("exc on")
+
def go():
try:
- raise MyException("exc one")
- except Exception as err:
- type_, value, tb = sys.exc_info()
- util.reraise(type_, err, tb, value)
+ raise me
+ except Exception:
+ util.raise_from_cause(MyOtherException("exc two"))
- assert_raises_message(AssertionError, "Same cause emitted", go)
+ try:
+ go()
+ assert False
+ except MyOtherException as moe:
+ if testing.requires.python3.enabled:
+ is_(moe.__cause__, me)
- def test_raise_from_cause(self):
+ def test_raise_from(self):
class MyException(Exception):
pass
@@ -2924,8 +2932,8 @@ class ReraiseTest(fixtures.TestBase):
def go():
try:
raise me
- except Exception:
- util.raise_from_cause(MyOtherException("exc two"))
+ except Exception as err:
+ util.raise_(MyOtherException("exc two"), from_=err)
try:
go()