From 2467acab4c353cc1b662ee0af85700ca7dce5a64 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 21 Oct 2026 10:16:17 -0700 Subject: [PATCH] gh-114258: Use Argument Clinic for ``__getitem__`` --- Lib/test/test_clinic.py | 32 ++++++------ Modules/_collectionsmodule.c | 22 ++++----- Modules/_ctypes/_ctypes.c | 71 +++++++++++++-------------- Modules/_ctypes/clinic/_ctypes.c.h | 47 +++++++++++++++++- Modules/_dbmmodule.c | 22 ++++----- Modules/_gdbmmodule.c | 22 ++++----- Modules/clinic/_collectionsmodule.c.h | 17 ++++++- Modules/clinic/_dbmmodule.c.h | 17 ++++++- Modules/clinic/_gdbmmodule.c.h | 17 ++++++- Modules/clinic/mmapmodule.c.h | 32 +++++++++++- Modules/mmapmodule.c | 39 +++++++-------- Objects/bytearrayobject.c | 23 ++++----- Objects/clinic/bytearrayobject.c.h | 17 ++++++- Tools/clinic/libclinic/dsl_parser.py | 1 - Tools/clinic/libclinic/slots.py | 1 + 15 files changed, 248 insertions(+), 132 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index e3dd4a2be9e410a..bba93f9489e839e 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -34,7 +34,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, unsupported_special_methods + from libclinic.dsl_parser import DSLParser from libclinic.slots import SlotSpec, SLOTS from libclinic.cli import parse_file, Clinic @@ -3357,8 +3357,8 @@ class Foo "FooObject *" "" self.assertEqual(list(function.parameters), ["self"]) def test_slot_parameters(self): - # __getitem__ is not supported yet; its parameter gives the slot - # (Py_ssize_t for sq_item, object for mp_subscript) and is not parsed. + # The parameter of __getitem__ gives the slot (Py_ssize_t for + # sq_item, object for mp_subscript) and is not parsed. block = """ module foo class Foo "FooObject *" "" @@ -3372,21 +3372,17 @@ class Foo "FooObject *" "" ("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__': SlotSpec(return_converter='object')}), - mock.patch.object(libclinic.dsl_parser, - 'unsupported_special_methods', supported)): - 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.assertEqual(list(function.parameters), ["self", "key"]) - for body, err in errors: - with self.subTest(block=body): - body = 'class Foo "FooObject *" "&Foo_Type"\n' + body - self.expect_failure(body, err) + 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.assertEqual(list(function.parameters), ["self", "key"]) + for body, err in errors: + with self.subTest(block=body): + body = 'class Foo "FooObject *" "&Foo_Type"\n' + body + self.expect_failure(body, err) def test_slot_errors(self): cases = [ diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index 19910dbc0a5d421..718bf214d08b42f 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1380,8 +1380,17 @@ valid_index(Py_ssize_t i, Py_ssize_t limit) return (size_t) i < (size_t) limit; } +/*[clinic input] +@critical_section +_collections.deque.__getitem__ as deque_item + deque: dequeobject + i: Py_ssize_t + / +[clinic start generated code]*/ + static PyObject * -deque_item_lock_held(dequeobject *deque, Py_ssize_t i) +deque_item_impl(dequeobject *deque, Py_ssize_t i) +/*[clinic end generated code: output=52e62af1427d61ef input=89f14f06b29122a1]*/ { block *b; PyObject *item; @@ -1419,17 +1428,6 @@ deque_item_lock_held(dequeobject *deque, Py_ssize_t i) return Py_NewRef(item); } -static PyObject * -deque_item(PyObject *self, Py_ssize_t i) -{ - dequeobject *deque = dequeobject_CAST(self); - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(deque); - result = deque_item_lock_held(deque, i); - Py_END_CRITICAL_SECTION(); - return result; -} - static int deque_del_item(dequeobject *deque, Py_ssize_t i) { diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 39ca44790d4244f..38bbbd2d1822a41 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -133,8 +133,10 @@ bytes(cdata) /*[clinic input] module _ctypes class _ctypes.CFuncPtr "PyCFuncPtrObject *" "&PyCFuncPtr_Type" +class _ctypes.Array "CDataObject *" "clinic_state()->PyCArray_Type" +class _ctypes.Pointer "CDataObject *" "clinic_state()->PyCPointer_Type" [clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=58e8c99474bc631e]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=56de8bab30675d93]*/ #define clinic_state() (get_module_state_by_class(cls)) #define clinic_state_sub() (get_module_state_by_class(cls->tp_base)) @@ -5023,8 +5025,17 @@ Array_init(PyObject *self, PyObject *args, PyObject *kw) return 0; } +/*[clinic input] +@critical_section +_ctypes.Array.__getitem__ as Array_item + self as myself: self(type="PyObject *") + index: Py_ssize_t + / +[clinic start generated code]*/ + static PyObject * -Array_item_lock_held(PyObject *myself, Py_ssize_t index) +Array_item_impl(PyObject *myself, Py_ssize_t index) +/*[clinic end generated code: output=d038162d727d7d9b input=5979a1200a1397ca]*/ { _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself); @@ -5053,18 +5064,17 @@ Array_item_lock_held(PyObject *myself, Py_ssize_t index) index, size, self->b_ptr + offset); } -static PyObject * -Array_item(PyObject *myself, Py_ssize_t index) -{ - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(myself); - result = Array_item_lock_held(myself, index); - Py_END_CRITICAL_SECTION(); - return result; -} +/*[clinic input] +@critical_section +_ctypes.Array.__getitem__ as Array_subscript + self as myself: self(type="PyObject *") + item: object + / +[clinic start generated code]*/ static PyObject * -Array_subscript_lock_held(PyObject *myself, PyObject *item) +Array_subscript_impl(PyObject *myself, PyObject *item) +/*[clinic end generated code: output=5c88d612ad5c8a1a input=1a68b5a5ba349556]*/ { _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself); @@ -5077,7 +5087,7 @@ Array_subscript_lock_held(PyObject *myself, PyObject *item) return NULL; if (i < 0) i += self->b_length; - return Array_item_lock_held(myself, i); + return Array_item_impl(myself, i); } else if (PySlice_Check(item)) { PyObject *proto; @@ -5165,7 +5175,7 @@ Array_subscript_lock_held(PyObject *myself, PyObject *item) for (cur = start, i = 0; i < slicelen; cur += step, i++) { - PyObject *v = Array_item_lock_held(myself, cur); + PyObject *v = Array_item_impl(myself, cur); if (v == NULL) { Py_DECREF(np); return NULL; @@ -5182,16 +5192,6 @@ Array_subscript_lock_held(PyObject *myself, PyObject *item) } -static PyObject * -Array_subscript(PyObject *myself, PyObject *item) -{ - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(myself); - result = Array_subscript_lock_held(myself, item); - Py_END_CRITICAL_SECTION(); - return result; -} - static int Array_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value) { @@ -5574,8 +5574,17 @@ static PyType_Spec pycsimple_spec = { /* PyCPointer_Type */ +/*[clinic input] +@critical_section +_ctypes.Pointer.__getitem__ as Pointer_item + self as myself: self(type="PyObject *") + index: Py_ssize_t + / +[clinic start generated code]*/ + static PyObject * -Pointer_item_lock_held(PyObject *myself, Py_ssize_t index) +Pointer_item_impl(PyObject *myself, Py_ssize_t index) +/*[clinic end generated code: output=4ae9d566e8038853 input=5e1c027271cdbb9f]*/ { CDataObject *self = _CDataObject_CAST(myself); _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); @@ -5614,16 +5623,6 @@ Pointer_item_lock_held(PyObject *myself, Py_ssize_t index) index, size, (char *)((char *)deref + offset)); } -static PyObject * -Pointer_item(PyObject *self, Py_ssize_t index) -{ - PyObject *res; - Py_BEGIN_CRITICAL_SECTION(self); - res = Pointer_item_lock_held(self, index); - Py_END_CRITICAL_SECTION(); - return res; -} - static int Pointer_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value) { @@ -5815,7 +5814,7 @@ copy_pointer_to_list_lock_held(PyObject *myself, PyObject *np, Py_ssize_t len, Py_ssize_t i; size_t cur; for (cur = start, i = 0; i < len; cur += step, i++) { - PyObject *v = Pointer_item_lock_held(myself, cur); + PyObject *v = Pointer_item_impl(myself, cur); if (!v) { return -1; } diff --git a/Modules/_ctypes/clinic/_ctypes.c.h b/Modules/_ctypes/clinic/_ctypes.c.h index fa9e30586a51d9a..abd14b9d5eabb3c 100644 --- a/Modules/_ctypes/clinic/_ctypes.c.h +++ b/Modules/_ctypes/clinic/_ctypes.c.h @@ -746,6 +746,36 @@ _ctypes_CFuncPtr_argtypes_get(PyObject *self, void *Py_UNUSED(context)) return return_value; } +static PyObject * +Array_item_impl(PyObject *myself, Py_ssize_t index); + +static PyObject * +Array_item(PyObject *myself, Py_ssize_t index) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(myself); + return_value = Array_item_impl(myself, index); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + +static PyObject * +Array_subscript_impl(PyObject *myself, PyObject *item); + +static PyObject * +Array_subscript(PyObject *myself, PyObject *item) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(myself); + return_value = Array_subscript_impl(myself, item); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + static int _ctypes_Simple_value_set_impl(CDataObject *self, PyObject *value); @@ -804,6 +834,21 @@ Simple_from_outparm(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py } return Simple_from_outparm_impl(self, cls); } + +static PyObject * +Pointer_item_impl(PyObject *myself, Py_ssize_t index); + +static PyObject * +Pointer_item(PyObject *myself, Py_ssize_t index) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(myself); + return_value = Pointer_item_impl(myself, index); + Py_END_CRITICAL_SECTION(); + + return return_value; +} #define _CTYPES_CTYPE_TYPE___POINTER_TYPE___GETSETDEF {"__pointer_type__", (getter)_ctypes_CType_Type___pointer_type___get, (setter)_ctypes_CType_Type___pointer_type___set, NULL}, #define _CTYPES_PYCARRAYTYPE_TYPE_RAW_GETSETDEF {"raw", (getter)_ctypes_PyCArrayType_Type_raw_get, (setter)_ctypes_PyCArrayType_Type_raw_set, NULL}, @@ -818,4 +863,4 @@ Simple_from_outparm(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py #define _CTYPES_SIMPLE_VALUE_GETSETDEF {"value", (getter)_ctypes_Simple_value_get, (setter)_ctypes_Simple_value_set, NULL}, -/*[clinic end generated code: output=fdf9a3519e34b6f4 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a6b5a6ff6c6db097 input=a9049054013a1b77]*/ diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c index 3186ce92749b1b1..d4951344512b5c7 100644 --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -187,12 +187,20 @@ dbm_bool(PyObject *self) return result; } +/*[clinic input] +@critical_section +_dbm.dbm.__getitem__ as dbm_subscript + key: object + / +[clinic start generated code]*/ + static PyObject * -dbm_subscript_lock_held(PyObject *self, PyObject *key) +dbm_subscript_impl(dbmobject *self, PyObject *key) +/*[clinic end generated code: output=6278465f3ff37eaa input=e748b684a0870527]*/ { datum drec, krec; Py_ssize_t tmp_size; - dbmobject *dp = dbmobject_CAST(self); + dbmobject *dp = self; _dbm_state *state = PyType_GetModuleState(Py_TYPE(dp)); assert(state != NULL); if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size)) { @@ -214,16 +222,6 @@ dbm_subscript_lock_held(PyObject *self, PyObject *key) return PyBytes_FromStringAndSize(drec.dptr, drec.dsize); } -static PyObject * -dbm_subscript(PyObject *self, PyObject *key) -{ - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(self); - result = dbm_subscript_lock_held(self, key); - Py_END_CRITICAL_SECTION(); - return result; -} - static int dbm_ass_sub_lock_held(PyObject *self, PyObject *v, PyObject *w) { diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index 0a15012e6eb01a8..60af35e11b3a953 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -250,12 +250,20 @@ parse_datum(PyObject *o, datum *d, const char *failmsg) return 1; } +/*[clinic input] +@critical_section +_gdbm.gdbm.__getitem__ as gdbm_subscript + key: object + / +[clinic start generated code]*/ + static PyObject * -gdbm_subscript_lock_held(PyObject *op, PyObject *key) +gdbm_subscript_impl(gdbmobject *self, PyObject *key) +/*[clinic end generated code: output=e96388d53678e967 input=8324d90d97ae98f0]*/ { PyObject *v; datum drec, krec; - gdbmobject *dp = _gdbmobject_CAST(op); + gdbmobject *dp = self; _gdbm_state *state = PyType_GetModuleState(Py_TYPE(dp)); if (!parse_datum(key, &krec, NULL)) { @@ -276,16 +284,6 @@ gdbm_subscript_lock_held(PyObject *op, PyObject *key) return v; } -static PyObject * -gdbm_subscript(PyObject *op, PyObject *key) -{ - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(op); - result = gdbm_subscript_lock_held(op, key); - Py_END_CRITICAL_SECTION(); - return result; -} - /*[clinic input] _gdbm.gdbm.get diff --git a/Modules/clinic/_collectionsmodule.c.h b/Modules/clinic/_collectionsmodule.c.h index 6c60678a6fbd51a..5a9c33e5c1da4b0 100644 --- a/Modules/clinic/_collectionsmodule.c.h +++ b/Modules/clinic/_collectionsmodule.c.h @@ -410,6 +410,21 @@ deque_insert(PyObject *deque, PyObject *const *args, Py_ssize_t nargs) return return_value; } +static PyObject * +deque_item_impl(dequeobject *deque, Py_ssize_t i); + +static PyObject * +deque_item(PyObject *deque, Py_ssize_t i) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(deque); + return_value = deque_item_impl((dequeobject *)deque, i); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(deque_remove__doc__, "remove($self, value, /)\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=4c362ea1fe477edf input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_dbmmodule.c.h b/Modules/clinic/_dbmmodule.c.h index 4e1a9eaa741e4e5..7fa05cacab3ef8c 100644 --- a/Modules/clinic/_dbmmodule.c.h +++ b/Modules/clinic/_dbmmodule.c.h @@ -21,6 +21,21 @@ dbm_length(PyObject *self) return return_value; } +static PyObject * +dbm_subscript_impl(dbmobject *self, PyObject *key); + +static PyObject * +dbm_subscript(PyObject *self, PyObject *key) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = dbm_subscript_impl((dbmobject *)self, key); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(_dbm_dbm_close__doc__, "close($self, /)\n" "--\n" @@ -264,4 +279,4 @@ dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=7ae22ba4a7aeffa2 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f3831b4846089429 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_gdbmmodule.c.h b/Modules/clinic/_gdbmmodule.c.h index 55e2100751f0906..2fd8d192d5f750c 100644 --- a/Modules/clinic/_gdbmmodule.c.h +++ b/Modules/clinic/_gdbmmodule.c.h @@ -21,6 +21,21 @@ gdbm_length(PyObject *self) return return_value; } +static PyObject * +gdbm_subscript_impl(gdbmobject *self, PyObject *key); + +static PyObject * +gdbm_subscript(PyObject *self, PyObject *key) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = gdbm_subscript_impl((gdbmobject *)self, key); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(_gdbm_gdbm_get__doc__, "get($self, key, default=None, /)\n" "--\n" @@ -416,4 +431,4 @@ dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=36e1ed1cc3523aeb input=a9049054013a1b77]*/ +/*[clinic end generated code: output=fd78ee3e670bd0ce input=a9049054013a1b77]*/ diff --git a/Modules/clinic/mmapmodule.c.h b/Modules/clinic/mmapmodule.c.h index 9874ab2b3de0a96..368ca6063ec17ec 100644 --- a/Modules/clinic/mmapmodule.c.h +++ b/Modules/clinic/mmapmodule.c.h @@ -877,6 +877,36 @@ mmap_length(PyObject *self) return return_value; } +static PyObject * +mmap_item_impl(mmap_object *self, Py_ssize_t i); + +static PyObject * +mmap_item(PyObject *self, Py_ssize_t i) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = mmap_item_impl((mmap_object *)self, i); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + +static PyObject * +mmap_subscript_impl(mmap_object *self, PyObject *item); + +static PyObject * +mmap_subscript(PyObject *self, PyObject *item) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = mmap_subscript_impl((mmap_object *)self, item); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + #ifndef MMAP_MMAP_RESIZE_METHODDEF #define MMAP_MMAP_RESIZE_METHODDEF #endif /* !defined(MMAP_MMAP_RESIZE_METHODDEF) */ @@ -892,4 +922,4 @@ mmap_length(PyObject *self) #ifndef MMAP_MMAP_MADVISE_METHODDEF #define MMAP_MMAP_MADVISE_METHODDEF #endif /* !defined(MMAP_MMAP_MADVISE_METHODDEF) */ -/*[clinic end generated code: output=e52bf0f914c90a96 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a5c293446053b503 input=a9049054013a1b77]*/ diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 124245e4bec1057..77353b7f45117b4 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -1500,10 +1500,17 @@ mmap_length_impl(mmap_object *self) return self->size; } +/*[clinic input] +@critical_section +mmap.mmap.__getitem__ as mmap_item + i: Py_ssize_t + / +[clinic start generated code]*/ + static PyObject * -mmap_item_lock_held(PyObject *op, Py_ssize_t i) +mmap_item_impl(mmap_object *self, Py_ssize_t i) +/*[clinic end generated code: output=70bd231b581a7bc1 input=af6ad504b5fcd499]*/ { - mmap_object *self = mmap_object_CAST(op); CHECK_VALID(NULL); if (i < 0 || i >= self->size) { PyErr_SetString(PyExc_IndexError, "mmap index out of range"); @@ -1517,19 +1524,17 @@ mmap_item_lock_held(PyObject *op, Py_ssize_t i) return PyBytes_FromStringAndSize(&dest, 1); } -static PyObject * -mmap_item(PyObject *op, Py_ssize_t i) { - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(op); - result = mmap_item_lock_held(op, i); - Py_END_CRITICAL_SECTION(); - return result; -} +/*[clinic input] +@critical_section +mmap.mmap.__getitem__ as mmap_subscript + item: object + / +[clinic start generated code]*/ static PyObject * -mmap_subscript_lock_held(PyObject *op, PyObject *item) +mmap_subscript_impl(mmap_object *self, PyObject *item) +/*[clinic end generated code: output=cd6ccc1eb3399f98 input=72f708669823dbba]*/ { - mmap_object *self = mmap_object_CAST(op); CHECK_VALID(NULL); if (PyIndex_Check(item)) { Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); @@ -1589,16 +1594,6 @@ mmap_subscript_lock_held(PyObject *op, PyObject *item) } } -static PyObject * -mmap_subscript(PyObject *op, PyObject *item) -{ - PyObject *result; - Py_BEGIN_CRITICAL_SECTION(op); - result = mmap_subscript_lock_held(op, item); - Py_END_CRITICAL_SECTION(); - return result; -} - static int mmap_ass_item_lock_held(PyObject *op, Py_ssize_t i, PyObject *v) { diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 429b8d993867f0c..a229ec207f8a855 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -511,11 +511,18 @@ bytearray_irepeat(PyObject *op, Py_ssize_t count) return ret; } +/*[clinic input] +@critical_section +bytearray.__getitem__ as bytearray_getitem + i: Py_ssize_t + / +[clinic start generated code]*/ + static PyObject * -bytearray_getitem_lock_held(PyObject *op, Py_ssize_t i) +bytearray_getitem_impl(PyByteArrayObject *self, Py_ssize_t i) +/*[clinic end generated code: output=743cb7a5f892f7ed input=f90e2c70f80e2917]*/ { - _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); - PyByteArrayObject *self = _PyByteArray_CAST(op); + _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); if (i < 0 || i >= Py_SIZE(self)) { PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); return NULL; @@ -523,16 +530,6 @@ bytearray_getitem_lock_held(PyObject *op, Py_ssize_t i) return _PyLong_FromUnsignedChar((unsigned char)(self->ob_start[i])); } -static PyObject * -bytearray_getitem(PyObject *op, Py_ssize_t i) -{ - PyObject *ret; - Py_BEGIN_CRITICAL_SECTION(op); - ret = bytearray_getitem_lock_held(op, i); - Py_END_CRITICAL_SECTION(); - return ret; -} - static PyObject * bytearray_subscript_lock_held(PyObject *op, PyObject *index) { diff --git a/Objects/clinic/bytearrayobject.c.h b/Objects/clinic/bytearrayobject.c.h index 9c8ba841200f306..533f7e055eb2fe2 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_getitem_impl(PyByteArrayObject *self, Py_ssize_t i); + +static PyObject * +bytearray_getitem(PyObject *self, Py_ssize_t i) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = bytearray_getitem_impl((PyByteArrayObject *)self, i); + 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=11fb050a5f46470b input=a9049054013a1b77]*/ diff --git a/Tools/clinic/libclinic/dsl_parser.py b/Tools/clinic/libclinic/dsl_parser.py index 4a38cb6b314fff0..8c8e91a3f6b4cf8 100644 --- a/Tools/clinic/libclinic/dsl_parser.py +++ b/Tools/clinic/libclinic/dsl_parser.py @@ -51,7 +51,6 @@ __ge__ __getattr__ __getattribute__ -__getitem__ __gt__ __hash__ __iadd__ diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index 95137800fd868c0..e416ebd0f444d87 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -40,4 +40,5 @@ def make_return_converter(self) -> CReturnConverter: SLOTS: Final[dict[str, SlotSpec]] = { '__next__': SlotSpec(return_converter='object'), '__len__': SlotSpec(return_converter='Py_ssize_t'), + '__getitem__': SlotSpec(return_converter='object'), }