From cf0cece8370d09e86bcb05941a34e8b73e545e7e Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sat, 26 Jun 2021 16:40:28 +1000 Subject: [PATCH] Add back refresh_cm --- contextlib2.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/contextlib2.py b/contextlib2.py index 3599644..c8b4ad0 100644 --- a/contextlib2.py +++ b/contextlib2.py @@ -63,6 +63,23 @@ class AbstractAsyncContextManager(abc.ABC): class ContextDecorator(object): "A base class or mixin that enables context managers to work as decorators." + def refresh_cm(self): + """Returns the context manager used to actually wrap the call to the + decorated function. + + The default implementation just returns *self*. + + Overriding this method allows otherwise one-shot context managers + like _GeneratorContextManager to support use as decorators via + implicit recreation. + + DEPRECATED: refresh_cm was never added to the standard library's + ContextDecorator API + """ + warnings.warn("refresh_cm was never added to the standard library", + DeprecationWarning) + return self._recreate_cm() + def _recreate_cm(self): """Return a recreated instance of self.