mirror of
https://github.com/python/cpython.git
synced 2025-09-05 00:11:10 +00:00
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().
This commit is contained in:
parent
c8979f780e
commit
dd3adc013b
1 changed files with 19 additions and 21 deletions
|
@ -1124,25 +1124,24 @@ _PyObject_NextNotImplemented(PyObject *self)
|
||||||
int
|
int
|
||||||
_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
|
_PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
|
||||||
{
|
{
|
||||||
PyTypeObject *tp = Py_TYPE(obj);
|
|
||||||
PyObject *descr;
|
|
||||||
descrgetfunc f = NULL;
|
|
||||||
PyObject **dictptr, *dict;
|
|
||||||
PyObject *attr;
|
|
||||||
int meth_found = 0;
|
int meth_found = 0;
|
||||||
|
|
||||||
assert(*method == NULL);
|
assert(*method == NULL);
|
||||||
|
|
||||||
if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
|
PyTypeObject *tp = Py_TYPE(obj);
|
||||||
|| !PyUnicode_Check(name)) {
|
if (!_PyType_IsReady(tp)) {
|
||||||
|
if (PyType_Ready(tp) < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_Check(name)) {
|
||||||
*method = PyObject_GetAttr(obj, name);
|
*method = PyObject_GetAttr(obj, name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
|
PyObject *descr = _PyType_Lookup(tp, name);
|
||||||
return 0;
|
descrgetfunc f = NULL;
|
||||||
|
|
||||||
descr = _PyType_Lookup(tp, name);
|
|
||||||
if (descr != NULL) {
|
if (descr != NULL) {
|
||||||
Py_INCREF(descr);
|
Py_INCREF(descr);
|
||||||
if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
|
if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
|
||||||
|
@ -1157,25 +1156,24 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dictptr = _PyObject_GetDictPtr(obj);
|
PyObject **dictptr = _PyObject_GetDictPtr(obj);
|
||||||
|
PyObject *dict;
|
||||||
if (dictptr != NULL && (dict = *dictptr) != NULL) {
|
if (dictptr != NULL && (dict = *dictptr) != NULL) {
|
||||||
Py_INCREF(dict);
|
Py_INCREF(dict);
|
||||||
attr = PyDict_GetItemWithError(dict, name);
|
PyObject *attr = PyDict_GetItemWithError(dict, name);
|
||||||
if (attr != NULL) {
|
if (attr != NULL) {
|
||||||
Py_INCREF(attr);
|
*method = Py_NewRef(attr);
|
||||||
*method = attr;
|
|
||||||
Py_DECREF(dict);
|
Py_DECREF(dict);
|
||||||
Py_XDECREF(descr);
|
Py_XDECREF(descr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
Py_DECREF(dict);
|
Py_DECREF(dict);
|
||||||
|
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_XDECREF(descr);
|
Py_XDECREF(descr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (meth_found) {
|
if (meth_found) {
|
||||||
*method = descr;
|
*method = descr;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue