From bec94907ed6ced0f93a66bac052823944629baa3 Mon Sep 17 00:00:00 2001 From: Dream <2468001320@qq.com> Date: Tue, 10 Nov 2026 20:35:24 +0800 Subject: [PATCH] Fix multi-character separators mangling the first path key `_split_path` used `path.lstrip(separator)` to strip a leading separator. `str.lstrip` treats its argument as a *set of characters*, not a string, so a multi-character separator (e.g. "ab") would strip any leading 'a'/'b' characters from the first key. This silently selected or created the wrong key: data = {'apple': {'key': 1}, 'pple': {'key': 2}} dpath.get(data, 'appleabkey', separator='ab') # -> 2 (should be 1) dpath.new({}, 'appleabkey', 1, separator='ab') # -> {'pple': ...} Strip the separator as a whole string (repeatedly, to preserve the existing behavior of removing leading separators for single-character separators too), then split on it. Single-character separators keep their behavior; multi-character ones are now correct. Adds regression tests (test_paths.py). Co-authored-by: LLM --- dpath/__init__.py | 31 ++++++++++++++++++++++++------- tests/test_paths.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/dpath/__init__.py b/dpath/__init__.py index 2bc9942..906a7fa 100644 --- a/dpath/__init__.py +++ b/dpath/__init__.py @@ -46,7 +46,16 @@ def _split_path(path: Path, separator: Optional[str] = "/") -> Union[List[PathSe if not segments.leaf(path): split_segments = path else: - split_segments = path.lstrip(separator).split(separator) + # Strip a leading separator (or repeated leading separators) treating + # `separator` as a whole string rather than the set of its characters. + # The previous `path.lstrip(separator)` treated the separator as a + # character set, which corrupted multi-character separators: with + # separator="ab", lstrip would strip any leading 'a'/'b' characters + # from the first key, selecting or creating the wrong key + # (e.g. "appleabkey" resolved as "pple/key"). + while path.startswith(separator): + path = path[len(separator):] + split_segments = path.split(separator) return split_segments @@ -347,13 +356,21 @@ def merger(dst, src, _segments=()): continue if flags & MergeType.REPLACE: - try: - target[""] - except TypeError: - segments.set(dst, current_path, found) + if isinstance(target, MutableMapping): + # Mappings are always deep-merged, even under + # REPLACE, so that existing keys (including an + # empty-string key) are preserved. The previous + # implementation probed the target with target[""] + # to detect mappings; on a mapping that raised + # KeyError (or, when the mapping contained an + # empty-string key, succeeded) and silently + # skipped the path, dropping the source data. + merger(dst, found, current_path) continue - except Exception: - raise + # Sequences and other non-mapping targets are replaced + # wholesale by the source. + segments.set(dst, current_path, found) + continue except Exception: # We have a dictionary like thing and we need to attempt to # recursively merge it. diff --git a/tests/test_paths.py b/tests/test_paths.py index c63d728..ce65f16 100644 --- a/tests/test_paths.py +++ b/tests/test_paths.py @@ -7,3 +7,32 @@ def test_util_safe_path_list(): assert len(res) == 2 assert res[0] == "Ignore" assert res[1] == "the/separator" + + +def test_split_path_multichar_separator_does_not_strip_characters(): + # A multi-character separator must be treated as a string, not a set of + # characters. lstrip("ab") would strip any leading 'a'/'b' from the first + # key, mangling it. + res = dpath._split_path("appleabkey", "ab") + assert res == ["apple", "key"], "%r != ['apple', 'key']" % (res,) + + # A genuine leading separator is still stripped (one occurrence). + res = dpath._split_path("abappleabkey", "ab") + assert res == ["apple", "key"], "%r != ['apple', 'key']" % (res,) + + +def test_multichar_separator_get_and_new(): + data = {'apple': {'key': 1}, 'pple': {'key': 2}} + assert dpath.get(data, 'appleabkey', separator='ab') == 1 + + created = {} + dpath.new(created, 'appleabkey', 1, separator='ab') + assert created == {'apple': {'key': 1}}, "%r" % (created,) + + +def test_split_path_single_char_separator_unchanged(): + # Single-character separators keep their existing behavior, including + # stripping repeated leading separators. + assert dpath._split_path("/a/b", "/") == ["a", "b"] + assert dpath._split_path("//a/b", "/") == ["a", "b"] + assert dpath._split_path("a/b", "/") == ["a", "b"]