Message77290
When a context manager's __exit__() method returns an object whose
conversion to bool raises an exception, 'with' loses that exception. For
example:
>>> class CM(object):
... def __init__(self, exit_result):
... self.exit_result = exit_result
... def __enter__(self):
... return 3
... def __exit__(self, a, b, c):
... return self.exit_result()
...
>>> class TrueAsBool(object):
... def __nonzero__(self): return True
...
>>> class FalseAsBool(object):
... def __nonzero__(self): return False
...
>>> class FailAsBool(object):
... def __nonzero__(self):
... raise RuntimeError("Should see this but won't")
...
>>> with CM(TrueAsBool):
... raise AssertionError("Should NOT see this")
...
>>> with CM(FalseAsBool):
... raise AssertionError("Should see this")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
AssertionError: Should see this
>>> with CM(FailAsBool):
... raise AssertionError("Should see RuntimeException (oops)")
...
>>>
The problem is that WITH_CLEANUP only checks if PyObject_IsTrue(x)
returns non-zero, but that function returns <0 when the bool conversion
raises an exception. |
|
Date |
User |
Action |
Args |
2008-12-08 06:21:11 | jyasskin | set | recipients:
+ jyasskin |
2008-12-08 06:21:10 | jyasskin | set | messageid: <1228717270.54.0.0773896272442.issue4589@psf.upfronthosting.co.za> |
2008-12-08 06:21:08 | jyasskin | link | issue4589 messages |
2008-12-08 06:21:07 | jyasskin | create | |
|