Skip to content

Commit 6b8dc49

Browse files
committed
Revert "gh-137044: Make resource.RLIM_INFINITY always positive (GH-137511)"
This reverts commit 0324c72.
1 parent 6e2b9a2 commit 6b8dc49

File tree

5 files changed

+26
-45
lines changed

5 files changed

+26
-45
lines changed

Doc/library/resource.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ this module for those platforms.
5050
.. data:: RLIM_INFINITY
5151

5252
Constant used to represent the limit for an unlimited resource.
53-
Its value is larger than any limited resource value.
54-
55-
.. versionchanged:: next
56-
It is now always positive.
57-
Previously, it could be negative, such as -1 or -3.
5853

5954

6055
.. data:: RLIM_SAVED_CUR

Doc/whatsnew/3.15.rst

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

607-
* :data:`resource.RLIM_INFINITY` is now always positive.
608-
Passing a negative integer value that corresponded to its old value
609-
(such as ``-1`` or ``-3``, depending on platform) to
610-
:func:`resource.setrlimit` and :func:`resource.prlimit` is now deprecated.
611-
(Contributed by Serhiy Storchaka in :gh:`137044`.)
612-
613607

614608
Deprecated C APIs
615609
-----------------

Lib/test/test_resource.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ def test_fsize_ismax(self):
4040
# we need to test that the get/setrlimit functions properly convert
4141
# the number to a C long long and that the conversion doesn't raise
4242
# an error.
43-
self.assertGreater(resource.RLIM_INFINITY, 0)
4443
self.assertEqual(resource.RLIM_INFINITY, max)
45-
self.assertLessEqual(cur, max)
46-
resource.setrlimit(resource.RLIMIT_FSIZE, (max, max))
4744
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
4845

4946
@unittest.skipIf(sys.platform == "vxworks",
@@ -116,53 +113,56 @@ def test_fsize_not_too_big(self):
116113
self.addCleanup(resource.setrlimit, resource.RLIMIT_FSIZE, (cur, max))
117114

118115
def expected(cur):
119-
return (min(cur, resource.RLIM_INFINITY), max)
116+
if resource.RLIM_INFINITY < 0:
117+
return [(cur, max), (resource.RLIM_INFINITY, max)]
118+
elif resource.RLIM_INFINITY < cur:
119+
return [(resource.RLIM_INFINITY, max)]
120+
else:
121+
return [(cur, max)]
120122

121123
resource.setrlimit(resource.RLIMIT_FSIZE, (2**31-5, max))
122124
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), (2**31-5, max))
123-
resource.setrlimit(resource.RLIMIT_FSIZE, (2**31, max))
124-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**31))
125-
resource.setrlimit(resource.RLIMIT_FSIZE, (2**32-5, max))
126-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32-5))
127125

128126
try:
129127
resource.setrlimit(resource.RLIMIT_FSIZE, (2**32, max))
130128
except OverflowError:
131-
pass
129+
resource.setrlimit(resource.RLIMIT_FSIZE, (2**31, max))
130+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**31))
131+
resource.setrlimit(resource.RLIMIT_FSIZE, (2**32-5, max))
132+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32-5))
132133
else:
133-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32))
134+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**32))
135+
resource.setrlimit(resource.RLIMIT_FSIZE, (2**31, max))
136+
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), (2**31, max))
137+
resource.setrlimit(resource.RLIMIT_FSIZE, (2**32-5, max))
138+
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), (2**32-5, max))
134139

135140
resource.setrlimit(resource.RLIMIT_FSIZE, (2**63-5, max))
136-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63-5))
141+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63-5))
137142
try:
138143
resource.setrlimit(resource.RLIMIT_FSIZE, (2**63, max))
139144
except ValueError:
140145
# There is a hard limit on macOS.
141146
pass
142147
else:
143-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63))
148+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**63))
144149
resource.setrlimit(resource.RLIMIT_FSIZE, (2**64-5, max))
145-
self.assertEqual(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**64-5))
150+
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**64-5))
146151

147152
@unittest.skipIf(sys.platform == "vxworks",
148153
"setting RLIMIT_FSIZE is not supported on VxWorks")
149154
@unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')
150155
def test_fsize_negative(self):
151-
self.assertGreater(resource.RLIM_INFINITY, 0)
152156
(cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
153157
for value in -5, -2**31, -2**32-5, -2**63, -2**64-5, -2**1000:
154158
with self.subTest(value=value):
159+
# This test assumes that the values don't map to RLIM_INFINITY,
160+
# though Posix doesn't guarantee it.
161+
self.assertNotEqual(value, resource.RLIM_INFINITY)
162+
155163
self.assertRaises(ValueError, resource.setrlimit, resource.RLIMIT_FSIZE, (value, max))
156164
self.assertRaises(ValueError, resource.setrlimit, resource.RLIMIT_FSIZE, (cur, value))
157165

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-
166166
@unittest.skipUnless(hasattr(resource, "getrusage"), "needs getrusage")
167167
def test_getrusage(self):
168168
self.assertRaises(TypeError, resource.getrusage)

Misc/NEWS.d/next/Library/2025-08-07-12-32-23.gh-issue-137044.abNoIy.rst

Lines changed: 0 additions & 4 deletions
This file was deleted.

Modules/resource.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,7 @@ py2rlim(PyObject *obj, rlim_t *out)
164164
if (bytes < 0) {
165165
return -1;
166166
}
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) {
167+
else if (neg && (*out != RLIM_INFINITY || bytes > (Py_ssize_t)sizeof(*out))) {
175168
PyErr_SetString(PyExc_ValueError,
176169
"Cannot convert negative int");
177170
return -1;
@@ -217,6 +210,9 @@ py2rlimit(PyObject *limits, struct rlimit *rl_out)
217210
static PyObject*
218211
rlim2py(rlim_t value)
219212
{
213+
if (value == RLIM_INFINITY) {
214+
return PyLong_FromNativeBytes(&value, sizeof(value), -1);
215+
}
220216
return PyLong_FromUnsignedNativeBytes(&value, sizeof(value), -1);
221217
}
222218

0 commit comments

Comments
 (0)