Skip to content

Commit 9c8539b

Browse files
committed
gh-106320: Make some PyDict C-API functions public that should have been public right away.
See #108449
1 parent 9bb202a commit 9c8539b

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Diff for: Doc/c-api/dict.rst

+10
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ Dictionary Objects
173173
174174
.. versionadded:: 3.4
175175
176+
177+
.. c:function:: PyObject* PyDict_Pop(PyObject *p, PyObject *key, PyObject *defaultobj)
178+
179+
This is the same as the Python-level :meth:`dict.pop`. It removes the *key*
180+
from the dictionary *p* and returns its value. If the key is not in the dict,
181+
the value *defaultobj* is returned instead if it is not ``NULL``, or otherwise a
182+
:exc:`KeyError` is raised and ``NULL`` is returned.
183+
184+
.. versionadded:: 3.13
185+
176186
.. c:function:: PyObject* PyDict_Items(PyObject *p)
177187
178188
Return a :c:type:`PyListObject` containing all the items from the dictionary.

Diff for: Include/cpython/dictobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
4545
#define PyDict_GET_SIZE(op) PyDict_GET_SIZE(_PyObject_CAST(op))
4646

4747
PyAPI_FUNC(int) PyDict_ContainsString(PyObject *mp, const char *key);
48-
48+
PyAPI_FUNC(PyObject *) PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value);
4949

5050
/* Dictionary watchers */
5151

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a new C-API function PyDict_Pop to replace a previously removed underscore function.

Diff for: Objects/dictobject.c

+10
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,16 @@ _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
22722272
return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
22732273
}
22742274

2275+
PyObject *
2276+
PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
2277+
{
2278+
if (!PyDict_Check(dict)) {
2279+
PyErr_BadInternalCall();
2280+
return -1;
2281+
}
2282+
return _PyDict_Pop(dict, key, deflt);
2283+
}
2284+
22752285
/* Internal version of dict.from_keys(). It is subclass-friendly. */
22762286
PyObject *
22772287
_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)

0 commit comments

Comments
 (0)