Skip to content

Commit 0a17f26

Browse files
committed
-
1 parent c43448f commit 0a17f26

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

python_toolbox/temp_value_setters/temp_value_setter.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,14 @@ def __init__(self, variable, value, assert_no_fiddling=True):
9696

9797
self.value = value
9898
'''The value to temporarily set to the variable.'''
99+
100+
self.active = False
99101

100102

101103
def __enter__(self):
102104

105+
self.active = True
106+
103107
self.old_value = self.getter()
104108
'''The old value of the variable, before entering the suite.'''
105109

@@ -108,7 +112,7 @@ def __enter__(self):
108112
# In `__exit__` we'll want to check if anyone changed the value of the
109113
# variable in the suite, which is unallowed. But we can't compare to
110114
# `.value`, because sometimes when you set a value to a variable, some
111-
# mechanism modifies that value fore various reasons, resulting in a
115+
# mechanism modifies that value for various reasons, resulting in a
112116
# supposedly equivalent, but not identical, value. For example this
113117
# happens when you set the current working directory on Mac OS.
114118
#
@@ -119,11 +123,12 @@ def __enter__(self):
119123
return self
120124

121125

122-
def __exit__(self, *args, **kwargs):
126+
def __exit__(self, exc_type, exc_value, exc_traceback):
123127

124128
if self.assert_no_fiddling:
125129
# Asserting no-one inside the suite changed our variable:
126130
assert self.getter() == self._value_right_after_setting
127131

128132
self.setter(self.old_value)
129-
133+
134+
self.active = False

test_python_toolbox/test_temp_value_setters/test_temp_value_setter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ def test_simple():
2525
with TempValueSetter((a, 'x'), 2):
2626
assert a.x == 2
2727
assert a.x == 1
28+
29+
30+
def test_active():
31+
a = Object()
32+
a.x = 1
2833

34+
assert a.x == 1
35+
temp_value_setter = TempValueSetter((a, 'x'), 2)
36+
assert not temp_value_setter.active
37+
with temp_value_setter:
38+
assert a.x == 2
39+
assert temp_value_setter.active
40+
assert not temp_value_setter.active
41+
assert a.x == 1
42+
2943

3044
def test_setter_getter():
3145
'''Test `TempValueSetter` with variable inputted as `(getter, setter)`.'''

0 commit comments

Comments
 (0)