Skip to content

Commit 26c875a

Browse files
samuel-gauthierrjarry
authored andcommitted
xpath: properly process key names with \ in xpath_split
There is no reason to remove the '\' in a key name if they are not escaping a quote. Leave them untouched. Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent bca074e commit 26c875a

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

libyang/xpath.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@ def xpath_split(xpath: str) -> Iterator[Tuple[str, str, List[Tuple[str, str]]]]:
6060
quote = xpath[j + 1] # record opening quote character
6161
j = i = j + 2 # skip '=' and opening quote
6262
while True:
63-
if xpath[j] == "\\":
64-
j += 1 # skip escaped character
65-
elif xpath[j] == quote:
66-
break # end of key value
63+
if xpath[j] == quote and xpath[j - 1] != "\\":
64+
break
6765
j += 1
6866
# replace escaped chars by their non-escape version
69-
key_value = xpath[i:j].replace("\\", "")
67+
key_value = xpath[i:j].replace(f"\\{quote}", f"{quote}")
7068
keys.append((key_name, key_value))
7169
i = j + 2 # skip closing quote and ']'
7270

tests/test_xpath.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ def test_xpath_getall(self):
236236
"/nam1/p:nam2": [(None, "nam1", []), ("p", "nam2", [])],
237237
'/p:nam/lst[k1="foo"]': [("p", "nam", []), (None, "lst", [("k1", "foo")])],
238238
'/p:nam/lst[.="foo"]': [("p", "nam", []), (None, "lst", [(".", "foo")])],
239+
'/p:nam/lst[.="foo\\bar"]': [("p", "nam", []), (None, "lst", [(".", "foo\\bar")])],
239240
"/nam/p:lst[k1='foo'][k2='bar']/x": [
240241
(None, "nam", []),
241242
("p", "lst", [("k1", "foo"), ("k2", "bar")]),

0 commit comments

Comments
 (0)