Skip to content

Commit

Permalink
Use _PyDict_LoadGlobal in _PyDict_LoadGlobalStackRef
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Sep 14, 2024
1 parent 4491343 commit 67bc9cc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
22 changes: 4 additions & 18 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2471,27 +2471,13 @@ _PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
void
_PyDict_LoadGlobalStackRef(PyDictObject *globals, PyDictObject *builtins, PyObject *key, _PyStackRef *res)
{
Py_ssize_t ix;
Py_hash_t hash;

hash = _PyObject_HashFast(key);
if (hash == -1) {
PyObject *obj = _PyDict_LoadGlobal(globals, builtins, key);
if (obj == NULL) {
*res = PyStackRef_NULL;
return;
}

/* namespace 1: globals */
ix = _Py_dict_lookup_threadsafe_stackref(globals, key, hash, res);
if (ix == DKIX_ERROR) {
*res = PyStackRef_NULL;
}
if (ix != DKIX_EMPTY && !PyStackRef_IsNull(*res)) {
return;
else {
*res = PyStackRef_FromPyObjectSteal(obj);
}

/* namespace 2: builtins */
ix = _Py_dict_lookup_threadsafe_stackref(builtins, key, hash, res);
assert(ix >= 0 || PyStackRef_IsNull(*res));
}

/* Consumes references to key and value */
Expand Down
13 changes: 3 additions & 10 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3150,22 +3150,15 @@ void
_PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name, _PyStackRef *writeto)
{
if (PyDict_CheckExact(globals) && PyDict_CheckExact(builtins)) {
PyObject *res = _PyDict_LoadGlobal((PyDictObject *)globals,
_PyDict_LoadGlobalStackRef((PyDictObject *)globals,
(PyDictObject *)builtins,
name);
if (res == NULL && !PyErr_Occurred()) {
name, writeto);
if (PyStackRef_IsNull(*writeto) && !PyErr_Occurred()) {
/* _PyDict_LoadGlobal() returns NULL without raising
* an exception if the key doesn't exist */
_PyEval_FormatExcCheckArg(PyThreadState_GET(), PyExc_NameError,
NAME_ERROR_MSG, name);
}

if (res == NULL) {
*writeto = PyStackRef_NULL;
}
else {
*writeto = PyStackRef_FromPyObjectSteal(res);
}
}
else {
/* Slow-path if globals or builtins is not a dict */
Expand Down

0 comments on commit 67bc9cc

Please sign in to comment.