Skip to content

Commit 8ec8730

Browse files
authored
registry: Fix some config values being reset when running upkeep
For example, with `supybot.protocols.http.proxy`: When upkeep is being executed, it runs the flushers: https://github.com/progval/Limnoria/blob/246f4d3e626cdbb1d744ba9c4e06c704d8c30a95/src/world.py#L148-L150 In the main limnoria script, it registers a flusher that saves the registry to disk: https://github.com/progval/Limnoria/blob/246f4d3e626cdbb1d744ba9c4e06c704d8c30a95/src/scripts/limnoria.py#L243-L252 When saving the registry to disk, the code instantiates the class with its default value to print it out in the file: https://github.com/progval/Limnoria/blob/246f4d3e626cdbb1d744ba9c4e06c704d8c30a95/src/registry.py#L149-L159 Instantiating the class calls `setValue()` by default: https://github.com/progval/Limnoria/blob/246f4d3e626cdbb1d744ba9c4e06c704d8c30a95/src/registry.py#L347-L348 supybot.protocols.http.proxy uses a custom type that changes global state when `setValue()` is called: https://github.com/progval/Limnoria/blob/246f4d3e626cdbb1d744ba9c4e06c704d8c30a95/src/conf.py#L1416-L1432 Fixed GH-1349.
1 parent cb51940 commit 8ec8730

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/registry.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ def close(registry, filename, private=True):
148148
if value._showDefault:
149149
lines.append('#\n')
150150
try:
151-
x = value.__class__(value._default, value._help)
151+
# We set setDefault to False and manually call
152+
# Value._setValue, just in case the class inherits
153+
# Value.setValue to set some global state (#1349)
154+
x = value.__class__(value._default, value._help,
155+
setDefault=False)
156+
x.value = value._default
152157
except Exception as e:
153158
exception('Exception instantiating default for %s:' %
154159
value._name)

test/test_registry.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ def testSpacesValues(self):
224224
registry.open_registry(filename)
225225
self.assertEqual(conf.supybot.networks.test.password(), ' foo ')
226226

227+
def testSetValueUncalledOnClose(self):
228+
values_set = 0
229+
class StringWithSetLogging(registry.String):
230+
def setValue(self, v):
231+
nonlocal values_set
232+
values_set += 1
233+
234+
super(StringWithSetLogging, self).setValue(v)
235+
236+
group = registry.Group()
237+
group.setName('group')
238+
conf.registerGlobalValue(group, 'string', StringWithSetLogging('test', 'help'))
239+
group.string.set('mrrp')
240+
241+
filename = conf.supybot.directories.conf.dirize('setvaluecalls.conf')
242+
registry.close(group, filename)
243+
self.assertEqual(values_set, 2)
244+
227245
def testReload(self):
228246
import supybot.world as world
229247
with conf.supybot.reply.whenAddressedBy.chars.context('@'):

0 commit comments

Comments
 (0)