summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2012-05-14 22:09:31 -0700
committerBenjamin Peterson <benjamin@python.org>2012-05-14 22:09:31 -0700
commitd5a1c44455d969968f453f029727bfc45e4ce0a9 (patch)
tree98e91aa8130d600df0b1fdf329bbdc9d60d4b14c /Lib
parentd91dc623791fd9973b914a57540d89cb986da7c9 (diff)
downloadcpython-git-d5a1c44455d969968f453f029727bfc45e4ce0a9.tar.gz
PEP 415: Implement suppression of __context__ display with an exception attribute
This replaces the original PEP 409 implementation. See #14133.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_exceptions.py15
-rw-r--r--Lib/test/test_raise.py7
-rw-r--r--Lib/test/test_sys.py6
-rw-r--r--Lib/traceback.py13
4 files changed, 23 insertions, 18 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 39ff85fc19..97762f9830 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -388,18 +388,18 @@ class ExceptionTests(unittest.TestCase):
def testChainingAttrs(self):
e = Exception()
self.assertIsNone(e.__context__)
- self.assertIs(e.__cause__, Ellipsis)
+ self.assertIsNone(e.__cause__)
e = TypeError()
self.assertIsNone(e.__context__)
- self.assertIs(e.__cause__, Ellipsis)
+ self.assertIsNone(e.__cause__)
class MyException(EnvironmentError):
pass
e = MyException()
self.assertIsNone(e.__context__)
- self.assertIs(e.__cause__, Ellipsis)
+ self.assertIsNone(e.__cause__)
def testChainingDescriptors(self):
try:
@@ -408,15 +408,16 @@ class ExceptionTests(unittest.TestCase):
e = exc
self.assertIsNone(e.__context__)
- self.assertIs(e.__cause__, Ellipsis)
+ self.assertIsNone(e.__cause__)
+ self.assertFalse(e.__suppress_context__)
e.__context__ = NameError()
e.__cause__ = None
self.assertIsInstance(e.__context__, NameError)
self.assertIsNone(e.__cause__)
-
- e.__cause__ = Ellipsis
- self.assertIs(e.__cause__, Ellipsis)
+ self.assertTrue(e.__suppress_context__)
+ e.__suppress_context__ = False
+ self.assertFalse(e.__suppress_context__)
def testKeywordArgs(self):
# test that builtin exception don't take keyword args,
diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py
index 8ae92105d1..b169867e1f 100644
--- a/Lib/test/test_raise.py
+++ b/Lib/test/test_raise.py
@@ -174,11 +174,14 @@ class TestCause(unittest.TestCase):
raise ValueError from None
except ValueError as exc:
self.assertIsNone(exc.__cause__)
- raise exc from Ellipsis
+ self.assertTrue(exc.__suppress_context__)
+ exc.__suppress_context__ = False
+ raise exc
except ValueError as exc:
e = exc
- self.assertIs(e.__cause__, Ellipsis)
+ self.assertIsNone(e.__cause__)
+ self.assertFalse(e.__suppress_context__)
self.assertIsInstance(e.__context__, TypeError)
def test_invalid_cause(self):
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 2b9abfc91c..71dbd2967c 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -700,14 +700,14 @@ class SizeofTest(unittest.TestCase):
class C(object): pass
check(C.__dict__, size(h + 'P'))
# BaseException
- check(BaseException(), size(h + '5P'))
+ check(BaseException(), size(h + '5Pi'))
# UnicodeEncodeError
- check(UnicodeEncodeError("", "", 0, 0, ""), size(h + '5P 2P2PP'))
+ check(UnicodeEncodeError("", "", 0, 0, ""), size(h + '5Pi 2P2PP'))
# UnicodeDecodeError
# XXX
# check(UnicodeDecodeError("", "", 0, 0, ""), size(h + '5P2PP'))
# UnicodeTranslateError
- check(UnicodeTranslateError("", 0, 1, ""), size(h + '5P 2P2PP'))
+ check(UnicodeTranslateError("", 0, 1, ""), size(h + '5Pi 2P2PP'))
# ellipses
check(Ellipsis, size(h + ''))
# EncodingMap
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 35858af1cc..eeb9e7387c 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -119,15 +119,16 @@ def _iter_chain(exc, custom_tb=None, seen=None):
seen = set()
seen.add(exc)
its = []
+ context = exc.__context__
cause = exc.__cause__
- if cause is Ellipsis:
- context = exc.__context__
- if context is not None and context not in seen:
- its.append(_iter_chain(context, None, seen))
- its.append([(_context_message, None)])
- elif cause is not None and cause not in seen:
+ if cause is not None and cause not in seen:
its.append(_iter_chain(cause, False, seen))
its.append([(_cause_message, None)])
+ elif (context is not None and
+ not exc.__suppress_context__ and
+ context not in seen):
+ its.append(_iter_chain(context, None, seen))
+ its.append([(_context_message, None)])
its.append([(exc, custom_tb or exc.__traceback__)])
# itertools.chain is in an extension module and may be unavailable
for it in its: