[issue11647] function decorated with a context manager can only be invoked once

Nick Coghlan report at bugs.python.org
Wed Mar 23 05:10:41 CET 2011


Nick Coghlan <ncoghlan at gmail.com> added the comment:

On Wed, Mar 23, 2011 at 12:04 PM, Antoine Pitrou <report at bugs.python.org> wrote:
> Clearly there is a problem in the API or its implementation, as in a function decorated with a context manager can only be invoked once! This isn't good...

The problem stems from a fundamentally bad call on my part: making
ContextGenerator inherit from ContextDecorator.

I failed to account for the fact that in order to work correctly the
latter requires "reusable" context managers that can be used more than
once, but ContextGenerator objects are one-shots (i.e. once __enter__
has been called once, you can't use them again).

We can either hack this to work by providing ContextDecorator with a
way to get the underlying context manager to create a new copy of
itself each time, or else revert to the 3.1 status quo and declare
that context managers created via contextlib.contextmanager simply
can't be used as decorators. I'm inclined to favour the latter option.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11647>
_______________________________________


More information about the Python-bugs-list mailing list