mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
gh-106521: Remove _PyObject_LookupAttr() function (GH-106642)
This commit is contained in:
parent
e8ab0096a5
commit
be1b968dc1
45 changed files with 351 additions and 352 deletions
|
|
@ -182,7 +182,7 @@ PyObject_GetItem(PyObject *o, PyObject *key)
|
|||
return Py_GenericAlias(o, key);
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr(o, &_Py_ID(__class_getitem__), &meth) < 0) {
|
||||
if (PyObject_GetOptionalAttr(o, &_Py_ID(__class_getitem__), &meth) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (meth && meth != Py_None) {
|
||||
|
|
@ -2552,7 +2552,7 @@ abstract_get_bases(PyObject *cls)
|
|||
{
|
||||
PyObject *bases;
|
||||
|
||||
(void)_PyObject_LookupAttr(cls, &_Py_ID(__bases__), &bases);
|
||||
(void)PyObject_GetOptionalAttr(cls, &_Py_ID(__bases__), &bases);
|
||||
if (bases != NULL && !PyTuple_Check(bases)) {
|
||||
Py_DECREF(bases);
|
||||
return NULL;
|
||||
|
|
@ -2636,7 +2636,7 @@ object_isinstance(PyObject *inst, PyObject *cls)
|
|||
if (PyType_Check(cls)) {
|
||||
retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
|
||||
if (retval == 0) {
|
||||
retval = _PyObject_LookupAttr(inst, &_Py_ID(__class__), &icls);
|
||||
retval = PyObject_GetOptionalAttr(inst, &_Py_ID(__class__), &icls);
|
||||
if (icls != NULL) {
|
||||
if (icls != (PyObject *)(Py_TYPE(inst)) && PyType_Check(icls)) {
|
||||
retval = PyType_IsSubtype(
|
||||
|
|
@ -2654,7 +2654,7 @@ object_isinstance(PyObject *inst, PyObject *cls)
|
|||
if (!check_class(cls,
|
||||
"isinstance() arg 2 must be a type, a tuple of types, or a union"))
|
||||
return -1;
|
||||
retval = _PyObject_LookupAttr(inst, &_Py_ID(__class__), &icls);
|
||||
retval = PyObject_GetOptionalAttr(inst, &_Py_ID(__class__), &icls);
|
||||
if (icls != NULL) {
|
||||
retval = abstract_issubclass(icls, cls);
|
||||
Py_DECREF(icls);
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ object_is_not_callable(PyThreadState *tstate, PyObject *callable)
|
|||
goto basic_type_error;
|
||||
}
|
||||
PyObject *attr;
|
||||
int res = _PyObject_LookupAttr(callable, name, &attr);
|
||||
int res = PyObject_GetOptionalAttr(callable, name, &attr);
|
||||
if (res < 0) {
|
||||
_PyErr_Clear(tstate);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -275,9 +275,9 @@ method_repr(PyMethodObject *a)
|
|||
PyObject *funcname, *result;
|
||||
const char *defname = "?";
|
||||
|
||||
if (_PyObject_LookupAttr(func, &_Py_ID(__qualname__), &funcname) < 0 ||
|
||||
if (PyObject_GetOptionalAttr(func, &_Py_ID(__qualname__), &funcname) < 0 ||
|
||||
(funcname == NULL &&
|
||||
_PyObject_LookupAttr(func, &_Py_ID(__name__), &funcname) < 0))
|
||||
PyObject_GetOptionalAttr(func, &_Py_ID(__name__), &funcname) < 0))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -479,7 +479,7 @@ instancemethod_repr(PyObject *self)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr(func, &_Py_ID(__name__), &funcname) < 0) {
|
||||
if (PyObject_GetOptionalAttr(func, &_Py_ID(__name__), &funcname) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (funcname != NULL && !PyUnicode_Check(funcname)) {
|
||||
|
|
|
|||
|
|
@ -1792,7 +1792,7 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
|
|||
}
|
||||
/* if no docstring given and the getter has one, use that one */
|
||||
else if (fget != NULL) {
|
||||
int rc = _PyObject_LookupAttr(fget, &_Py_ID(__doc__), &prop_doc);
|
||||
int rc = PyObject_GetOptionalAttr(fget, &_Py_ID(__doc__), &prop_doc);
|
||||
if (rc <= 0) {
|
||||
return rc;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2664,7 +2664,7 @@ dict_update_arg(PyObject *self, PyObject *arg)
|
|||
return PyDict_Merge(self, arg, 1);
|
||||
}
|
||||
PyObject *func;
|
||||
if (_PyObject_LookupAttr(arg, &_Py_ID(keys), &func) < 0) {
|
||||
if (PyObject_GetOptionalAttr(arg, &_Py_ID(keys), &func) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (func != NULL) {
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@ BaseException_add_note(PyObject *self, PyObject *note)
|
|||
}
|
||||
|
||||
PyObject *notes;
|
||||
if (_PyObject_LookupAttr(self, &_Py_ID(__notes__), ¬es) < 0) {
|
||||
if (PyObject_GetOptionalAttr(self, &_Py_ID(__notes__), ¬es) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (notes == NULL) {
|
||||
|
|
@ -941,7 +941,7 @@ exceptiongroup_subset(
|
|||
PyException_SetCause(eg, PyException_GetCause(orig));
|
||||
|
||||
PyObject *notes;
|
||||
if (_PyObject_LookupAttr(orig, &_Py_ID(__notes__), ¬es) < 0) {
|
||||
if (PyObject_GetOptionalAttr(orig, &_Py_ID(__notes__), ¬es) < 0) {
|
||||
goto error;
|
||||
}
|
||||
if (notes) {
|
||||
|
|
|
|||
|
|
@ -176,7 +176,7 @@ PyObject_AsFileDescriptor(PyObject *o)
|
|||
if (PyLong_Check(o)) {
|
||||
fd = _PyLong_AsInt(o);
|
||||
}
|
||||
else if (_PyObject_LookupAttr(o, &_Py_ID(fileno), &meth) < 0) {
|
||||
else if (PyObject_GetOptionalAttr(o, &_Py_ID(fileno), &meth) < 0) {
|
||||
return -1;
|
||||
}
|
||||
else if (meth != NULL) {
|
||||
|
|
|
|||
|
|
@ -943,7 +943,7 @@ static int
|
|||
functools_copy_attr(PyObject *wrapper, PyObject *wrapped, PyObject *name)
|
||||
{
|
||||
PyObject *value;
|
||||
int res = _PyObject_LookupAttr(wrapped, name, &value);
|
||||
int res = PyObject_GetOptionalAttr(wrapped, name, &value);
|
||||
if (value != NULL) {
|
||||
res = PyObject_SetAttr(wrapper, name, value);
|
||||
Py_DECREF(value);
|
||||
|
|
|
|||
|
|
@ -63,12 +63,12 @@ ga_repr_item(_PyUnicodeWriter *writer, PyObject *p)
|
|||
goto done;
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr(p, &_Py_ID(__origin__), &tmp) < 0) {
|
||||
if (PyObject_GetOptionalAttr(p, &_Py_ID(__origin__), &tmp) < 0) {
|
||||
goto done;
|
||||
}
|
||||
if (tmp != NULL) {
|
||||
Py_DECREF(tmp);
|
||||
if (_PyObject_LookupAttr(p, &_Py_ID(__args__), &tmp) < 0) {
|
||||
if (PyObject_GetOptionalAttr(p, &_Py_ID(__args__), &tmp) < 0) {
|
||||
goto done;
|
||||
}
|
||||
if (tmp != NULL) {
|
||||
|
|
@ -78,13 +78,13 @@ ga_repr_item(_PyUnicodeWriter *writer, PyObject *p)
|
|||
}
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr(p, &_Py_ID(__qualname__), &qualname) < 0) {
|
||||
if (PyObject_GetOptionalAttr(p, &_Py_ID(__qualname__), &qualname) < 0) {
|
||||
goto done;
|
||||
}
|
||||
if (qualname == NULL) {
|
||||
goto use_repr;
|
||||
}
|
||||
if (_PyObject_LookupAttr(p, &_Py_ID(__module__), &module) < 0) {
|
||||
if (PyObject_GetOptionalAttr(p, &_Py_ID(__module__), &module) < 0) {
|
||||
goto done;
|
||||
}
|
||||
if (module == NULL || module == Py_None) {
|
||||
|
|
@ -257,7 +257,7 @@ _Py_make_parameters(PyObject *args)
|
|||
if (PyType_Check(t)) {
|
||||
continue;
|
||||
}
|
||||
if (_PyObject_LookupAttr(t, &_Py_ID(__typing_subst__), &subst) < 0) {
|
||||
if (PyObject_GetOptionalAttr(t, &_Py_ID(__typing_subst__), &subst) < 0) {
|
||||
Py_DECREF(parameters);
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -267,7 +267,7 @@ _Py_make_parameters(PyObject *args)
|
|||
}
|
||||
else {
|
||||
PyObject *subparams;
|
||||
if (_PyObject_LookupAttr(t, &_Py_ID(__parameters__),
|
||||
if (PyObject_GetOptionalAttr(t, &_Py_ID(__parameters__),
|
||||
&subparams) < 0) {
|
||||
Py_DECREF(parameters);
|
||||
return NULL;
|
||||
|
|
@ -310,7 +310,7 @@ subs_tvars(PyObject *obj, PyObject *params,
|
|||
PyObject **argitems, Py_ssize_t nargs)
|
||||
{
|
||||
PyObject *subparams;
|
||||
if (_PyObject_LookupAttr(obj, &_Py_ID(__parameters__), &subparams) < 0) {
|
||||
if (PyObject_GetOptionalAttr(obj, &_Py_ID(__parameters__), &subparams) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (subparams && PyTuple_Check(subparams) && PyTuple_GET_SIZE(subparams)) {
|
||||
|
|
@ -361,7 +361,7 @@ _is_unpacked_typevartuple(PyObject *arg)
|
|||
if (PyType_Check(arg)) { // TODO: Add test
|
||||
return 0;
|
||||
}
|
||||
int res = _PyObject_LookupAttr(arg, &_Py_ID(__typing_is_unpacked_typevartuple__), &tmp);
|
||||
int res = PyObject_GetOptionalAttr(arg, &_Py_ID(__typing_is_unpacked_typevartuple__), &tmp);
|
||||
if (res > 0) {
|
||||
res = PyObject_IsTrue(tmp);
|
||||
Py_DECREF(tmp);
|
||||
|
|
@ -383,7 +383,7 @@ _unpacked_tuple_args(PyObject *arg)
|
|||
return Py_NewRef(result);
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr(arg, &_Py_ID(__typing_unpacked_tuple_args__), &result) > 0) {
|
||||
if (PyObject_GetOptionalAttr(arg, &_Py_ID(__typing_unpacked_tuple_args__), &result) > 0) {
|
||||
if (result == Py_None) {
|
||||
Py_DECREF(result);
|
||||
return NULL;
|
||||
|
|
@ -448,7 +448,7 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
|
|||
for (Py_ssize_t i = 0; i < nparams; i++) {
|
||||
PyObject *param = PyTuple_GET_ITEM(parameters, i);
|
||||
PyObject *prepare, *tmp;
|
||||
if (_PyObject_LookupAttr(param, &_Py_ID(__typing_prepare_subst__), &prepare) < 0) {
|
||||
if (PyObject_GetOptionalAttr(param, &_Py_ID(__typing_prepare_subst__), &prepare) < 0) {
|
||||
Py_DECREF(item);
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -503,7 +503,7 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
|
|||
return NULL;
|
||||
}
|
||||
PyObject *subst;
|
||||
if (_PyObject_LookupAttr(arg, &_Py_ID(__typing_subst__), &subst) < 0) {
|
||||
if (PyObject_GetOptionalAttr(arg, &_Py_ID(__typing_subst__), &subst) < 0) {
|
||||
Py_DECREF(newargs);
|
||||
Py_DECREF(item);
|
||||
return NULL;
|
||||
|
|
|
|||
|
|
@ -317,7 +317,7 @@ gen_close_iter(PyObject *yf)
|
|||
}
|
||||
else {
|
||||
PyObject *meth;
|
||||
if (_PyObject_LookupAttr(yf, &_Py_ID(close), &meth) < 0) {
|
||||
if (PyObject_GetOptionalAttr(yf, &_Py_ID(close), &meth) < 0) {
|
||||
PyErr_WriteUnraisable(yf);
|
||||
}
|
||||
if (meth) {
|
||||
|
|
@ -492,7 +492,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
|
|||
} else {
|
||||
/* `yf` is an iterator or a coroutine-like object. */
|
||||
PyObject *meth;
|
||||
if (_PyObject_LookupAttr(yf, &_Py_ID(throw), &meth) < 0) {
|
||||
if (PyObject_GetOptionalAttr(yf, &_Py_ID(throw), &meth) < 0) {
|
||||
Py_DECREF(yf);
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -742,7 +742,7 @@ _PyModuleSpec_IsInitializing(PyObject *spec)
|
|||
{
|
||||
if (spec != NULL) {
|
||||
PyObject *value;
|
||||
int ok = _PyObject_LookupAttr(spec, &_Py_ID(_initializing), &value);
|
||||
int ok = PyObject_GetOptionalAttr(spec, &_Py_ID(_initializing), &value);
|
||||
if (ok == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2154,7 +2154,7 @@ mutablemapping_update_arg(PyObject *self, PyObject *arg)
|
|||
return res;
|
||||
}
|
||||
PyObject *func;
|
||||
if (_PyObject_LookupAttr(arg, &_Py_ID(keys), &func) < 0) {
|
||||
if (PyObject_GetOptionalAttr(arg, &_Py_ID(keys), &func) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (func != NULL) {
|
||||
|
|
@ -2186,7 +2186,7 @@ mutablemapping_update_arg(PyObject *self, PyObject *arg)
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
if (_PyObject_LookupAttr(arg, &_Py_ID(items), &func) < 0) {
|
||||
if (PyObject_GetOptionalAttr(arg, &_Py_ID(items), &func) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (func != NULL) {
|
||||
|
|
|
|||
|
|
@ -2359,7 +2359,7 @@ static PyObject *
|
|||
class_name(PyObject *cls)
|
||||
{
|
||||
PyObject *name;
|
||||
if (_PyObject_LookupAttr(cls, &_Py_ID(__name__), &name) == 0) {
|
||||
if (PyObject_GetOptionalAttr(cls, &_Py_ID(__name__), &name) == 0) {
|
||||
name = PyObject_Repr(cls);
|
||||
}
|
||||
return name;
|
||||
|
|
@ -3865,7 +3865,7 @@ type_new_get_bases(type_new_ctx *ctx, PyObject **type)
|
|||
continue;
|
||||
}
|
||||
PyObject *mro_entries;
|
||||
if (_PyObject_LookupAttr(base, &_Py_ID(__mro_entries__),
|
||||
if (PyObject_GetOptionalAttr(base, &_Py_ID(__mro_entries__),
|
||||
&mro_entries) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -5147,7 +5147,7 @@ merge_class_dict(PyObject *dict, PyObject *aclass)
|
|||
assert(aclass);
|
||||
|
||||
/* Merge in the type's dict (if any). */
|
||||
if (_PyObject_LookupAttr(aclass, &_Py_ID(__dict__), &classdict) < 0) {
|
||||
if (PyObject_GetOptionalAttr(aclass, &_Py_ID(__dict__), &classdict) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (classdict != NULL) {
|
||||
|
|
@ -5158,7 +5158,7 @@ merge_class_dict(PyObject *dict, PyObject *aclass)
|
|||
}
|
||||
|
||||
/* Recursively merge in the base types' (if any) dicts. */
|
||||
if (_PyObject_LookupAttr(aclass, &_Py_ID(__bases__), &bases) < 0) {
|
||||
if (PyObject_GetOptionalAttr(aclass, &_Py_ID(__bases__), &bases) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (bases != NULL) {
|
||||
|
|
@ -5984,7 +5984,7 @@ object_getstate_default(PyObject *obj, int required)
|
|||
PyObject *name, *value;
|
||||
|
||||
name = Py_NewRef(PyList_GET_ITEM(slotnames, i));
|
||||
if (_PyObject_LookupAttr(obj, name, &value) < 0) {
|
||||
if (PyObject_GetOptionalAttr(obj, name, &value) < 0) {
|
||||
Py_DECREF(name);
|
||||
goto error;
|
||||
}
|
||||
|
|
@ -6381,7 +6381,7 @@ object___reduce_ex___impl(PyObject *self, int protocol)
|
|||
}
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr(self, &_Py_ID(__reduce__), &reduce) < 0) {
|
||||
if (PyObject_GetOptionalAttr(self, &_Py_ID(__reduce__), &reduce) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (reduce != NULL) {
|
||||
|
|
@ -6500,7 +6500,7 @@ object___dir___impl(PyObject *self)
|
|||
PyObject *itsclass = NULL;
|
||||
|
||||
/* Get __dict__ (which may or may not be a real dict...) */
|
||||
if (_PyObject_LookupAttr(self, &_Py_ID(__dict__), &dict) < 0) {
|
||||
if (PyObject_GetOptionalAttr(self, &_Py_ID(__dict__), &dict) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (dict == NULL) {
|
||||
|
|
@ -6520,7 +6520,7 @@ object___dir___impl(PyObject *self)
|
|||
goto error;
|
||||
|
||||
/* Merge in attrs reachable from its class. */
|
||||
if (_PyObject_LookupAttr(self, &_Py_ID(__class__), &itsclass) < 0) {
|
||||
if (PyObject_GetOptionalAttr(self, &_Py_ID(__class__), &itsclass) < 0) {
|
||||
goto error;
|
||||
}
|
||||
/* XXX(tomer): Perhaps fall back to Py_TYPE(obj) if no
|
||||
|
|
@ -8393,7 +8393,7 @@ method_is_overloaded(PyObject *left, PyObject *right, PyObject *name)
|
|||
PyObject *a, *b;
|
||||
int ok;
|
||||
|
||||
if (_PyObject_LookupAttr((PyObject *)(Py_TYPE(right)), name, &b) < 0) {
|
||||
if (PyObject_GetOptionalAttr((PyObject *)(Py_TYPE(right)), name, &b) < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (b == NULL) {
|
||||
|
|
@ -8401,7 +8401,7 @@ method_is_overloaded(PyObject *left, PyObject *right, PyObject *name)
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr((PyObject *)(Py_TYPE(left)), name, &a) < 0) {
|
||||
if (PyObject_GetOptionalAttr((PyObject *)(Py_TYPE(left)), name, &a) < 0) {
|
||||
Py_DECREF(b);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -10373,7 +10373,7 @@ supercheck(PyTypeObject *type, PyObject *obj)
|
|||
/* Try the slow way */
|
||||
PyObject *class_attr;
|
||||
|
||||
if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &class_attr) < 0) {
|
||||
if (PyObject_GetOptionalAttr(obj, &_Py_ID(__class__), &class_attr) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (class_attr != NULL &&
|
||||
|
|
|
|||
|
|
@ -194,13 +194,13 @@ union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
|
|||
return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr(p, &_Py_ID(__origin__), &tmp) < 0) {
|
||||
if (PyObject_GetOptionalAttr(p, &_Py_ID(__origin__), &tmp) < 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (tmp) {
|
||||
Py_DECREF(tmp);
|
||||
if (_PyObject_LookupAttr(p, &_Py_ID(__args__), &tmp) < 0) {
|
||||
if (PyObject_GetOptionalAttr(p, &_Py_ID(__args__), &tmp) < 0) {
|
||||
goto exit;
|
||||
}
|
||||
if (tmp) {
|
||||
|
|
@ -210,13 +210,13 @@ union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
|
|||
}
|
||||
}
|
||||
|
||||
if (_PyObject_LookupAttr(p, &_Py_ID(__qualname__), &qualname) < 0) {
|
||||
if (PyObject_GetOptionalAttr(p, &_Py_ID(__qualname__), &qualname) < 0) {
|
||||
goto exit;
|
||||
}
|
||||
if (qualname == NULL) {
|
||||
goto use_repr;
|
||||
}
|
||||
if (_PyObject_LookupAttr(p, &_Py_ID(__module__), &module) < 0) {
|
||||
if (PyObject_GetOptionalAttr(p, &_Py_ID(__module__), &module) < 0) {
|
||||
goto exit;
|
||||
}
|
||||
if (module == NULL || module == Py_None) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue