Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8d34031

Browse filesBrowse files
authored
gh-104078: Improve performance of PyObject_HasAttrString (#104079)
1 parent fdb3ef8 commit 8d34031
Copy full SHA for 8d34031

File tree

Expand file treeCollapse file tree

2 files changed

+18
-6
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+18
-6
lines changed
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve the performance of :c:func:`PyObject_HasAttrString`

‎Objects/object.c

Copy file name to clipboardExpand all lines: Objects/object.c
+17-6Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -918,13 +918,24 @@ PyObject_GetAttrString(PyObject *v, const char *name)
918918
int
919919
PyObject_HasAttrString(PyObject *v, const char *name)
920920
{
921-
PyObject *res = PyObject_GetAttrString(v, name);
922-
if (res != NULL) {
923-
Py_DECREF(res);
924-
return 1;
921+
if (Py_TYPE(v)->tp_getattr != NULL) {
922+
PyObject *res = (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
923+
if (res != NULL) {
924+
Py_DECREF(res);
925+
return 1;
926+
}
927+
PyErr_Clear();
928+
return 0;
925929
}
926-
PyErr_Clear();
927-
return 0;
930+
931+
PyObject *attr_name = PyUnicode_FromString(name);
932+
if (attr_name == NULL) {
933+
PyErr_Clear();
934+
return 0;
935+
}
936+
int ok = PyObject_HasAttr(v, attr_name);
937+
Py_DECREF(attr_name);
938+
return ok;
928939
}
929940

930941
int

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.