8000 gh-119004: fix a crash in equality testing between `OrderedDict` by picnixz · Pull Request #121329 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-119004: fix a crash in equality testing between OrderedDict #121329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 23, 2024
Merged
Changes from 1 commit
Commits
8000 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
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
picnixz committed Jul 3, 2024
commit 6cdabc6296e76cd81b51549449da8cb546093928
40 changes: 40 additions & 0 deletions Lib/test/test_ordered_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import contextlib
import copy
import gc
import operator
import pickle
import re
from random import randrange, shuffle
import struct
import sys
Expand Down Expand Up @@ -612,6 +614,44 @@ def test_issue24667(self):
key = c0 + c1
od[key] = key

def test_issue119004_change_size(self):
count = 0
class ChangeExternSize:
def __eq__(self, other):
nonlocal count
if count == 1:
l.clear()
count += 1
return True
def __hash__(self):
return 3

l0, r0 = ChangeExternSize(), ChangeExternSize()
l = self.OrderedDict({l0: 1, 2: 3})
r = self.OrderedDict({r0: 1, 2: 3})
msg = re.escape("OrderedDict changed size during iteration")
self.assertRaisesRegex(RuntimeError, msg, operator.eq, l, r)

def test_issue119004_change_item(self):
count = 0
class ChangeExternItem:
def __eq__(self, other):
nonlocal count
if count == 1:
# change the layout of the underlying linked list
del lhs[0]
lhs[object()] = object()
count += 1
return True
def __hash__(self):
return 3

l1, r1 = ChangeExternItem(), ChangeExternItem()
lhs = self.OrderedDict(dict.fromkeys((0, l1)))
rhs = self.OrderedDict(dict.fromkeys((0, r1)))
msg = re.escape("OrderedDict mutated during iteration")
self.assertRaisesRegex(RuntimeError, msg, operator.eq, lhs, rhs)

# Direct use of dict methods

def test_dict_setitem(self):
Expand Down
0