Skip to content

Commit dd1ce79

Browse files
vstinnerscoderpitrou
committed
pythongh-111262: Add PyDict_Pop() function
Change the API of the internal _PyDict_Pop_KnownHash() function to remove the default value and return an int. Co-Authored-By: Stefan Behnel <stefan_ml@behnel.de> Co-authored-by: Antoine Pitrou <pitrou@free.fr>
1 parent 1c7ed7e commit dd1ce79

19 files changed

+191
-67
lines changed

Doc/c-api/dict.rst

+14
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ Dictionary Objects
173173
174174
.. versionadded:: 3.4
175175
176+
177+
.. c:function:: int PyDict_Pop(PyObject *p, PyObject *key, PyObject **result)
178+
179+
Remove *key* from dictionary *p* and return the removed value.
180+
181+
- If the key is present, set *\*result* to a new reference to the removed
182+
value, and return ``1``.
183+
- If the key is missing, set *\*result* to ``NULL``, and return ``0``.
184+
- On error, raise an exception and return ``-1``.
185+
186+
This is the similar to :meth:`dict.pop` without the default value.
187+
188+
.. versionadded:: 3.13
189+
176190
.. c:function:: PyObject* PyDict_Items(PyObject *p)
177191
178192
Return a :c:type:`PyListObject` containing all the items from the dictionary.

Doc/data/stable_abi.dat

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/whatsnew/3.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,11 @@ New Features
11641164
:c:func:`PyErr_WriteUnraisable`, but allow to customize the warning mesage.
11651165
(Contributed by Serhiy Storchaka in :gh:`108082`.)
11661166

1167+
* Add :c:func:`PyDict_Pop` function: remove a key from a dictionary and return
1168+
the removed value. This is the similar to :meth:`dict.pop` without the
1169+
default value.
1170+
(Contributed by Victor Stinner in :gh:`111262`.)
1171+
11671172

11681173
Porting to Python 3.13
11691174
----------------------

Include/dictobject.h

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
6666
// - On error, raise an exception and return -1.
6767
PyAPI_FUNC(int) PyDict_GetItemRef(PyObject *mp, PyObject *key, PyObject **result);
6868
PyAPI_FUNC(int) PyDict_GetItemStringRef(PyObject *mp, const char *key, PyObject **result);
69+
70+
PyAPI_FUNC(int) PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result);
6971
#endif
7072

7173
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000

Include/internal/pycore_dict.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ extern PyObject *_PyDict_LoadGlobal(PyDictObject *, PyDictObject *, PyObject *);
116116
extern int _PyDict_SetItem_Take2(PyDictObject *op, PyObject *key, PyObject *value);
117117
extern int _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, PyObject *name, PyObject *value);
118118

119-
extern PyObject *_PyDict_Pop_KnownHash(PyObject *, PyObject *, Py_hash_t, PyObject *);
119+
extern int _PyDict_Pop_KnownHash(
120+
PyDictObject *dict,
121+
PyObject *key,
122+
Py_hash_t hash,
123+
PyObject **result);
120124

121125
#define DKIX_EMPTY (-1)
122126
#define DKIX_DUMMY (-2) /* Used internally */

Lib/test/test_capi/test_dict.py

+39
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,45 @@ def test_dict_mergefromseq2(self):
432432
# CRASHES mergefromseq2({}, NULL, 0)
433433
# CRASHES mergefromseq2(NULL, {}, 0)
434434

435+
def test_dict_pop(self):
436+
# Test PyDict_Pop()
437+
dict_pop = _testcapi.dict_pop
438+
default = object()
439+
440+
def expect_value(mydict, key, expected_value):
441+
self.assertEqual(dict_pop(mydict.copy(), key),
442+
(1, expected_value))
443+
444+
def expect_missing(mydict, key):
445+
self.assertEqual(dict_pop(mydict, key, default), (0, None))
446+
447+
# key present
448+
mydict = {"key": 2, "key2": 5}
449+
expect_value(mydict, "key", 2)
450+
expect_value(mydict, "key2", 5)
451+
452+
# key missing
453+
expect_missing({}, "key") # empty dict has a fast path
454+
expect_missing({"a": 1}, "key")
455+
self.assertRaises(KeyError, dict_pop, {}, "key", NULL)
456+
self.assertRaises(KeyError, dict_pop, {"a": 1}, "key", NULL)
457+
458+
# dict error
459+
not_dict = "string"
460+
self.assertRaises(SystemError, dict_pop, not_dict, "key", default)
461+
462+
# key error
463+
not_hashable_key = ["list"]
464+
expect_missing({}, not_hashable_key) # don't hash key if dict is empty
465+
with self.assertRaises(TypeError):
466+
dict_pop({'key': 1}, not_hashable_key, NULL)
467+
with self.assertRaises(TypeError):
468+
dict_pop({'key': 1}, not_hashable_key, default)
469+
dict_pop({}, NULL, default) # don't check key if dict is empty
470+
471+
# CRASHES dict_pop(NULL, "key")
472+
# CRASHES dict_pop({"a": 1}, NULL, default)
473+
435474

