Skip to content

Commit a5568d0

Browse files
gh-141510 Add frozendict fast paths to abstract.c (#150692)
Add frozendict to the fast paths of PyMapping_GetOptionalItem(), PyMapping_Keys(), PyMapping_Values(), and PyMapping_Items(). Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 551f8e1 commit a5568d0

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

Lib/test/test_capi/test_abstract.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ def test_mapping_getoptionalitem(self):
411411
self.assertEqual(getitem(dct2, 'a'), 1)
412412
self.assertEqual(getitem(dct2, 'b'), KeyError)
413413

414+
frozendct = frozendict(dct)
415+
self.assertEqual(getitem(frozendct, 'a'), 1)
416+
self.assertEqual(getitem(frozendct, 'b'), KeyError)
417+
self.assertEqual(getitem(frozendct, '\U0001f40d'), 2)
418+
414419
self.assertEqual(getitem(['a', 'b', 'c'], 1), 'b')
415420

416421
self.assertRaises(TypeError, getitem, 42, 'a')
@@ -431,6 +436,11 @@ def test_mapping_getoptionalitemstring(self):
431436
self.assertEqual(getitemstring(dct2, b'a'), 1)
432437
self.assertEqual(getitemstring(dct2, b'b'), KeyError)
433438

439+
frozendct = frozendict(dct)
440+
self.assertEqual(getitemstring(frozendct, 'a'), 1)
441+
self.assertEqual(getitemstring(frozendct, 'b'), KeyError)
442+
self.assertEqual(getitemstring(frozendct, '\U0001f40d'.encode()), 2)
443+
434444
self.assertRaises(TypeError, getitemstring, 42, b'a')
435445
self.assertRaises(UnicodeDecodeError, getitemstring, {}, b'\xff')
436446
self.assertRaises(SystemError, getitemstring, {}, NULL)
@@ -677,8 +687,10 @@ def items(self):
677687
dict_obj = {'foo': 1, 'bar': 2, 'spam': 3}
678688

679689
for mapping in [{}, OrderedDict(), Mapping1(), Mapping2(),
690+
frozendict(),
680691
dict_obj, OrderedDict(dict_obj),
681-
Mapping1(dict_obj), Mapping2(dict_obj)]:
692+
Mapping1(dict_obj), Mapping2(dict_obj),
693+
frozendict(dict_obj)]:
682694
self.assertListEqual(_testlimitedcapi.mapping_keys(mapping),
683695
list(mapping.keys()))
684696
self.assertListEqual(_testlimitedcapi.mapping_values(mapping),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :class:`frozendict` to the fast paths of :c:func:`PyMapping_GetOptionalItem`, :c:func:`PyMapping_Keys`, :c:func:`PyMapping_Values`, and :c:func:`PyMapping_Items`.

Objects/abstract.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ PyObject_GetItem(PyObject *o, PyObject *key)
208208
int
209209
PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
210210
{
211-
if (PyDict_CheckExact(obj)) {
211+
if (PyAnyDict_CheckExact(obj)) {
212212
return PyDict_GetItemRef(obj, key, result);
213213
}
214214

@@ -2462,7 +2462,7 @@ PyMapping_Keys(PyObject *o)
24622462
if (o == NULL) {
24632463
return null_error();
24642464
}
2465-
if (PyDict_CheckExact(o)) {
2465+
if (PyAnyDict_CheckExact(o)) {
24662466
return PyDict_Keys(o);
24672467
}
24682468
return method_output_as_list(o, &_Py_ID(keys));
@@ -2474,7 +2474,7 @@ PyMapping_Items(PyObject *o)
24742474
if (o == NULL) {
24752475
return null_error();
24762476
}
2477-
if (PyDict_CheckExact(o)) {
2477+
if (PyAnyDict_CheckExact(o)) {
24782478
return PyDict_Items(o);
24792479
}
24802480
return method_output_as_list(o, &_Py_ID(items));
@@ -2486,7 +2486,7 @@ PyMapping_Values(PyObject *o)
24862486
if (o == NULL) {
24872487
return null_error();
24882488
}
2489-
if (PyDict_CheckExact(o)) {
2489+
if (PyAnyDict_CheckExact(o)) {
24902490
return PyDict_Values(o);
24912491
}
24922492
return method_output_as_list(o, &_Py_ID(values));

0 commit comments

Comments
 (0)