Skip to content

Commit 93d292c

Browse files
gh-106303: Use _PyObject_LookupAttr() instead of PyObject_GetAttr() (GH-106304)
It simplifies and speed up the code.
1 parent d137c2c commit 93d292c

6 files changed

+17
-20
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ struct _Py_global_strings {
157157
STRUCT_FOR_ID(__lshift__)
158158
STRUCT_FOR_ID(__lt__)
159159
STRUCT_FOR_ID(__main__)
160+
STRUCT_FOR_ID(__match_args__)
160161
STRUCT_FOR_ID(__matmul__)
161162
STRUCT_FOR_ID(__missing__)
162163
STRUCT_FOR_ID(__mod__)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/funcobject.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -942,17 +942,12 @@ PyTypeObject PyFunction_Type = {
942942
static int
943943
functools_copy_attr(PyObject *wrapper, PyObject *wrapped, PyObject *name)
944944
{
945-
PyObject *value = PyObject_GetAttr(wrapped, name);
946-
if (value == NULL) {
947-
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
948-
PyErr_Clear();
949-
return 0;
950-
}
951-
return -1;
945+
PyObject *value;
946+
int res = _PyObject_LookupAttr(wrapped, name, &value);
947+
if (value != NULL) {
948+
res = PyObject_SetAttr(wrapper, name, value);
949+
Py_DECREF(value);
952950
}
953-
954-
int res = PyObject_SetAttr(wrapper, name, value);
955-
Py_DECREF(value);
956951
return res;
957952
}
958953

Python/ceval.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,8 @@ match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type,
418418
}
419419
return NULL;
420420
}
421-
PyObject *attr = PyObject_GetAttr(subject, name);
422-
if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
423-
_PyErr_Clear(tstate);
424-
}
421+
PyObject *attr;
422+
(void)_PyObject_LookupAttr(subject, name, &attr);
425423
return attr;
426424
}
427425

@@ -456,7 +454,9 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
456454
// First, the positional subpatterns:
457455
if (nargs) {
458456
int match_self = 0;
459-
match_args = PyObject_GetAttrString(type, "__match_args__");
457+
if (_PyObject_LookupAttr(type, &_Py_ID(__match_args__), &match_args) < 0) {
458+
goto fail;
459+
}
460460
if (match_args) {
461461
if (!PyTuple_CheckExact(match_args)) {
462462
const char *e = "%s.__match_args__ must be a tuple (got %s)";
@@ -466,8 +466,7 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
466466
goto fail;
467467
}
468468
}
469-
else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
470-
_PyErr_Clear(tstate);
469+
else {
471470
// _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
472471
// define __match_args__. This is natural behavior for subclasses:
473472
// it's as if __match_args__ is some "magic" value that is lost as
@@ -476,9 +475,6 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
476475
match_self = PyType_HasFeature((PyTypeObject*)type,
477476
_Py_TPFLAGS_MATCH_SELF);
478477
}
479-
else {
480-
goto fail;
481-
}
482478
assert(PyTuple_CheckExact(match_args));
483479
Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
484480
if (allowed < nargs) {

0 commit comments

Comments
 (0)