summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-07-11 19:13:27 (GMT)
committerGitHub <noreply@github.com>2023-07-11 19:13:27 (GMT)
commit579aa89e68a6607398317a50586af781981e89fb (patch)
tree9f12badb2781f6af3fcc73ef65a940e885b5683a /Objects
parentcabd6e8a107127ff02f0b514148f648fb2472a58 (diff)
downloadcpython-579aa89e68a6607398317a50586af781981e89fb.zip
cpython-579aa89e68a6607398317a50586af781981e89fb.tar.gz
cpython-579aa89e68a6607398317a50586af781981e89fb.tar.bz2
gh-106521: Add PyObject_GetOptionalAttr() function (GH-106522)
It is a new name of former _PyObject_LookupAttr(). Add also PyObject_GetOptionalAttrString().
Diffstat (limited to 'Objects')
-rw-r--r--Objects/object.c34
1 files changed, 24 insertions, 10 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 540ba5d..d30e048 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -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;
}