Skip to content

Strip complete leading path separators instead of individual characters - #216

Open
Mrun25 wants to merge 1 commit into
dpath-maintainers:masterfrom
Mrun25:bugfix/215_separator_prefix
Open

Mrun25 wants to merge 1 commit into
dpath-maintainers:masterfrom
Mrun25:bugfix/215_separator_prefix

Conversation

@Mrun25

@Mrun25 Mrun25 commented Nov 7, 2026

Copy link
Copy Markdown

_split_path() currently passes the separator to str.lstrip(), which removes individual separator characters. With separator ab, appleabkey becomes pple/key. If both keys exist, get() silently returns the wrong value; new() can create the wrong key.

Strip only complete leading separator tokens before splitting the path. Repeated whole separators still work. Single-character separators, list paths, None and empty-separator behavior are preserved.

The regression tests cover partial prefixes, repeated leading separators, get(), new(), set(), delete() and search(). Five tests fail against unchanged master and pass with the fix; a separate control checks existing separator behavior.

Validation:

  • Canonical tox environments pass on CPython 3.8-3.14 and PyPy 3.7: 85 tests each.
  • Repository flake8 check passes.
  • Source and wheel builds pass; Twine metadata checks pass with the existing missing content-type warning.
  • Tests ran on Linux. Remote CI has not been verified.

AI assistance was used to prepare and locally test this change. The reproduction uses synthetic inputs, not a claimed production incident.

@vaclavstacke

Copy link
Copy Markdown
  1. Root Cause & Silent Corruption Mechanism
    When splitting a hierarchical path string, leading delimiters are typically stripped so that a leading separator does not produce an empty root segment (e.g., "/a/b" becoming ["a", "b"] instead of ["", "a", "b"]).

However, because str.lstrip(separator) removes any leading character present anywhere in separator, passing a multi-character delimiter breaks token boundaries:

Character-set vs. token matching: Given separator="ab", str.lstrip("ab") strips any sequence of 'a' and 'b' characters from the start of the string. If the first path segment legitimately begins with a partial prefix of that delimiter (e.g., "appleabkey"), the leading 'a' is erroneously stripped, mutating the segment into "pple".

Downstream CRUD impact: Because the path parser silently alters the key name before tree traversal, read operations (get(), search()) either fail to find existing keys or silently resolve to an unintended sibling key ("pple"). Similarly, write and delete operations (new(), set(), delete()) traverse the wrong branch, creating corrupted structures or mutating unrelated data without raising an exception.

  1. Theoretical Resolution
    The theoretical fix decouples leading-delimiter normalization from character-set stripping by enforcing atomic prefix matching:

Iterative Whole-Token Removal: Instead of stripping characters belonging to the delimiter set, the algorithm must check whether the string begins with the exact delimiter sequence and slice off the entire token length (len(separator)) iteratively:

while path.startswith
(
sep
)
:
path
←
path
[
len
(
sep
)
:
]
while path.startswith(sep):path←path[len(sep):]
Why Iterative Slicing Over Regex: While an anchored regular expression (e.g., ^(?:sep)+) achieves the same formal language matching, iterative slicing with startswith() avoids regular-expression compilation overhead and eliminates subtle bugs when delimiters contain regex metacharacters (such as ., $, +, or [).

  1. Preserving Behavioral Invariants
    A sound theoretical fix must alter only the defective multi-character prefix behavior while keeping all existing edge-case contracts intact:

Repeated Leading Delimiters: Because the prefix check runs in a loop, consecutive full delimiter tokens (e.g., "ababappleabkey") are still stripped completely down to "appleabkey", while a trailing partial match (e.g., "abaappleabkey") strips only the complete "ab" token and preserves "aapple".

Single-Character Delimiters: When
∣
separator
∣

1
∣separator∣=1, a single-character token is identical to its character set, making atomic prefix removal mathematically equivalent to str.lstrip().

Whitespace (separator=None) Handling: In Python, str.split(None) splits on arbitrary runs of whitespace and automatically ignores leading whitespace. Guarding the token-stripping loop with a truthiness check (if separator:) bypasses manual stripping when separator is None, preserving native whitespace-splitting semantics.

Empty Delimiter (separator="") Handling: Guarding with if separator: also prevents an infinite loop (since every string starts with ""), allowing execution to fall through to str.split(""), which raises the standard ValueError: empty separator.

Pre-Split Sequence Paths: Checking for sequence types (list, tuple) prior to string normalization ensures non-string paths bypass delimiter stripping altogether.

  1. Verification Strategy
    To prove correctness without regressions, verification relies on differential testing:

Failure-to-Pass Regression Cases: Construct colliding keys (e.g., "apple" alongside "pple") with multi-character delimiters and execute read, create, update, search, and delete operations. On unpatched code, these tests fail due to silent key aliasing; with atomic prefix stripping, they resolve strictly to the intended key.

Invariant Control Cases: Run parallel assertions on single-character separators, repeated leading separators, None, empty strings, and list paths to confirm zero behavioral drift across standard usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants