Skip to content

Commit 2467aca

Browse files
committed
gh-114258: Use Argument Clinic for __getitem__
1 parent 50c2491 commit 2467aca

15 files changed

Lines changed: 248 additions & 132 deletions

File tree

‎Lib/test/test_clinic.py‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from libclinic.return_converters import return_converters, int_return_converter
3535
from libclinic.block_parser import Block, BlockParser
3636
from libclinic.codegen import BlockPrinter, Destination
37-
from libclinic.dsl_parser import DSLParser, unsupported_special_methods
37+
from libclinic.dsl_parser import DSLParser
3838
from libclinic.slots import SlotSpec, SLOTS
3939
from libclinic.cli import parse_file, Clinic
4040

@@ -3357,8 +3357,8 @@ class Foo "FooObject *" ""
33573357
self.assertEqual(list(function.parameters), ["self"])
33583358

33593359
def test_slot_parameters(self):
3360-
# __getitem__ is not supported yet; its parameter gives the slot
3361-
# (Py_ssize_t for sq_item, object for mp_subscript) and is not parsed.
3360+
# The parameter of __getitem__ gives the slot (Py_ssize_t for
3361+
# sq_item, object for mp_subscript) and is not parsed.
33623362
block = """
33633363
module foo
33643364
class Foo "FooObject *" ""
@@ -3372,21 +3372,17 @@ class Foo "FooObject *" ""
33723372
("Foo.__getitem__\n key: object = None\n /",
33733373
"The parameters of '__getitem__' cannot have a default value"),
33743374
]
3375-
supported = unsupported_special_methods - {'__getitem__'}
3376-
with (mock.patch.dict(SLOTS, {'__getitem__': SlotSpec(return_converter='object')}),
3377-
mock.patch.object(libclinic.dsl_parser,
3378-
'unsupported_special_methods', supported)):
3379-
for converter in "Py_ssize_t", "object":
3380-
with self.subTest(converter=converter):
3381-
function = self.parse_function(
3382-
block.format(converter=converter),
3383-
signatures_in_block=3, function_index=2)
3384-
self.assertIs(function.kind, FunctionKind.SLOT)
3385-
self.assertEqual(list(function.parameters), ["self", "key"])
3386-
for body, err in errors:
3387-
with self.subTest(block=body):
3388-
body = 'class Foo "FooObject *" "&Foo_Type"\n' + body
3389-
self.expect_failure(body, err)
3375+
for converter in "Py_ssize_t", "object":
3376+
with self.subTest(converter=converter):
3377+
function = self.parse_function(
3378+
block.format(converter=converter),
3379+
signatures_in_block=3, function_index=2)
3380+
self.assertIs(function.kind, FunctionKind.SLOT)
3381+
self.assertEqual(list(function.parameters), ["self", "key"])
3382+
for body, err in errors:
3383+
with self.subTest(block=body):
3384+
body = 'class Foo "FooObject *" "&Foo_Type"\n' + body
3385+
self.expect_failure(body, err)
33903386

33913387
def test_slot_errors(self):
33923388
cases = [

‎Modules/_collectionsmodule.c‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,8 +1380,17 @@ valid_index(Py_ssize_t i, Py_ssize_t limit)
13801380
return (size_t) i < (size_t) limit;
13811381
}
13821382

1383+
/*[clinic input]
1384+
@critical_section
1385+
_collections.deque.__getitem__ as deque_item
1386+
deque: dequeobject
1387+
i: Py_ssize_t
1388+
/
1389+
[clinic start generated code]*/
1390+
13831391
static PyObject *
1384-
deque_item_lock_held(dequeobject *deque, Py_ssize_t i)
1392+
deque_item_impl(dequeobject *deque, Py_ssize_t i)
1393+
/*[clinic end generated code: output=52e62af1427d61ef input=89f14f06b29122a1]*/
13851394
{
13861395
block *b;
13871396
PyObject *item;
@@ -1419,17 +1428,6 @@ deque_item_lock_held(dequeobject *deque, Py_ssize_t i)
14191428
return Py_NewRef(item);
14201429
}
14211430

1422-
static PyObject *
1423-
deque_item(PyObject *self, Py_ssize_t i)
1424-
{
1425-
dequeobject *deque = dequeobject_CAST(self);
1426-
PyObject *result;
1427-
Py_BEGIN_CRITICAL_SECTION(deque);
1428-
result = deque_item_lock_held(deque, i);
1429-
Py_END_CRITICAL_SECTION();
1430-
return result;
1431-
}
1432-
14331431
static int
14341432
deque_del_item(dequeobject *deque, Py_ssize_t i)
14351433
{

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ bytes(cdata)
133133
/*[clinic input]
134134
module _ctypes
135135
class _ctypes.CFuncPtr "PyCFuncPtrObject *" "&PyCFuncPtr_Type"
136+
class _ctypes.Array "CDataObject *" "clinic_state()->PyCArray_Type"
137+
class _ctypes.Pointer "CDataObject *" "clinic_state()->PyCPointer_Type"
136138
[clinic start generated code]*/
137-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=58e8c99474bc631e]*/
139+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=56de8bab30675d93]*/
138140

139141
#define clinic_state() (get_module_state_by_class(cls))
140142
#define clinic_state_sub() (get_module_state_by_class(cls->tp_base))
@@ -5023,8 +5025,17 @@ Array_init(PyObject *self, PyObject *args, PyObject *kw)
50235025
return 0;
50245026
}
50255027

5028+
/*[clinic input]
5029+
@critical_section
5030+
_ctypes.Array.__getitem__ as Array_item
5031+
self as myself: self(type="PyObject *")
5032+
index: Py_ssize_t
5033+
/
5034+
[clinic start generated code]*/
5035+
50265036
static PyObject *
5027-
Array_item_lock_held(PyObject *myself, Py_ssize_t index)
5037+
Array_item_impl(PyObject *myself, Py_ssize_t index)
5038+
/*[clinic end generated code: output=d038162d727d7d9b input=5979a1200a1397ca]*/
50285039
{
50295040
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself);
50305041

@@ -5053,18 +5064,17 @@ Array_item_lock_held(PyObject *myself, Py_ssize_t index)
50535064
index, size, self->b_ptr + offset);
50545065
}
50555066

5056-
static PyObject *
5057-
Array_item(PyObject *myself, Py_ssize_t index)
5058-
{
5059-
PyObject *result;
5060-
Py_BEGIN_CRITICAL_SECTION(myself);
5061-
result = Array_item_lock_held(myself, index);
5062-
Py_END_CRITICAL_SECTION();
5063-
return result;
5064-
}
5067+
/*[clinic input]
5068+
@critical_section
5069+
_ctypes.Array.__getitem__ as Array_subscript
5070+
self as myself: self(type="PyObject *")
5071+
item: object
5072+
/
5073+
[clinic start generated code]*/
50655074

50665075
static PyObject *
5067-
Array_subscript_lock_held(PyObject *myself, PyObject *item)
5076+
Array_subscript_impl(PyObject *myself, PyObject *item)
5077+
/*[clinic end generated code: output=5c88d612ad5c8a1a input=1a68b5a5ba349556]*/
50685078
{
50695079
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself);
50705080

@@ -5077,7 +5087,7 @@ Array_subscript_lock_held(PyObject *myself, PyObject *item)
50775087
return NULL;
50785088
if (i < 0)
50795089
i += self->b_length;
5080-
return Array_item_lock_held(myself, i);
5090+
return Array_item_impl(myself, i);
50815091
}
50825092
else if (PySlice_Check(item)) {
50835093
PyObject *proto;
@@ -5165,7 +5175,7 @@ Array_subscript_lock_held(PyObject *myself, PyObject *item)
51655175

51665176
for (cur = start, i = 0; i < slicelen;
51675177
cur += step, i++) {
5168-
PyObject *v = Array_item_lock_held(myself, cur);
5178+
PyObject *v = Array_item_impl(myself, cur);
51695179
if (v == NULL) {
51705180
Py_DECREF(np);
51715181
return NULL;
@@ -5182,16 +5192,6 @@ Array_subscript_lock_held(PyObject *myself, PyObject *item)
51825192
}
51835193

51845194

5185-
static PyObject *
5186-
Array_subscript(PyObject *myself, PyObject *item)
5187-
{
5188-
PyObject *result;
5189-
Py_BEGIN_CRITICAL_SECTION(myself);
5190-
result = Array_subscript_lock_held(myself, item);
5191-
Py_END_CRITICAL_SECTION();
5192-
return result;
5193-
}
5194-
51955195
static int
51965196
Array_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value)
51975197
{
@@ -5574,8 +5574,17 @@ static PyType_Spec pycsimple_spec = {
55745574
/*
55755575
PyCPointer_Type
55765576
*/
5577+
/*[clinic input]
5578+
@critical_section
5579+
_ctypes.Pointer.__getitem__ as Pointer_item
5580+
self as myself: self(type="PyObject *")
5581+
index: Py_ssize_t
5582+
/
5583+
[clinic start generated code]*/
5584+
55775585
static PyObject *
5578-
Pointer_item_lock_held(PyObject *myself, Py_ssize_t index)
5586+
Pointer_item_impl(PyObject *myself, Py_ssize_t index)
5587+
/*[clinic end generated code: output=4ae9d566e8038853 input=5e1c027271cdbb9f]*/
55795588
{
55805589
CDataObject *self = _CDataObject_CAST(myself);
55815590
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
@@ -5614,16 +5623,6 @@ Pointer_item_lock_held(PyObject *myself, Py_ssize_t index)
56145623
index, size, (char *)((char *)deref + offset));
56155624
}
56165625

5617-
static PyObject *
5618-
Pointer_item(PyObject *self, Py_ssize_t index)
5619-
{
5620-
PyObject *res;
5621-
Py_BEGIN_CRITICAL_SECTION(self);
5622-
res = Pointer_item_lock_held(self, index);
5623-
Py_END_CRITICAL_SECTION();
5624-
return res;
5625-
}
5626-
56275626
static int
56285627
Pointer_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value)
56295628
{
@@ -5815,7 +5814,7 @@ copy_pointer_to_list_lock_held(PyObject *myself, PyObject *np, Py_ssize_t len,
58155814
Py_ssize_t i;
58165815
size_t cur;
58175816
for (cur = start, i = 0; i < len; cur += step, i++) {
5818-
PyObject *v = Pointer_item_lock_held(myself, cur);
5817+
PyObject *v = Pointer_item_impl(myself, cur);
58195818
if (!v) {
58205819
return -1;
58215820
}

‎Modules/_ctypes/clinic/_ctypes.c.h‎

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Modules/_dbmmodule.c‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,20 @@ dbm_bool(PyObject *self)
187187
return result;
188188
}
189189

190+
/*[clinic input]
191+
@critical_section
192+
_dbm.dbm.__getitem__ as dbm_subscript
193+
key: object
194+
/
195+
[clinic start generated code]*/
196+
190197
static PyObject *
191-
dbm_subscript_lock_held(PyObject *self, PyObject *key)
198+
dbm_subscript_impl(dbmobject *self, PyObject *key)
199+
/*[clinic end generated code: output=6278465f3ff37eaa input=e748b684a0870527]*/
192200
{
193201
datum drec, krec;
194202
Py_ssize_t tmp_size;
195-
dbmobject *dp = dbmobject_CAST(self);
203+
dbmobject *dp = self;
196204
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
197205
assert(state != NULL);
198206
if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size)) {
@@ -214,16 +222,6 @@ dbm_subscript_lock_held(PyObject *self, PyObject *key)
214222
return PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
215223
}
216224

217-
static PyObject *
218-
dbm_subscript(PyObject *self, PyObject *key)
219-
{
220-
PyObject *result;
221-
Py_BEGIN_CRITICAL_SECTION(self);
222-
result = dbm_subscript_lock_held(self, key);
223-
Py_END_CRITICAL_SECTION();
224-
return result;
225-
}
226-
227225
static int
228226
dbm_ass_sub_lock_held(PyObject *self, PyObject *v, PyObject *w)
229227
{

‎Modules/_gdbmmodule.c‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,20 @@ parse_datum(PyObject *o, datum *d, const char *failmsg)
250250
return 1;
251251
}
252252

253+
/*[clinic input]
254+
@critical_section
255+
_gdbm.gdbm.__getitem__ as gdbm_subscript
256+
key: object
257+
/
258+
[clinic start generated code]*/
259+
253260
static PyObject *
254-
gdbm_subscript_lock_held(PyObject *op, PyObject *key)
261+
gdbm_subscript_impl(gdbmobject *self, PyObject *key)
262+
/*[clinic end generated code: output=e96388d53678e967 input=8324d90d97ae98f0]*/
255263
{
256264
PyObject *v;
257265
datum drec, krec;
258-
gdbmobject *dp = _gdbmobject_CAST(op);
266+
gdbmobject *dp = self;
259267
_gdbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
260268

261269
if (!parse_datum(key, &krec, NULL)) {
@@ -276,16 +284,6 @@ gdbm_subscript_lock_held(PyObject *op, PyObject *key)
276284
return v;
277285
}
278286

279-
static PyObject *
280-
gdbm_subscript(PyObject *op, PyObject *key)
281-
{
282-
PyObject *result;
283-
Py_BEGIN_CRITICAL_SECTION(op);
284-
result = gdbm_subscript_lock_held(op, key);
285-
Py_END_CRITICAL_SECTION();
286-
return result;
287-
}
288-
289287
/*[clinic input]
290288
_gdbm.gdbm.get
291289

0 commit comments

Comments
 (0)