From 80d9a8a32bb13c8b4977249db80146814cdce27a Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 20 Oct 2026 22:55:41 -0400 Subject: [PATCH] Use AC for __repr__ --- Modules/clinic/mmapmodule.c.h | 17 ++++++++++++++++- Modules/mmapmodule.c | 20 +++++++++----------- Objects/bytearrayobject.c | 20 +++++++++----------- Objects/clinic/bytearrayobject.c.h | 17 ++++++++++++++++- Objects/clinic/dictobject.c.h | 17 ++++++++++++++++- Objects/clinic/listobject.c.h | 17 ++++++++++++++++- Objects/clinic/setobject.c.h | 17 ++++++++++++++++- Objects/dictobject.c | 20 +++++++++----------- Objects/listobject.c | 26 ++++++++++++-------------- Objects/setobject.c | 21 +++++++++------------ Tools/clinic/libclinic/dsl_parser.py | 7 ++----- Tools/clinic/libclinic/slots.py | 3 +++ 12 files changed, 133 insertions(+), 69 deletions(-) diff --git a/Modules/clinic/mmapmodule.c.h b/Modules/clinic/mmapmodule.c.h index 1343eda42e84ec..b5a5f796663fe5 100644 --- a/Modules/clinic/mmapmodule.c.h +++ b/Modules/clinic/mmapmodule.c.h @@ -701,6 +701,21 @@ mmap_mmap___exit__(PyObject *self, PyObject *const *args, Py_ssize_t nargs) return return_value; } +static PyObject * +mmap__repr__method_impl(PyObject *op); + +static PyObject * +mmap__repr__method(PyObject *op) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(op); + return_value = mmap__repr__method_impl(op); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + #if defined(MS_WINDOWS) PyDoc_STRVAR(mmap_mmap___sizeof____doc__, @@ -877,4 +892,4 @@ mmap_mmap_madvise(PyObject *self, PyObject *const *args, Py_ssize_t nargs) #ifndef MMAP_MMAP_MADVISE_METHODDEF #define MMAP_MMAP_MADVISE_METHODDEF #endif /* !defined(MMAP_MMAP_MADVISE_METHODDEF) */ -/*[clinic end generated code: output=d2ae0d1d754f51cc input=a9049054013a1b77]*/ +/*[clinic end generated code: output=664297064075b598 input=a9049054013a1b77]*/ diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 766e85a1bdba98..0ae4b3ef388478 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1256,8 +1256,16 @@ mmap_mmap___exit___impl(mmap_object *self, PyObject *exc_type, return mmap_mmap_close_impl(self); } +/*[clinic input] +@critical_section +mmap.mmap.__repr__ as mmap__repr__method + + op: self(type="PyObject *") +[clinic start generated code]*/ + static PyObject * -mmap__repr__method_lock_held(PyObject *op) +mmap__repr__method_impl(PyObject *op) +/*[clinic end generated code: output=c896c8c4cd07228c input=a030361fbcfe851a]*/ { mmap_object *mobj = mmap_object_CAST(op); @@ -1301,16 +1309,6 @@ mmap__repr__method_lock_held(PyObject *op) } } -static PyObject * -mmap__repr__method(PyObject *op) -{ - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(op); - result = mmap__repr__method_lock_held(op); - Py_END_CRITICAL_SECTION(); - return result; -} - #ifdef MS_WINDOWS /*[clinic input] @critical_section diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 429b8d993867f0..03401010198fe7 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1182,8 +1182,16 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg, return -1; } +/*[clinic input] +@critical_section +bytearray.__repr__ + + op: self(type="PyObject *") +[clinic start generated code]*/ + static PyObject * -bytearray_repr_lock_held(PyObject *op) +bytearray_repr_impl(PyObject *op) +/*[clinic end generated code: output=4086ab6928ebab1d input=d46ad3c1f3071b27]*/ { _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); const char *className = _PyType_Name(Py_TYPE(op)); @@ -1198,16 +1206,6 @@ bytearray_repr_lock_held(PyObject *op) return res; } -static PyObject * -bytearray_repr(PyObject *op) -{ - PyObject *ret; - Py_BEGIN_CRITICAL_SECTION(op); - ret = bytearray_repr_lock_held(op); - Py_END_CRITICAL_SECTION(); - return ret; -} - static PyObject * bytearray_str(PyObject *op) { diff --git a/Objects/clinic/bytearrayobject.c.h b/Objects/clinic/bytearrayobject.c.h index 9c8ba841200f30..de4004201cbd12 100644 --- a/Objects/clinic/bytearrayobject.c.h +++ b/Objects/clinic/bytearrayobject.c.h @@ -179,6 +179,21 @@ bytearray_vectorcall(PyObject *type, PyObject *const *args, return return_value; } +static PyObject * +bytearray_repr_impl(PyObject *op); + +static PyObject * +bytearray_repr(PyObject *op) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(op); + return_value = bytearray_repr_impl(op); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(bytearray_find__doc__, "find($self, sub[, start[, end]], /)\n" "--\n" @@ -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=6635eceaaa27c054 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/dictobject.c.h b/Objects/clinic/dictobject.c.h index 15b8705d9c78e3..038f56d62b6d8c 100644 --- a/Objects/clinic/dictobject.c.h +++ b/Objects/clinic/dictobject.c.h @@ -5,6 +5,21 @@ preserve #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_CheckPositional() +static PyObject * +dict_repr_impl(PyObject *self); + +static PyObject * +dict_repr(PyObject *self) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = dict_repr_impl(self); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(dict_fromkeys__doc__, "fromkeys($type, iterable, value=None, /)\n" "--\n" @@ -341,4 +356,4 @@ frozendict_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) { return frozendict_copy_impl((PyFrozenDictObject *)self); } -/*[clinic end generated code: output=f4c88a3464928ae3 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=46d2d00147dd7917 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/listobject.c.h b/Objects/clinic/listobject.c.h index f3821ef5f70c21..01b5f7481faae5 100644 --- a/Objects/clinic/listobject.c.h +++ b/Objects/clinic/listobject.c.h @@ -10,6 +10,21 @@ preserve #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_CheckPositional() +static PyObject * +list_repr_impl(PyListObject *v); + +static PyObject * +list_repr(PyObject *v) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(v); + return_value = list_repr_impl((PyListObject *)v); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(list_insert__doc__, "insert($self, index, object, /)\n" "--\n" @@ -468,4 +483,4 @@ list___reversed__(PyObject *self, PyObject *Py_UNUSED(ignored)) { return list___reversed___impl((PyListObject *)self); } -/*[clinic end generated code: output=06c21b0bffbe8d84 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=1bf2de94b856ceed input=a9049054013a1b77]*/ diff --git a/Objects/clinic/setobject.c.h b/Objects/clinic/setobject.c.h index 098c4bcb53d3fb..8c9beaadccde86 100644 --- a/Objects/clinic/setobject.c.h +++ b/Objects/clinic/setobject.c.h @@ -4,6 +4,21 @@ preserve #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() +static PyObject * +set_repr_impl(PySetObject *so); + +static PyObject * +set_repr(PyObject *so) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(so); + return_value = set_repr_impl((PySetObject *)so); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(set_pop__doc__, "pop($self, /)\n" "--\n" @@ -552,4 +567,4 @@ set___sizeof__(PyObject *so, PyObject *Py_UNUSED(ignored)) return return_value; } -/*[clinic end generated code: output=5800c0bf136a5a0a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=5d794137d2614aaf input=a9049054013a1b77]*/ diff --git a/Objects/dictobject.c b/Objects/dictobject.c index bff543d9fdede4..b3e6323fb79335 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -3772,23 +3772,21 @@ anydict_repr_impl(PyObject *self) return NULL; } +/*[clinic input] +@critical_section +dict.__repr__ + + self: self(type="PyObject *") +[clinic start generated code]*/ + static PyObject * -dict_repr_lock_held(PyObject *self) +dict_repr_impl(PyObject *self) +/*[clinic end generated code: output=059263af818a321d input=f9c423cbdf534674]*/ { ASSERT_DICT_LOCKED((PyDictObject *)self); return anydict_repr_impl(self); } -static PyObject * -dict_repr(PyObject *self) -{ - PyObject *res; - Py_BEGIN_CRITICAL_SECTION(self); - res = dict_repr_lock_held(self); - Py_END_CRITICAL_SECTION(); - return res; -} - static Py_ssize_t dict_length(PyObject *self) { diff --git a/Objects/listobject.c b/Objects/listobject.c index f9488cc5c384ba..e736f29c82deb5 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -632,9 +632,21 @@ list_dealloc(PyObject *self) } } +/*[clinic input] +@critical_section +list.__repr__ + + v: self(type="PyListObject *") +[clinic start generated code]*/ + static PyObject * list_repr_impl(PyListObject *v) +/*[clinic end generated code: output=c5ffe3b9cfee36ce input=f4b85c550d11de32]*/ { + if (PyList_GET_SIZE(v) == 0) { + return PyUnicode_FromString("[]"); + } + int res = Py_ReprEnter((PyObject*)v); if (res != 0) { return (res > 0 ? PyUnicode_FromString("[...]") : NULL); @@ -687,20 +699,6 @@ list_repr_impl(PyListObject *v) return NULL; } -static PyObject * -list_repr(PyObject *self) -{ - if (PyList_GET_SIZE(self) == 0) { - return PyUnicode_FromString("[]"); - } - PyListObject *v = (PyListObject *)self; - PyObject *ret = NULL; - Py_BEGIN_CRITICAL_SECTION(v); - ret = list_repr_impl(v); - Py_END_CRITICAL_SECTION(); - return ret; -} - static Py_ssize_t list_length(PyObject *a) { diff --git a/Objects/setobject.c b/Objects/setobject.c index 8fdd1eb26118c0..a762edf5933e18 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -753,8 +753,16 @@ set_dealloc(PyObject *self) Py_TYPE(so)->tp_free(so); } +/*[clinic input] +@critical_section +set.__repr__ + + so: setobject +[clinic start generated code]*/ + static PyObject * -set_repr_lock_held(PySetObject *so) +set_repr_impl(PySetObject *so) +/*[clinic end generated code: output=5dfbb5e7f7c947ad input=f6743ec76163d7b3]*/ { PyObject *result=NULL, *keys, *listrepr, *tmp; int status = Py_ReprEnter((PyObject*)so); @@ -807,17 +815,6 @@ set_repr_lock_held(PySetObject *so) return result; } -static PyObject * -set_repr(PyObject *self) -{ - PySetObject *so = _PySet_CAST(self); - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(so); - result = set_repr_lock_held(so); - Py_END_CRITICAL_SECTION(); - return result; -} - static Py_ssize_t set_len(PyObject *self) { diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index 60730b8828d843..6757f38a698805 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -85,7 +85,6 @@ __radd__ __rand__ __rdivmod__ -__repr__ __rfloordiv__ __rlshift__ __rmatmul__ @@ -920,10 +919,6 @@ def state_parameters_start(self, line: str) -> None: assert self.function is not None if self.function.kind is GETTER: fail("@getter methods cannot define parameters") - if self.function.kind is SLOT: - assert self.function.slot is not None - if not self.function.slot.takes_parameters: - fail(f"{self.function.name!r} methods cannot define parameters") self.parameter_continuation = '' return self.next(self.state_parameter, line) @@ -1680,6 +1675,8 @@ def check_slot_parameters(self, func: Function) -> None: """Check that the parameters of a slot method select a C slot.""" assert func.slot is not None params = list(func.parameters.values())[1:] + if params and not func.slot.takes_parameters: + fail(f"{func.name!r} methods cannot define parameters") for p in params: if not p.is_positional_only(): fail(f"The parameters of {func.name!r} must be positional-only") diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index ee38b06c2ce4a8..e7a30f6c1adf17 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -56,4 +56,7 @@ def describe_signatures(self) -> str: '__next__': SlotSpec('__next__', 'PyObject *', 'object', { (): 'tp_iternext', }), + '__repr__': SlotSpec('__repr__', 'PyObject *', 'object', { + (): 'tp_repr', + }), }