Skip to content

Commit 2601647

Browse files
committed
gh-141510: Add frozendict fast-path to the set type
1 parent 6ef2578 commit 2601647

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

Lib/test/test_set.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ def test_symmetric_difference(self):
188188
self.assertEqual(type(i), self.basetype)
189189
self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru())
190190
self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
191-
for C in set, frozenset, dict.fromkeys, str, list, tuple:
191+
constructors = (set, frozenset,
192+
dict.fromkeys, frozendict.fromkeys,
193+
str, list, tuple)
194+
for C in constructors:
192195
self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd'))
193196
self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg'))
194197
self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a'))
@@ -1591,6 +1594,14 @@ def setUp(self):
15911594

15921595
#------------------------------------------------------------------------------
15931596

1597+
class TestOnlySetsFrozenDict(TestOnlySetsInBinaryOps, unittest.TestCase):
1598+
def setUp(self):
1599+
self.set = set((1, 2, 3))
1600+
self.other = frozendict({1:2, 3:4})
1601+
self.otherIsIterable = True
1602+
1603+
#------------------------------------------------------------------------------
1604+
15941605
class TestOnlySetsOperator(TestOnlySetsInBinaryOps, unittest.TestCase):
15951606
def setUp(self):
15961607
self.set = set((1, 2, 3))

Objects/setobject.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ set_iter(PyObject *so)
11831183
static int
11841184
set_update_dict_lock_held(PySetObject *so, PyObject *other)
11851185
{
1186-
assert(PyDict_CheckExact(other));
1186+
assert(PyAnyDict_CheckExact(other));
11871187

11881188
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
11891189
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(other);
@@ -1242,7 +1242,7 @@ set_update_lock_held(PySetObject *so, PyObject *other)
12421242
if (PyAnySet_Check(other)) {
12431243
return set_merge_lock_held(so, other);
12441244
}
1245-
else if (PyDict_CheckExact(other)) {
1245+
else if (PyAnyDict_CheckExact(other)) {
12461246
return set_update_dict_lock_held(so, other);
12471247
}
12481248
return set_update_iterable_lock_held(so, other);
@@ -1260,7 +1260,7 @@ set_update_local(PySetObject *so, PyObject *other)
12601260
Py_END_CRITICAL_SECTION();
12611261
return rv;
12621262
}
1263-
else if (PyDict_CheckExact(other)) {
1263+
else if (PyAnyDict_CheckExact(other)) {
12641264
int rv;
12651265
Py_BEGIN_CRITICAL_SECTION(other);
12661266
rv = set_update_dict_lock_held(so, other);
@@ -1283,7 +1283,7 @@ set_update_internal(PySetObject *so, PyObject *other)
12831283
Py_END_CRITICAL_SECTION2();
12841284
return rv;
12851285
}
1286-
else if (PyDict_CheckExact(other)) {
1286+
else if (PyAnyDict_CheckExact(other)) {
12871287
int rv;
12881288
Py_BEGIN_CRITICAL_SECTION2(so, other);
12891289
rv = set_update_dict_lock_held(so, other);
@@ -2030,7 +2030,7 @@ set_difference(PySetObject *so, PyObject *other)
20302030
if (PyAnySet_Check(other)) {
20312031
other_size = PySet_GET_SIZE(other);
20322032
}
2033-
else if (PyDict_CheckExact(other)) {
2033+
else if (PyAnyDict_CheckExact(other)) {
20342034
other_size = PyDict_GET_SIZE(other);
20352035
}
20362036
else {
@@ -2047,7 +2047,7 @@ set_difference(PySetObject *so, PyObject *other)
20472047
if (result == NULL)
20482048
return NULL;
20492049

2050-
if (PyDict_CheckExact(other)) {
2050+
if (PyAnyDict_CheckExact(other)) {
20512051
while (set_next(so, &pos, &entry)) {
20522052
key = entry->key;
20532053
hash = entry->hash;
@@ -2238,7 +2238,7 @@ set_symmetric_difference_update_impl(PySetObject *so, PyObject *other)
22382238
}
22392239

22402240
int rv;
2241-
if (PyDict_CheckExact(other)) {
2241+
if (PyAnyDict_CheckExact(other)) {
22422242
Py_BEGIN_CRITICAL_SECTION2(so, other);
22432243
rv = set_symmetric_difference_update_dict(so, other);
22442244
Py_END_CRITICAL_SECTION2();

0 commit comments

Comments
 (0)