436475
if __name__ == "__main__":
437476
unittest.main()

Lib/test/test_stable_abi_ctypes.py

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :c:func:`PyDict_Pop` function: remove a key from a dictionary and return
2+
the removed value. This is the same as :meth:`dict.pop`. Patch by Stefan
3+
Behnel and Victor Stinner.

Misc/stable_abi.toml

+2
Original file line numberDiff line numberDiff line change
@@ -2481,3 +2481,5 @@
24812481
[function._Py_SetRefcnt]
24822482
added = '3.13'
24832483
abi_only = true
2484+
[function.PyDict_Pop]
2485+
added = '3.13'

Modules/_functoolsmodule.c

+15-13
Original file line numberDiff line numberDiff line change
@@ -1087,19 +1087,9 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
10871087
The cache dict holds one reference to the link.
10881088
We created one other reference when the link was created.
10891089
The linked list only has borrowed references. */
1090-
popresult = _PyDict_Pop_KnownHash(self->cache, link->key,
1091-
link->hash, Py_None);
1092-
if (popresult == Py_None) {
1093-
/* Getting here means that the user function call or another
1094-
thread has already removed the old key from the dictionary.
1095-
This link is now an orphan. Since we don't want to leave the
1096-
cache in an inconsistent state, we don't restore the link. */
1097-
Py_DECREF(popresult);
1098-
Py_DECREF(link);
1099-
Py_DECREF(key);
1100-
return result;
1101-
}
1102-
if (popresult == NULL) {
1090+
int res = _PyDict_Pop_KnownHash((PyDictObject*)self->cache, link->key,
1091+
link->hash, &popresult);
1092+
if (res < 0) {
11031093
/* An error arose while trying to remove the oldest key (the one
11041094
being evicted) from the cache. We restore the link to its
11051095
original position as the oldest link. Then we allow the
@@ -1110,6 +1100,18 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
11101100
Py_DECREF(result);
11111101
return NULL;
11121102
}
1103+
if (res == 0) {
1104+
/* Getting here means that the user function call or another
1105+
thread has already removed the old key from the dictionary.
1106+
This link is now an orphan. Since we don't want to leave the
1107+
cache in an inconsistent state, we don't restore the link. */
1108+
assert(popresult == NULL);
1109+
Py_DECREF(link);
1110+
Py_DECREF(key);
1111+
return result;
1112+
}
1113+
assert(popresult != NULL);
1114+
11131115
/* Keep a reference to the old key and old result to prevent their
11141116
ref counts from going to zero during the update. That will
11151117
prevent potentially arbitrary object clean-up code (i.e. __del__)

Modules/_testcapi/dict.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,24 @@ dict_mergefromseq2(PyObject *self, PyObject *args)
331331
}
332332

333333

334+
static PyObject *
335+
dict_pop(PyObject *self, PyObject *args)
336+
{
337+
PyObject *dict, *key;
338+
if (!PyArg_ParseTuple(args, "OO", &dict, &key)) {
339+
return NULL;
340+
}
341+
NULLABLE(dict);
342+
NULLABLE(key);
343+
PyObject *result = UNINITIALIZED_PTR;
344+
int res = PyDict_Pop(dict, key, &result);
345+
if (result == NULL) {
346+
return Py_NewRef(Py_None);
347+
}
348+
return Py_BuildValue("iN", res, result);
349+
}
350+
351+
334352
static PyMethodDef test_methods[] = {
335353
{"dict_check", dict_check, METH_O},
336354
{"dict_checkexact", dict_checkexact, METH_O},
@@ -358,7 +376,7 @@ static PyMethodDef test_methods[] = {
358376
{"dict_merge", dict_merge, METH_VARARGS},
359377
{"dict_update", dict_update, METH_VARARGS},
360378
{"dict_mergefromseq2", dict_mergefromseq2, METH_VARARGS},
361-
379+
{"dict_pop", dict_pop, METH_VARARGS},
362380
{NULL},
363381
};
364382

Modules/_threadmodule.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -967,12 +967,13 @@ local_clear(localobject *self)
967967
HEAD_UNLOCK(runtime);
968968
while (tstate) {
969969
if (tstate->dict) {
970-
PyObject *v = _PyDict_Pop(tstate->dict, self->key, Py_None);
971-
if (v != NULL) {
972-
Py_DECREF(v);
970+
PyObject *v;
971+
if (PyDict_Pop(tstate->dict, self->key, &v) < 0) {
972+
// Silently ignore error
973+
PyErr_Clear();
973974
}
974975
else {
975-
PyErr_Clear();
976+
Py_XDECREF(v);
976977
}
977978
}
978979
HEAD_LOCK(runtime);

Modules/socketmodule.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,13 @@ remove_unusable_flags(PyObject *m)
396396
if (flag_name == NULL) {
397397
return -1;
398398
}
399-
PyObject *v = _PyDict_Pop(dict, flag_name, Py_None);
400-
Py_DECREF(flag_name);
401-
if (v == NULL) {
399+
PyObject *v;
400+
if (PyDict_Pop(dict, flag_name, &v) < 0) {
401+
Py_DECREF(flag_name);
402402
return -1;
403403
}
404-
Py_DECREF(v);
404+
Py_DECREF(flag_name);
405+
Py_XDECREF(v);
405406
}
406407
}
407408
return 0;

Objects/dictobject.c

+55-34
Original file line numberDiff line numberDiff line change
@@ -2226,64 +2226,85 @@ PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
22262226
return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
22272227
}
22282228

2229+
22292230
/* Internal version of dict.pop(). */
2230-
PyObject *
2231-
_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
2231+
int
2232+
_PyDict_Pop_KnownHash(PyDictObject *mp, PyObject *key, Py_hash_t hash,
2233+
PyObject **result)
22322234
{
2233-
Py_ssize_t ix;
2234-
PyObject *old_value;
2235-
PyDictObject *mp;
2236-
PyInterpreterState *interp = _PyInterpreterState_GET();
2237-
2238-
assert(PyDict_Check(dict));
2239-
mp = (PyDictObject *)dict;
2235+
assert(PyDict_Check(mp));
22402236

22412237
if (mp->ma_used == 0) {
2242-
if (deflt) {
2243-
return Py_NewRef(deflt);
2244-
}
2245-
_PyErr_SetKeyError(key);
2246-
return NULL;
2238+
*result = NULL;
2239+
return 0;
22472240
}
2248-
ix = _Py_dict_lookup(mp, key, hash, &old_value);
2249-
if (ix == DKIX_ERROR)
2250-
return NULL;
2241+
2242+
PyObject *old_value;
2243+
Py_ssize_t ix = _Py_dict_lookup(mp, key, hash, &old_value);
2244+
if (ix == DKIX_ERROR) {
2245+
*result = NULL;
2246+
return -1;
2247+
}
2248+
22512249
if (ix == DKIX_EMPTY || old_value == NULL) {
2252-
if (deflt) {
2253-
return Py_NewRef(deflt);
2254-
}
2255-
_PyErr_SetKeyError(key);
2256-
return NULL;
2250+
*result = NULL;
2251+
return 0;
22572252
}
2253+
22582254
assert(old_value != NULL);
2255+
PyInterpreterState *interp = _PyInterpreterState_GET();
22592256
uint64_t new_version = _PyDict_NotifyEvent(
22602257
interp, PyDict_EVENT_DELETED, mp, key, NULL);
22612258
delitem_common(mp, hash, ix, Py_NewRef(old_value), new_version);
22622259

22632260
ASSERT_CONSISTENT(mp);
2264-
return old_value;
2261+
*result = old_value;
2262+
return 1;
22652263
}
22662264

2267-
PyObject *
2268-
_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
2265+
2266+
int
2267+
PyDict_Pop(PyObject *op, PyObject *key, PyObject **result)
22692268
{
2269+
if (!PyDict_Check(op)) {
2270+
*result = NULL;
2271+
PyErr_BadInternalCall();
2272+
return -1;
2273+
}
2274+
PyDictObject *dict = (PyDictObject *)op;
2275+
2276+
if (dict->ma_used == 0) {
2277+
*result = NULL;
2278+
return 0;
2279+
}
2280+
22702281
Py_hash_t hash;
2282+
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
2283+
hash = PyObject_Hash(key);
2284+
if (hash == -1) {
2285+
*result = NULL;
2286+
return -1;
2287+
}
2288+
}
2289+
return _PyDict_Pop_KnownHash(dict, key, hash, result);
2290+
}
2291+
22712292

