Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -3368,8 +3368,8 @@ class Foo "FooObject *" ""
self.assertIn("METH_COEXIST", function.methoddef_flags)

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 *" ""
Expand All @@ -3383,21 +3383,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 = [
Expand Down
22 changes: 10 additions & 12 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,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;
Expand Down Expand Up @@ -1415,17 +1424,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)
{
Expand Down
77 changes: 37 additions & 40 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5026,8 +5026,17 @@ Array_init(PyObject *self, PyObject *args, PyObject *kw)
return 0;
}

/*[clinic input]
@critical_section
_ctypes.Array.__getitem__ as Array_item
myself: self
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=68fa9889c77bb8c9]*/
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself);

Expand Down Expand Up @@ -5056,18 +5065,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
myself: self
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=510935495a857cac]*/
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself);

Expand All @@ -5080,7 +5088,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;
Expand Down Expand Up @@ -5168,7 +5176,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;
Expand All @@ -5185,16 +5193,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;
}

/*[clinic input]
@critical_section
_ctypes.Array.__setitem__ as Array_ass_item
Expand Down Expand Up @@ -5577,8 +5575,22 @@ static PyType_Spec pycsimple_spec = {
/*
PyCPointer_Type
*/
/*[clinic input]
class _ctypes._Pointer "PyObject *" "clinic_state()->PyCPointer_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=452695db70aa7c3e]*/

/*[clinic input]
@critical_section
_ctypes._Pointer.__getitem__ as Pointer_item
myself: self
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=17943427cd04ed08]*/
{
CDataObject *self = _CDataObject_CAST(myself);
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
Expand Down Expand Up @@ -5617,21 +5629,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;
}

/*[clinic input]
class _ctypes._Pointer "PyObject *" "clinic_state()->PyCPointer_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=452695db70aa7c3e]*/

/*[clinic input]
@critical_section
_ctypes._Pointer.__setitem__ as Pointer_ass_item
Expand Down Expand Up @@ -5824,7 +5821,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;
}
Expand Down
47 changes: 46 additions & 1 deletion Modules/_ctypes/clinic/_ctypes.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}

/*[clinic input]
@critical_section
_dbm.dbm.__setitem__ as dbm_ass_sub
Expand Down
22 changes: 10 additions & 12 deletions Modules/_gdbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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

Expand Down
17 changes: 16 additions & 1 deletion Modules/clinic/_collectionsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading