Skip to content

Commit 03b8f07

Browse files
Continue support of negative values.
1 parent 55ca981 commit 03b8f07

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,12 @@ Porting to Python 3.15
560560
The |pythoncapi_compat_project| can be used to get most of these new
561561
functions on Python 3.14 and older.
562562

563+
* :data:`resource.RLIM_INFINITY` is now always positive.
564+
Passing a negative integer value that corresponded to its old value
565+
(such as ``-1`` or ``-3``, depending on platform) to
566+
:func:`resource.setrlimit` and :func:`resource.prlimit` is now deprecated.
567+
(Contributed by Serhiy Storchaka in :gh:`137044`.)
568+
563569

564570
Deprecated C APIs
565571
-----------------

Lib/test/test_resource.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,19 @@ def expected(cur):
150150
def test_fsize_negative(self):
151151
self.assertGreater(resource.RLIM_INFINITY, 0)
152152
(cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
153-
for value in -5, -3, -1, -2**31, -2**32-5, -2**63, -2**64-5, -2**1000:
153+
for value in -5, -2**31, -2**32-5, -2**63, -2**64-5, -2**1000:
154154
with self.subTest(value=value):
155155
self.assertRaises(ValueError, resource.setrlimit, resource.RLIMIT_FSIZE, (value, max))
156156
self.assertRaises(ValueError, resource.setrlimit, resource.RLIMIT_FSIZE, (cur, value))
157157

158+
if resource.RLIM_INFINITY in (2**32-3, 2**32-1, 2**64-3, 2**64-1):
159+
value = (resource.RLIM_INFINITY & 0xffff) - 0x10000
160+
with self.assertWarnsRegex(DeprecationWarning, "RLIM_INFINITY"):
161+
resource.setrlimit(resource.RLIMIT_FSIZE, (value, max))
162+
with self.assertWarnsRegex(DeprecationWarning, "RLIM_INFINITY"):
163+
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, value))
164+
165+
158166
@unittest.skipUnless(hasattr(resource, "getrusage"), "needs getrusage")
159167
def test_getrusage(self):
160168
self.assertRaises(TypeError, resource.getrusage)

Modules/resource.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,26 @@ py2rlim(PyObject *obj, rlim_t *out)
156156
return -1;
157157
}
158158
int neg = PyLong_IsNegative(obj);
159-
if (neg) {
160-
PyErr_SetString(PyExc_ValueError,
161-
"Cannot convert negative int");
162-
Py_DECREF(obj);
163-
return -1;
164-
}
165159
assert(neg >= 0);
166160
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, out, sizeof(*out),
167161
Py_ASNATIVEBYTES_NATIVE_ENDIAN |
168-
Py_ASNATIVEBYTES_UNSIGNED_BUFFER |
169-
Py_ASNATIVEBYTES_REJECT_NEGATIVE |
170-
Py_ASNATIVEBYTES_ALLOW_INDEX);
162+
Py_ASNATIVEBYTES_UNSIGNED_BUFFER);
171163
Py_DECREF(obj);
172164
if (bytes < 0) {
173165
return -1;
174166
}
167+
else if (neg && *out == RLIM_INFINITY && bytes <= (Py_ssize_t)sizeof(*out)) {
168+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
169+
"Use RLIM_INFINITY instead of negative limit value.", 1))
170+
{
171+
return -1;
172+
}
173+
}
174+
else if (neg) {
175+
PyErr_SetString(PyExc_ValueError,
176+
"Cannot convert negative int");
177+
return -1;
178+
}
175179
else if (bytes > (Py_ssize_t)sizeof(*out)) {
176180
PyErr_SetString(PyExc_OverflowError,
177181
"Python int too large to convert to C rlim_t");

0 commit comments

Comments
 (0)