Skip to content

Commit dd3adc0

Browse files
authored
bpo-43770: Cleanup _PyObject_GetMethod() (GH-26946)
_PyObject_GetMethod() now uses _PyType_IsReady() to decide if PyType_Ready() must be called or not, rather than testing if tp->tp_dict is NULL. Move also variable declarations closer to where they are used, and use Py_NewRef().
1 parent c8979f7 commit dd3adc0

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

Objects/object.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,25 +1124,24 @@ _PyObject_NextNotImplemented(PyObject *self)
11241124
int
11251125
_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
11261126
{
1127-
PyTypeObject *tp = Py_TYPE(obj);
1128-
PyObject *descr;
1129-
descrgetfunc f = NULL;
1130-
PyObject **dictptr, *dict;
1131-
PyObject *attr;
11321127
int meth_found = 0;
11331128

11341129
assert(*method == NULL);
11351130

1136-
if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1137-
|| !PyUnicode_Check(name)) {
1138-
*method = PyObject_GetAttr(obj, name);
1139-
return 0;
1131+
PyTypeObject *tp = Py_TYPE(obj);
1132+
if (!_PyType_IsReady(tp)) {
1133+
if (PyType_Ready(tp) < 0) {
1134+
return 0;
1135+
}
11401136
}
11411137

1142-
if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1138+
if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_Check(name)) {
1139+
*method = PyObject_GetAttr(obj, name);
11431140
return 0;
1141+
}
11441142

1145-
descr = _PyType_Lookup(tp, name);
1143+
PyObject *descr = _PyType_Lookup(tp, name);
1144+
descrgetfunc f = NULL;
11461145
if (descr != NULL) {
11471146
Py_INCREF(descr);
11481147
if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
@@ -1157,23 +1156,22 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
11571156
}
11581157
}
11591158

1160-
dictptr = _PyObject_GetDictPtr(obj);
1159+
PyObject **dictptr = _PyObject_GetDictPtr(obj);
1160+
PyObject *dict;
11611161
if (dictptr != NULL && (dict = *dictptr) != NULL) {
11621162
Py_INCREF(dict);
1163-
attr = PyDict_GetItemWithError(dict, name);
1163+
PyObject *attr = PyDict_GetItemWithError(dict, name);
11641164
if (attr != NULL) {
1165-
Py_INCREF(attr);
1166-
*method = attr;
1165+
*method = Py_NewRef(attr);
11671166
Py_DECREF(dict);
11681167
Py_XDECREF(descr);
11691168
return 0;
11701169
}
1171-
else {
1172-
Py_DECREF(dict);
1173-
if (PyErr_Occurred()) {
1174-
Py_XDECREF(descr);
1175-
return 0;
1176-
}
1170+
Py_DECREF(dict);
1171+
1172+
if (PyErr_Occurred()) {
1173+
Py_XDECREF(descr);
1174+
return 0;
11771175
}
11781176
}
11791177

0 commit comments

Comments
 (0)