summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorItamar Ostricher <itamarost@gmail.com>2023-05-03 07:20:00 (GMT)
committerGitHub <noreply@github.com>2023-05-03 07:20:00 (GMT)
commit8d34031068ece75667260f6526d3165efe34e054 (patch)
treeada4a24cab4185b9a883427a7006d30d3049210b /Objects
parentfdb3ef8c0f94c7e55870a585dc6499aca46f9f90 (diff)
downloadcpython-8d34031068ece75667260f6526d3165efe34e054.zip
cpython-8d34031068ece75667260f6526d3165efe34e054.tar.gz
cpython-8d34031068ece75667260f6526d3165efe34e054.tar.bz2
gh-104078: Improve performance of PyObject_HasAttrString (#104079)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/object.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/Objects/object.c b/Objects/object.c
index ee86901..c6ef592 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -918,13 +918,24 @@ PyObject_GetAttrString(PyObject *v, const char *name)
int
PyObject_HasAttrString(PyObject *v, const char *name)
{
- PyObject *res = PyObject_GetAttrString(v, name);
- if (res != NULL) {
- Py_DECREF(res);
- return 1;
+ if (Py_TYPE(v)->tp_getattr != NULL) {
+ PyObject *res = (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
+ if (res != NULL) {
+ Py_DECREF(res);
+ return 1;
+ }
+ PyErr_Clear();
+ return 0;
}
- PyErr_Clear();
- return 0;
+
+ PyObject *attr_name = PyUnicode_FromString(name);
+ if (attr_name == NULL) {
+ PyErr_Clear();
+ return 0;
+ }
+ int ok = PyObject_HasAttr(v, attr_name);
+ Py_DECREF(attr_name);
+ return ok;
}
int