Skip to content

Commit 7428fd4

Browse files
gh-137512: Add new constants in the resource module
* RLIMIT_NTHR * RLIMIT_THREADS * RLIMIT_UMTXP * RLIM_SAVED_CUR * RLIM_SAVED_MAX
1 parent baefaa6 commit 7428fd4

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

Doc/library/resource.rst

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ this module for those platforms.
5252
Constant used to represent the limit for an unlimited resource.
5353

5454

55+
.. data:: RLIM_SAVED_CUR
56+
.. data:: RLIM_SAVED_MAX
57+
58+
Constants used to represent the soft and hard limit values if they
59+
cannot be represented in the ``rlim_t`` value in C.
60+
On FreeBSD they are aliases of :data:`RLIM_INFINITY`.
61+
62+
.. availability:: Solaris, OpenIndiana, AIX, FreeBSD
63+
64+
.. versionadded:: next
65+
66+
5567
.. function:: getrlimit(resource)
5668

5769
Returns a tuple ``(soft, hard)`` with the current soft and hard limits of
@@ -177,7 +189,7 @@ platform.
177189

178190
The largest area of mapped memory which the process may occupy.
179191

180-
.. availability:: FreeBSD >= 11.
192+
.. availability:: Solaris, OpenIndiana, FreeBSD.
181193

182194

183195
.. data:: RLIMIT_AS
@@ -230,6 +242,7 @@ platform.
230242

231243
.. versionadded:: 3.4
232244

245+
233246
.. data:: RLIMIT_SBSIZE
234247

235248
The maximum size (in bytes) of socket buffer usage for this user.
@@ -240,6 +253,7 @@ platform.
240253

241254
.. versionadded:: 3.4
242255

256+
243257
.. data:: RLIMIT_SWAP
244258

245259
The maximum size (in bytes) of the swap space that may be reserved or
@@ -249,18 +263,20 @@ platform.
249263
`tuning(7) <https://man.freebsd.org/cgi/man.cgi?query=tuning&sektion=7>`__
250264
for a complete description of this sysctl.
251265

252-
.. availability:: FreeBSD.
266+
.. availability:: FreeBSD >= 8.
253267

254268
.. versionadded:: 3.4
255269

270+
256271
.. data:: RLIMIT_NPTS
257272

258273
The maximum number of pseudo-terminals created by this user id.
259274

260-
.. availability:: FreeBSD.
275+
.. availability:: FreeBSD >= 8.
261276

262277
.. versionadded:: 3.4
263278

279+
264280
.. data:: RLIMIT_KQUEUES
265281

266282
The maximum number of kqueues this user id is allowed to create.
@@ -269,6 +285,36 @@ platform.
269285

270286
.. versionadded:: 3.10
271287

288+
289+
.. data:: RLIMIT_NTHR
290+
291+
The maximum number of threads for this user id, not counting the main
292+
and kernel threads.
293+
294+
.. availability:: NetBSD >= 7.0.
295+
296+
.. versionadded:: next
297+
298+
299+
.. data:: RLIMIT_THREADS
300+
301+
The maximum number of threads each process can create.
302+
303+
.. availability:: AIX.
304+
305+
.. versionadded:: next
306+
307+
308+
.. data:: RLIMIT_UMTXP
309+
310+
The limit of the number of process-shared Posix thread library objects
311+
allocated by user id.
312+
313+
.. availability:: FreeBSD >= 11.
314+
315+
.. versionadded:: next
316+
317+
272318
Resource Usage
273319
--------------
274320

Doc/whatsnew/3.15.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ os.path
277277
the resulting path can be missing but it will be free of symlinks.
278278
(Contributed by Petr Viktorin for :cve:`2025-4517`.)
279279

280+
resource
281+
--------
282+
283+
* Add new constants: :data:`~resource.RLIMIT_NTHR`,
284+
:data:`~resource.RLIMIT_UMTXP`, :data:`~resource.RLIMIT_THREADS`,
285+
:data:`~resource.RLIM_SAVED_CUR`, and :data:`~resource.RLIM_SAVED_MAX`.
286+
(Contributed by Serhiy Storchaka in :gh:`137512`.)
287+
288+
280289
shelve
281290
------
282291

Lib/test/test_resource.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,23 @@ def test_pagesize(self):
200200
self.assertIsInstance(pagesize, int)
201201
self.assertGreaterEqual(pagesize, 0)
202202

203+
def test_contants(self):
204+
self.assertIsInstance(resource.RLIM_INFINITY, int)
205+
if sys.platform.startswith(('freebsd', 'solaris', 'sunos', 'aix')):
206+
self.assertHasAttr(resource, 'RLIM_SAVED_CUR')
207+
self.assertHasAttr(resource, 'RLIM_SAVED_MAX')
208+
if hasattr(resource, 'RLIM_SAVED_CUR'):
209+
self.assertIsInstance(resource.RLIM_SAVED_CUR, int)
210+
self.assertIsInstance(resource.RLIM_SAVED_MAX, int)
211+
203212
@unittest.skipUnless(sys.platform in ('linux', 'android'), 'Linux only')
204213
def test_linux_constants(self):
205214
for attr in ['MSGQUEUE', 'NICE', 'RTPRIO', 'RTTIME', 'SIGPENDING']:
206215
with contextlib.suppress(AttributeError):
207216
self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int)
208217

209218
def test_freebsd_contants(self):
210-
for attr in ['SWAP', 'SBSIZE', 'NPTS']:
219+
for attr in ['SWAP', 'SBSIZE', 'NPTS', 'UMTXP', 'VMEM']:
211220
with contextlib.suppress(AttributeError):
212221
self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int)
213222

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add new constants in the :mod:`resource` module:
2+
:data:`~resource.RLIMIT_NTHR`, :data:`~resource.RLIMIT_UMTXP`,
3+
:data:`~resource.RLIMIT_THREADS`, :data:`~resource.RLIM_SAVED_CUR`, and
4+
:data:`~resource.RLIM_SAVED_MAX`.

Modules/resource.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,38 @@ resource_exec(PyObject *module)
518518
ADD_INT(module, RLIMIT_KQUEUES);
519519
#endif
520520

521+
#ifdef RLIMIT_NTHR
522+
ADD_INT(module, RLIMIT_NTHR);
523+
#endif
524+
525+
#ifdef RLIMIT_THREADS
526+
ADD_INT(module, RLIMIT_THREADS);
527+
#endif
528+
529+
#ifdef RLIMIT_UMTXP
530+
ADD_INT(module, RLIMIT_UMTXP);
531+
#endif
532+
533+
#ifdef RLIMIT_PIPEBUF
534+
ADD_INT(module, RLIMIT_PIPEBUF);
535+
#endif
536+
521537
if (PyModule_Add(module, "RLIM_INFINITY", rlim2py(RLIM_INFINITY)) < 0) {
522538
return -1;
523539
}
540+
541+
#ifdef RLIM_SAVED_CUR
542+
if (PyModule_Add(module, "RLIM_SAVED_CUR", rlim2py(RLIM_SAVED_CUR)) < 0) {
543+
return -1;
544+
}
545+
#endif
546+
547+
#ifdef RLIM_SAVED_MAX
548+
if (PyModule_Add(module, "RLIM_SAVED_MAX", rlim2py(RLIM_SAVED_MAX)) < 0) {
549+
return -1;
550+
}
551+
#endif
552+
524553
return 0;
525554

526555
#undef ADD_INT

0 commit comments

Comments
 (0)