summaryrefslogtreecommitdiff
path: root/Lib/contextlib.py
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-06-30 12:17:50 +0000
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-06-30 12:17:50 +0000
commitb3a89844888d1b8eacdacc14cc6db1e2125d2d6a (patch)
treee2704acb44e0b3dbd9f7d4b6ed33a478f03e739f /Lib/contextlib.py
parentcba8c10b5c262f41873ac877d25c242823ab668c (diff)
downloadcpython-git-b3a89844888d1b8eacdacc14cc6db1e2125d2d6a.tar.gz
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.
Diffstat (limited to 'Lib/contextlib.py')
-rw-r--r--Lib/contextlib.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py
index e26d77ae2a..e37fde8a02 100644
--- a/Lib/contextlib.py
+++ b/Lib/contextlib.py
@@ -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):