]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-104078: Improve performance of PyObject_HasAttrString (#104079)
authorItamar Ostricher <itamarost@gmail.com>
Wed, 3 May 2023 07:20:00 +0000 (00:20 -0700)
committerGitHub <noreply@github.com>
Wed, 3 May 2023 07:20:00 +0000 (00:20 -0700)
Misc/NEWS.d/next/Core and Builtins/2023-05-01-21-05-47.gh-issue-104078.vRaBsU.rst [new file with mode: 0644]
Objects/object.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-01-21-05-47.gh-issue-104078.vRaBsU.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-01-21-05-47.gh-issue-104078.vRaBsU.rst
new file mode 100644 (file)
index 0000000..6f24529
--- /dev/null
@@ -0,0 +1 @@
+Improve the performance of :c:func:`PyObject_HasAttrString`
index ee8690101d3cc09b3bba89c3c25b567ac20c7f77..c6ef5928164824e320b8d354d5ef8e2b5183dc60 100644 (file)
@@ -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