Skip to content

Commit

Permalink
pythongh-106320: Make some PyDict C-API functions public that should …
Browse files Browse the repository at this point in the history
…have been public right away.

See python#108449
  • Loading branch information
scoder committed Oct 24, 2023
1 parent 9bb202a commit 16a95d4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Doc/c-api/dict.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ Dictionary Objects
.. versionadded:: 3.4
.. c:function:: PyObject* PyDict_Pop(PyObject *p, PyObject *key, PyObject *defaultobj)
This is the same as the Python-level :meth:`dict.pop`. It removes the *key*
from the dictionary *p* and returns its value. If the key is not in the dict,
the value *defaultobj* is returned instead if it is not ``NULL``, or otherwise a
:exc:`KeyError` is raised and ``NULL`` is returned.
.. versionadded:: 3.13
.. c:function:: PyObject* PyDict_Items(PyObject *p)
Return a :c:type:`PyListObject` containing all the items from the dictionary.
Expand Down
2 changes: 1 addition & 1 deletion Include/cpython/dictobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
#define PyDict_GET_SIZE(op) PyDict_GET_SIZE(_PyObject_CAST(op))

PyAPI_FUNC(int) PyDict_ContainsString(PyObject *mp, const char *key);

PyAPI_FUNC(PyObject *) PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value);

/* Dictionary watchers */

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a new C-API function PyDict_Pop to replace a previously removed underscore function.
10 changes: 10 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,16 @@ _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
}

PyObject *
PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
{
if (!PyDict_Check(dict)) {
PyErr_BadInternalCall();
return -1;
}
return _PyDict_Pop(dict, key, deflt);
}

/* Internal version of dict.from_keys(). It is subclass-friendly. */
PyObject *
_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
Expand Down

0 comments on commit 16a95d4

Please sign in to comment.