Skip to content

Commit 6e2b9a2

Browse files
gh-137512: Add new constants in the resource module (GH-137513)
* RLIMIT_NTHR * RLIMIT_THREADS * RLIMIT_UMTXP * RLIM_SAVED_CUR * RLIM_SAVED_MAX * Document RLIMIT_PIPEBUF. Other doc fixes.
1 parent 3706ef6 commit 6e2b9a2

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

Doc/library/resource.rst

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ this module for those platforms.
5757
Previously, it could be negative, such as -1 or -3.
5858

5959

60+
.. data:: RLIM_SAVED_CUR
61+
.. data:: RLIM_SAVED_MAX
62+
63+
Constants used to represent the soft and hard limit values if they
64+
cannot be represented in the ``rlim_t`` value in C.
65+
Can be equal to :data:`RLIM_INFINITY`.
66+
67+
.. versionadded:: next
68+
69+
6070
.. function:: getrlimit(resource)
6171

6272
Returns a tuple ``(soft, hard)`` with the current soft and hard limits of
@@ -181,8 +191,9 @@ platform.
181191
.. data:: RLIMIT_VMEM
182192

183193
The largest area of mapped memory which the process may occupy.
194+
Usually an alias of :const:`RLIMIT_AS`.
184195

185-
.. availability:: FreeBSD >= 11.
196+
.. availability:: Solaris, FreeBSD, NetBSD.
186197

187198

188199
.. data:: RLIMIT_AS
@@ -235,16 +246,18 @@ platform.
235246

236247
.. versionadded:: 3.4
237248

249+
238250
.. data:: RLIMIT_SBSIZE
239251

240252
The maximum size (in bytes) of socket buffer usage for this user.
241253
This limits the amount of network memory, and hence the amount of mbufs,
242254
that this user may hold at any time.
243255

244-
.. availability:: FreeBSD.
256+
.. availability:: FreeBSD, NetBSD.
245257

246258
.. versionadded:: 3.4
247259

260+
248261
.. data:: RLIMIT_SWAP
249262

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

257-
.. availability:: FreeBSD.
270+
.. availability:: FreeBSD >= 8.
258271

259272
.. versionadded:: 3.4
260273

274+
261275
.. data:: RLIMIT_NPTS
262276

263277
The maximum number of pseudo-terminals created by this user id.
264278

265-
.. availability:: FreeBSD.
279+
.. availability:: FreeBSD >= 8.
266280

267281
.. versionadded:: 3.4
268282

283+
269284
.. data:: RLIMIT_KQUEUES
270285

271286
The maximum number of kqueues this user id is allowed to create.
@@ -274,6 +289,46 @@ platform.
274289

275290
.. versionadded:: 3.10
276291

292+
293+
.. data:: RLIMIT_NTHR
294+
295+
The maximum number of threads for this user id, not counting the main
296+
and kernel threads.
297+
298+
.. availability:: NetBSD >= 7.0.
299+
300+
.. versionadded:: next
301+
302+
303+
.. data:: RLIMIT_PIPEBUF
304+
305+
The maximum total size of in-kernel buffers for bi-directional pipes/fifos
306+
that this user id is allowed to consume.
307+
308+
.. availability:: FreeBSD >= 14.2.
309+
310+
.. versionadded:: next
311+
312+
313+
.. data:: RLIMIT_THREADS
314+
315+
The maximum number of threads each process can create.
316+
317+
.. availability:: AIX.
318+
319+
.. versionadded:: next
320+
321+
322+
.. data:: RLIMIT_UMTXP
323+
324+
The limit of the number of process-shared Posix thread library objects
325+
allocated by user id.
326+
327+
.. availability:: FreeBSD >= 11.
328+
329+
.. versionadded:: next
330+
331+
277332
Resource Usage
278333
--------------
279334

Doc/whatsnew/3.15.rst

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

315+
resource
316+
--------
317+
318+
* Add new constants: :data:`~resource.RLIMIT_NTHR`,
319+
:data:`~resource.RLIMIT_UMTXP`, :data:`~resource.RLIMIT_THREADS`,
320+
:data:`~resource.RLIM_SAVED_CUR`, and :data:`~resource.RLIM_SAVED_MAX`.
321+
(Contributed by Serhiy Storchaka in :gh:`137512`.)
322+
323+
315324
shelve
316325
------
317326

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', 'PIPEBUF']:
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_PIPEBUF`, :data:`~resource.RLIMIT_THREADS`,
4+
:data:`~resource.RLIM_SAVED_CUR`, and :data:`~resource.RLIM_SAVED_MAX`.

Modules/resource.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,38 @@ resource_exec(PyObject *module)
522522
ADD_INT(module, RLIMIT_KQUEUES);
523523
#endif
524524

525+
#ifdef RLIMIT_NTHR
526+
ADD_INT(module, RLIMIT_NTHR);
527+
#endif
528+
529+
#ifdef RLIMIT_THREADS
530+
ADD_INT(module, RLIMIT_THREADS);
531+
#endif
532+
533+
#ifdef RLIMIT_UMTXP
534+
ADD_INT(module, RLIMIT_UMTXP);
535+
#endif
536+
537+
#ifdef RLIMIT_PIPEBUF
538+
ADD_INT(module, RLIMIT_PIPEBUF);
539+
#endif
540+
525541
if (PyModule_Add(module, "RLIM_INFINITY", rlim2py(RLIM_INFINITY)) < 0) {
526542
return -1;
527543
}
544+
545+
#ifdef RLIM_SAVED_CUR
546+
if (PyModule_Add(module, "RLIM_SAVED_CUR", rlim2py(RLIM_SAVED_CUR)) < 0) {
547+
return -1;
548+
}
549+
#endif
550+
551+
#ifdef RLIM_SAVED_MAX
552+
if (PyModule_Add(module, "RLIM_SAVED_MAX", rlim2py(RLIM_SAVED_MAX)) < 0) {
553+
return -1;
554+
}
555+
#endif
556+
528557
return 0;
529558

530559
#undef ADD_INT

0 commit comments

Comments
 (0)