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
29 changes: 21 additions & 8 deletions deepdiff/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def _add_to_elements(elements, elem, inside):


DEFAULT_FIRST_ELEMENT = ('root', GETATTR)
_ESCAPED_STR_RE = re.compile(r"""u(?:'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*")(?=\])""")


@lru_cache(maxsize=1024 * 128)
Expand All @@ -96,7 +97,22 @@ def _path_to_elements(path, root_element=DEFAULT_FIRST_ELEMENT):
brackets = []
inside_quotes = False
quote_used = ''
for char in path:
literal_end = 0
for index, char in enumerate(path):
if index < literal_end:
continue
if char == 'u' and not inside_quotes and inside == '[' and not elem:
match = _ESCAPED_STR_RE.match(path, index)
if match:
try:
value = literal_eval(match.group())
except (ValueError, SyntaxError):
pass
else:
elements.append((value, GET))
literal_end = match.end()
prev_char = None
continue
if prev_char == '𝆺𝅥𝅯':
elem += char
elif char in {'"', "'"}:
Expand Down Expand Up @@ -313,13 +329,10 @@ def parse_path(path, root_element=DEFAULT_FIRST_ELEMENT, include_actions=False):
def stringify_element(param, quote_str=None):
has_quote = "'" in param
has_double_quote = '"' in param
if has_quote and has_double_quote and not quote_str:
new_param = []
for char in param:
if char in {'"', "'"}:
new_param.append('𝆺𝅥𝅯')
new_param.append(char)
result = '"' + ''.join(new_param) + '"'
if has_quote and has_double_quote:
# Mark escaped literals explicitly so legacy raw-backslash paths retain
# their meaning, including keys ending in a backslash before a new segment.
result = 'u' + str.__repr__(param)
elif has_quote:
result = f'"{param}"'
elif has_double_quote:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_diff_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def test_diff_quote_and_double_quote_in_string(self):
t1 = {'''a'"a''': 1}
t2 = {'''a'"a''': 2}
diff = DeepDiff(t1, t2)
expected = {'values_changed': {'root["a\'"a"]': {'new_value': 2, 'old_value': 1}}}
expected = {'values_changed': {"root[u'a\\\'\"a']": {'new_value': 2, 'old_value': 1}}}
assert expected == diff

def test_bytes(self):
Expand Down
82 changes: 82 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from deepdiff import DeepDiff, Delta
from deepdiff.path import _path_to_elements, GET, GETATTR, extract, parse_path, stringify_path, _add_to_elements


Expand Down Expand Up @@ -56,6 +57,87 @@ def test_parse_path():
assert [{'element': 'joe', 'action': 'GET'}, {'element': 'age', 'action': 'GETATTR'}] == result4


@pytest.mark.parametrize('key', [
"a'\"a",
"a\"'a",
"a'\"\\",
"a'\"\\r\\n",
"a'\"\r\n",
"a'\"\U0001d1c0",
"a'][\"b.c",
])
@pytest.mark.parametrize('change', ['added', 'removed', 'changed'])
def test_string_key_with_both_quotes_round_trip(key, change):
old = {'container': {'unchanged': 0}}
new = {'container': {'unchanged': 0}}
if change != 'added':
old['container'][key] = 1
if change != 'removed':
new['container'][key] = 2
diff = DeepDiff(old, new, threshold_to_diff_deeper=0)
category = 'values_changed' if change == 'changed' else f'dictionary_item_{change}'
path = next(iter(diff[category]))

assert parse_path(path) == ['container', key]
assert stringify_path([('container', GET), (key, GET)]) == path
source = old if change == 'removed' else new
assert extract(source, path) == source['container'][key]
delta = Delta(diff, bidirectional=True, raise_errors=True)
assert old + delta == new
assert new - delta == old
assert old == {'container': {'unchanged': 0, **({key: 1} if change != 'added' else {})}}
assert new == {'container': {'unchanged': 0, **({key: 2} if change != 'removed' else {})}}


@pytest.mark.parametrize('key', [
'plain', "only'quote", 'only"quote', r'back\slash', 'ending\\',
"marker\U0001d1c0'quote", 'marker\U0001d1c0\U0001d1c0text', '*', '**',
])
def test_existing_string_key_path_round_trip(key):
path = stringify_path([(key, GET)])
assert parse_path(path) == [key]
assert extract({key: 1}, path) == 1


@pytest.mark.parametrize('keys', [
['ending\\', "']"],
['"\\', ']'],
["'\\", '"]'],
["marker\U0001d1c0'quote", 'child'],
])
def test_existing_nested_string_key_path_round_trip(keys):
path = stringify_path([(key, GET) for key in keys])
assert parse_path(path) == keys
assert extract({keys[0]: {keys[1]: 1}}, path) == 1


def test_escaped_string_key_followed_by_attribute():
key = "a'\"a"
path = stringify_path([(key, GET), ('age', GETATTR)])
assert parse_path(path, include_actions=True) == [
{'element': key, 'action': GET}, {'element': 'age', 'action': GETATTR},
]


def test_string_subclass_key_with_both_quotes_round_trip():
class NamedString(str):
def __repr__(self):
return f'NamedString({str.__repr__(self)})'

def __str__(self):
return 'custom string representation'

key = NamedString("a'\"a")
old, new = {key: 1}, {key: 2}
diff = DeepDiff(old, new)
path = next(iter(diff['values_changed']))
assert parse_path(path) == [key]
assert extract(new, path) == 2
delta = Delta(diff, bidirectional=True, raise_errors=True)
assert old + delta == new
assert new - delta == old


@pytest.mark.parametrize('test_num, elem, inside, expected', [
(
1,
Expand Down