Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions dpath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions tests/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]