FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-111262: Make PyDict_Pop() public by scoder · Pull Request #111263 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .rst  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 10 additions & 0 deletions Doc/c-api/dict.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Back | FazBrowse Home | New Git URL