gh-137044: Make resource.RLIM_INFINITY always positive (GH-137511)

It is now a positive integer larger larger than any limited resource value.
This simplifies comparison of the resource values.
Previously, it could be negative, such as -1 or -3, depending on platform.

Deprecation warning is emitted if the old negative value is passed.
This commit is contained in:
Serhiy Storchaka 2025-08-18 19:28:56 +03:00 committed by GitHub
parent 138ed6db9f
commit 0324c726de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 45 additions and 26 deletions

View file

@ -164,7 +164,14 @@ py2rlim(PyObject *obj, rlim_t *out)
if (bytes < 0) {
return -1;
}
else if (neg && (*out != RLIM_INFINITY || bytes > (Py_ssize_t)sizeof(*out))) {
else if (neg && *out == RLIM_INFINITY && bytes <= (Py_ssize_t)sizeof(*out)) {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"Use RLIM_INFINITY instead of negative limit value.", 1))
{
return -1;
}
}
else if (neg) {
PyErr_SetString(PyExc_ValueError,
"Cannot convert negative int");
return -1;
@ -210,9 +217,6 @@ py2rlimit(PyObject *limits, struct rlimit *rl_out)
static PyObject*
rlim2py(rlim_t value)
{
if (value == RLIM_INFINITY) {
return PyLong_FromNativeBytes(&value, sizeof(value), -1);
}
return PyLong_FromUnsignedNativeBytes(&value, sizeof(value), -1);
}