mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
This commit is contained in:
parent
cba8c10b5c
commit
b3a8984488
4 changed files with 237 additions and 2 deletions
|
|
@ -4,9 +4,20 @@ import sys
|
|||
from functools import wraps
|
||||
from warnings import warn
|
||||
|
||||
__all__ = ["contextmanager", "closing"]
|
||||
__all__ = ["contextmanager", "closing", "ContextDecorator"]
|
||||
|
||||
class GeneratorContextManager(object):
|
||||
|
||||
class ContextDecorator(object):
|
||||
"A base class or mixin that enables context managers to work as decorators."
|
||||
def __call__(self, func):
|
||||
@wraps(func)
|
||||
def inner(*args, **kwds):
|
||||
with self:
|
||||
return func(*args, **kwds)
|
||||
return inner
|
||||
|
||||
|
||||
class GeneratorContextManager(ContextDecorator):
|
||||
"""Helper for @contextmanager decorator."""
|
||||
|
||||
def __init__(self, gen):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue