Skip to content

Commit abf3bb6

Browse files
committed
-
1 parent dc4ee5e commit abf3bb6

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

source_py3/python_toolbox/context_management/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,8 @@ def do_stuff():
108108
your generator functions with `ContextManagerType`) to enjoy all these
109109
benefits.
110110
111-
112-
This package also defines a bunch of helpful context manager classes on top of
113-
`ContextManager`: Those are `BlankContextManager`, `ReentrantContextManager`
114-
and `DelegatingContextManager`. See these classes' docstrings for more info.
111+
This package also defines a bunch of helpful classes and modules related to
112+
context managers. See their docstrings for more info.
115113
'''
116114

117115
# todo: review the few external tests that I'm skipping.

source_py3/python_toolbox/context_management/functions.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def as_idempotent(context_manager):
6363
`ExitStack` closes. This way you don't risk an exception by having the
6464
context manager exit twice.
6565
66-
Note: The value returned by `reentrant_enter` will be returned by all the
67-
no-op `__enter__` actions contained in the outermost suite.
66+
Note: The first value returned by `__enter__` will be returned by all the
67+
subsequent no-op `__enter__` calls.
6868
'''
6969
return _IdempotentContextManager(context_manager)
7070

@@ -77,8 +77,8 @@ def as_reentrant(context_manager):
7777
times, and only after it's been exited the same number of times that it has
7878
been entered will the original `__exit__` method be called.
7979
80-
Note: The value returned by `reentrant_enter` will be returned by all the
81-
no-op `__enter__` actions contained in the outermost suite.
80+
Note: The first value returned by `__enter__` will be returned by all the
81+
subsequent no-op `__enter__` calls.
8282
'''
8383
return _ReentrantContextManager(context_manager)
8484

@@ -134,7 +134,9 @@ def __enter__(self):
134134
def __exit__(self, exc_type=None, exc_value=None, exc_traceback=None):
135135
assert self.depth >= 1
136136
if self.depth == 1:
137-
exit_value = self.__exit__(exc_type, exc_value, exc_traceback)
137+
exit_value = self.__wrapped__.__exit__(
138+
exc_type, exc_value, exc_traceback
139+
)
138140
self._enter_value = None
139141
else:
140142
exit_value = None

source_py3/python_toolbox/wx_tools/widgets/hue_selection_dialog/textual.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
from python_toolbox import freezing
1515
from python_toolbox import wx_tools
1616
from python_toolbox.wx_tools.widgets.cute_panel import CutePanel
17-
from python_toolbox.context_management import ReentrantContextManager
18-
1917

2018
def ratio_to_round_degrees(ratio):
2119
return int(ratio * 360)
@@ -25,18 +23,6 @@ def degrees_to_ratio(degrees):
2523
return degrees / 360
2624

2725

28-
#class ValueFreezer(ReentrantContextManager):
29-
#'''
30-
#Freezer for not changing the `Textual`'s text value.
31-
32-
#Used as a context manager. Anything that happens inside the `with` suite
33-
#will not cause the `Textual` to update its text value.
34-
35-
#This is useful because when the `Textual`'s value changes, some platforms
36-
#automatically select all the text in the `Textual`, which is really
37-
#annoying if you're just typing in it.
38-
#'''
39-
4026

4127
class Textual(CutePanel):
4228
'''Display (and allow modifying) the hue as a number 0-359.'''

source_py3/test_python_toolbox/test_context_management/test_idempotentify.py renamed to source_py3/test_python_toolbox/test_context_management/test_as_idempotent.py

File renamed without changes.

source_py3/test_python_toolbox/test_context_management/test_reentrant_context_manager.py renamed to source_py3/test_python_toolbox/test_context_management/test_as_reentrant.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
7878

7979

8080
def test_exception_swallowing():
81-
class SwallowingReentrantContextManager(ReentrantContextManager):
81+
class SwallowingContextManager(ContextManager):
8282
def __init__(self):
8383
self.times_entered = 0
8484
self.times_exited = 0
85-
def reentrant_enter(self):
85+
def __enter__(self):
8686
self.times_entered += 1
8787
return self
88-
def reentrant_exit(self, exc_type, exc_value, exc_traceback):
88+
def __exit__(self, exc_type, exc_value, exc_traceback):
8989
self.times_exited += 1
9090
if isinstance(exc_value, MyException):
9191
return True
9292

93-
swallowing_rcm = SwallowingReentrantContextManager()
93+
swallowing_rcm = as_reentrant(SwallowingContextManager())
9494

9595
my_set = set()
9696

@@ -118,34 +118,35 @@ def test_order_of_depth_modification():
118118

119119
depth_log = queue_module.Queue()
120120

121-
class JohnnyReentrantContextManager(ReentrantContextManager):
122-
def reentrant_enter(self):
121+
class JohnnyContextManager(ContextManager):
122+
def __enter__(self):
123123
depth_log.put(self.depth)
124124
return self
125-
def reentrant_exit(self, exc_type, exc_value, exc_traceback):
125+
def __exit__(self, exc_type, exc_value, exc_traceback):
126126
depth_log.put(self.depth)
127127

128-
johnny_reentrant_context_manager = JohnnyReentrantContextManager()
128+
johnny_reentrant_context_manager = as_reentrant(JohnnyContextManager())
129129
assert johnny_reentrant_context_manager.depth == 0
130130
with johnny_reentrant_context_manager:
131131
assert johnny_reentrant_context_manager.depth == 1
132132

133-
# `reentrant_enter` saw a depth of 0, because the depth increment
134-
# happens *after* `reentrant_enter` is called:
133+
# `.__wrapped__.__enter__` saw a depth of 0, because the depth
134+
# increment happens *after* `.__wrapped__.__enter__` is called:
135135
assert depth_log.get(block=False) == 0
136136

137137
with johnny_reentrant_context_manager:
138138

139139
assert johnny_reentrant_context_manager.depth == 2
140140
assert depth_log.qsize() == 0 # We're in a depth greater than 1,
141-
# so `reentrant_enter` wasn't even
142-
# called.
141+
# so `.__wrapped__.__enter__` wasn't
142+
# even called.
143143

144144
assert johnny_reentrant_context_manager.depth == 1
145145

146146
assert depth_log.qsize() == 0 # We came out of a depth greater than 1,
147-
# so `reentrant_exit` wasn't even called.
147+
# so `.__wrapped__.__enter__` wasn't even
148+
# called.
148149

149-
# `reentrant_exit` saw a depth of 1, because the depth decrement happens
150-
# *after* `reentrant_exit` is called:
150+
# `.__wrapped__.__enter__` saw a depth of 1, because the depth decrement
151+
# happens *after* `.__wrapped__.__enter__` is called:
151152
assert depth_log.get(block=False) == 1

0 commit comments

Comments
 (0)