fix: refuse a yaml mapping that repeats a key

This commit is contained in:
Abdessamad Derraz committed 2026-10-17 15:52:59 +02:00
1 parent a8bfadd4c1
commit 9db68c9d52
2 files changed
+38 -1

No files matched your search

+25 -1
View File
@@ -60,7 +60,31 @@ def _pick_yaml_loader():
"""
if yaml is None:
return None
return getattr(yaml, "CSafeLoader", None) or yaml.SafeLoader
base = getattr(yaml, "CSafeLoader", None) or yaml.SafeLoader
class _UniqueKeyLoader(base):
"""Refuses a mapping that names a key twice.
PyYAML keeps the last value without a word: the registry once
carried two target_source keys for Recalbox and read only one. Only
a mapping that came out shorter than its source is re-read. No data
here uses merge keys (<<), which would count as repeats too.
"""
def construct_mapping(self, node, deep=False):
mapping = super().construct_mapping(node, deep=deep)
if len(mapping) < len(node.value):
seen: set = set()
for key_node, _value in node.value:
key = self.construct_object(key_node, deep=deep)
if key in seen:
raise yaml.constructor.ConstructorError(
None, None, f"duplicate key {key!r}", key_node.start_mark
)
seen.add(key)
return mapping
return _UniqueKeyLoader
_YAML_LOADER = _pick_yaml_loader()