Skip to content

Commit fe70b06

Browse files
gh-114258: Use Argument Clinic for __mul__/__imul__ (GH-160049)
1 parent 867d16e commit fe70b06

8 files changed

Lines changed: 136 additions & 65 deletions

File tree

‎Modules/_collectionsmodule.c‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,18 @@ deque_clearmethod_impl(dequeobject *deque)
819819
Py_RETURN_NONE;
820820
}
821821

822+
/*[clinic input]
823+
@critical_section
824+
_collections.deque.__imul__ as deque_inplace_repeat
825+
826+
deque: dequeobject
827+
n: Py_ssize_t
828+
/
829+
[clinic start generated code]*/
830+
822831
static PyObject *
823-
deque_inplace_repeat_lock_held(dequeobject *deque, Py_ssize_t n)
832+
deque_inplace_repeat_impl(dequeobject *deque, Py_ssize_t n)
833+
/*[clinic end generated code: output=e9052495a8eb46f9 input=0ebbaedad03b8abf]*/
824834
{
825835
Py_ssize_t i, m, size;
826836
PyObject *seq;
@@ -896,17 +906,6 @@ deque_inplace_repeat_lock_held(dequeobject *deque, Py_ssize_t n)
896906
return (PyObject *)deque;
897907
}
898908

899-
static PyObject *
900-
deque_inplace_repeat(PyObject *self, Py_ssize_t n)
901-
{
902-
dequeobject *deque = dequeobject_CAST(self);
903-
PyObject *result;
904-
Py_BEGIN_CRITICAL_SECTION(deque);
905-
result = deque_inplace_repeat_lock_held(deque, n);
906-
Py_END_CRITICAL_SECTION();
907-
return result;
908-
}
909-
910909
static PyObject *
911910
deque_repeat(PyObject *self, Py_ssize_t n)
912911
{
@@ -921,7 +920,7 @@ deque_repeat(PyObject *self, Py_ssize_t n)
921920
return NULL;
922921
// It's safe to not acquire the per-object lock for new_deque; it's
923922
// invisible to other threads.
924-
rv = deque_inplace_repeat_lock_held(new_deque, n);
923+
rv = deque_inplace_repeat_impl(new_deque, n);
925924
Py_DECREF(new_deque);
926925
return rv;
927926
}

‎Modules/clinic/_collectionsmodule.c.h‎

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

‎Objects/bytearrayobject.c‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,19 @@ bytearray_iconcat(PyObject *op, PyObject *other)
442442
return ret;
443443
}
444444

445+
/*[clinic input]
446+
@critical_section
447+
bytearray.__mul__ as bytearray_repeat
448+
449+
count: Py_ssize_t
450+
/
451+
[clinic start generated code]*/
452+
445453
static PyObject *
446-
bytearray_repeat_lock_held(PyObject *op, Py_ssize_t count)
454+
bytearray_repeat_impl(PyByteArrayObject *self, Py_ssize_t count)
455+
/*[clinic end generated code: output=0889e9ebbe61890a input=3363528e6db1f39b]*/
447456
{
448-
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
449-
PyByteArrayObject *self = _PyByteArray_CAST(op);
457+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
450458
if (count < 0) {
451459
count = 0;
452460
}
@@ -464,21 +472,19 @@ bytearray_repeat_lock_held(PyObject *op, Py_ssize_t count)
464472
return (PyObject *)result;
465473
}
466474

467-
static PyObject *
468-
bytearray_repeat(PyObject *op, Py_ssize_t count)
469-
{
470-
PyObject *ret;
471-
Py_BEGIN_CRITICAL_SECTION(op);
472-
ret = bytearray_repeat_lock_held(op, count);
473-
Py_END_CRITICAL_SECTION();
474-
return ret;
475-
}
475+
/*[clinic input]
476+
@critical_section
477+
bytearray.__imul__ as bytearray_irepeat
478+
479+
count: Py_ssize_t
480+
/
481+
[clinic start generated code]*/
476482

477483
static PyObject *
478-
bytearray_irepeat_lock_held(PyObject *op, Py_ssize_t count)
484+
bytearray_irepeat_impl(PyByteArrayObject *self, Py_ssize_t count)
485+
/*[clinic end generated code: output=4cfee919b3489219 input=e12991c89153490b]*/
479486
{
480-
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
481-
PyByteArrayObject *self = _PyByteArray_CAST(op);
487+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
482488
if (count < 0) {
483489
count = 0;
484490
}
@@ -501,16 +507,6 @@ bytearray_irepeat_lock_held(PyObject *op, Py_ssize_t count)
501507
return Py_NewRef(self);
502508
}
503509

