From c08c7b100b5a9c8fb84aaa6b45559e6cb31c6ed6 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sun, 5 Apr 2026 11:12:39 +0800 Subject: [PATCH 1/3] gh-148119: Refactor `get_type_attr_as_size` to improve error handling in `structseq.c` --- Objects/structseq.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Objects/structseq.c b/Objects/structseq.c index 8fa9cbba3bcce3..66abda185032ac 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -28,13 +28,23 @@ static Py_ssize_t get_type_attr_as_size(PyTypeObject *tp, PyObject *name) { PyObject *v = PyDict_GetItemWithError(_PyType_GetDict(tp), name); - if (v == NULL && !PyErr_Occurred()) { + + if (v == NULL) { + if (PyErr_Occurred()) { + return -1; + } PyErr_Format(PyExc_TypeError, "Missed attribute '%U' of type %s", name, tp->tp_name); return -1; } - return PyLong_AsSsize_t(v); + + Py_ssize_t result = PyLong_AsSsize_t(v); + if (result == -1 && PyErr_Occurred()) { + return -1; + } + + return result; } #define VISIBLE_SIZE(op) Py_SIZE(op) From abcab3640e3f6158301e10d85ab431e444f96163 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sun, 5 Apr 2026 18:59:13 +0800 Subject: [PATCH 2/3] update --- Objects/structseq.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Objects/structseq.c b/Objects/structseq.c index 66abda185032ac..9e09b1b7cb6a2e 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -39,12 +39,7 @@ get_type_attr_as_size(PyTypeObject *tp, PyObject *name) return -1; } - Py_ssize_t result = PyLong_AsSsize_t(v); - if (result == -1 && PyErr_Occurred()) { - return -1; - } - - return result; + return PyLong_AsSsize_t(v); } #define VISIBLE_SIZE(op) Py_SIZE(op) From 3094d66ce1c5d0035e4d921ab4178d89fdeca940 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sun, 5 Apr 2026 19:00:29 +0800 Subject: [PATCH 3/3] update --- Objects/structseq.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Objects/structseq.c b/Objects/structseq.c index 9e09b1b7cb6a2e..9130fe6a133b1e 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -38,7 +38,6 @@ get_type_attr_as_size(PyTypeObject *tp, PyObject *name) name, tp->tp_name); return -1; } - return PyLong_AsSsize_t(v); }