8000 gh-106320: Make some PyDict C-API functions public that should have b… · python/cpython@5674aa3 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5674aa3

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 5674aa3

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Doc/c-api/dict.rst

Lines changed: 10 additions & 0 deletions
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 a
182+
:exc:`KeyError` is raised if it is ``NULL``.
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.

Include/cpython/dictobject.h

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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.

Objects/dictobject.c

Lines changed: 10 additions & 0 deletions
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)
0