summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_with.py
diff options
context:
space:
mode:
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>2008-12-10 23:22:49 (GMT)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>2008-12-10 23:22:49 (GMT)
commitad9b5992e3525c8a53ec6add3fee1f97142dc503 (patch)
tree889d5d5133ffeea501626369c635c67306fd927f /Lib/test/test_with.py
parentffd42cf4442418ad8f22080377717d9d2b27f695 (diff)
downloadcpython-ad9b5992e3525c8a53ec6add3fee1f97142dc503.zip
cpython-ad9b5992e3525c8a53ec6add3fee1f97142dc503.tar.gz
cpython-ad9b5992e3525c8a53ec6add3fee1f97142dc503.tar.bz2
#4559: When a context manager's __exit__() method returns an object whose
conversion to bool raises an exception, 'with' loses that exception. Reviewed by Jeffrey Yasskin. Already ported to 2.5, will port to 2.6 and 3.0
Diffstat (limited to 'Lib/test/test_with.py')
-rw-r--r--Lib/test/test_with.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py
index 3007e5a..bfeb06b 100644
--- a/Lib/test/test_with.py
+++ b/Lib/test/test_with.py
@@ -503,6 +503,36 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
self.assertRaises(GeneratorExit, shouldThrow)
+ def testErrorsInBool(self):
+ # issue4589: __exit__ return code may raise an exception
+ # when looking at its truth value.
+
+ class cm(object):
+ def __init__(self, bool_conversion):
+ class Bool:
+ def __nonzero__(self):
+ return bool_conversion()
+ self.exit_result = Bool()
+ def __enter__(self):
+ return 3
+ def __exit__(self, a, b, c):
+ return self.exit_result
+
+ def trueAsBool():
+ with cm(lambda: True):
+ self.fail("Should NOT see this")
+ trueAsBool()
+
+ def falseAsBool():
+ with cm(lambda: False):
+ self.fail("Should raise")
+ self.assertRaises(AssertionError, falseAsBool)
+
+ def failAsBool():
+ with cm(lambda: 1//0):
+ self.fail("Should NOT see this")
+ self.assertRaises(ZeroDivisionError, failAsBool)
+
class NonLocalFlowControlTestCase(unittest.TestCase):