summaryrefslogtreecommitdiffstats
path: root/Python/ceval.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-07-09 12:27:03 (GMT)
committerGitHub <noreply@github.com>2023-07-09 12:27:03 (GMT)
commit93d292c2b3f8e85ef562c37f59678c639b9b8fcb (patch)
tree17f1ff429d63ab4c900d2fd2a3bcf143e53e760f /Python/ceval.c
parentd137c2cae28b79555433079d917c3e0614bdcd61 (diff)
downloadcpython-93d292c2b3f8e85ef562c37f59678c639b9b8fcb.zip
cpython-93d292c2b3f8e85ef562c37f59678c639b9b8fcb.tar.gz
cpython-93d292c2b3f8e85ef562c37f59678c639b9b8fcb.tar.bz2
gh-106303: Use _PyObject_LookupAttr() instead of PyObject_GetAttr() (GH-106304)
It simplifies and speed up the code.
Diffstat (limited to 'Python/ceval.c')
-rw-r--r--Python/ceval.c16
1 files changed, 6 insertions, 10 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 1b8650a..da11355 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -418,10 +418,8 @@ match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type,
}
return NULL;
}
- PyObject *attr = PyObject_GetAttr(subject, name);
- if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
- _PyErr_Clear(tstate);
- }
+ PyObject *attr;
+ (void)_PyObject_LookupAttr(subject, name, &attr);
return attr;
}
@@ -456,7 +454,9 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
// First, the positional subpatterns:
if (nargs) {
int match_self = 0;
- match_args = PyObject_GetAttrString(type, "__match_args__");
+ if (_PyObject_LookupAttr(type, &_Py_ID(__match_args__), &match_args) < 0) {
+ goto fail;
+ }
if (match_args) {
if (!PyTuple_CheckExact(match_args)) {
const char *e = "%s.__match_args__ must be a tuple (got %s)";
@@ -466,8 +466,7 @@ match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
goto fail;
}
}
- else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
- _PyErr_Clear(tstate);
+ else {
// _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
// define __match_args__. This is natural behavior for subclasses:
// 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,
match_self = PyType_HasFeature((PyTypeObject*)type,
_Py_TPFLAGS_MATCH_SELF);
}
- else {
- goto fail;
- }
assert(PyTuple_CheckExact(match_args));
Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
if (allowed < nargs) {