Skip to content

gh-136776: Adding deepget method to dictionary #136805

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

Closed
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
1 change: 0 additions & 1 deletion Include/dictobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ PyAPI_DATA(PyTypeObject) PyDict_Type;
#define PyDict_Check(op) \
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS)
#define PyDict_CheckExact(op) Py_IS_TYPE((op), &PyDict_Type)

PyAPI_FUNC(PyObject *) PyDict_New(void);
PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *mp, PyObject *key);
PyAPI_FUNC(PyObject *) PyDict_GetItemWithError(PyObject *mp, PyObject *key);
Expand Down
40 changes: 40 additions & 0 deletions Objects/clinic/dictobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4312,6 +4312,40 @@ dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
return val;
}

static PyObject *
dict_deepget_impl(PyDictObject *self, PyObject * const *keylist, PyObject *default_value)
{
Py_ssize_t n = PyList_GET_SIZE(keylist);
if (n == 0) {
return default_value;
}

PyObject *val = (PyObject *)self; // Start with top-level dict
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *key = PyList_GET_ITEM(keylist, i);

if (!PyDict_Check(val)) {
return default_value;
}

Py_hash_t hash = _PyObject_HashFast(key);
if (hash == -1) {
dict_unhashable_type(key);
return default_value;
}

PyObject *next = NULL;
Py_ssize_t ix = _Py_dict_lookup_threadsafe((PyDictObject *)val, key, hash, &next);
if (ix == DKIX_ERROR || ix == DKIX_EMPTY || next == NULL) {
return Py_NewRef(default_value);
}

val = next;
}

return Py_NewRef(val);
}

static int
dict_setdefault_ref_lock_held(PyObject *d, PyObject *key, PyObject *default_value,
PyObject **result, int incref_result)
Expand Down Expand Up @@ -4741,6 +4775,7 @@ static PyMethodDef mapp_methods[] = {
getitem__doc__},
DICT___SIZEOF___METHODDEF
DICT_GET_METHODDEF
DICT_DEEPGET_METHODDEF
DICT_SETDEFAULT_METHODDEF
DICT_POP_METHODDEF
DICT_POPITEM_METHODDEF
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy