Skip to content

Commit c0c2bba

Browse files
AA-Turnerlkollar
authored andcommitted
pythonGH-135763: AC: Use Py_ssize_t(allow_negative=False) (python#138394)
1 parent 9869e76 commit c0c2bba

18 files changed

+152
-126
lines changed

Modules/_bisectmodule.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t
5757
Py_ssize_t mid;
5858
int res;
5959

60-
if (lo < 0) {
61-
PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
62-
return -1;
63-
}
6460
if (hi == -1) {
6561
hi = PySequence_Size(list);
6662
if (hi < 0)
@@ -153,7 +149,7 @@ _bisect.bisect_right -> Py_ssize_t
153149
154150
a: object
155151
x: object
156-
lo: Py_ssize_t = 0
152+
lo: Py_ssize_t(allow_negative=False) = 0
157153
hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
158154
*
159155
key: object = None
@@ -173,7 +169,7 @@ A custom key function can be supplied to customize the sort order.
173169
static Py_ssize_t
174170
_bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x,
175171
Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
176-
/*[clinic end generated code: output=3a4bc09cc7c8a73d input=43071869772dd53a]*/
172+
/*[clinic end generated code: output=3a4bc09cc7c8a73d input=b476bc45667273ac]*/
177173
{
178174
return internal_bisect_right(a, x, lo, hi, key);
179175
}
@@ -183,7 +179,7 @@ _bisect.insort_right
183179
184180
a: object
185181
x: object
186-
lo: Py_ssize_t = 0
182+
lo: Py_ssize_t(allow_negative=False) = 0
187183
hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
188184
*
189185
key: object = None
@@ -201,7 +197,7 @@ A custom key function can be supplied to customize the sort order.
201197
static PyObject *
202198
_bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x,
203199
Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
204-
/*[clinic end generated code: output=ac3bf26d07aedda2 input=f60777d2b6ddb239]*/
200+
/*[clinic end generated code: output=ac3bf26d07aedda2 input=f2caa8abec0763e8]*/
205201
{
206202
PyObject *result, *key_x;
207203
Py_ssize_t index;
@@ -241,10 +237,6 @@ internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t h
241237
Py_ssize_t mid;
242238
int res;
243239

244-
if (lo < 0) {
245-
PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
246-
return -1;
247-
}
248240
if (hi == -1) {
249241
hi = PySequence_Size(list);
250242
if (hi < 0)
@@ -338,7 +330,7 @@ _bisect.bisect_left -> Py_ssize_t
338330
339331
a: object
340332
x: object
341-
lo: Py_ssize_t = 0
333+
lo: Py_ssize_t(allow_negative=False) = 0
342334
hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
343335
*
344336
key: object = None
@@ -358,7 +350,7 @@ A custom key function can be supplied to customize the sort order.
358350
static Py_ssize_t
359351
_bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x,
360352
Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
361-
/*[clinic end generated code: output=70749d6e5cae9284 input=f29c4fe7f9b797c7]*/
353+
/*[clinic end generated code: output=70749d6e5cae9284 input=9b4d49b5ddecfad7]*/
362354
{
363355
return internal_bisect_left(a, x, lo, hi, key);
364356
}
@@ -369,7 +361,7 @@ _bisect.insort_left
369361
370362
a: object
371363
x: object
372-
lo: Py_ssize_t = 0
364+
lo: Py_ssize_t(allow_negative=False) = 0
373365
hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
374366
*
375367
key: object = None
@@ -387,7 +379,7 @@ A custom key function can be supplied to customize the sort order.
387379
static PyObject *
388380
_bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x,
389381
Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
390-
/*[clinic end generated code: output=b1d33e5e7ffff11e input=0a700a82edbd472c]*/
382+
/*[clinic end generated code: output=b1d33e5e7ffff11e input=ff85a79826e22f31]*/
391383
{
392384
PyObject *result, *key_x;
393385
Py_ssize_t index;

Modules/_ctypes/_ctypes.c

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ _ctypes.CDataType.from_buffer as CDataType_from_buffer
859859
type: self
860860
cls: defining_class
861861
obj: object
862-
offset: Py_ssize_t = 0
862+
offset: Py_ssize_t(allow_negative=False) = 0
863863
/
864864
865865
C.from_buffer(object, offset=0) -> C instance
@@ -870,7 +870,7 @@ Create a C instance from a writeable buffer.
870870
static PyObject *
871871
CDataType_from_buffer_impl(PyObject *type, PyTypeObject *cls, PyObject *obj,
872872
Py_ssize_t offset)
873-
/*[clinic end generated code: output=57604e99635abd31 input=0f36cedd105ca28d]*/
873+
/*[clinic end generated code: output=57604e99635abd31 input=8f43e6bc44373180]*/
874874
{
875875
PyObject *mv;
876876
PyObject *result;
@@ -906,13 +906,6 @@ CDataType_from_buffer_impl(PyObject *type, PyTypeObject *cls, PyObject *obj,
906906
return NULL;
907907
}
908908

909-
if (offset < 0) {
910-
PyErr_SetString(PyExc_ValueError,
911-
"offset cannot be negative");
912-
Py_DECREF(mv);
913-
return NULL;
914-
}
915-
916909
if (info->size > buffer->len - offset) {
917910
PyErr_Format(PyExc_ValueError,
918911
"Buffer size too small "
@@ -955,7 +948,7 @@ _ctypes.CDataType.from_buffer_copy as CDataType_from_buffer_copy
955948
type: self
956949
cls: defining_class
957950
buffer: Py_buffer
958-
offset: Py_ssize_t = 0
951+
offset: Py_ssize_t(allow_negative=False) = 0
959952
/
960953
961954
C.from_buffer_copy(object, offset=0) -> C instance
@@ -966,7 +959,7 @@ Create a C instance from a readable buffer.
966959
static PyObject *
967960
CDataType_from_buffer_copy_impl(PyObject *type, PyTypeObject *cls,
968961
Py_buffer *buffer, Py_ssize_t offset)
969-
/*[clinic end generated code: output=c8fc62b03e5cc6fa input=2a81e11b765a6253]*/
962+
/*[clinic end generated code: output=c8fc62b03e5cc6fa input=41f97f512295ceec]*/
970963
{
971964
PyObject *result;
972965

@@ -980,12 +973,6 @@ CDataType_from_buffer_copy_impl(PyObject *type, PyTypeObject *cls,
980973
return NULL;
981974
}
982975

983-
if (offset < 0) {
984-
PyErr_SetString(PyExc_ValueError,
985-
"offset cannot be negative");
986-
return NULL;
987-
}
988-
989976
if (info->size > buffer->len - offset) {
990977
PyErr_Format(PyExc_ValueError,
991978
"Buffer size too small (%zd instead of at least %zd bytes)",

Modules/_ctypes/clinic/_ctypes.c.h

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

Modules/_hashopenssl.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,23 +1017,18 @@ static PyType_Spec HASHobject_type_spec = {
10171017
/*[clinic input]
10181018
_hashlib.HASHXOF.digest
10191019
1020-
length: Py_ssize_t
1020+
length: Py_ssize_t(allow_negative=False)
10211021
10221022
Return the digest value as a bytes object.
10231023
[clinic start generated code]*/
10241024

10251025
static PyObject *
10261026
_hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
1027-
/*[clinic end generated code: output=dcb09335dd2fe908 input=3eb034ce03c55b21]*/
1027+
/*[clinic end generated code: output=dcb09335dd2fe908 input=224d047da2c12a42]*/
10281028
{
10291029
EVP_MD_CTX *temp_ctx;
10301030
PyObject *retval;
10311031

1032-
if (length < 0) {
1033-
PyErr_SetString(PyExc_ValueError, "negative digest length");
1034-
return NULL;
1035-
}
1036-
10371032
if (length == 0) {
10381033
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
10391034
}
@@ -1072,24 +1067,19 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
10721067
/*[clinic input]
10731068
_hashlib.HASHXOF.hexdigest
10741069
1075-
length: Py_ssize_t
1070+
length: Py_ssize_t(allow_negative=False)
10761071
10771072
Return the digest value as a string of hexadecimal digits.
10781073
[clinic start generated code]*/
10791074

10801075
static PyObject *
10811076
_hashlib_HASHXOF_hexdigest_impl(HASHobject *self, Py_ssize_t length)
1082-
/*[clinic end generated code: output=519431cafa014f39 input=0e58f7238adb7ab8]*/
1077+
/*[clinic end generated code: output=519431cafa014f39 input=4a41b8ab5d3bfee2]*/
10831078
{
10841079
unsigned char *digest;
10851080
EVP_MD_CTX *temp_ctx;
10861081
PyObject *retval;
10871082

1088-
if (length < 0) {
1089-
PyErr_SetString(PyExc_ValueError, "negative digest length");
1090-
return NULL;
1091-
}
1092-
10931083
if (length == 0) {
10941084
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
10951085
}

Modules/arraymodule.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ array.array.fromfile
15181518
15191519
cls: defining_class
15201520
f: object
1521-
n: Py_ssize_t
1521+
n: Py_ssize_t(allow_negative=False)
15221522
/
15231523
15241524
Read n objects from the file object f and append them to the end of the array.
@@ -1527,17 +1527,13 @@ Read n objects from the file object f and append them to the end of the array.
15271527
static PyObject *
15281528
array_array_fromfile_impl(arrayobject *self, PyTypeObject *cls, PyObject *f,
15291529
Py_ssize_t n)
1530-
/*[clinic end generated code: output=83a667080b345ebc input=b2b4bdfb7ad4d4ae]*/
1530+
/*[clinic end generated code: output=83a667080b345ebc input=db46b06ac1b6de87]*/
15311531
{
15321532
PyObject *b, *res;
15331533
Py_ssize_t itemsize = self->ob_descr->itemsize;
15341534
Py_ssize_t nbytes;
15351535
int not_enough_bytes;
15361536

1537-
if (n < 0) {
1538-
PyErr_SetString(PyExc_ValueError, "negative count");
1539-
return NULL;
1540-
}
15411537
if (n > PY_SSIZE_T_MAX / itemsize) {
15421538
PyErr_NoMemory();
15431539
return NULL;

Modules/clinic/_bisectmodule.c.h

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

Modules/clinic/_hashopenssl.c.h

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

Modules/clinic/arraymodule.c.h

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

0 commit comments

Comments
 (0)