Skip to content

Commit b6e317f

Browse files
committed
--
1 parent 95aa49c commit b6e317f

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

source_py3/python_toolbox/context_management/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def do_stuff():
123123
# that will cause it to be pickled by reference to the decorated function
124124

125125

126+
from .abstract_context_manager import AbstractContextManager
126127
from .context_manager_type_type import ContextManagerTypeType
127128
from .context_manager_type import ContextManagerType
128129
from .context_manager import ContextManager
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2009-2015 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
import sys
5+
import types
6+
import abc
7+
8+
from python_toolbox import decorator_tools
9+
10+
from .base_classes.decorating_context_manager import DecoratingContextManager
11+
from .context_manager_type import ContextManagerType
12+
from .self_hook import SelfHook
13+
14+
15+
class AbstractContextManager(metaclass=abc.ABCMeta):
16+
'''
17+
blocktodo: add tests
18+
'''
19+
@abc.abstractmethod
20+
def __enter__(self):
21+
'''Prepare for suite execution.'''
22+
23+
24+
@abc.abstractmethod
25+
def __exit__(self, exc_type, exc_value, exc_traceback):
26+
'''Cleanup after suite execution.'''
27+
28+
@classmethod
29+
def __subclasshook__(cls, candidate_class):
30+
if cls is AbstractContextManager:
31+
return (
32+
hasattr(candidate_class, '__enter__') and
33+
candidate_class.__enter__ is not none and
34+
hasattr(candidate_class, '__exit__') and
35+
candidate_class.__exit__ is not none
36+
)
37+
else:
38+
return NotImplemented
39+
40+
41+

source_py3/python_toolbox/context_management/context_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
from python_toolbox import decorator_tools
99

10+
from .abstract_context_manager import AbstractContextManager
1011
from .base_classes.decorating_context_manager import DecoratingContextManager
1112
from .context_manager_type import ContextManagerType
1213
from .self_hook import SelfHook
1314

1415

15-
class ContextManager(DecoratingContextManager, metaclass=ContextManagerType):
16+
class ContextManager(AbstractContextManager, DecoratingContextManager,
17+
metaclass=ContextManagerType):
1618
'''
1719
Allows running preparation code before a given suite and cleanup after.
1820

source_py3/python_toolbox/context_management/functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ def __init__(self, wrapped_context_manager):
106106

107107
@classmethod
108108
def _wrap_context_manager_or_class(cls, thing):
109-
if hasattr(type(thing), '__enter__'):
110-
# It's a context manager.
109+
from .abstract_context_manager import AbstractContextManager
110+
if isinstance(thing, AbstractContextManager):
111111
return cls(thing)
112112
else:
113-
assert issubclass(thing, ContextManager)
113+
assert issubclass(thing, AbstractContextManager)
114114
# It's a context manager class.
115115
property_name = '__%s_context_manager_%s' % (
116116
thing.__name__,

0 commit comments

Comments
 (0)