mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
gh-106521: Add PyObject_GetOptionalAttr() function (GH-106522)
It is a new name of former _PyObject_LookupAttr(). Add also PyObject_GetOptionalAttrString().
This commit is contained in:
parent
cabd6e8a10
commit
579aa89e68
11 changed files with 119 additions and 23 deletions
|
|
@ -692,7 +692,7 @@ _PyObject_FunctionStr(PyObject *x)
|
|||
{
|
||||
assert(!PyErr_Occurred());
|
||||
PyObject *qualname;
|
||||
int ret = _PyObject_LookupAttr(x, &_Py_ID(__qualname__), &qualname);
|
||||
int ret = PyObject_GetOptionalAttr(x, &_Py_ID(__qualname__), &qualname);
|
||||
if (qualname == NULL) {
|
||||
if (ret < 0) {
|
||||
return NULL;
|
||||
|
|
@ -701,7 +701,7 @@ _PyObject_FunctionStr(PyObject *x)
|
|||
}
|
||||
PyObject *module;
|
||||
PyObject *result = NULL;
|
||||
ret = _PyObject_LookupAttr(x, &_Py_ID(__module__), &module);
|
||||
ret = PyObject_GetOptionalAttr(x, &_Py_ID(__module__), &module);
|
||||
if (module != NULL && module != Py_None) {
|
||||
ret = PyObject_RichCompareBool(module, &_Py_ID(builtins), Py_NE);
|
||||
if (ret < 0) {
|
||||
|
|
@ -957,7 +957,7 @@ _PyObject_IsAbstract(PyObject *obj)
|
|||
if (obj == NULL)
|
||||
return 0;
|
||||
|
||||
res = _PyObject_LookupAttr(obj, &_Py_ID(__isabstractmethod__), &isabstract);
|
||||
res = PyObject_GetOptionalAttr(obj, &_Py_ID(__isabstractmethod__), &isabstract);
|
||||
if (res > 0) {
|
||||
res = PyObject_IsTrue(isabstract);
|
||||
Py_DECREF(isabstract);
|
||||
|
|
@ -1049,7 +1049,7 @@ PyObject_GetAttr(PyObject *v, PyObject *name)
|
|||
}
|
||||
|
||||
int
|
||||
_PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
|
||||
PyObject_GetOptionalAttr(PyObject *v, PyObject *name, PyObject **result)
|
||||
{
|
||||
PyTypeObject *tp = Py_TYPE(v);
|
||||
|
||||
|
|
@ -1117,21 +1117,35 @@ _PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
|
|||
}
|
||||
|
||||
int
|
||||
_PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
|
||||
PyObject_GetOptionalAttrString(PyObject *obj, const char *name, PyObject **result)
|
||||
{
|
||||
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
|
||||
if (!oname) {
|
||||
*result = NULL;
|
||||
if (Py_TYPE(obj)->tp_getattr == NULL) {
|
||||
PyObject *oname = PyUnicode_FromString(name);
|
||||
if (oname == NULL) {
|
||||
*result = NULL;
|
||||
return -1;
|
||||
}
|
||||
int rc = PyObject_GetOptionalAttr(obj, oname, result);
|
||||
Py_DECREF(oname);
|
||||
return rc;
|
||||
}
|
||||
|
||||
*result = (*Py_TYPE(obj)->tp_getattr)(obj, (char*)name);
|
||||
if (*result != NULL) {
|
||||
return 1;
|
||||
}
|
||||
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
||||
return -1;
|
||||
}
|
||||
return _PyObject_LookupAttr(v, oname, result);
|
||||
PyErr_Clear();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
PyObject_HasAttr(PyObject *v, PyObject *name)
|
||||
{
|
||||
PyObject *res;
|
||||
if (_PyObject_LookupAttr(v, name, &res) < 0) {
|
||||
if (PyObject_GetOptionalAttr(v, name, &res) < 0) {
|
||||
PyErr_Clear();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue