summaryrefslogtreecommitdiffstats
path: root/Objects/dictobject.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2022-02-08 20:39:07 (GMT)
committerGitHub <noreply@github.com>2022-02-08 20:39:07 (GMT)
commit81c72044a181dbbfbf689d7a977d0d99090f26a8 (patch)
tree14329746bd6f179cf2ae7c9818e1ae881eb46360 /Objects/dictobject.c
parentc018d3037b5b62e6d48d5985d1a37b91762fbffb (diff)
downloadcpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.zip
cpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.tar.gz
cpython-81c72044a181dbbfbf689d7a977d0d99090f26a8.tar.bz2
bpo-46541: Replace core use of _Py_IDENTIFIER() with statically initialized global objects. (gh-30928)
We're no longer using _Py_IDENTIFIER() (or _Py_static_string()) in any core CPython code. It is still used in a number of non-builtin stdlib modules. The replacement is: PyUnicodeObject (not pointer) fields under _PyRuntimeState, statically initialized as part of _PyRuntime. A new _Py_GET_GLOBAL_IDENTIFIER() macro facilitates lookup of the fields (along with _Py_GET_GLOBAL_STRING() for non-identifier strings). https://bugs.python.org/issue46541#msg411799 explains the rationale for this change. The core of the change is in: * (new) Include/internal/pycore_global_strings.h - the declarations for the global strings, along with the macros * Include/internal/pycore_runtime_init.h - added the static initializers for the global strings * Include/internal/pycore_global_objects.h - where the struct in pycore_global_strings.h is hooked into _PyRuntimeState * Tools/scripts/generate_global_objects.py - added generation of the global string declarations and static initializers I've also added a --check flag to generate_global_objects.py (along with make check-global-objects) to check for unused global strings. That check is added to the PR CI config. The remainder of this change updates the core code to use _Py_GET_GLOBAL_IDENTIFIER() instead of _Py_IDENTIFIER() and the related _Py*Id functions (likewise for _Py_GET_GLOBAL_STRING() instead of _Py_static_string()). This includes adding a few functions where there wasn't already an alternative to _Py*Id(), replacing the _Py_Identifier * parameter with PyObject *. The following are not changed (yet): * stop using _Py_IDENTIFIER() in the stdlib modules * (maybe) get rid of _Py_IDENTIFIER(), etc. entirely -- this may not be doable as at least one package on PyPI using this (private) API * (maybe) intern the strings during runtime init https://bugs.python.org/issue46541
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r--Objects/dictobject.c40
1 files changed, 23 insertions, 17 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index c41bdb8..18de42c 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -1485,6 +1485,17 @@ PyDict_GetItemWithError(PyObject *op, PyObject *key)
}
PyObject *
+_PyDict_GetItemWithError(PyObject *dp, PyObject *kv)
+{
+ assert(PyUnicode_CheckExact(kv));
+ Py_hash_t hash = kv->ob_type->tp_hash(kv);
+ if (hash == -1) {
+ return NULL;
+ }
+ return _PyDict_GetItem_KnownHash(dp, kv, hash);
+}
+
+PyObject *
_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
{
PyObject *kv;
@@ -2166,8 +2177,8 @@ dict_subscript(PyDictObject *mp, PyObject *key)
if (!PyDict_CheckExact(mp)) {
/* Look up __missing__ method if we're a subclass. */
PyObject *missing, *res;
- _Py_IDENTIFIER(__missing__);
- missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
+ missing = _PyObject_LookupSpecial(
+ (PyObject *)mp, &_Py_ID(__missing__));
if (missing != NULL) {
res = PyObject_CallOneArg(missing, key);
Py_DECREF(missing);
@@ -2369,9 +2380,8 @@ dict_update_arg(PyObject *self, PyObject *arg)
if (PyDict_CheckExact(arg)) {
return PyDict_Merge(self, arg, 1);
}
- _Py_IDENTIFIER(keys);
PyObject *func;
- if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
+ if (_PyObject_LookupAttr(arg, &_Py_ID(keys), &func) < 0) {
return -1;
}
if (func != NULL) {
@@ -4128,7 +4138,6 @@ dict___reversed___impl(PyDictObject *self)
static PyObject *
dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
{
- _Py_IDENTIFIER(iter);
/* copy the iterator state */
dictiterobject tmp = *di;
Py_XINCREF(tmp.di_dict);
@@ -4138,7 +4147,7 @@ dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
if (list == NULL) {
return NULL;
}
- return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
+ return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list);
}
PyTypeObject PyDictRevIterItem_Type = {
@@ -4408,9 +4417,8 @@ dictviews_sub(PyObject *self, PyObject *other)
return NULL;
}
- _Py_IDENTIFIER(difference_update);
- PyObject *tmp = _PyObject_CallMethodIdOneArg(
- result, &PyId_difference_update, other);
+ PyObject *tmp = PyObject_CallMethodOneArg(
+ result, &_Py_ID(difference_update), other);
if (tmp == NULL) {
Py_DECREF(result);
return NULL;
@@ -4446,8 +4454,8 @@ _PyDictView_Intersect(PyObject* self, PyObject *other)
/* if other is a set and self is smaller than other,
reuse set intersection logic */
if (PySet_CheckExact(other) && len_self <= PyObject_Size(other)) {
- _Py_IDENTIFIER(intersection);
- return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
+ return PyObject_CallMethodObjArgs(
+ other, &_Py_ID(intersection), self, NULL);
}
/* if other is another dict view, and it is bigger than self,
@@ -4587,9 +4595,8 @@ dictitems_xor(PyObject *self, PyObject *other)
}
key = val1 = val2 = NULL;
- _Py_IDENTIFIER(items);
- PyObject *remaining_pairs = _PyObject_CallMethodIdNoArgs(temp_dict,
- &PyId_items);
+ PyObject *remaining_pairs = PyObject_CallMethodNoArgs(
+ temp_dict, &_Py_ID(items));
if (remaining_pairs == NULL) {
goto error;
}
@@ -4621,9 +4628,8 @@ dictviews_xor(PyObject* self, PyObject *other)
return NULL;
}
- _Py_IDENTIFIER(symmetric_difference_update);
- PyObject *tmp = _PyObject_CallMethodIdOneArg(
- result, &PyId_symmetric_difference_update, other);
+ PyObject *tmp = PyObject_CallMethodOneArg(
+ result, &_Py_ID(symmetric_difference_update), other);
if (tmp == NULL) {
Py_DECREF(result);
return NULL;