504-
static PyObject *
505-
bytearray_irepeat(PyObject *op, Py_ssize_t count)
506-
{
507-
PyObject *ret;
508-
Py_BEGIN_CRITICAL_SECTION(op);
509-
ret = bytearray_irepeat_lock_held(op, count);
510-
Py_END_CRITICAL_SECTION();
511-
return ret;
512-
}
513-
514510
static PyObject *
515511
bytearray_getitem_lock_held(PyObject *op, Py_ssize_t i)
516512
{

‎Objects/clinic/bytearrayobject.c.h‎

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

‎Objects/clinic/listobject.c.h‎

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

‎Objects/listobject.c‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,18 @@ _PyList_Concat(PyObject *aa, PyObject *bb)
869869
return ret;
870870
}
871871

872+
/*[clinic input]
873+
@critical_section
874+
list.__mul__ as list_repeat
875+
876+
a: self(type="PyListObject *")
877+
n: Py_ssize_t
878+
/
879+
[clinic start generated code]*/
880+
872881
static PyObject *
873-
list_repeat_lock_held(PyListObject *a, Py_ssize_t n)
882+
list_repeat_impl(PyListObject *a, Py_ssize_t n)
883+
/*[clinic end generated code: output=65bb40ed76d3e733 input=dfef16e067b2abb6]*/
874884
{
875885
const Py_ssize_t input_size = Py_SIZE(a);
876886
if (input_size == 0 || n <= 0)
@@ -911,17 +921,6 @@ list_repeat_lock_held(PyListObject *a, Py_ssize_t n)
911921
return (PyObject *) np;
912922
}
913923

914-
static PyObject *
915-
list_repeat(PyObject *aa, Py_ssize_t n)
916-
{
917-
PyObject *ret;
918-
PyListObject *a = (PyListObject *)aa;
919-
Py_BEGIN_CRITICAL_SECTION(a);
920-
ret = list_repeat_lock_held(a, n);
921-
Py_END_CRITICAL_SECTION();
922-
return ret;
923-
}
924-
925924
static void
926925
list_clear_impl(PyListObject *a, bool is_resize)
927926
{
@@ -1171,20 +1170,22 @@ list_inplace_repeat_lock_held(PyListObject *self, Py_ssize_t n)
11711170
return 0;
11721171
}
11731172

1173+
/*[clinic input]
1174+
@critical_section
1175+
list.__imul__ as list_inplace_repeat
1176+
1177+
n: Py_ssize_t
1178+
/
1179+
[clinic start generated code]*/
1180+
11741181
static PyObject *
1175-
list_inplace_repeat(PyObject *_self, Py_ssize_t n)
1182+
list_inplace_repeat_impl(PyListObject *self, Py_ssize_t n)
1183+
/*[clinic end generated code: output=d318a9f047b4fcaf input=530d74de0e4a61f0]*/
11761184
{
1177-
PyObject *ret;
1178-
PyListObject *self = (PyListObject *) _self;
1179-
Py_BEGIN_CRITICAL_SECTION(self);
11801185
if (list_inplace_repeat_lock_held(self, n) < 0) {
1181-
ret = NULL;
1182-
}
1183-
else {
1184-
ret = Py_NewRef(self);
1186+
return NULL;
11851187
}
1186-
Py_END_CRITICAL_SECTION();
1187-
return ret;
1188+
return Py_NewRef(self);
11881189
}
11891190

11901191
/*[clinic input]

‎Tools/clinic/libclinic/dsl_parser.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
__ilshift__
6161
__imatmul__
6262
__imod__
63-
__imul__
6463
__index__
6564
__int__
6665
__invert__
@@ -76,7 +75,6 @@
7675
__lt__
7776
__matmul__
7877
__mod__
79-
__mul__
8078
__neg__
8179
__or__
8280
__pos__

‎Tools/clinic/libclinic/slots.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def make_return_converter(self) -> CReturnConverter:
4040
SLOTS: Final[dict[str, SlotSpec]] = {
4141
'__next__': SlotSpec(return_converter='object'),
4242
'__len__': SlotSpec(return_converter='Py_ssize_t'),
43+
'__mul__': SlotSpec(return_converter='object'),
44+
'__imul__': SlotSpec(return_converter='object'),
4345
'__repr__': SlotSpec(return_converter='object'),
4446
'__setitem__': SlotSpec(return_converter='int'),
4547
}

0 commit comments

Comments
 (0)