summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2021-07-01 01:11:59 (GMT)
committerGitHub <noreply@github.com>2021-07-01 01:11:59 (GMT)
commitdd3adc013b21ec1338bb5fea2e2c04a4fc650306 (patch)
treeac19cecda575162fbb0fbe4a7ea2c5bf079d57c8
parentc8979f780e4b7d6db5693cb26a2956cc785abb48 (diff)
downloadcpython-dd3adc013b21ec1338bb5fea2e2c04a4fc650306.zip
cpython-dd3adc013b21ec1338bb5fea2e2c04a4fc650306.tar.gz
cpython-dd3adc013b21ec1338bb5fea2e2c04a4fc650306.tar.bz2
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().
-rw-r--r--Objects/object.c40
1 files changed, 19 insertions, 21 deletions
diff --git a/Objects/object.c b/Objects/object.c
index c87a83f..8a854c7 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1124,25 +1124,24 @@ _PyObject_NextNotImplemented(PyObject *self)
int
_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;
assert(*method == NULL);
- if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
- || !PyUnicode_Check(name)) {
- *method = PyObject_GetAttr(obj, name);
- return 0;
+ PyTypeObject *tp = Py_TYPE(obj);
+ if (!_PyType_IsReady(tp)) {
+ if (PyType_Ready(tp) < 0) {
+ return 0;
+ }
}
- if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
+ if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_Check(name)) {
+ *method = PyObject_GetAttr(obj, name);
return 0;
+ }
- descr = _PyType_Lookup(tp, name);
+ PyObject *descr = _PyType_Lookup(tp, name);
+ descrgetfunc f = NULL;
if (descr != NULL) {
Py_INCREF(descr);
if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
@@ -1157,23 +1156,22 @@ _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) {
Py_INCREF(dict);
- attr = PyDict_GetItemWithError(dict, name);
+ PyObject *attr = PyDict_GetItemWithError(dict, name);
if (attr != NULL) {
- Py_INCREF(attr);
- *method = attr;
+ *method = Py_NewRef(attr);
Py_DECREF(dict);
Py_XDECREF(descr);
return 0;
}
- else {
- Py_DECREF(dict);
- if (PyErr_Occurred()) {
- Py_XDECREF(descr);
- return 0;
- }
+ Py_DECREF(dict);
+
+ if (PyErr_Occurred()) {
+ Py_XDECREF(descr);
+ return 0;
}
}