Skip to content

Commit 5afb7c3

Browse files
Update collections from v3.14.3 (RustPython#7135)
- Updated Lib/collections from CPython v3.14.3 - Preserved RustPython's fallback implementation for defaultdict - Added test for update_reentrant_add_clears_counter - Minor formatting fixes in test file Co-authored-by: CPython Developers <>
1 parent fed2826 commit 5afb7c3

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

Lib/collections/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@
5757
try:
5858
from _collections import defaultdict
5959
except ImportError:
60-
# FIXME: try to implement defaultdict in collections.rs rather than in Python
61-
# I (coolreader18) couldn't figure out some class stuff with __new__ and
62-
# __init__ and __missing__ and subclassing built-in types from Rust, so I went
63-
# with this instead.
60+
# TODO: RUSTPYTHON - implement defaultdict in Rust
6461
from ._defaultdict import defaultdict
6562

6663
heapq = None # Lazily imported

Lib/test/test_collections.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def __contains__(self, key):
262262
d = c.new_child(b=20, c=30)
263263
self.assertEqual(d.maps, [{'b': 20, 'c': 30}, {'a': 1, 'b': 2}])
264264

265-
@unittest.expectedFailure # TODO: RUSTPYTHON
265+
@unittest.expectedFailure # TODO: RUSTPYTHON
266266
def test_union_operators(self):
267267
cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4))
268268
cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4))
@@ -1957,7 +1957,7 @@ class X(ByteString): pass
19571957
# No metaclass conflict
19581958
class Z(ByteString, Awaitable): pass
19591959

1960-
@unittest.expectedFailure # TODO: RUSTPYTHON; Need to implement __buffer__ and __release_buffer__ (https://docs.python.org/3.13/reference/datamodel.html#emulating-buffer-types)
1960+
@unittest.expectedFailure # TODO: RUSTPYTHON; Need to implement __buffer__ and __release_buffer__ (https://docs.python.org/3.13/reference/datamodel.html#emulating-buffer-types)
19611961
def test_Buffer(self):
19621962
for sample in [bytes, bytearray, memoryview]:
19631963
self.assertIsInstance(sample(b"x"), Buffer)
@@ -2029,7 +2029,7 @@ def insert(self, index, value):
20292029
self.assertEqual(len(mss), len(mss2))
20302030
self.assertEqual(list(mss), list(mss2))
20312031

2032-
@unittest.expectedFailure # TODO: RUSTPYTHON
2032+
@unittest.expectedFailure # TODO: RUSTPYTHON
20332033
def test_illegal_patma_flags(self):
20342034
with self.assertRaises(TypeError):
20352035
class Both(Collection):
@@ -2121,6 +2121,19 @@ def test_basics(self):
21212121
self.assertEqual(c.setdefault('e', 5), 5)
21222122
self.assertEqual(c['e'], 5)
21232123

2124+
def test_update_reentrant_add_clears_counter(self):
2125+
c = Counter()
2126+
key = object()
2127+
2128+
class Evil(int):
2129+
def __add__(self, other):
2130+
c.clear()
2131+
return NotImplemented
2132+
2133+
c[key] = Evil()
2134+
c.update([key])
2135+
self.assertEqual(c[key], 1)
2136+
21242137
def test_init(self):
21252138
self.assertEqual(list(Counter(self=42).items()), [('self', 42)])
21262139
self.assertEqual(list(Counter(iterable=42).items()), [('iterable', 42)])

0 commit comments

Comments
 (0)