This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jyasskin
Recipients jyasskin
Date 2008-12-08.06:21:06
SpamBayes Score 5.508902e-05
Marked as misclassified No
Message-id <1228717270.54.0.0773896272442.issue4589@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2008-12-08 06:21:11jyasskinsetrecipients: + jyasskin
2008-12-08 06:21:10jyasskinsetmessageid: <1228717270.54.0.0773896272442.issue4589@psf.upfronthosting.co.za>
2008-12-08 06:21:08jyasskinlinkissue4589 messages
2008-12-08 06:21:07jyasskincreate