From 197d28e8e59686c4c41c5d626ab65ebfecb6d1f0 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 20 Oct 2026 20:39:27 -0700 Subject: [PATCH 1/6] gh-114258: Use Argument Clinic for ``__len__`` --- Lib/test/test_clinic.py | 77 ++++++++++++++++++---------- Modules/_dbmmodule.c | 20 +++----- Modules/_gdbmmodule.c | 20 +++----- Modules/clinic/_dbmmodule.c.h | 17 +++++- Modules/clinic/_gdbmmodule.c.h | 17 +++++- Modules/clinic/mmapmodule.c.h | 17 +++++- Modules/mmapmodule.c | 19 +++---- Tools/clinic/libclinic/dsl_parser.py | 42 ++++++++------- Tools/clinic/libclinic/slots.py | 47 ++++++++--------- 9 files changed, 165 insertions(+), 111 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 8d2aced0b9caaf0..63f8ceccfbaa65f 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -35,7 +35,7 @@ from libclinic.return_converters import return_converters, int_return_converter from libclinic.block_parser import Block, BlockParser from libclinic.codegen import BlockPrinter, Destination - from libclinic.dsl_parser import DSLParser + from libclinic.dsl_parser import DSLParser, unsupported_special_methods from libclinic.slots import SlotSpec, SLOTS from libclinic.cli import parse_file, Clinic @@ -858,11 +858,11 @@ class Foo "FooObject *" "&Foo_Type" def test_slot_with_parameters_and_return_type(self): # Slots which take arguments or return something else than an - # object are not supported yet; check the machinery with fake ones. + # object are not supported yet; check the machinery with them. slots = { - '__contains__': SlotSpec('__contains__', 'int', 'int', - {(object_converter,): 'sq_contains'}), - '__bool__': SlotSpec('__bool__', 'int', 'int', {(): 'nb_bool'}), + '__contains__': (SlotSpec('__contains__', 'sq_contains', 'int', + 'int', (object_converter,)),), + '__bool__': (SlotSpec('__bool__', 'nb_bool', 'int', 'int'),), } block = """ /*[clinic input] @@ -3353,42 +3353,63 @@ class Foo "FooObject *" "" Foo.__next__ """, signatures_in_block=3, function_index=2) self.assertIs(function.kind, FunctionKind.SLOT) - self.assertEqual(function.slot.name, "__next__") + self.assertEqual(function.slot.c_slot, "tp_iternext") self.assertEqual(function.c_basename, "Foo_next") self.assertEqual(function.return_converter.type, "PyObject *") self.assertEqual(list(function.parameters), ["self"]) def test_slot_parameters(self): - slot = SlotSpec('__contains__', 'int', 'int', { - (Py_ssize_t_converter,): 'sq_fake', - (object_converter,): 'sq_contains'}) + # __getitem__ is not supported yet: the parameters select the slot. + slots = ( + SlotSpec('__getitem__', 'sq_item', 'PyObject *', 'object', + (Py_ssize_t_converter,)), + SlotSpec('__getitem__', 'mp_subscript', 'PyObject *', 'object', + (object_converter,)), + ) block = """ module foo class Foo "FooObject *" "" - Foo.__contains__ - value: {converter} + Foo.__getitem__ + key: {converter} / """ - errors = [ - ("Foo.__contains__", - "'__contains__' must declare (Py_ssize_t) for sq_fake or " - "(object) for sq_contains; got ()"), - ("Foo.__contains__\n value: int\n /", - "'__contains__' must declare"), - ("Foo.__contains__\n value: object", - "The parameters of '__contains__' must be positional-only"), - ("Foo.__contains__\n value: object = None\n /", - "The parameters of '__contains__' cannot have a default value"), - ] - with mock.patch.dict(SLOTS, {'__contains__': slot}): - for converter in "Py_ssize_t", "object": + supported = unsupported_special_methods - {'__getitem__'} + with (mock.patch.dict(SLOTS, {'__getitem__': slots}), + mock.patch.object(libclinic.dsl_parser, + 'unsupported_special_methods', supported)): + for converter, c_slot in ("Py_ssize_t", "sq_item"), ("object", "mp_subscript"): with self.subTest(converter=converter): function = self.parse_function( block.format(converter=converter), signatures_in_block=3, function_index=2) self.assertIs(function.kind, FunctionKind.SLOT) - self.assertEqual(function.return_converter.type, "int") - self.assertEqual(list(function.parameters), ["self", "value"]) + self.assertEqual(function.slot.c_slot, c_slot) + self.assertEqual(list(function.parameters), ["self", "key"]) + + def test_slot_parameter_errors(self): + # __setattr__ is not planned to be supported; its two slots make + # the error message list the alternatives. + slots = ( + SlotSpec('__setattr__', 'tp_setattro', 'int', 'int', + (object_converter, object_converter)), + SlotSpec('__setattr__', 'tp_setattr', 'int', 'int', + (str_converter, object_converter)), + ) + errors = [ + ("Foo.__setattr__", + "'__setattr__' must declare (object, object) for tp_setattro or " + "(str, object) for tp_setattr; got ()"), + ("Foo.__setattr__\n name: int\n value: object\n /", + "'__setattr__' must declare"), + ("Foo.__setattr__\n name: object\n value: object", + "The parameters of '__setattr__' must be positional-only"), + ("Foo.__setattr__\n name: object\n value: object = None\n /", + "The parameters of '__setattr__' cannot have a default value"), + ] + supported = unsupported_special_methods - {'__setattr__'} + with (mock.patch.dict(SLOTS, {'__setattr__': slots}), + mock.patch.object(libclinic.dsl_parser, + 'unsupported_special_methods', supported)): for body, err in errors: with self.subTest(block=body): body = 'class Foo "FooObject *" "&Foo_Type"\n' + body @@ -3660,10 +3681,10 @@ def test_illegal_c_identifier(self): self.expect_failure(block, err, lineno=2) def test_cannot_convert_special_method(self): - err = "'__len__' is a special method and cannot be converted" + err = "'__iter__' is a special method and cannot be converted" block = """ class T "" "" - T.__len__ + T.__iter__ """ self.expect_failure(block, err, lineno=1) diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c index 39fa804d1dcef42..3186ce92749b1b1 100644 --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -116,10 +116,16 @@ dbm_dealloc(PyObject *self) Py_DECREF(tp); } +/*[clinic input] +@critical_section +_dbm.dbm.__len__ as dbm_length +[clinic start generated code]*/ + static Py_ssize_t -dbm_length_lock_held(PyObject *self) +dbm_length_impl(dbmobject *self) +/*[clinic end generated code: output=d7ea9510562e5f86 input=f912d65e9b06fef5]*/ { - dbmobject *dp = dbmobject_CAST(self); + dbmobject *dp = self; _dbm_state *state = PyType_GetModuleState(Py_TYPE(dp)); assert(state != NULL); if (dp->di_dbm == NULL) { @@ -139,16 +145,6 @@ dbm_length_lock_held(PyObject *self) return dp->di_size; } -static Py_ssize_t -dbm_length(PyObject *self) -{ - Py_ssize_t result; - Py_BEGIN_CRITICAL_SECTION(self); - result = dbm_length_lock_held(self); - Py_END_CRITICAL_SECTION(); - return result; -} - static int dbm_bool_lock_held(PyObject *self) { diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index bdc2d96d8324b0a..0a15012e6eb01a8 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -136,10 +136,16 @@ gdbm_dealloc(PyObject *op) Py_DECREF(tp); } +/*[clinic input] +@critical_section +_gdbm.gdbm.__len__ as gdbm_length +[clinic start generated code]*/ + static Py_ssize_t -gdbm_length_lock_held(PyObject *op) +gdbm_length_impl(gdbmobject *self) +/*[clinic end generated code: output=3ccb956974ed73b5 input=57878f60d264ed98]*/ { - gdbmobject *dp = _gdbmobject_CAST(op); + gdbmobject *dp = self; _gdbm_state *state = PyType_GetModuleState(Py_TYPE(dp)); if (dp->di_dbm == NULL) { PyErr_SetString(state->gdbm_error, "GDBM object has already been closed"); @@ -183,16 +189,6 @@ gdbm_length_lock_held(PyObject *op) return dp->di_size; } -static Py_ssize_t -gdbm_length(PyObject *op) -{ - Py_ssize_t result; - Py_BEGIN_CRITICAL_SECTION(op); - result = gdbm_length_lock_held(op); - Py_END_CRITICAL_SECTION(); - return result; -} - static int gdbm_bool_lock_held(PyObject *op) { diff --git a/Modules/clinic/_dbmmodule.c.h b/Modules/clinic/_dbmmodule.c.h index 14657b5b688221e..4e1a9eaa741e4e5 100644 --- a/Modules/clinic/_dbmmodule.c.h +++ b/Modules/clinic/_dbmmodule.c.h @@ -6,6 +6,21 @@ preserve #include "pycore_modsupport.h" // _PyArg_CheckPositional() #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL() +static Py_ssize_t +dbm_length_impl(dbmobject *self); + +static Py_ssize_t +dbm_length(PyObject *self) +{ + Py_ssize_t return_value = -1; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = dbm_length_impl((dbmobject *)self); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(_dbm_dbm_close__doc__, "close($self, /)\n" "--\n" @@ -249,4 +264,4 @@ dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=f9d904af4ea43d1d input=a9049054013a1b77]*/ +/*[clinic end generated code: output=7ae22ba4a7aeffa2 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_gdbmmodule.c.h b/Modules/clinic/_gdbmmodule.c.h index a6991041ffae041..55e2100751f0906 100644 --- a/Modules/clinic/_gdbmmodule.c.h +++ b/Modules/clinic/_gdbmmodule.c.h @@ -6,6 +6,21 @@ preserve #include "pycore_modsupport.h" // _PyArg_CheckPositional() #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL() +static Py_ssize_t +gdbm_length_impl(gdbmobject *self); + +static Py_ssize_t +gdbm_length(PyObject *self) +{ + Py_ssize_t return_value = -1; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = gdbm_length_impl((gdbmobject *)self); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(_gdbm_gdbm_get__doc__, "get($self, key, default=None, /)\n" "--\n" @@ -401,4 +416,4 @@ dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=ea86a820d233fcad input=a9049054013a1b77]*/ +/*[clinic end generated code: output=36e1ed1cc3523aeb input=a9049054013a1b77]*/ diff --git a/Modules/clinic/mmapmodule.c.h b/Modules/clinic/mmapmodule.c.h index 1343eda42e84ec2..9874ab2b3de0a96 100644 --- a/Modules/clinic/mmapmodule.c.h +++ b/Modules/clinic/mmapmodule.c.h @@ -862,6 +862,21 @@ mmap_mmap_madvise(PyObject *self, PyObject *const *args, Py_ssize_t nargs) #endif /* defined(HAVE_MADVISE) */ +static Py_ssize_t +mmap_length_impl(mmap_object *self); + +static Py_ssize_t +mmap_length(PyObject *self) +{ + Py_ssize_t return_value = -1; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = mmap_length_impl((mmap_object *)self); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + #ifndef MMAP_MMAP_RESIZE_METHODDEF #define MMAP_MMAP_RESIZE_METHODDEF #endif /* !defined(MMAP_MMAP_RESIZE_METHODDEF) */ @@ -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=e52bf0f914c90a96 input=a9049054013a1b77]*/ diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 766e85a1bdba984..124245e4bec1057 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1487,24 +1487,19 @@ mmap_buffer_releasebuf(PyObject *op, Py_buffer *Py_UNUSED(view)) Py_END_CRITICAL_SECTION(); } +/*[clinic input] +@critical_section +mmap.mmap.__len__ as mmap_length +[clinic start generated code]*/ + static Py_ssize_t -mmap_length_lock_held(PyObject *op) +mmap_length_impl(mmap_object *self) +/*[clinic end generated code: output=a63e62615cdd9960 input=b7bbf695dc9b2b66]*/ { - mmap_object *self = mmap_object_CAST(op); CHECK_VALID(-1); return self->size; } -static Py_ssize_t -mmap_length(PyObject *op) -{ - Py_ssize_t result; - Py_BEGIN_CRITICAL_SECTION(op); - result = mmap_length_lock_held(op); - Py_END_CRITICAL_SECTION(); - return result; -} - static PyObject * mmap_item_lock_held(PyObject *op, Py_ssize_t i) { diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index 60730b8828d8438..77daf9b7ff8a704 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -72,7 +72,6 @@ __itruediv__ __ixor__ __le__ -__len__ __lshift__ __lt__ __matmul__ @@ -267,7 +266,8 @@ class DSLParser: critical_section: bool target_critical_section: list[str] disable_fastcall: bool - slot: SlotSpec | None + # The slots which the function can implement (see libclinic.slots). + slots: tuple[SlotSpec, ...] # Line of the file which is being parsed. line_number: int | None from_version_re = re.compile(r'([*/]) +\[from +(.+)\]') @@ -315,7 +315,7 @@ def reset(self) -> None: self.target_critical_section = [] self.disable_fastcall = False self.vectorcall: bool = False - self.slot = None + self.slots = () self.permit_long_summary = False self.permit_long_docstring_body = False @@ -634,8 +634,8 @@ def normalize_function_kind(self, fullname: str) -> None: fail("'__new__' must be a class method!") if self.kind in ACCESSORS and not cls: fail("@getter and @setter must be methods") - slot = SLOTS.get(name) - if slot is not None: + slots = SLOTS.get(name, ()) + if slots: if self.kind is not CALLABLE: fail(f"{name!r} must be a normal method; got '{self.kind}'!") if not cls: @@ -648,9 +648,9 @@ def normalize_function_kind(self, fullname: str) -> None: self.kind = METHOD_NEW elif name == '__init__': self.kind = METHOD_INIT - elif slot is not None: + elif slots: self.kind = SLOT - self.slot = slot + self.slots = slots # Validate @vectorcall usage. if self.vectorcall: @@ -694,8 +694,8 @@ def resolve_return_converter( if self.kind in {METHOD_INIT} | SETTERS: return int_return_converter() if self.kind is SLOT: - assert self.slot is not None - return return_converters[self.slot.return_converter]() + # Every slot of a special method has the same return type. + return return_converters[self.slots[0].return_converter]() return CReturnConverter() def parse_cloned_function(self, names: FunctionNames, existing: str) -> None: @@ -727,7 +727,7 @@ def parse_cloned_function(self, names: FunctionNames, existing: str) -> None: "docstring_line_number": None, "line_number": self.line_number, } - if existing_function.kind is SLOT or self.slot is not None: + if existing_function.kind is SLOT or self.slots: fail("Type slot methods cannot be cloned") if not (existing_function.kind is self.kind and existing_function.coexist == self.coexist): @@ -803,7 +803,9 @@ def state_modulename_name(self, line: str) -> None: forced_text_signature=self.forced_text_signature, line_number=self.line_number, vectorcall=self.vectorcall, - slot=self.slot, + # Replaced by the slot which the parameters select, see + # check_slot_parameters(). + slot=self.slots[0] if self.slots else None, ) self.add_function(func) @@ -921,8 +923,7 @@ def state_parameters_start(self, line: str) -> 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: + if not any(slot.parameters for slot in self.slots): fail(f"{self.function.name!r} methods cannot define parameters") self.parameter_continuation = '' @@ -1677,8 +1678,7 @@ def check_vectorcall_parameters(self, lineno: int) -> None: line_number=lineno) 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 + """Select the slot which the parameters of a slot method implement.""" params = list(func.parameters.values())[1:] for p in params: if not p.is_positional_only(): @@ -1686,10 +1686,14 @@ def check_slot_parameters(self, func: Function) -> None: if p.default is not unspecified: fail(f"The parameters of {func.name!r} cannot have a default value") converters = [p.converter for p in params] - if func.slot.c_slot(converters) is None: - got = ', '.join(c.dsl_name() for c in converters) - fail(f"{func.name!r} must declare " - f"{func.slot.describe_signatures()}; got ({got})") + for slot in self.slots: + if slot.matches(converters): + func.slot = slot + return + got = ', '.join(c.dsl_name() for c in converters) + fail(f"{func.name!r} must declare " + f"{' or '.join(slot.describe() for slot in self.slots)}; " + f"got ({got})") def do_post_block_processing_cleanup(self, lineno: int) -> None: """ diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index ee38b06c2ce4a8a..4398a08c00bb7e4 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -21,39 +21,36 @@ class SlotSpec: # The special method name, e.g. '__next__'. name: str + # The C slot which the function implements, e.g. 'tp_iternext'. + c_slot: str # The C return type of the slot function. return_type: str # The name of the return converter which declares `return_type`. return_converter: str - # Maps the converter classes of the parameters (excluding self) to - # the C slot. - signatures: dict[tuple[type[CConverter], ...], str] + # The converter classes of the parameters, excluding self. + parameters: tuple[type[CConverter], ...] = () @property def error_value(self) -> str: """The value returned by the slot function on error.""" return 'NULL' if self.return_type.endswith('*') else '-1' - @property - def takes_parameters(self) -> bool: - return any(self.signatures) - - def c_slot(self, converters: list[CConverter]) -> str | None: - """Return the C slot for the parameter converters, or None.""" - classes = tuple(type(c) for c in converters) - return self.signatures.get(classes) - - def describe_signatures(self) -> str: - return " or ".join( - f"({', '.join(c.dsl_name() for c in sig) or 'no parameters'})" - f" for {slot}" - for sig, slot in self.signatures.items()) - - -# Maps the name of a special method to its slot. A name listed here must -# not be in dsl_parser.unsupported_special_methods. -SLOTS: Final[dict[str, SlotSpec]] = { - '__next__': SlotSpec('__next__', 'PyObject *', 'object', { - (): 'tp_iternext', - }), + def matches(self, converters: list[CConverter]) -> bool: + """Return whether the parameter converters are those of the slot.""" + return tuple(type(c) for c in converters) == self.parameters + + def describe(self) -> str: + params = ', '.join(c.dsl_name() for c in self.parameters) + return f"({params or 'no parameters'}) for {self.c_slot}" + + +# Maps the name of a special method to the slots it can implement. A name +# listed here must not be in dsl_parser.unsupported_special_methods. +SLOTS: Final[dict[str, tuple[SlotSpec, ...]]] = { + '__next__': ( + SlotSpec('__next__', 'tp_iternext', 'PyObject *', 'object'), + ), + '__len__': ( + SlotSpec('__len__', 'sq_length', 'Py_ssize_t', 'Py_ssize_t'), + ), } From 754ddab6a3292466d7a11633737923a81bbed4cb Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 20 Oct 2026 20:52:34 -0700 Subject: [PATCH 2/6] Address code review --- Lib/test/test_clinic.py | 32 +++++++++++++++++++++----------- Tools/clinic/libclinic/slots.py | 8 +++++--- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 63f8ceccfbaa65f..d68fd0853971a89 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -860,9 +860,15 @@ def test_slot_with_parameters_and_return_type(self): # Slots which take arguments or return something else than an # object are not supported yet; check the machinery with them. slots = { - '__contains__': (SlotSpec('__contains__', 'sq_contains', 'int', - 'int', (object_converter,)),), - '__bool__': (SlotSpec('__bool__', 'nb_bool', 'int', 'int'),), + '__contains__': ( + SlotSpec(name='__contains__', c_slot='sq_contains', + return_type='int', return_converter='int', + parameters=(object_converter,)), + ), + '__bool__': ( + SlotSpec(name='__bool__', c_slot='nb_bool', + return_type='int', return_converter='int'), + ), } block = """ /*[clinic input] @@ -3361,10 +3367,12 @@ class Foo "FooObject *" "" def test_slot_parameters(self): # __getitem__ is not supported yet: the parameters select the slot. slots = ( - SlotSpec('__getitem__', 'sq_item', 'PyObject *', 'object', - (Py_ssize_t_converter,)), - SlotSpec('__getitem__', 'mp_subscript', 'PyObject *', 'object', - (object_converter,)), + SlotSpec(name='__getitem__', c_slot='sq_item', + return_type='PyObject *', return_converter='object', + parameters=(Py_ssize_t_converter,)), + SlotSpec(name='__getitem__', c_slot='mp_subscript', + return_type='PyObject *', return_converter='object', + parameters=(object_converter,)), ) block = """ module foo @@ -3390,10 +3398,12 @@ def test_slot_parameter_errors(self): # __setattr__ is not planned to be supported; its two slots make # the error message list the alternatives. slots = ( - SlotSpec('__setattr__', 'tp_setattro', 'int', 'int', - (object_converter, object_converter)), - SlotSpec('__setattr__', 'tp_setattr', 'int', 'int', - (str_converter, object_converter)), + SlotSpec(name='__setattr__', c_slot='tp_setattro', + return_type='int', return_converter='int', + parameters=(object_converter, object_converter)), + SlotSpec(name='__setattr__', c_slot='tp_setattr', + return_type='int', return_converter='int', + parameters=(str_converter, object_converter)), ) errors = [ ("Foo.__setattr__", diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index 4398a08c00bb7e4..575c15e24f799aa 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -17,7 +17,7 @@ from libclinic.converter import CConverter -@dc.dataclass(frozen=True, slots=True) +@dc.dataclass(frozen=True, slots=True, kw_only=True) class SlotSpec: # The special method name, e.g. '__next__'. name: str @@ -48,9 +48,11 @@ def describe(self) -> str: # listed here must not be in dsl_parser.unsupported_special_methods. SLOTS: Final[dict[str, tuple[SlotSpec, ...]]] = { '__next__': ( - SlotSpec('__next__', 'tp_iternext', 'PyObject *', 'object'), + SlotSpec(name='__next__', c_slot='tp_iternext', + return_type='PyObject *', return_converter='object'), ), '__len__': ( - SlotSpec('__len__', 'sq_length', 'Py_ssize_t', 'Py_ssize_t'), + SlotSpec(name='__len__', c_slot='sq_length', + return_type='Py_ssize_t', return_converter='Py_ssize_t'), ), } From 1e0ba6945691a5be29ac85490f8dd5d16caa1b47 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 20 Oct 2026 20:57:20 -0700 Subject: [PATCH 3/6] Make more compact --- Lib/test/test_clinic.py | 36 +++++++++++++++------------------ Tools/clinic/libclinic/slots.py | 13 ++++-------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index d68fd0853971a89..d33785ce7a03441 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -861,13 +861,11 @@ def test_slot_with_parameters_and_return_type(self): # object are not supported yet; check the machinery with them. slots = { '__contains__': ( - SlotSpec(name='__contains__', c_slot='sq_contains', - return_type='int', return_converter='int', + SlotSpec(return_type='int', return_converter='int', parameters=(object_converter,)), ), '__bool__': ( - SlotSpec(name='__bool__', c_slot='nb_bool', - return_type='int', return_converter='int'), + SlotSpec(return_type='int', return_converter='int'), ), } block = """ @@ -3359,19 +3357,18 @@ class Foo "FooObject *" "" Foo.__next__ """, signatures_in_block=3, function_index=2) self.assertIs(function.kind, FunctionKind.SLOT) - self.assertEqual(function.slot.c_slot, "tp_iternext") + self.assertIs(function.slot, SLOTS["__next__"][0]) self.assertEqual(function.c_basename, "Foo_next") self.assertEqual(function.return_converter.type, "PyObject *") self.assertEqual(list(function.parameters), ["self"]) def test_slot_parameters(self): - # __getitem__ is not supported yet: the parameters select the slot. + # __getitem__ is not supported yet: the parameters select the slot + # (sq_item or mp_subscript). slots = ( - SlotSpec(name='__getitem__', c_slot='sq_item', - return_type='PyObject *', return_converter='object', + SlotSpec(return_type='PyObject *', return_converter='object', parameters=(Py_ssize_t_converter,)), - SlotSpec(name='__getitem__', c_slot='mp_subscript', - return_type='PyObject *', return_converter='object', + SlotSpec(return_type='PyObject *', return_converter='object', parameters=(object_converter,)), ) block = """ @@ -3385,30 +3382,29 @@ class Foo "FooObject *" "" with (mock.patch.dict(SLOTS, {'__getitem__': slots}), mock.patch.object(libclinic.dsl_parser, 'unsupported_special_methods', supported)): - for converter, c_slot in ("Py_ssize_t", "sq_item"), ("object", "mp_subscript"): + for converter, slot in zip(("Py_ssize_t", "object"), slots): with self.subTest(converter=converter): function = self.parse_function( block.format(converter=converter), signatures_in_block=3, function_index=2) self.assertIs(function.kind, FunctionKind.SLOT) - self.assertEqual(function.slot.c_slot, c_slot) + self.assertIs(function.slot, slot) self.assertEqual(list(function.parameters), ["self", "key"]) def test_slot_parameter_errors(self): - # __setattr__ is not planned to be supported; its two slots make - # the error message list the alternatives. + # __setattr__ is not planned to be supported; its two slots + # (tp_setattro and tp_setattr) make the error message list the + # alternatives. slots = ( - SlotSpec(name='__setattr__', c_slot='tp_setattro', - return_type='int', return_converter='int', + SlotSpec(return_type='int', return_converter='int', parameters=(object_converter, object_converter)), - SlotSpec(name='__setattr__', c_slot='tp_setattr', - return_type='int', return_converter='int', + SlotSpec(return_type='int', return_converter='int', parameters=(str_converter, object_converter)), ) errors = [ ("Foo.__setattr__", - "'__setattr__' must declare (object, object) for tp_setattro or " - "(str, object) for tp_setattr; got ()"), + "'__setattr__' must declare (object, object) or (str, object); " + "got ()"), ("Foo.__setattr__\n name: int\n value: object\n /", "'__setattr__' must declare"), ("Foo.__setattr__\n name: object\n value: object", diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index 575c15e24f799aa..5befc76f6db0152 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -19,10 +19,6 @@ @dc.dataclass(frozen=True, slots=True, kw_only=True) class SlotSpec: - # The special method name, e.g. '__next__'. - name: str - # The C slot which the function implements, e.g. 'tp_iternext'. - c_slot: str # The C return type of the slot function. return_type: str # The name of the return converter which declares `return_type`. @@ -40,19 +36,18 @@ def matches(self, converters: list[CConverter]) -> bool: return tuple(type(c) for c in converters) == self.parameters def describe(self) -> str: + """The parameters of the slot as they are declared in the DSL.""" params = ', '.join(c.dsl_name() for c in self.parameters) - return f"({params or 'no parameters'}) for {self.c_slot}" + return f"({params or 'no parameters'})" # Maps the name of a special method to the slots it can implement. A name # listed here must not be in dsl_parser.unsupported_special_methods. SLOTS: Final[dict[str, tuple[SlotSpec, ...]]] = { '__next__': ( - SlotSpec(name='__next__', c_slot='tp_iternext', - return_type='PyObject *', return_converter='object'), + SlotSpec(return_type='PyObject *', return_converter='object'), ), '__len__': ( - SlotSpec(name='__len__', c_slot='sq_length', - return_type='Py_ssize_t', return_converter='Py_ssize_t'), + SlotSpec(return_type='Py_ssize_t', return_converter='Py_ssize_t'), ), } From 33a0522ed5b66376a79d55bf3ca6caac1db3e7a2 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 20 Oct 2026 21:05:11 -0700 Subject: [PATCH 4/6] More compact --- Lib/test/test_clinic.py | 12 ++++++------ Tools/clinic/libclinic/slots.py | 14 +++++++++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index d33785ce7a03441..138f2d723b25dda 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -861,11 +861,11 @@ def test_slot_with_parameters_and_return_type(self): # object are not supported yet; check the machinery with them. slots = { '__contains__': ( - SlotSpec(return_type='int', return_converter='int', + SlotSpec(return_converter='int', parameters=(object_converter,)), ), '__bool__': ( - SlotSpec(return_type='int', return_converter='int'), + SlotSpec(return_converter='int'), ), } block = """ @@ -3366,9 +3366,9 @@ def test_slot_parameters(self): # __getitem__ is not supported yet: the parameters select the slot # (sq_item or mp_subscript). slots = ( - SlotSpec(return_type='PyObject *', return_converter='object', + SlotSpec(return_converter='object', parameters=(Py_ssize_t_converter,)), - SlotSpec(return_type='PyObject *', return_converter='object', + SlotSpec(return_converter='object', parameters=(object_converter,)), ) block = """ @@ -3396,9 +3396,9 @@ def test_slot_parameter_errors(self): # (tp_setattro and tp_setattr) make the error message list the # alternatives. slots = ( - SlotSpec(return_type='int', return_converter='int', + SlotSpec(return_converter='int', parameters=(object_converter, object_converter)), - SlotSpec(return_type='int', return_converter='int', + SlotSpec(return_converter='int', parameters=(str_converter, object_converter)), ) errors = [ diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index 5befc76f6db0152..bffc74c7f2be9de 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -15,17 +15,21 @@ from typing import Final from libclinic.converter import CConverter +from libclinic.return_converters import return_converters @dc.dataclass(frozen=True, slots=True, kw_only=True) class SlotSpec: - # The C return type of the slot function. - return_type: str - # The name of the return converter which declares `return_type`. + # The name of the return converter of the implementation function. return_converter: str # The converter classes of the parameters, excluding self. parameters: tuple[type[CConverter], ...] = () + @property + def return_type(self) -> str: + """The C return type of the slot function.""" + return return_converters[self.return_converter]().type + @property def error_value(self) -> str: """The value returned by the slot function on error.""" @@ -45,9 +49,9 @@ def describe(self) -> str: # listed here must not be in dsl_parser.unsupported_special_methods. SLOTS: Final[dict[str, tuple[SlotSpec, ...]]] = { '__next__': ( - SlotSpec(return_type='PyObject *', return_converter='object'), + SlotSpec(return_converter='object'), ), '__len__': ( - SlotSpec(return_type='Py_ssize_t', return_converter='Py_ssize_t'), + SlotSpec(return_converter='Py_ssize_t'), ), } From 06a5b69bd0e443774f952b31a6cd48db39a7934a Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 20 Oct 2026 21:27:24 -0700 Subject: [PATCH 5/6] Remove unncessary implemeneation --- Lib/test/test_clinic.py | 63 +++++++--------------------- Tools/clinic/libclinic/dsl_parser.py | 41 ++++++------------ Tools/clinic/libclinic/slots.py | 57 ++++++++++--------------- 3 files changed, 49 insertions(+), 112 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 138f2d723b25dda..1f137652fd43d4c 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -23,8 +23,7 @@ import libclinic from libclinic import ClinicError, unspecified, NULL, fail from libclinic.converters import ( - int_converter, object_converter, str_converter, self_converter, - Py_ssize_t_converter) + int_converter, object_converter, str_converter, self_converter) from libclinic.function import ( Module, Class, Function, FunctionKind, Parameter, permute_optional_groups, permute_right_option_groups, @@ -860,13 +859,8 @@ def test_slot_with_parameters_and_return_type(self): # Slots which take arguments or return something else than an # object are not supported yet; check the machinery with them. slots = { - '__contains__': ( - SlotSpec(return_converter='int', - parameters=(object_converter,)), - ), - '__bool__': ( - SlotSpec(return_converter='int'), - ), + '__contains__': SlotSpec(return_type='int'), + '__bool__': SlotSpec(return_type='int'), } block = """ /*[clinic input] @@ -3357,20 +3351,14 @@ class Foo "FooObject *" "" Foo.__next__ """, signatures_in_block=3, function_index=2) self.assertIs(function.kind, FunctionKind.SLOT) - self.assertIs(function.slot, SLOTS["__next__"][0]) + self.assertIs(function.slot, SLOTS["__next__"]) self.assertEqual(function.c_basename, "Foo_next") self.assertEqual(function.return_converter.type, "PyObject *") self.assertEqual(list(function.parameters), ["self"]) def test_slot_parameters(self): - # __getitem__ is not supported yet: the parameters select the slot - # (sq_item or mp_subscript). - slots = ( - SlotSpec(return_converter='object', - parameters=(Py_ssize_t_converter,)), - SlotSpec(return_converter='object', - parameters=(object_converter,)), - ) + # __getitem__ is not supported yet; its parameter gives the slot + # (Py_ssize_t for sq_item, object for mp_subscript) and is not parsed. block = """ module foo class Foo "FooObject *" "" @@ -3378,44 +3366,23 @@ class Foo "FooObject *" "" key: {converter} / """ + errors = [ + ("Foo.__getitem__\n key: object", + "The parameters of '__getitem__' must be positional-only"), + ("Foo.__getitem__\n key: object = None\n /", + "The parameters of '__getitem__' cannot have a default value"), + ] supported = unsupported_special_methods - {'__getitem__'} - with (mock.patch.dict(SLOTS, {'__getitem__': slots}), + with (mock.patch.dict(SLOTS, {'__getitem__': SlotSpec(return_type='PyObject *')}), mock.patch.object(libclinic.dsl_parser, 'unsupported_special_methods', supported)): - for converter, slot in zip(("Py_ssize_t", "object"), slots): + for converter in "Py_ssize_t", "object": with self.subTest(converter=converter): function = self.parse_function( block.format(converter=converter), signatures_in_block=3, function_index=2) self.assertIs(function.kind, FunctionKind.SLOT) - self.assertIs(function.slot, slot) self.assertEqual(list(function.parameters), ["self", "key"]) - - def test_slot_parameter_errors(self): - # __setattr__ is not planned to be supported; its two slots - # (tp_setattro and tp_setattr) make the error message list the - # alternatives. - slots = ( - SlotSpec(return_converter='int', - parameters=(object_converter, object_converter)), - SlotSpec(return_converter='int', - parameters=(str_converter, object_converter)), - ) - errors = [ - ("Foo.__setattr__", - "'__setattr__' must declare (object, object) or (str, object); " - "got ()"), - ("Foo.__setattr__\n name: int\n value: object\n /", - "'__setattr__' must declare"), - ("Foo.__setattr__\n name: object\n value: object", - "The parameters of '__setattr__' must be positional-only"), - ("Foo.__setattr__\n name: object\n value: object = None\n /", - "The parameters of '__setattr__' cannot have a default value"), - ] - supported = unsupported_special_methods - {'__setattr__'} - with (mock.patch.dict(SLOTS, {'__setattr__': slots}), - mock.patch.object(libclinic.dsl_parser, - 'unsupported_special_methods', supported)): for body, err in errors: with self.subTest(block=body): body = 'class Foo "FooObject *" "&Foo_Type"\n' + body @@ -3433,8 +3400,6 @@ def test_slot_errors(self): "@coexist cannot be applied to '__next__'"), ("@vectorcall\nFoo.__next__", "@vectorcall can only be used with __init__ and __new__"), - ("Foo.__next__\n a: int", - "'__next__' methods cannot define parameters"), ("Foo.__next__ -> int", "'Foo.__next__' cannot define a return type"), ("Foo.__next__\n\nReturn the next item.", diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index 77daf9b7ff8a704..022b248abf5eae1 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -266,8 +266,7 @@ class DSLParser: critical_section: bool target_critical_section: list[str] disable_fastcall: bool - # The slots which the function can implement (see libclinic.slots). - slots: tuple[SlotSpec, ...] + slot: SlotSpec | None # Line of the file which is being parsed. line_number: int | None from_version_re = re.compile(r'([*/]) +\[from +(.+)\]') @@ -315,7 +314,7 @@ def reset(self) -> None: self.target_critical_section = [] self.disable_fastcall = False self.vectorcall: bool = False - self.slots = () + self.slot = None self.permit_long_summary = False self.permit_long_docstring_body = False @@ -634,8 +633,8 @@ def normalize_function_kind(self, fullname: str) -> None: fail("'__new__' must be a class method!") if self.kind in ACCESSORS and not cls: fail("@getter and @setter must be methods") - slots = SLOTS.get(name, ()) - if slots: + slot = SLOTS.get(name) + if slot is not None: if self.kind is not CALLABLE: fail(f"{name!r} must be a normal method; got '{self.kind}'!") if not cls: @@ -648,9 +647,9 @@ def normalize_function_kind(self, fullname: str) -> None: self.kind = METHOD_NEW elif name == '__init__': self.kind = METHOD_INIT - elif slots: + elif slot is not None: self.kind = SLOT - self.slots = slots + self.slot = slot # Validate @vectorcall usage. if self.vectorcall: @@ -694,8 +693,8 @@ def resolve_return_converter( if self.kind in {METHOD_INIT} | SETTERS: return int_return_converter() if self.kind is SLOT: - # Every slot of a special method has the same return type. - return return_converters[self.slots[0].return_converter]() + assert self.slot is not None + return self.slot.return_converter() return CReturnConverter() def parse_cloned_function(self, names: FunctionNames, existing: str) -> None: @@ -727,7 +726,7 @@ def parse_cloned_function(self, names: FunctionNames, existing: str) -> None: "docstring_line_number": None, "line_number": self.line_number, } - if existing_function.kind is SLOT or self.slots: + if existing_function.kind is SLOT or self.slot is not None: fail("Type slot methods cannot be cloned") if not (existing_function.kind is self.kind and existing_function.coexist == self.coexist): @@ -803,9 +802,7 @@ def state_modulename_name(self, line: str) -> None: forced_text_signature=self.forced_text_signature, line_number=self.line_number, vectorcall=self.vectorcall, - # Replaced by the slot which the parameters select, see - # check_slot_parameters(). - slot=self.slots[0] if self.slots else None, + slot=self.slot, ) self.add_function(func) @@ -922,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: - if not any(slot.parameters for slot in self.slots): - fail(f"{self.function.name!r} methods cannot define parameters") - self.parameter_continuation = '' return self.next(self.state_parameter, line) @@ -1678,22 +1671,12 @@ def check_vectorcall_parameters(self, lineno: int) -> None: line_number=lineno) def check_slot_parameters(self, func: Function) -> None: - """Select the slot which the parameters of a slot method implement.""" - params = list(func.parameters.values())[1:] - for p in params: + """Check that the parameters of a slot method fit a C signature.""" + for p in list(func.parameters.values())[1:]: if not p.is_positional_only(): fail(f"The parameters of {func.name!r} must be positional-only") if p.default is not unspecified: fail(f"The parameters of {func.name!r} cannot have a default value") - converters = [p.converter for p in params] - for slot in self.slots: - if slot.matches(converters): - func.slot = slot - return - got = ', '.join(c.dsl_name() for c in converters) - fail(f"{func.name!r} must declare " - f"{' or '.join(slot.describe() for slot in self.slots)}; " - f"got ({got})") def do_post_block_processing_cleanup(self, lineno: int) -> None: """ diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index bffc74c7f2be9de..a90157c697e7a7d 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -5,53 +5,42 @@ the implementation function. The wrapper is the place where ``@critical_section`` acquires the lock. -The parameters declared in the DSL only select the C signature; they must be -positional-only, without a default value. When a special method maps to -several slots (``__getitem__`` is ``sq_item`` or ``mp_subscript``), -the converters of its parameters determine which one it implements. +The parameters declared in the DSL only give the C signature of the slot; +they must be positional-only, without a default value. """ from __future__ import annotations import dataclasses as dc from typing import Final -from libclinic.converter import CConverter -from libclinic.return_converters import return_converters +from libclinic.return_converters import CReturnConverter, return_converters + + +# The return converter which declares each C return type of a slot. +_RETURN_CONVERTERS: Final[dict[str, str]] = { + 'PyObject *': 'object', + 'Py_ssize_t': 'Py_ssize_t', + 'int': 'int', +} @dc.dataclass(frozen=True, slots=True, kw_only=True) class SlotSpec: - # The name of the return converter of the implementation function. - return_converter: str - # The converter classes of the parameters, excluding self. - parameters: tuple[type[CConverter], ...] = () - - @property - def return_type(self) -> str: - """The C return type of the slot function.""" - return return_converters[self.return_converter]().type + # The C return type of the slot function. + return_type: str @property def error_value(self) -> str: """The value returned by the slot function on error.""" return 'NULL' if self.return_type.endswith('*') else '-1' - def matches(self, converters: list[CConverter]) -> bool: - """Return whether the parameter converters are those of the slot.""" - return tuple(type(c) for c in converters) == self.parameters - - def describe(self) -> str: - """The parameters of the slot as they are declared in the DSL.""" - params = ', '.join(c.dsl_name() for c in self.parameters) - return f"({params or 'no parameters'})" - - -# Maps the name of a special method to the slots it can implement. A name -# listed here must not be in dsl_parser.unsupported_special_methods. -SLOTS: Final[dict[str, tuple[SlotSpec, ...]]] = { - '__next__': ( - SlotSpec(return_converter='object'), - ), - '__len__': ( - SlotSpec(return_converter='Py_ssize_t'), - ), + def return_converter(self) -> CReturnConverter: + """The return converter of the implementation function.""" + return return_converters[_RETURN_CONVERTERS[self.return_type]]() + + +# Maps the name of a special method to its slot. A name listed here must +# not be in dsl_parser.unsupported_special_methods. +SLOTS: Final[dict[str, SlotSpec]] = { + '__next__': SlotSpec(return_type='PyObject *'), + '__len__': SlotSpec(return_type='Py_ssize_t'), } From cc5b4f7d82006c2d58145e26870b23bbc9603abe Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 20 Oct 2026 21:30:46 -0700 Subject: [PATCH 6/6] Update --- Lib/test/test_clinic.py | 6 +++--- Tools/clinic/libclinic/dsl_parser.py | 2 +- Tools/clinic/libclinic/slots.py | 27 ++++++++++++--------------- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index 1f137652fd43d4c..e3dd4a2be9e410a 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -859,8 +859,8 @@ def test_slot_with_parameters_and_return_type(self): # Slots which take arguments or return something else than an # object are not supported yet; check the machinery with them. slots = { - '__contains__': SlotSpec(return_type='int'), - '__bool__': SlotSpec(return_type='int'), + '__contains__': SlotSpec(return_converter='int'), + '__bool__': SlotSpec(return_converter='int'), } block = """ /*[clinic input] @@ -3373,7 +3373,7 @@ class Foo "FooObject *" "" "The parameters of '__getitem__' cannot have a default value"), ] supported = unsupported_special_methods - {'__getitem__'} - with (mock.patch.dict(SLOTS, {'__getitem__': SlotSpec(return_type='PyObject *')}), + with (mock.patch.dict(SLOTS, {'__getitem__': SlotSpec(return_converter='object')}), mock.patch.object(libclinic.dsl_parser, 'unsupported_special_methods', supported)): for converter in "Py_ssize_t", "object": diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index 022b248abf5eae1..4a38cb6b314fff0 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -694,7 +694,7 @@ def resolve_return_converter( return int_return_converter() if self.kind is SLOT: assert self.slot is not None - return self.slot.return_converter() + return self.slot.make_return_converter() return CReturnConverter() def parse_cloned_function(self, names: FunctionNames, existing: str) -> None: diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index a90157c697e7a7d..95137800fd868c0 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -15,32 +15,29 @@ from libclinic.return_converters import CReturnConverter, return_converters -# The return converter which declares each C return type of a slot. -_RETURN_CONVERTERS: Final[dict[str, str]] = { - 'PyObject *': 'object', - 'Py_ssize_t': 'Py_ssize_t', - 'int': 'int', -} - - @dc.dataclass(frozen=True, slots=True, kw_only=True) class SlotSpec: - # The C return type of the slot function. - return_type: str + # The name of the return converter of the implementation function, + # which declares the C return type of the slot function. + return_converter: str + + @property + def return_type(self) -> str: + """The C return type of the slot function.""" + return self.make_return_converter().type @property def error_value(self) -> str: """The value returned by the slot function on error.""" return 'NULL' if self.return_type.endswith('*') else '-1' - def return_converter(self) -> CReturnConverter: - """The return converter of the implementation function.""" - return return_converters[_RETURN_CONVERTERS[self.return_type]]() + def make_return_converter(self) -> CReturnConverter: + return return_converters[self.return_converter]() # Maps the name of a special method to its slot. A name listed here must # not be in dsl_parser.unsupported_special_methods. SLOTS: Final[dict[str, SlotSpec]] = { - '__next__': SlotSpec(return_type='PyObject *'), - '__len__': SlotSpec(return_type='Py_ssize_t'), + '__next__': SlotSpec(return_converter='object'), + '__len__': SlotSpec(return_converter='Py_ssize_t'), }