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:

246f4d3e62/src/world.py (L148-L150)

In the main limnoria script, it registers a flusher that saves the registry to disk:

246f4d3e62/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:

246f4d3e62/src/registry.py (L149-L159)

Instantiating the class calls `setValue()` by default:

246f4d3e62/src/registry.py (L347-L348)

supybot.protocols.http.proxy uses a custom type that changes global state when `setValue()` is called:

246f4d3e62/src/conf.py (L1416-L1432)


Fixed GH-1349.
This commit is contained in:
Claire 2024-10-23 18:19:37 +00:00 committed by GitHub
parent cb51940b42
commit 8ec873015a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -148,7 +148,12 @@ def close(registry, filename, private=True):
if value._showDefault:
lines.append('#\n')
try:
x = value.__class__(value._default, value._help)
# We set setDefault to False and manually call
# Value._setValue, just in case the class inherits
# Value.setValue to set some global state (#1349)
x = value.__class__(value._default, value._help,
setDefault=False)
x.value = value._default
except Exception as e:
exception('Exception instantiating default for %s:' %
value._name)

View File

@ -224,6 +224,24 @@ class ValuesTestCase(SupyTestCase):
registry.open_registry(filename)
self.assertEqual(conf.supybot.networks.test.password(), ' foo ')
def testSetValueUncalledOnClose(self):
values_set = 0
class StringWithSetLogging(registry.String):
def setValue(self, v):
nonlocal values_set
values_set += 1
super(StringWithSetLogging, self).setValue(v)
group = registry.Group()
group.setName('group')
conf.registerGlobalValue(group, 'string', StringWithSetLogging('test', 'help'))
group.string.set('mrrp')
filename = conf.supybot.directories.conf.dirize('setvaluecalls.conf')
registry.close(group, filename)
self.assertEqual(values_set, 2)
def testReload(self):
import supybot.world as world
with conf.supybot.reply.whenAddressedBy.chars.context('@'):