mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
gh-111789: Use PyDict_GetItemRef() in Objects/ (GH-111827)
This commit is contained in:
parent
e31d65e0b7
commit
18203a6bc9
5 changed files with 76 additions and 120 deletions
|
|
@ -1092,14 +1092,9 @@ type_module(PyTypeObject *type, void *context)
|
|||
|
||||
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
|
||||
PyObject *dict = lookup_tp_dict(type);
|
||||
mod = PyDict_GetItemWithError(dict, &_Py_ID(__module__));
|
||||
if (mod == NULL) {
|
||||
if (!PyErr_Occurred()) {
|
||||
PyErr_Format(PyExc_AttributeError, "__module__");
|
||||
}
|
||||
return NULL;
|
||||
if (PyDict_GetItemRef(dict, &_Py_ID(__module__), &mod) == 0) {
|
||||
PyErr_Format(PyExc_AttributeError, "__module__");
|
||||
}
|
||||
Py_INCREF(mod);
|
||||
}
|
||||
else {
|
||||
const char *s = strrchr(type->tp_name, '.');
|
||||
|
|
@ -1134,17 +1129,16 @@ type_abstractmethods(PyTypeObject *type, void *context)
|
|||
PyObject *mod = NULL;
|
||||
/* type itself has an __abstractmethods__ descriptor (this). Don't return
|
||||
that. */
|
||||
if (type != &PyType_Type) {
|
||||
PyObject *dict = lookup_tp_dict(type);
|
||||
mod = PyDict_GetItemWithError(dict, &_Py_ID(__abstractmethods__));
|
||||
if (type == &PyType_Type) {
|
||||
PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__));
|
||||
}
|
||||
if (!mod) {
|
||||
if (!PyErr_Occurred()) {
|
||||
else {
|
||||
PyObject *dict = lookup_tp_dict(type);
|
||||
if (PyDict_GetItemRef(dict, &_Py_ID(__abstractmethods__), &mod) == 0) {
|
||||
PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
return Py_NewRef(mod);
|
||||
return mod;
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
@ -1435,19 +1429,15 @@ type_get_doc(PyTypeObject *type, void *context)
|
|||
return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc);
|
||||
}
|
||||
PyObject *dict = lookup_tp_dict(type);
|
||||
result = PyDict_GetItemWithError(dict, &_Py_ID(__doc__));
|
||||
if (result == NULL) {
|
||||
if (!PyErr_Occurred()) {
|
||||
result = Py_NewRef(Py_None);
|
||||
if (PyDict_GetItemRef(dict, &_Py_ID(__doc__), &result) == 0) {
|
||||
result = Py_NewRef(Py_None);
|
||||
}
|
||||
else if (result) {
|
||||
descrgetfunc descr_get = Py_TYPE(result)->tp_descr_get;
|
||||
if (descr_get) {
|
||||
Py_SETREF(result, descr_get(result, NULL, (PyObject *)type));
|
||||
}
|
||||
}
|
||||
else if (Py_TYPE(result)->tp_descr_get) {
|
||||
result = Py_TYPE(result)->tp_descr_get(result, NULL,
|
||||
(PyObject *)type);
|
||||
}
|
||||
else {
|
||||
Py_INCREF(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -1477,16 +1467,16 @@ type_get_annotations(PyTypeObject *type, void *context)
|
|||
|
||||
PyObject *annotations;
|
||||
PyObject *dict = lookup_tp_dict(type);
|
||||
annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
|
||||
if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (annotations) {
|
||||
if (Py_TYPE(annotations)->tp_descr_get) {
|
||||
annotations = Py_TYPE(annotations)->tp_descr_get(
|
||||
annotations, NULL, (PyObject *)type);
|
||||
} else {
|
||||
Py_INCREF(annotations);
|
||||
descrgetfunc get = Py_TYPE(annotations)->tp_descr_get;
|
||||
if (get) {
|
||||
Py_SETREF(annotations, get(annotations, NULL, (PyObject *)type));
|
||||
}
|
||||
}
|
||||
else if (!PyErr_Occurred()) {
|
||||
else {
|
||||
annotations = PyDict_New();
|
||||
if (annotations) {
|
||||
int result = PyDict_SetItem(
|
||||
|
|
@ -1533,16 +1523,11 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
|
|||
static PyObject *
|
||||
type_get_type_params(PyTypeObject *type, void *context)
|
||||
{
|
||||
PyObject *params = PyDict_GetItemWithError(lookup_tp_dict(type), &_Py_ID(__type_params__));
|
||||
|
||||
if (params) {
|
||||
return Py_NewRef(params);
|
||||
PyObject *params;
|
||||
if (PyDict_GetItemRef(lookup_tp_dict(type), &_Py_ID(__type_params__), ¶ms) == 0) {
|
||||
return PyTuple_New(0);
|
||||
}
|
||||
if (PyErr_Occurred()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyTuple_New(0);
|
||||
return params;
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
@ -3436,18 +3421,13 @@ type_new_set_module(PyTypeObject *type)
|
|||
return 0;
|
||||
}
|
||||
|
||||
PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__));
|
||||
if (module == NULL) {
|
||||
if (PyErr_Occurred()) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
PyObject *module;
|
||||
r = PyDict_GetItemRef(globals, &_Py_ID(__name__), &module);
|
||||
if (module) {
|
||||
r = PyDict_SetItem(dict, &_Py_ID(__module__), module);
|
||||
Py_DECREF(module);
|
||||
}
|
||||
|
||||
if (PyDict_SetItem(dict, &_Py_ID(__module__), module) < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -3458,23 +3438,24 @@ type_new_set_ht_name(PyTypeObject *type)
|
|||
{
|
||||
PyHeapTypeObject *et = (PyHeapTypeObject *)type;
|
||||
PyObject *dict = lookup_tp_dict(type);
|
||||
PyObject *qualname = PyDict_GetItemWithError(dict, &_Py_ID(__qualname__));
|
||||
PyObject *qualname;
|
||||
if (PyDict_GetItemRef(dict, &_Py_ID(__qualname__), &qualname) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (qualname != NULL) {
|
||||
if (!PyUnicode_Check(qualname)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"type __qualname__ must be a str, not %s",
|
||||
Py_TYPE(qualname)->tp_name);
|
||||
Py_DECREF(qualname);
|
||||
return -1;
|
||||
}
|
||||
et->ht_qualname = Py_NewRef(qualname);
|
||||
et->ht_qualname = qualname;
|
||||
if (PyDict_DelItem(dict, &_Py_ID(__qualname__)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (PyErr_Occurred()) {
|
||||
return -1;
|
||||
}
|
||||
et->ht_qualname = Py_NewRef(et->ht_name);
|
||||
}
|
||||
return 0;
|
||||
|
|
@ -5888,24 +5869,22 @@ _PyType_GetSlotNames(PyTypeObject *cls)
|
|||
|
||||
/* Get the slot names from the cache in the class if possible. */
|
||||
PyObject *dict = lookup_tp_dict(cls);
|
||||
slotnames = PyDict_GetItemWithError(dict, &_Py_ID(__slotnames__));
|
||||
if (PyDict_GetItemRef(dict, &_Py_ID(__slotnames__), &slotnames) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (slotnames != NULL) {
|
||||
if (slotnames != Py_None && !PyList_Check(slotnames)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"%.200s.__slotnames__ should be a list or None, "
|
||||
"not %.200s",
|
||||
cls->tp_name, Py_TYPE(slotnames)->tp_name);
|
||||
Py_DECREF(slotnames);
|
||||
return NULL;
|
||||
}
|
||||
return Py_NewRef(slotnames);
|
||||
}
|
||||
else {
|
||||
if (PyErr_Occurred()) {
|
||||
return NULL;
|
||||
}
|
||||
/* The class does not have the slot names cached yet. */
|
||||
return slotnames;
|
||||
}
|
||||
|
||||
/* The class does not have the slot names cached yet. */
|
||||
copyreg = import_copyreg();
|
||||
if (copyreg == NULL)
|
||||
return NULL;
|
||||
|
|
@ -10264,23 +10243,18 @@ _super_lookup_descr(PyTypeObject *su_type, PyTypeObject *su_obj_type, PyObject *
|
|||
return NULL;
|
||||
|
||||
/* keep a strong reference to mro because su_obj_type->tp_mro can be
|
||||
replaced during PyDict_GetItemWithError(dict, name) */
|
||||
replaced during PyDict_GetItemRef(dict, name, &res) */
|
||||
Py_INCREF(mro);
|
||||
do {
|
||||
PyObject *obj = PyTuple_GET_ITEM(mro, i);
|
||||
PyObject *dict = lookup_tp_dict(_PyType_CAST(obj));
|
||||
assert(dict != NULL && PyDict_Check(dict));
|
||||
|
||||
res = PyDict_GetItemWithError(dict, name);
|
||||
if (res != NULL) {
|
||||
Py_INCREF(res);
|
||||
if (PyDict_GetItemRef(dict, name, &res) != 0) {
|
||||
// found or error
|
||||
Py_DECREF(mro);
|
||||
return res;
|
||||
}
|
||||
else if (PyErr_Occurred()) {
|
||||
Py_DECREF(mro);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
i++;
|
||||
} while (i < n);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue