From 3cbfe828c7baf80a82e06e28924bb16854c3f2a8 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 21 Oct 2026 12:42:04 -0400 Subject: [PATCH] Use Argument Clinic for __add__/__iadd__ --- Modules/_collectionsmodule.c | 23 +++++++++++------------ Modules/clinic/_collectionsmodule.c.h | 17 ++++++++++++++++- Objects/bytearrayobject.c | 24 +++++++++++------------- Objects/clinic/bytearrayobject.c.h | 17 ++++++++++++++++- Tools/clinic/libclinic/dsl_parser.py | 2 -- Tools/clinic/libclinic/slots.py | 2 ++ 6 files changed, 56 insertions(+), 29 deletions(-) diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 19910dbc0a5d421..c8fd4016cf2cf04 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -674,8 +674,18 @@ deque___copy___impl(dequeobject *deque) return deque_copy_impl(deque); } +/*[clinic input] +@critical_section +_collections.deque.__add__ as deque_concat + + deque: dequeobject + other: object + / +[clinic start generated code]*/ + static PyObject * -deque_concat_lock_held(dequeobject *deque, PyObject *other) +deque_concat_impl(dequeobject *deque, PyObject *other) +/*[clinic end generated code: output=fa6c28f7752f0b28 input=1816c51a4c6c4f05]*/ { PyObject *new_deque, *result; int rv; @@ -706,17 +716,6 @@ deque_concat_lock_held(dequeobject *deque, PyObject *other) return new_deque; } -static PyObject * -deque_concat(PyObject *self, PyObject *other) -{ - dequeobject *deque = dequeobject_CAST(self); - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(deque); - result = deque_concat_lock_held(deque, other); - Py_END_CRITICAL_SECTION(); - return result; -} - static int deque_clear(PyObject *self) { diff --git a/Modules/clinic/_collectionsmodule.c.h b/Modules/clinic/_collectionsmodule.c.h index 6c60678a6fbd51a..dd30e7b7db85bbf 100644 --- a/Modules/clinic/_collectionsmodule.c.h +++ b/Modules/clinic/_collectionsmodule.c.h @@ -202,6 +202,21 @@ deque___copy__(PyObject *deque, PyObject *Py_UNUSED(ignored)) return return_value; } +static PyObject * +deque_concat_impl(dequeobject *deque, PyObject *other); + +static PyObject * +deque_concat(PyObject *deque, PyObject *other) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(deque); + return_value = deque_concat_impl((dequeobject *)deque, other); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(deque_clearmethod__doc__, "clear($self, /)\n" "--\n" @@ -632,4 +647,4 @@ tuplegetter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=f5a388add99d3d15 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f66063dc478053e6 input=a9049054013a1b77]*/ diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 429b8d993867f0c..0179fbd9f2e2919 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -403,11 +403,19 @@ bytearray_length(PyObject *op) return PyByteArray_GET_SIZE(op); } +/*[clinic input] +@critical_section +bytearray.__iadd__ as bytearray_iconcat + + other: object + / +[clinic start generated code]*/ + static PyObject * -bytearray_iconcat_lock_held(PyObject *op, PyObject *other) +bytearray_iconcat_impl(PyByteArrayObject *self, PyObject *other) +/*[clinic end generated code: output=ba172f2db8f7a8dd input=5b6fb07ab387084d]*/ { - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); - PyByteArrayObject *self = _PyByteArray_CAST(op); + _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); Py_buffer vo; if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) { @@ -432,16 +440,6 @@ bytearray_iconcat_lock_held(PyObject *op, PyObject *other) return Py_NewRef(self); } -static PyObject * -bytearray_iconcat(PyObject *op, PyObject *other) -{ - PyObject *ret; - Py_BEGIN_CRITICAL_SECTION(op); - ret = bytearray_iconcat_lock_held(op, other); - Py_END_CRITICAL_SECTION(); - return ret; -} - static PyObject * bytearray_repeat_lock_held(PyObject *op, Py_ssize_t count) { diff --git a/Objects/clinic/bytearrayobject.c.h b/Objects/clinic/bytearrayobject.c.h index 9c8ba841200f306..f437fa464f14e70 100644 --- a/Objects/clinic/bytearrayobject.c.h +++ b/Objects/clinic/bytearrayobject.c.h @@ -11,6 +11,21 @@ preserve #include "pycore_runtime.h" // _Py_SINGLETON() #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL() +static PyObject * +bytearray_iconcat_impl(PyByteArrayObject *self, PyObject *other); + +static PyObject * +bytearray_iconcat(PyObject *self, PyObject *other) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = bytearray_iconcat_impl((PyByteArrayObject *)self, other); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + static int bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, const char *encoding, const char *errors); @@ -1946,4 +1961,4 @@ bytearray_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored)) { return bytearray_sizeof_impl((PyByteArrayObject *)self); } -/*[clinic end generated code: output=b67d1f1e84c7ed0f input=a9049054013a1b77]*/ +/*[clinic end generated code: output=7684cc5969b4b429 input=a9049054013a1b77]*/ diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index 4a38cb6b314fff0..212db1ae15d0c78 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -40,7 +40,6 @@ unsupported_special_methods: set[str] = set(""" __abs__ -__add__ __and__ __call__ __delitem__ @@ -54,7 +53,6 @@ __getitem__ __gt__ __hash__ -__iadd__ __iand__ __ifloordiv__ __ilshift__ diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index 95137800fd868c0..bfebda13b17a2cf 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -40,4 +40,6 @@ def make_return_converter(self) -> CReturnConverter: SLOTS: Final[dict[str, SlotSpec]] = { '__next__': SlotSpec(return_converter='object'), '__len__': SlotSpec(return_converter='Py_ssize_t'), + '__add__': SlotSpec(return_converter='object'), + '__iadd__': SlotSpec(return_converter='object'), }