2272-
if (((PyDictObject *)dict)->ma_used == 0) {
2273-
if (deflt) {
2274-
return Py_NewRef(deflt);
2293+
PyObject *
2294+
_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value)
2295+
{
2296+
PyObject *result;
2297+
if (PyDict_Pop(dict, key, &result) == 0) {
2298+
if (default_value != NULL) {
2299+
return Py_NewRef(default_value);
22752300
}
22762301
_PyErr_SetKeyError(key);
22772302
return NULL;
22782303
}
2279-
if (!PyUnicode_CheckExact(key) || (hash = unicode_get_hash(key)) == -1) {
2280-
hash = PyObject_Hash(key);
2281-
if (hash == -1)
2282-
return NULL;
2283-
}
2284-
return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
2304+
return result;
22852305
}
22862306

2307+
22872308
/* Internal version of dict.from_keys(). It is subclass-friendly. */
22882309
PyObject *
22892310
_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)

Objects/odictobject.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,10 @@ _odict_popkey_hash(PyObject *od, PyObject *key, PyObject *failobj,
10491049
return NULL;
10501050
}
10511051
/* Now delete the value from the dict. */
1052-
value = _PyDict_Pop_KnownHash(od, key, hash, failobj);
1052+
if (_PyDict_Pop_KnownHash((PyDictObject *)od, key, hash,
1053+
&value) == 0) {
1054+
value = Py_NewRef(failobj);
1055+
}
10531056
}
10541057
else if (value == NULL && !PyErr_Occurred()) {
10551058
/* Apply the fallback value, if necessary. */

0 commit comments

Comments
 (0)