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
16 changes: 12 additions & 4 deletions deepdiff/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ def _set_new_value(self, parent, parent_to_obj_elem, parent_to_obj_action,
"""
Set the element value on an object and if necessary convert the object to the proper mutable type
"""
# The new value comes from the stored diff: copy it so the result
# never aliases the Delta's own values. Otherwise applying the
# same Delta to several objects (the default mutate=False mode)
# links all results together and lets one caller corrupt the
# Delta for the next.
new_value = deepcopy(new_value)
if isinstance(obj, tuple):
# Check if it's a NamedTuple and use _replace() to generate a new copy with the change
if hasattr(obj, '_fields') and hasattr(obj, '_replace'):
Expand Down Expand Up @@ -677,16 +683,18 @@ def _do_iterable_opcodes(self):
# import pytest; pytest.set_trace()
obj = self.get_nested_obj(obj=self, elements=elements)
is_obj_tuple = isinstance(obj, tuple)
for opcode in opcodes:
for opcode in opcodes:
if opcode.tag == 'replace':
# Replace items in list a[i1:i2] with b[j1:j2]
transformed.extend(opcode.new_values)
# Replace items in list a[i1:i2] with b[j1:j2].
# Copy the new values: they belong to the stored
# diff and must not be aliased into the result.
transformed.extend(deepcopy(opcode.new_values))
elif opcode.tag == 'delete':
# Delete items from list a[i1:i2], so we do nothing here
continue
elif opcode.tag == 'insert':
# Insert items from list b[j1:j2] into the new list
transformed.extend(opcode.new_values)
transformed.extend(deepcopy(opcode.new_values))
elif opcode.tag == 'equal':
# Items are the same in both lists, so we add them to the result
transformed.extend(obj[opcode.t1_from_index:opcode.t1_to_index]) # type: ignore
Expand Down
20 changes: 20 additions & 0 deletions tests/test_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3058,3 +3058,23 @@ def test_moved_and_type_changed(self):
assert [item for item in result if "id" not in item] == [], \
"Phantom entries (dicts missing 'id') found in result"
assert result == t2

def test_delta_applied_twice_results_are_independent(self):
# Regression test for #655: values inserted by a Delta were
# references into the stored diff, so every application of the
# same Delta shared them.
delta = Delta(DeepDiff({}, {"tags": []}))
left = {} + delta
right = {} + delta
left["tags"].append("left only")
assert right == {"tags": []}
assert {} + delta == {"tags": []}

def test_delta_type_change_mutable_value_is_copied(self):
# Same aliasing issue through a type change whose new value is
# mutable: editing one result must not leak into the next
# application of the Delta.
delta = Delta(DeepDiff({"a": None}, {"a": {"k": []}}))
first = {"a": None} + delta
first["a"]["k"].append(1)
assert {"a": None} + delta == {"a": {"k": []}}