]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106303: Use _PyObject_LookupAttr() instead of PyObject_GetAttr() (GH-106304)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 9 Jul 2023 12:27:03 +0000 (15:27 +0300)
committerGitHub <noreply@github.com>
Sun, 9 Jul 2023 12:27:03 +0000 (15:27 +0300)
It simplifies and speed up the code.

Include/internal/pycore_global_objects_fini_generated.h
Include/internal/pycore_global_strings.h
Include/internal/pycore_runtime_init_generated.h
Include/internal/pycore_unicodeobject_generated.h
Objects/funcobject.c
Python/ceval.c

index f85207b4bde2928b3f2efd25a8413bd59070e7f7..6d50ffd0a02f1facba824c011e4c59eafced27f2 100644 (file)
@@ -668,6 +668,7 @@ _PyStaticObjects_CheckRefcnt(PyInterpreterState *interp) {
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__lshift__));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__lt__));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__main__));
+    _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__match_args__));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__matmul__));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__missing__));
     _PyStaticObject_CheckRefcnt((PyObject *)&_Py_ID(__mod__));
index 3c9c12202ba1b95553d3c518c8d770756f582fb7..bb1fb13f342fc697ebe63284cc0c7c769ab168d3 100644 (file)
@@ -157,6 +157,7 @@ struct _Py_global_strings {
         STRUCT_FOR_ID(__lshift__)
         STRUCT_FOR_ID(__lt__)
         STRUCT_FOR_ID(__main__)
+        STRUCT_FOR_ID(__match_args__)
         STRUCT_FOR_ID(__matmul__)
         STRUCT_FOR_ID(__missing__)
         STRUCT_FOR_ID(__mod__)
index 9a28368a124ce8837eafbbabed0548f44cb3136c..2d66647438b193a3297b71358896712fe6becbb7 100644 (file)
@@ -663,6 +663,7 @@ extern "C" {
     INIT_ID(__lshift__), \
     INIT_ID(__lt__), \
     INIT_ID(__main__), \
+    INIT_ID(__match_args__), \
     INIT_ID(__matmul__), \
     INIT_ID(__missing__), \
     INIT_ID(__mod__), \
index 9e13a9491b7da599075f3c6870df95255b921b9a..59f40075f93983bbab1bea7d52280040b2436983 100644 (file)
@@ -315,6 +315,9 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) {
     string = &_Py_ID(__main__);
     assert(_PyUnicode_CheckConsistency(string, 1));
     _PyUnicode_InternInPlace(interp, &string);
+    string = &_Py_ID(__match_args__);
+    assert(_PyUnicode_CheckConsistency(string, 1));
+    _PyUnicode_InternInPlace(interp, &string);
     string = &_Py_ID(__matmul__);
     assert(_PyUnicode_CheckConsistency(string, 1));
     _PyUnicode_InternInPlace(interp, &string);
index f43e3a2787b846c7dfe2007d0622035bd71bc327..a8a9ee2b9bad469958487f53ec388e2a4d680e48 100644 (file)
@@ -942,17 +942,12 @@ PyTypeObject PyFunction_Type = {
 static int
 functools_copy_attr(PyObject *wrapper, PyObject *wrapped, PyObject *name)
 {
-    PyObject *value = PyObject_GetAttr(wrapped, name);
-    if (value == NULL) {
-        if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
-            PyErr_Clear();
-            return 0;
-        }
-        return -1;
+    PyObject *value;
+    int res = _PyObject_LookupAttr(wrapped, name, &value);
+    if (value != NULL) {
+        res = PyObject_SetAttr(wrapper, name, value);
+        Py_DECREF(value);
     }
-
-    int res = PyObject_SetAttr(wrapper, name, value);
-    Py_DECREF(value);
     return res;
 }
 
index 1b8650a650412dca108c689de5f866a8a3896560..da1135549a255c1de037d8cf035a213fb6ddfb70 100644 (file)
@@ -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) {