Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-111262: Make PyDict_Pop() public #111263

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to return -1 on error, 0 if the key doesn't exist, 1 if the key exists.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would the caller know the popped value then?


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 @@
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;

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (x64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (x64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (x64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (x64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (arm64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (arm64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (arm64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Windows (arm64)

'return': 'PyObject *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]

Check warning on line 2280 in Objects/dictobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

returning ‘int’ from a function with return type ‘PyObject *’ {aka ‘struct _object *’} makes pointer from integer without a cast [-Wint-conversion]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should certainly be return NULL?

}
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
Loading