Skip to content

Commit b590c86

Browse files
committed
config: fix setting CLI_TIMEOUT configvar, and add "convert_setter"s
"type_=float" behaves a bit weirdly. Was kinda broken before, still not fully "fixed" here. With this commit, if used together with convert_setter, it at least behaves in a sane way. ``` $ ./run_electrum -o setconfig timeout 10 1.16 | E | __main__ | error running command (without daemon) Traceback (most recent call last): File "/home/user/wspace/electrum/./run_electrum", line 593, in handle_cmd result = fut.result() File "/usr/lib/python3.10/concurrent/futures/_base.py", line 458, in result return self.__get_result() File "/usr/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result raise self._exception File "/home/user/wspace/electrum/./run_electrum", line 268, in run_offline_command result = await func(*args, **kwargs) File "/home/user/wspace/electrum/electrum/commands.py", line 194, in func_wrapper return await func(*args, **kwargs) File "/home/user/wspace/electrum/electrum/commands.py", line 408, in setconfig self._setconfig(key, value) File "/home/user/wspace/electrum/electrum/commands.py", line 398, in _setconfig cv.set(value) File "/home/user/wspace/electrum/electrum/simple_config.py", line 126, in set self._config_var._set_config_value(self._config, value, save=save) File "/home/user/wspace/electrum/electrum/simple_config.py", line 89, in _set_config_value raise ValueError( ValueError: ConfigVar.set type-check failed. key='timeout'. type=<class 'float'>. value=10 ```
1 parent fa0921e commit b590c86

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

electrum/simple_config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(
3737
default: Union[Any, Callable[['SimpleConfig'], Any]], # typically a literal, but can also be a callable
3838
type_=None,
3939
convert_getter: Callable[[Any], Any] = None,
40+
convert_setter: Callable[[Any], Any] = None,
4041
short_desc: Callable[[], str] = None,
4142
long_desc: Callable[[], str] = None,
4243
plugin: Optional[str] = None,
@@ -45,6 +46,7 @@ def __init__(
4546
self._default = default
4647
self._type = type_
4748
self._convert_getter = convert_getter
49+
self._convert_setter = convert_setter
4850
# note: the descriptions are callables instead of str literals, to delay evaluating the _() translations
4951
# until after the language is set.
5052
assert short_desc is None or callable(short_desc)
@@ -84,6 +86,10 @@ def _get_config_value(self, config: 'SimpleConfig'):
8486
return value
8587

8688
def _set_config_value(self, config: 'SimpleConfig', value, *, save=True):
89+
# run converter
90+
if self._convert_setter is not None and value is not None:
91+
value = self._convert_setter(value)
92+
# type-check
8793
if self._type is not None and value is not None:
8894
if not isinstance(value, self._type):
8995
raise ValueError(
@@ -853,7 +859,7 @@ def __setattr__(self, name, value):
853859
'For more information, see https://openalias.org'),
854860
)
855861
HWD_SESSION_TIMEOUT = ConfigVar('session_timeout', default=300, type_=int)
856-
CLI_TIMEOUT = ConfigVar('timeout', default=60, type_=float)
862+
CLI_TIMEOUT = ConfigVar('timeout', default=60.0, type_=float, convert_setter=lambda v: float(v))
857863
AUTOMATIC_CENTRALIZED_UPDATE_CHECKS = ConfigVar(
858864
'check_updates', default=False, type_=bool,
859865
short_desc=lambda: _("Automatically check for software updates"),

tests/test_simple_config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ def test_configvars_convert_getter(self):
161161
config.NETWORK_PROXY = None
162162
self.assertEqual(None, config.NETWORK_PROXY)
163163

164+
def test_configvars_convert_setter(self):
165+
config = SimpleConfig(self.options)
166+
self.assertEqual(60, config.CLI_TIMEOUT)
167+
assert isinstance(config.CLI_TIMEOUT, float)
168+
169+
config.CLI_TIMEOUT = 10
170+
self.assertEqual(10, config.CLI_TIMEOUT)
171+
assert isinstance(config.CLI_TIMEOUT, float)
172+
173+
config.CLI_TIMEOUT = None
174+
self.assertEqual(60, config.CLI_TIMEOUT)
175+
assert isinstance(config.CLI_TIMEOUT, float)
176+
164177
def test_configvars_is_set(self):
165178
config = SimpleConfig(self.options)
166179
self.assertEqual(MAX_MSG_SIZE_DEFAULT, config.NETWORK_MAX_INCOMING_MSG_SIZE)

0 commit comments

Comments
 (0)