Skip to content

Commit 0f3f5bb

Browse files
authored
Fixed setting values in missing config sections (#8471)
2 parents ca74bf6 + 361f9be commit 0f3f5bb

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: src/tribler/test_unit/test_tribler_config.py

+11
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ def test_get_set_explicit(self) -> None:
4141
config.set("libtorrent/download_defaults/seeding_time", 42)
4242

4343
self.assertEqual(42, config.get("libtorrent/download_defaults/seeding_time"))
44+
45+
def test_set_on_old(self) -> None:
46+
"""
47+
Test if we can set a key on an old dict that is missing it.
48+
"""
49+
config = TriblerConfigManager()
50+
config.configuration = {k: v for k, v in config.configuration.items() if k != "rss"}
51+
52+
config.set("rss/enabled", True)
53+
54+
self.assertTrue(config.get("rss/enabled"))

Diff for: src/tribler/tribler_config.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,15 @@ def set(self, option: os.PathLike | str, value: dict | list | str | float | bool
332332
"""
333333
current = self.configuration
334334
for part in Path(option).parts[:-1]:
335-
current = current[part]
335+
if part in current:
336+
current = current[part]
337+
else:
338+
# Fetch from defaults instead. Same as ``get()``, but now we inject defaults before overwriting.
339+
out = DEFAULT_CONFIG
340+
for df_part in Path(option).parts:
341+
out = out.get(df_part)
342+
if df_part == part:
343+
# We found the missing section, inject the defaults here and continue traversing the dict.
344+
current[part] = out
345+
break
336346
current[Path(option).parts[-1]] = value

0 commit comments

Comments
 (0)