Skip to content

Commit 79456e5

Browse files
committed
Baaa
1 parent 3ce93b2 commit 79456e5

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

source_py3/python_toolbox/context_management/functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'''
99

1010
import sys
11+
import types
1112

1213
from python_toolbox import caching
1314

@@ -45,6 +46,15 @@ def nested(*managers):
4546
raise exc[1].with_traceback(exc[2])
4647

4748

49+
def _wrap_context_manager_or_context_generator(thing, wrapper_factory):
50+
if hasattr(type(thing), '__enter__'):
51+
# It's a context manager.
52+
return wrapper_factory(thing)
53+
else:
54+
assert isinstance(thing, types.FunctionType)
55+
# It's a context generator.
56+
57+
4858

4959
def as_idempotent(context_manager):
5060
'''

source_py3/test_python_toolbox/test_context_management/test_as_reentrant.py

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import queue as queue_module
55

6-
from python_toolbox.context_management import as_reentrant, ContextManager
6+
from python_toolbox.context_management import (as_reentrant, ContextManager,
7+
ContextManagerType)
78
from python_toolbox import cute_testing
89

910

@@ -149,7 +150,7 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
149150
# happens *after* `.__wrapped__.__enter__` is called:
150151
assert depth_log.get(block=False) == 1
151152

152-
def test_decorator():
153+
def test_decorator_manage_context():
153154
class Meow(ContextManager):
154155
n = 0
155156

@@ -175,4 +176,81 @@ def manage_context(self):
175176
assert meow.n == 0
176177

177178

179+
def test_decorator_class():
180+
181+
@as_reentrant
182+
class Meow(ContextManager):
183+
n = 0
184+
185+
def manage_context(self):
186+
self.n += 1
187+
try:
188+
yield
189+
finally:
190+
self.n -= 1
191+
192+
193+
meow = Meow()
194+
assert meow.n == 0
195+
with meow:
196+
assert meow.n == 1
197+
with meow:
198+
assert meow.n == 1
199+
with meow:
200+
assert meow.n == 1
201+
assert meow.n == 1
202+
assert meow.n == 1
203+
assert meow.n == 0
204+
205+
def test_decorator_class_enter_exit():
206+
207+
@as_reentrant
208+
class Meow(ContextManager):
209+
n = 0
210+
211+
def __enter__(self):
212+
self.n += 1
213+
return self
214+
215+
def __exit__(self, exc_type, exc_value, exc_traceback):
216+
self.n -= 1
217+
218+
219+
meow = Meow()
220+
assert meow.n == 0
221+
with meow:
222+
assert meow.n == 1
223+
with meow:
224+
assert meow.n == 1
225+
with meow:
226+
assert meow.n == 1
227+
assert meow.n == 1
228+
assert meow.n == 1
229+
assert meow.n == 0
230+
231+
232+
def test_decorator_decorator():
233+
@as_reentrant
234+
@ContextManagerType
235+
def Meow():
236+
Meow.n += 1
237+
try:
238+
yield
239+
finally:
240+
Meow.n -= 1
241+
242+
243+
meow = Meow()
244+
assert Meow.n == 0
245+
with meow:
246+
assert Meow.n == 1
247+
with meow:
248+
assert Meow.n == 1
249+
with meow:
250+
assert Meow.n == 1
251+
assert Meow.n == 1
252+
assert Meow.n == 1
253+
assert Meow.n == 0
254+
255+
178256

0 commit comments

Comments
 (0)