From 1d4e2ebed6b277b7c3c83a07a14f47e98797722f Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sun, 4 Feb 2024 07:20:05 +0000 Subject: [PATCH] ConfigVar: add "converter" for getter, add use it for "proxy" key fixes https://github.com/spesmilo/electrum/issues/8837 Alternatively we could do a config upgrade (convert_version_*) to fix that specific issue, but they are not safe against downgrade-upgrade-again, so probably not the best choice for crash-inducing values. And this kind of converter is a generic solution that can be useful for other configvars later (though for most usages I expect we will also need a converter for the setter). --- electrum/simple_config.py | 8 +++++++- electrum/tests/test_simple_config.py | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/electrum/simple_config.py b/electrum/simple_config.py index 81aa1772f..02d6a9db1 100644 --- a/electrum/simple_config.py +++ b/electrum/simple_config.py @@ -63,12 +63,14 @@ class ConfigVar(property): *, default: Union[Any, Callable[['SimpleConfig'], Any]], # typically a literal, but can also be a callable type_=None, + convert_getter: Callable[[Any], Any] = None, short_desc: Callable[[], str] = None, long_desc: Callable[[], str] = None, ): self._key = key self._default = default self._type = type_ + self._convert_getter = convert_getter # note: the descriptions are callables instead of str literals, to delay evaluating the _() translations # until after the language is set. assert short_desc is None or callable(short_desc) @@ -83,6 +85,10 @@ class ConfigVar(property): with config.lock: if config.is_set(self._key): value = config.get(self._key) + # run converter + if self._convert_getter is not None: + value = self._convert_getter(value) + # type-check if self._type is not None: assert value is not None, f"got None for key={self._key!r}" try: @@ -924,7 +930,7 @@ class SimpleConfig(Logger): # config variables -----> NETWORK_AUTO_CONNECT = ConfigVar('auto_connect', default=True, type_=bool) NETWORK_ONESERVER = ConfigVar('oneserver', default=False, type_=bool) - NETWORK_PROXY = ConfigVar('proxy', default=None, type_=str) + NETWORK_PROXY = ConfigVar('proxy', default=None, type_=str, convert_getter=lambda v: "none" if v is None else v) NETWORK_PROXY_USER = ConfigVar('proxy_user', default=None, type_=str) NETWORK_PROXY_PASSWORD = ConfigVar('proxy_password', default=None, type_=str) NETWORK_SERVER = ConfigVar('server', default=None, type_=str) diff --git a/electrum/tests/test_simple_config.py b/electrum/tests/test_simple_config.py index 62c6cdf72..e06beadb6 100644 --- a/electrum/tests/test_simple_config.py +++ b/electrum/tests/test_simple_config.py @@ -164,6 +164,14 @@ class Test_SimpleConfig(ElectrumTestCase): finally: constants.set_mainnet() + def test_configvars_convert_getter(self): + config = SimpleConfig(self.options) + self.assertEqual(None, config.NETWORK_PROXY) + config.user_config[config.cv.NETWORK_PROXY.key()] = None + self.assertEqual("none", config.NETWORK_PROXY) + config.NETWORK_PROXY = None + self.assertEqual(None, config.NETWORK_PROXY) + def test_configvars_is_set(self): config = SimpleConfig(self.options) self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE)