[issue1705170] contextmanager eats StopIteration

Nick Coghlan report at bugs.python.org
Fri Nov 2 10:45:18 CET 2007


Nick Coghlan added the comment:

Close, but not quite. The problem is that the 'value' argument may be
None if instantiation of the exception hasn't been forced before
__exit__ gets called.

>>> class TestWith(object):
...   def __enter__(self):
...     pass
...   def __exit__(self, exc_type, exc_value, exc_tb):
...     print exc_type, exc_value, exc_tb
...
>>> from __future__ import with_statement
>>> with TestWith(): iter([]).next()
...
<type 'exceptions.StopIteration'> None <traceback object at 0xb76bed4c>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

That 'None' in the middle there is the problem - contextmanager.__exit__
needs to be detecting that and instantiating the passed in exception if
it isn't already instantiated. Something like the following at the start
of the else clause should do the trick:

  if value is None:
    value = type()

----------
nosy: +ncoghlan

_____________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1705170>
_____________________________________


More information about the Python-bugs-list mailing list