diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c index 9b146265445d9a..3a1491e5b96f29 100644 --- a/Modules/_bisectmodule.c +++ b/Modules/_bisectmodule.c @@ -57,10 +57,6 @@ internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t Py_ssize_t mid; int res; - if (lo < 0) { - PyErr_SetString(PyExc_ValueError, "lo must be non-negative"); - return -1; - } if (hi == -1) { hi = PySequence_Size(list); if (hi < 0) @@ -153,7 +149,7 @@ _bisect.bisect_right -> Py_ssize_t a: object x: object - lo: Py_ssize_t = 0 + lo: Py_ssize_t(allow_negative=False) = 0 hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None * key: object = None @@ -173,7 +169,7 @@ A custom key function can be supplied to customize the sort order. static Py_ssize_t _bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x, Py_ssize_t lo, Py_ssize_t hi, PyObject *key) -/*[clinic end generated code: output=3a4bc09cc7c8a73d input=43071869772dd53a]*/ +/*[clinic end generated code: output=3a4bc09cc7c8a73d input=b476bc45667273ac]*/ { return internal_bisect_right(a, x, lo, hi, key); } @@ -183,7 +179,7 @@ _bisect.insort_right a: object x: object - lo: Py_ssize_t = 0 + lo: Py_ssize_t(allow_negative=False) = 0 hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None * key: object = None @@ -201,7 +197,7 @@ A custom key function can be supplied to customize the sort order. static PyObject * _bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x, Py_ssize_t lo, Py_ssize_t hi, PyObject *key) -/*[clinic end generated code: output=ac3bf26d07aedda2 input=f60777d2b6ddb239]*/ +/*[clinic end generated code: output=ac3bf26d07aedda2 input=f2caa8abec0763e8]*/ { PyObject *result, *key_x; Py_ssize_t index; @@ -241,10 +237,6 @@ internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t h Py_ssize_t mid; int res; - if (lo < 0) { - PyErr_SetString(PyExc_ValueError, "lo must be non-negative"); - return -1; - } if (hi == -1) { hi = PySequence_Size(list); if (hi < 0) @@ -338,7 +330,7 @@ _bisect.bisect_left -> Py_ssize_t a: object x: object - lo: Py_ssize_t = 0 + lo: Py_ssize_t(allow_negative=False) = 0 hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None * key: object = None @@ -358,7 +350,7 @@ A custom key function can be supplied to customize the sort order. static Py_ssize_t _bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x, Py_ssize_t lo, Py_ssize_t hi, PyObject *key) -/*[clinic end generated code: output=70749d6e5cae9284 input=f29c4fe7f9b797c7]*/ +/*[clinic end generated code: output=70749d6e5cae9284 input=9b4d49b5ddecfad7]*/ { return internal_bisect_left(a, x, lo, hi, key); } @@ -369,7 +361,7 @@ _bisect.insort_left a: object x: object - lo: Py_ssize_t = 0 + lo: Py_ssize_t(allow_negative=False) = 0 hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None * key: object = None @@ -387,7 +379,7 @@ A custom key function can be supplied to customize the sort order. static PyObject * _bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x, Py_ssize_t lo, Py_ssize_t hi, PyObject *key) -/*[clinic end generated code: output=b1d33e5e7ffff11e input=0a700a82edbd472c]*/ +/*[clinic end generated code: output=b1d33e5e7ffff11e input=ff85a79826e22f31]*/ { PyObject *result, *key_x; Py_ssize_t index; diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 4bd3e380b3bc4b..914833a3f39808 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -859,7 +859,7 @@ _ctypes.CDataType.from_buffer as CDataType_from_buffer type: self cls: defining_class obj: object - offset: Py_ssize_t = 0 + offset: Py_ssize_t(allow_negative=False) = 0 / C.from_buffer(object, offset=0) -> C instance @@ -870,7 +870,7 @@ Create a C instance from a writeable buffer. static PyObject * CDataType_from_buffer_impl(PyObject *type, PyTypeObject *cls, PyObject *obj, Py_ssize_t offset) -/*[clinic end generated code: output=57604e99635abd31 input=0f36cedd105ca28d]*/ +/*[clinic end generated code: output=57604e99635abd31 input=8f43e6bc44373180]*/ { PyObject *mv; PyObject *result; @@ -906,13 +906,6 @@ CDataType_from_buffer_impl(PyObject *type, PyTypeObject *cls, PyObject *obj, return NULL; } - if (offset < 0) { - PyErr_SetString(PyExc_ValueError, - "offset cannot be negative"); - Py_DECREF(mv); - return NULL; - } - if (info->size > buffer->len - offset) { PyErr_Format(PyExc_ValueError, "Buffer size too small " @@ -955,7 +948,7 @@ _ctypes.CDataType.from_buffer_copy as CDataType_from_buffer_copy type: self cls: defining_class buffer: Py_buffer - offset: Py_ssize_t = 0 + offset: Py_ssize_t(allow_negative=False) = 0 / C.from_buffer_copy(object, offset=0) -> C instance @@ -966,7 +959,7 @@ Create a C instance from a readable buffer. static PyObject * CDataType_from_buffer_copy_impl(PyObject *type, PyTypeObject *cls, Py_buffer *buffer, Py_ssize_t offset) -/*[clinic end generated code: output=c8fc62b03e5cc6fa input=2a81e11b765a6253]*/ +/*[clinic end generated code: output=c8fc62b03e5cc6fa input=41f97f512295ceec]*/ { PyObject *result; @@ -980,12 +973,6 @@ CDataType_from_buffer_copy_impl(PyObject *type, PyTypeObject *cls, return NULL; } - if (offset < 0) { - PyErr_SetString(PyExc_ValueError, - "offset cannot be negative"); - return NULL; - } - if (info->size > buffer->len - offset) { PyErr_Format(PyExc_ValueError, "Buffer size too small (%zd instead of at least %zd bytes)", diff --git a/Modules/_ctypes/clinic/_ctypes.c.h b/Modules/_ctypes/clinic/_ctypes.c.h index cf2e3fa21072ae..529872f0f17ebe 100644 --- a/Modules/_ctypes/clinic/_ctypes.c.h +++ b/Modules/_ctypes/clinic/_ctypes.c.h @@ -176,6 +176,11 @@ CDataType_from_buffer(PyObject *type, PyTypeObject *cls, PyObject *const *args, goto exit; } offset = ival; + if (offset < 0) { + PyErr_SetString(PyExc_ValueError, + "offset cannot be negative"); + goto exit; + } } skip_optional_posonly: return_value = CDataType_from_buffer_impl(type, cls, obj, offset); @@ -242,6 +247,11 @@ CDataType_from_buffer_copy(PyObject *type, PyTypeObject *cls, PyObject *const *a goto exit; } offset = ival; + if (offset < 0) { + PyErr_SetString(PyExc_ValueError, + "offset cannot be negative"); + goto exit; + } } skip_optional_posonly: return_value = CDataType_from_buffer_copy_impl(type, cls, &buffer, offset); @@ -1042,4 +1052,4 @@ Simple_from_outparm(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py } return Simple_from_outparm_impl(self, cls); } -/*[clinic end generated code: output=536c9bcf4e05913e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=22105663d71237ca input=a9049054013a1b77]*/ diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index a6496d0f04f2d0..d34dfad33ff666 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -1017,23 +1017,18 @@ static PyType_Spec HASHobject_type_spec = { /*[clinic input] _hashlib.HASHXOF.digest - length: Py_ssize_t + length: Py_ssize_t(allow_negative=False) Return the digest value as a bytes object. [clinic start generated code]*/ static PyObject * _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length) -/*[clinic end generated code: output=dcb09335dd2fe908 input=3eb034ce03c55b21]*/ +/*[clinic end generated code: output=dcb09335dd2fe908 input=224d047da2c12a42]*/ { EVP_MD_CTX *temp_ctx; PyObject *retval; - if (length < 0) { - PyErr_SetString(PyExc_ValueError, "negative digest length"); - return NULL; - } - if (length == 0) { return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); } @@ -1072,24 +1067,19 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length) /*[clinic input] _hashlib.HASHXOF.hexdigest - length: Py_ssize_t + length: Py_ssize_t(allow_negative=False) Return the digest value as a string of hexadecimal digits. [clinic start generated code]*/ static PyObject * _hashlib_HASHXOF_hexdigest_impl(HASHobject *self, Py_ssize_t length) -/*[clinic end generated code: output=519431cafa014f39 input=0e58f7238adb7ab8]*/ +/*[clinic end generated code: output=519431cafa014f39 input=4a41b8ab5d3bfee2]*/ { unsigned char *digest; EVP_MD_CTX *temp_ctx; PyObject *retval; - if (length < 0) { - PyErr_SetString(PyExc_ValueError, "negative digest length"); - return NULL; - } - if (length == 0) { return Py_GetConstant(Py_CONSTANT_EMPTY_STR); } diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index d9ac5b97f8258f..d97cf7af767ca3 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1518,7 +1518,7 @@ array.array.fromfile cls: defining_class f: object - n: Py_ssize_t + n: Py_ssize_t(allow_negative=False) / 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. static PyObject * array_array_fromfile_impl(arrayobject *self, PyTypeObject *cls, PyObject *f, Py_ssize_t n) -/*[clinic end generated code: output=83a667080b345ebc input=b2b4bdfb7ad4d4ae]*/ +/*[clinic end generated code: output=83a667080b345ebc input=db46b06ac1b6de87]*/ { PyObject *b, *res; Py_ssize_t itemsize = self->ob_descr->itemsize; Py_ssize_t nbytes; int not_enough_bytes; - if (n < 0) { - PyErr_SetString(PyExc_ValueError, "negative count"); - return NULL; - } if (n > PY_SSIZE_T_MAX / itemsize) { PyErr_NoMemory(); return NULL; diff --git a/Modules/clinic/_bisectmodule.c.h b/Modules/clinic/_bisectmodule.c.h index 314208bc41d0c1..8f3492cd54b5f2 100644 --- a/Modules/clinic/_bisectmodule.c.h +++ b/Modules/clinic/_bisectmodule.c.h @@ -93,6 +93,11 @@ _bisect_bisect_right(PyObject *module, PyObject *const *args, Py_ssize_t nargs, goto exit; } lo = ival; + if (lo < 0) { + PyErr_SetString(PyExc_ValueError, + "lo cannot be negative"); + goto exit; + } } if (!--noptargs) { goto skip_optional_pos; @@ -203,6 +208,11 @@ _bisect_insort_right(PyObject *module, PyObject *const *args, Py_ssize_t nargs, goto exit; } lo = ival; + if (lo < 0) { + PyErr_SetString(PyExc_ValueError, + "lo cannot be negative"); + goto exit; + } } if (!--noptargs) { goto skip_optional_pos; @@ -312,6 +322,11 @@ _bisect_bisect_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, P goto exit; } lo = ival; + if (lo < 0) { + PyErr_SetString(PyExc_ValueError, + "lo cannot be negative"); + goto exit; + } } if (!--noptargs) { goto skip_optional_pos; @@ -422,6 +437,11 @@ _bisect_insort_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, P goto exit; } lo = ival; + if (lo < 0) { + PyErr_SetString(PyExc_ValueError, + "lo cannot be negative"); + goto exit; + } } if (!--noptargs) { goto skip_optional_pos; @@ -446,4 +466,4 @@ _bisect_insort_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, P exit: return return_value; } -/*[clinic end generated code: output=729385c6a23828ab input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a3c44ed440dd6d81 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_hashopenssl.c.h b/Modules/clinic/_hashopenssl.c.h index 3d81a6dcce1440..7ae7be185eceb5 100644 --- a/Modules/clinic/_hashopenssl.c.h +++ b/Modules/clinic/_hashopenssl.c.h @@ -150,6 +150,11 @@ _hashlib_HASHXOF_digest(PyObject *self, PyObject *const *args, Py_ssize_t nargs, goto exit; } length = ival; + if (length < 0) { + PyErr_SetString(PyExc_ValueError, + "length cannot be negative"); + goto exit; + } } return_value = _hashlib_HASHXOF_digest_impl((HASHobject *)self, length); @@ -223,6 +228,11 @@ _hashlib_HASHXOF_hexdigest(PyObject *self, PyObject *const *args, Py_ssize_t nar goto exit; } length = ival; + if (length < 0) { + PyErr_SetString(PyExc_ValueError, + "length cannot be negative"); + goto exit; + } } return_value = _hashlib_HASHXOF_hexdigest_impl((HASHobject *)self, length); @@ -1976,4 +1986,4 @@ _hashlib_compare_digest(PyObject *module, PyObject *const *args, Py_ssize_t narg #ifndef _HASHLIB_OPENSSL_SHAKE_256_METHODDEF #define _HASHLIB_OPENSSL_SHAKE_256_METHODDEF #endif /* !defined(_HASHLIB_OPENSSL_SHAKE_256_METHODDEF) */ -/*[clinic end generated code: output=cd5ff436f6dc2938 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=9ba35fcc33795b1e input=a9049054013a1b77]*/ diff --git a/Modules/clinic/arraymodule.c.h b/Modules/clinic/arraymodule.c.h index 97e5ca771f3a90..2648583c654a04 100644 --- a/Modules/clinic/arraymodule.c.h +++ b/Modules/clinic/arraymodule.c.h @@ -419,6 +419,11 @@ array_array_fromfile(PyObject *self, PyTypeObject *cls, PyObject *const *args, P goto exit; } n = ival; + if (n < 0) { + PyErr_SetString(PyExc_ValueError, + "n cannot be negative"); + goto exit; + } } return_value = array_array_fromfile_impl((arrayobject *)self, cls, f, n); @@ -773,4 +778,4 @@ array_arrayiterator___setstate__(PyObject *self, PyObject *state) return return_value; } -/*[clinic end generated code: output=dd49451ac1cc3f39 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=c993c3598085840e input=a9049054013a1b77]*/ diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index 0af82e7eb05be8..49816bfcb42fec 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -345,6 +345,11 @@ itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs) goto exit; } n = ival; + if (n < 0) { + PyErr_SetString(PyExc_ValueError, + "n cannot be negative"); + goto exit; + } } skip_optional: return_value = itertools_tee_impl(module, iterable, n); @@ -569,6 +574,11 @@ itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) goto exit; } r = ival; + if (r < 0) { + PyErr_SetString(PyExc_ValueError, + "r cannot be negative"); + goto exit; + } } return_value = itertools_combinations_impl(type, iterable, r); @@ -643,6 +653,11 @@ itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyOb goto exit; } r = ival; + if (r < 0) { + PyErr_SetString(PyExc_ValueError, + "r cannot be negative"); + goto exit; + } } return_value = itertools_combinations_with_replacement_impl(type, iterable, r); @@ -965,4 +980,4 @@ itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=999758202a532e0a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=7f385837b13edbeb input=a9049054013a1b77]*/ diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h index df4f802ff0bdc9..455c938afc75f0 100644 --- a/Modules/clinic/posixmodule.c.h +++ b/Modules/clinic/posixmodule.c.h @@ -8689,6 +8689,11 @@ os_copy_file_range(PyObject *module, PyObject *const *args, Py_ssize_t nargs, Py goto exit; } count = ival; + if (count < 0) { + PyErr_SetString(PyExc_ValueError, + "count cannot be negative"); + goto exit; + } } if (!noptargs) { goto skip_optional_pos; @@ -8807,6 +8812,11 @@ os_splice(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *k goto exit; } count = ival; + if (count < 0) { + PyErr_SetString(PyExc_ValueError, + "count cannot be negative"); + goto exit; + } } if (!noptargs) { goto skip_optional_pos; @@ -11237,6 +11247,11 @@ os_urandom(PyObject *module, PyObject *arg) goto exit; } size = ival; + if (size < 0) { + PyErr_SetString(PyExc_ValueError, + "size cannot be negative"); + goto exit; + } } return_value = os_urandom_impl(module, size); @@ -13419,4 +13434,4 @@ os__emscripten_log(PyObject *module, PyObject *const *args, Py_ssize_t nargs, Py #ifndef OS__EMSCRIPTEN_LOG_METHODDEF #define OS__EMSCRIPTEN_LOG_METHODDEF #endif /* !defined(OS__EMSCRIPTEN_LOG_METHODDEF) */ -/*[clinic end generated code: output=23de5d098e2dd73f input=a9049054013a1b77]*/ +/*[clinic end generated code: output=77c20b53c34ccae4 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/sha3module.c.h b/Modules/clinic/sha3module.c.h index 1f631ff406e25b..7fdc707626c324 100644 --- a/Modules/clinic/sha3module.c.h +++ b/Modules/clinic/sha3module.c.h @@ -235,6 +235,11 @@ _sha3_shake_128_digest(PyObject *self, PyObject *const *args, Py_ssize_t nargs, goto exit; } length = ival; + if (length < 0) { + PyErr_SetString(PyExc_ValueError, + "length cannot be negative"); + goto exit; + } } return_value = _sha3_shake_128_digest_impl((SHA3object *)self, length); @@ -304,10 +309,15 @@ _sha3_shake_128_hexdigest(PyObject *self, PyObject *const *args, Py_ssize_t narg goto exit; } length = ival; + if (length < 0) { + PyErr_SetString(PyExc_ValueError, + "length cannot be negative"); + goto exit; + } } return_value = _sha3_shake_128_hexdigest_impl((SHA3object *)self, length); exit: return return_value; } -/*[clinic end generated code: output=48be77f8a31e8a3e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=78284adde71d590c input=a9049054013a1b77]*/ diff --git a/Modules/clinic/zlibmodule.c.h b/Modules/clinic/zlibmodule.c.h index 658cb1c2ac3207..6fba75339b3206 100644 --- a/Modules/clinic/zlibmodule.c.h +++ b/Modules/clinic/zlibmodule.c.h @@ -189,6 +189,11 @@ zlib_decompress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObj goto exit; } bufsize = ival; + if (bufsize < 0) { + PyErr_SetString(PyExc_ValueError, + "bufsize cannot be negative"); + goto exit; + } } skip_optional_pos: return_value = zlib_decompress_impl(module, &data, wbits, bufsize); @@ -567,6 +572,11 @@ zlib_Decompress_decompress(PyObject *self, PyTypeObject *cls, PyObject *const *a goto exit; } max_length = ival; + if (max_length < 0) { + PyErr_SetString(PyExc_ValueError, + "max_length cannot be negative"); + goto exit; + } } skip_optional_pos: return_value = zlib_Decompress_decompress_impl((compobject *)self, cls, &data, max_length); @@ -1392,4 +1402,4 @@ zlib_crc32_combine(PyObject *module, PyObject *const *args, Py_ssize_t nargs) #ifndef ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF #define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF #endif /* !defined(ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF) */ -/*[clinic end generated code: output=59184b81fea41d3d input=a9049054013a1b77]*/ +/*[clinic end generated code: output=fa5fc356f3090cce input=a9049054013a1b77]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index bc23ad7e8488ee..5d2506f48e31b4 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -1069,22 +1069,18 @@ static PyType_Spec tee_spec = { /*[clinic input] itertools.tee iterable: object - n: Py_ssize_t = 2 + n: Py_ssize_t(allow_negative=False) = 2 / Returns a tuple of n independent iterators. [clinic start generated code]*/ static PyObject * itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n) -/*[clinic end generated code: output=1c64519cd859c2f0 input=c99a1472c425d66d]*/ +/*[clinic end generated code: output=1c64519cd859c2f0 input=0f72d78e655f45cb]*/ { Py_ssize_t i; PyObject *it, *to, *result; - if (n < 0) { - PyErr_SetString(PyExc_ValueError, "n must be >= 0"); - return NULL; - } result = PyTuple_New(n); if (result == NULL) return NULL; @@ -2255,7 +2251,7 @@ typedef struct { @classmethod itertools.combinations.__new__ iterable: object - r: Py_ssize_t + r: Py_ssize_t(allow_negative=False) Return successive r-length combinations of elements in the iterable. combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3) @@ -2264,7 +2260,7 @@ combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3) static PyObject * itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r) -/*[clinic end generated code: output=87a689b39c40039c input=06bede09e3da20f8]*/ +/*[clinic end generated code: output=87a689b39c40039c input=a32f07a15cfa4676]*/ { combinationsobject *co; Py_ssize_t n; @@ -2276,10 +2272,6 @@ itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, if (pool == NULL) goto error; n = PyTuple_GET_SIZE(pool); - if (r < 0) { - PyErr_SetString(PyExc_ValueError, "r must be non-negative"); - goto error; - } indices = PyMem_New(Py_ssize_t, r); if (indices == NULL) { @@ -2510,7 +2502,7 @@ typedef struct { @classmethod itertools.combinations_with_replacement.__new__ iterable: object - r: Py_ssize_t + r: Py_ssize_t(allow_negative=False) Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats. combinations_with_replacement('ABC', 2) --> ('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C') @@ -2520,7 +2512,7 @@ static PyObject * itertools_combinations_with_replacement_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t r) -/*[clinic end generated code: output=48b26856d4e659ca input=26ebe0e42149e9fb]*/ +/*[clinic end generated code: output=48b26856d4e659ca input=828696750169e84f]*/ { cwrobject *co; Py_ssize_t n; @@ -2532,10 +2524,6 @@ itertools_combinations_with_replacement_impl(PyTypeObject *type, if (pool == NULL) goto error; n = PyTuple_GET_SIZE(pool); - if (r < 0) { - PyErr_SetString(PyExc_ValueError, "r must be non-negative"); - goto error; - } indices = PyMem_New(Py_ssize_t, r); if (indices == NULL) { diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index a1a1fc794fa563..f7e721e52e11b0 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -12483,7 +12483,7 @@ os.copy_file_range Source file descriptor. dst: int Destination file descriptor. - count: Py_ssize_t + count: Py_ssize_t(allow_negative=False) Number of bytes to copy. offset_src: object = None Starting offset in src. @@ -12499,7 +12499,7 @@ respectively for offset_dst. static PyObject * os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, PyObject *offset_src, PyObject *offset_dst) -/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ +/*[clinic end generated code: output=1a91713a1d99fc7a input=08dacb760869b87c]*/ { off_t offset_src_val, offset_dst_val; off_t *p_offset_src = NULL; @@ -12511,11 +12511,6 @@ os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, int flags = 0; - if (count < 0) { - PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); - return NULL; - } - if (offset_src != Py_None) { if (!Py_off_t_converter(offset_src, &offset_src_val)) { return NULL; @@ -12552,7 +12547,7 @@ os.splice Source file descriptor. dst: int Destination file descriptor. - count: Py_ssize_t + count: Py_ssize_t(allow_negative=False) Number of bytes to copy. offset_src: object = None Starting offset in src. @@ -12572,7 +12567,7 @@ static PyObject * os_splice_impl(PyObject *module, int src, int dst, Py_ssize_t count, PyObject *offset_src, PyObject *offset_dst, unsigned int flags) -/*[clinic end generated code: output=d0386f25a8519dc5 input=047527c66c6d2e0a]*/ +/*[clinic end generated code: output=d0386f25a8519dc5 input=034852a7b2e7af35]*/ { off_t offset_src_val, offset_dst_val; off_t *p_offset_src = NULL; @@ -12580,10 +12575,6 @@ os_splice_impl(PyObject *module, int src, int dst, Py_ssize_t count, Py_ssize_t ret; int async_err = 0; - if (count < 0) { - PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); - return NULL; - } if (offset_src != Py_None) { if (!Py_off_t_converter(offset_src, &offset_src_val)) { @@ -15196,7 +15187,7 @@ os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) @permit_long_summary os.urandom - size: Py_ssize_t + size: Py_ssize_t(allow_negative=False) / Return a bytes object containing random bytes suitable for cryptographic use. @@ -15204,14 +15195,11 @@ Return a bytes object containing random bytes suitable for cryptographic use. static PyObject * os_urandom_impl(PyObject *module, Py_ssize_t size) -/*[clinic end generated code: output=42c5cca9d18068e9 input=ade19e6b362e7388]*/ +/*[clinic end generated code: output=42c5cca9d18068e9 input=58a0def87dbc2c22]*/ { PyObject *bytes; int result; - if (size < 0) - return PyErr_Format(PyExc_ValueError, - "negative argument not allowed"); bytes = PyBytes_FromStringAndSize(NULL, size); if (bytes == NULL) return NULL; diff --git a/Modules/sha3module.c b/Modules/sha3module.c index de4bf09b8e7e0b..47fe5e57e6ddd9 100644 --- a/Modules/sha3module.c +++ b/Modules/sha3module.c @@ -471,10 +471,7 @@ SHA3_TYPE_SPEC(sha3_512_spec, "sha3_512", sha3_512_slots); static int sha3_shake_check_digest_length(Py_ssize_t length) { - if (length < 0) { - PyErr_SetString(PyExc_ValueError, "negative digest length"); - return -1; - } + assert(length >= 0); if ((size_t)length >= (1 << 29)) { /* * Raise OverflowError to match the semantics of OpenSSL SHAKE @@ -491,14 +488,14 @@ sha3_shake_check_digest_length(Py_ssize_t length) /*[clinic input] _sha3.shake_128.digest - length: Py_ssize_t + length: Py_ssize_t(allow_negative=False) Return the digest value as a bytes object. [clinic start generated code]*/ static PyObject * _sha3_shake_128_digest_impl(SHA3object *self, Py_ssize_t length) -/*[clinic end generated code: output=6c53fb71a6cff0a0 input=be03ade4b31dd54c]*/ +/*[clinic end generated code: output=6c53fb71a6cff0a0 input=1160c9f86ae0f867]*/ { if (sha3_shake_check_digest_length(length) < 0) { return NULL; @@ -526,14 +523,14 @@ _sha3_shake_128_digest_impl(SHA3object *self, Py_ssize_t length) /*[clinic input] _sha3.shake_128.hexdigest - length: Py_ssize_t + length: Py_ssize_t(allow_negative=False) Return the digest value as a string of hexadecimal digits. [clinic start generated code]*/ static PyObject * _sha3_shake_128_hexdigest_impl(SHA3object *self, Py_ssize_t length) -/*[clinic end generated code: output=a27412d404f64512 input=0d84d05d7a8ccd37]*/ +/*[clinic end generated code: output=a27412d404f64512 input=ff06c9362949d2c8]*/ { if (sha3_shake_check_digest_length(length) < 0) { return NULL; diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index 049dc6690ed7d9..0625a6f8052b6c 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -429,7 +429,7 @@ zlib.decompress / wbits: int(c_default="MAX_WBITS") = MAX_WBITS The window buffer size and container format. - bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE + bufsize: Py_ssize_t(c_default="DEF_BUF_SIZE", allow_negative=False) = DEF_BUF_SIZE The initial output buffer size. Returns a bytes object containing the uncompressed data. @@ -438,7 +438,7 @@ Returns a bytes object containing the uncompressed data. static PyObject * zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits, Py_ssize_t bufsize) -/*[clinic end generated code: output=77c7e35111dc8c42 input=a9ac17beff1f893f]*/ +/*[clinic end generated code: output=77c7e35111dc8c42 input=530077065b3a2233]*/ { PyObject *return_value; Byte *ibuf; @@ -450,10 +450,7 @@ zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits, zlibstate *state = get_zlib_state(module); - if (bufsize < 0) { - PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative"); - return NULL; - } else if (bufsize == 0) { + if (bufsize == 0) { bufsize = 1; } @@ -879,7 +876,7 @@ zlib.Decompress.decompress data: Py_buffer The binary data to decompress. / - max_length: Py_ssize_t = 0 + max_length: Py_ssize_t(allow_negative=False) = 0 The maximum allowable length of the decompressed data. Unconsumed input data will be stored in the unconsumed_tail attribute. @@ -894,7 +891,7 @@ Call the flush() method to clear these buffers. static PyObject * zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls, Py_buffer *data, Py_ssize_t max_length) -/*[clinic end generated code: output=b024a93c2c922d57 input=205667f8c387fce4]*/ +/*[clinic end generated code: output=b024a93c2c922d57 input=77de124bd2a2ecc0]*/ { int err = Z_OK; Py_ssize_t ibuflen; @@ -906,10 +903,7 @@ zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls, return NULL; zlibstate *state = get_zlib_state(module); - if (max_length < 0) { - PyErr_SetString(PyExc_ValueError, "max_length must be non-negative"); - return NULL; - } else if (max_length == 0) { + if (max_length == 0) { max_length = -1; } diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index a236a32c091c4c..c88772030ec283 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -340,6 +340,11 @@ int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject * goto exit; } length = ival; + if (length < 0) { + PyErr_SetString(PyExc_ValueError, + "length cannot be negative"); + goto exit; + } } if (!--noptargs) { goto skip_optional_pos; @@ -485,4 +490,4 @@ int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored)) { return int_is_integer_impl(self); } -/*[clinic end generated code: output=d23f8ce5bdf08a30 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e68f4e23ead3f649 input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 286afae4d7d9ff..b612eabaab480d 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -6356,7 +6356,7 @@ int_as_integer_ratio_impl(PyObject *self) /*[clinic input] int.to_bytes - length: Py_ssize_t = 1 + length: Py_ssize_t(allow_negative=False) = 1 Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1. @@ -6378,7 +6378,7 @@ Return an array of bytes representing an integer. static PyObject * int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, int is_signed) -/*[clinic end generated code: output=89c801df114050a3 input=a0103d0e9ad85c2b]*/ +/*[clinic end generated code: output=89c801df114050a3 input=66f9d0c20529b44f]*/ { int little_endian; PyObject *bytes; @@ -6395,12 +6395,6 @@ int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder, return NULL; } - if (length < 0) { - PyErr_SetString(PyExc_ValueError, - "length argument must be non-negative"); - return NULL; - } - bytes = PyBytes_FromStringAndSize(NULL, length); if (bytes == NULL) return NULL;