gh-135763: AC: Implement `allow_negative for Py_ssize_t` (#138150)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
Christoph Walcher 2025-09-01 23:55:22 +02:00 committed by GitHub
parent 7c92497e5c
commit 47bc10e6b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 200 additions and 24 deletions

View file

@ -33,6 +33,19 @@ _Py_convert_optional_to_ssize_t(PyObject *obj, void *result)
return 1;
}
int
_Py_convert_optional_to_non_negative_ssize_t(PyObject *obj, void *result)
{
if (!_Py_convert_optional_to_ssize_t(obj, result)) {
return 0;
}
if (obj != Py_None && *((Py_ssize_t *)result) < 0) {
PyErr_SetString(PyExc_ValueError, "argument cannot be negative");
return 0;
}
return 1;
}
/* Helper for mkvalue() to scan the length of a format */