'NoneType' in contextmanager prevent proper RuntimeError to be raised

Daniel Gonçalves daniel.base4 at gmail.com
Wed May 20 19:20:53 EDT 2015


When you decorate a function with contextmanager that didn't yield you got an AttributeError instead of a proper RuntimeError "generator didn't yield". For example:

>>> @contextlib.contextmanager
>>> def foo():
...     pass
...
>>> with foo():
...     do_something()
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
AttributeError: 'NoneType' object has no attribute 'next'

The proper exception should be a RuntimerError with the "generator didn't yield" message. At least for Python version 2.7.6.

Regards



More information about the Python-list mailing list