Total Complexity | 3 |
Total Lines | 19 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | import wrapt |
||
4 | class ContextDecorator(object): |
||
5 | """A base class enabling context managers to work as decorators.""" |
||
6 | |||
7 | def _recreate_cm(self): |
||
8 | """Return a recreated instance of self. |
||
9 | |||
10 | Allows an otherwise one-shot context manager like |
||
11 | _GeneratorContextManager to support use as |
||
12 | a decorator via implicit recreation. |
||
13 | |||
14 | This is a private interface just for _GeneratorContextManager. |
||
15 | See issue #11647 (https://bugs.python.org/issue11647) for details. |
||
16 | """ |
||
17 | return self |
||
18 | |||
19 | @wrapt.decorator |
||
20 | def __call__(self, wrapped, instance, args, kwargs): |
||
21 | with self._recreate_cm(): |
||
22 | return wrapped(*args, **kwargs) |
||
23 |