From 71671f0f6d6dd2cfc31ad2c63e70bfb7a065898d Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 7 Aug 2025 15:10:42 +0300 Subject: [PATCH 1/2] gh-137512: Add new constants in the resource module * RLIMIT_NTHR * RLIMIT_THREADS * RLIMIT_UMTXP * RLIM_SAVED_CUR * RLIM_SAVED_MAX --- Doc/library/resource.rst | 52 +++++++++++++++++-- Doc/whatsnew/3.15.rst | 9 ++++ Lib/test/test_resource.py | 11 +++- ...-08-07-15-07-44.gh-issue-137512.j2or5h.rst | 4 ++ Modules/resource.c | 29 +++++++++++ 5 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-08-07-15-07-44.gh-issue-137512.j2or5h.rst diff --git a/Doc/library/resource.rst b/Doc/library/resource.rst index 0515d205bbca0b..fdf34da3b2f103 100644 --- a/Doc/library/resource.rst +++ b/Doc/library/resource.rst @@ -52,6 +52,18 @@ this module for those platforms. Constant used to represent the limit for an unlimited resource. +.. data:: RLIM_SAVED_CUR +.. data:: RLIM_SAVED_MAX + + Constants used to represent the soft and hard limit values if they + cannot be represented in the ``rlim_t`` value in C. + On FreeBSD they are aliases of :data:`RLIM_INFINITY`. + + .. availability:: Solaris, AIX, FreeBSD + + .. versionadded:: next + + .. function:: getrlimit(resource) Returns a tuple ``(soft, hard)`` with the current soft and hard limits of @@ -177,7 +189,7 @@ platform. The largest area of mapped memory which the process may occupy. - .. availability:: FreeBSD >= 11. + .. availability:: Solaris, FreeBSD. .. data:: RLIMIT_AS @@ -230,6 +242,7 @@ platform. .. versionadded:: 3.4 + .. data:: RLIMIT_SBSIZE The maximum size (in bytes) of socket buffer usage for this user. @@ -240,6 +253,7 @@ platform. .. versionadded:: 3.4 + .. data:: RLIMIT_SWAP The maximum size (in bytes) of the swap space that may be reserved or @@ -249,18 +263,20 @@ platform. `tuning(7) `__ for a complete description of this sysctl. - .. availability:: FreeBSD. + .. availability:: FreeBSD >= 8. .. versionadded:: 3.4 + .. data:: RLIMIT_NPTS The maximum number of pseudo-terminals created by this user id. - .. availability:: FreeBSD. + .. availability:: FreeBSD >= 8. .. versionadded:: 3.4 + .. data:: RLIMIT_KQUEUES The maximum number of kqueues this user id is allowed to create. @@ -269,6 +285,36 @@ platform. .. versionadded:: 3.10 + +.. data:: RLIMIT_NTHR + + The maximum number of threads for this user id, not counting the main + and kernel threads. + + .. availability:: NetBSD >= 7.0. + + .. versionadded:: next + + +.. data:: RLIMIT_THREADS + + The maximum number of threads each process can create. + + .. availability:: AIX. + + .. versionadded:: next + + +.. data:: RLIMIT_UMTXP + + The limit of the number of process-shared Posix thread library objects + allocated by user id. + + .. availability:: FreeBSD >= 11. + + .. versionadded:: next + + Resource Usage -------------- diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 89644a509a0bb4..0b69dd69f68bf3 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -277,6 +277,15 @@ os.path the resulting path can be missing but it will be free of symlinks. (Contributed by Petr Viktorin for :cve:`2025-4517`.) +resource +-------- + +* Add new constants: :data:`~resource.RLIMIT_NTHR`, + :data:`~resource.RLIMIT_UMTXP`, :data:`~resource.RLIMIT_THREADS`, + :data:`~resource.RLIM_SAVED_CUR`, and :data:`~resource.RLIM_SAVED_MAX`. + (Contributed by Serhiy Storchaka in :gh:`137512`.) + + shelve ------ diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py index fe05224828bd27..f22f1de77c736f 100644 --- a/Lib/test/test_resource.py +++ b/Lib/test/test_resource.py @@ -200,6 +200,15 @@ def test_pagesize(self): self.assertIsInstance(pagesize, int) self.assertGreaterEqual(pagesize, 0) + def test_contants(self): + self.assertIsInstance(resource.RLIM_INFINITY, int) + if sys.platform.startswith(('freebsd', 'solaris', 'sunos', 'aix')): + self.assertHasAttr(resource, 'RLIM_SAVED_CUR') + self.assertHasAttr(resource, 'RLIM_SAVED_MAX') + if hasattr(resource, 'RLIM_SAVED_CUR'): + self.assertIsInstance(resource.RLIM_SAVED_CUR, int) + self.assertIsInstance(resource.RLIM_SAVED_MAX, int) + @unittest.skipUnless(sys.platform in ('linux', 'android'), 'Linux only') def test_linux_constants(self): for attr in ['MSGQUEUE', 'NICE', 'RTPRIO', 'RTTIME', 'SIGPENDING']: @@ -207,7 +216,7 @@ def test_linux_constants(self): self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int) def test_freebsd_contants(self): - for attr in ['SWAP', 'SBSIZE', 'NPTS']: + for attr in ['SWAP', 'SBSIZE', 'NPTS', 'UMTXP', 'VMEM']: with contextlib.suppress(AttributeError): self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int) diff --git a/Misc/NEWS.d/next/Library/2025-08-07-15-07-44.gh-issue-137512.j2or5h.rst b/Misc/NEWS.d/next/Library/2025-08-07-15-07-44.gh-issue-137512.j2or5h.rst new file mode 100644 index 00000000000000..19b95e03c303bc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-08-07-15-07-44.gh-issue-137512.j2or5h.rst @@ -0,0 +1,4 @@ +Add new constants in the :mod:`resource` module: +:data:`~resource.RLIMIT_NTHR`, :data:`~resource.RLIMIT_UMTXP`, +:data:`~resource.RLIMIT_THREADS`, :data:`~resource.RLIM_SAVED_CUR`, and +:data:`~resource.RLIM_SAVED_MAX`. diff --git a/Modules/resource.c b/Modules/resource.c index 2353bc6653abd8..0ffa950a6bfb75 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -518,9 +518,38 @@ resource_exec(PyObject *module) ADD_INT(module, RLIMIT_KQUEUES); #endif +#ifdef RLIMIT_NTHR + ADD_INT(module, RLIMIT_NTHR); +#endif + +#ifdef RLIMIT_THREADS + ADD_INT(module, RLIMIT_THREADS); +#endif + +#ifdef RLIMIT_UMTXP + ADD_INT(module, RLIMIT_UMTXP); +#endif + +#ifdef RLIMIT_PIPEBUF + ADD_INT(module, RLIMIT_PIPEBUF); +#endif + if (PyModule_Add(module, "RLIM_INFINITY", rlim2py(RLIM_INFINITY)) < 0) { return -1; } + +#ifdef RLIM_SAVED_CUR + if (PyModule_Add(module, "RLIM_SAVED_CUR", rlim2py(RLIM_SAVED_CUR)) < 0) { + return -1; + } +#endif + +#ifdef RLIM_SAVED_MAX + if (PyModule_Add(module, "RLIM_SAVED_MAX", rlim2py(RLIM_SAVED_MAX)) < 0) { + return -1; + } +#endif + return 0; #undef ADD_INT From f9b2fb92616aafbbdc45af78b81d37d3f72a000e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 18 Aug 2025 21:24:28 +0300 Subject: [PATCH 2/2] Document RLIMIT_PIPEBUF. Other doc fixes. --- Doc/library/resource.rst | 19 ++++++++++++++----- Lib/test/test_resource.py | 2 +- ...-08-07-15-07-44.gh-issue-137512.j2or5h.rst | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Doc/library/resource.rst b/Doc/library/resource.rst index 6adc8e468a58a8..ea75c8b2b98924 100644 --- a/Doc/library/resource.rst +++ b/Doc/library/resource.rst @@ -62,9 +62,7 @@ this module for those platforms. Constants used to represent the soft and hard limit values if they cannot be represented in the ``rlim_t`` value in C. - On FreeBSD they are aliases of :data:`RLIM_INFINITY`. - - .. availability:: Solaris, AIX, FreeBSD + Can be equal to :data:`RLIM_INFINITY`. .. versionadded:: next @@ -193,8 +191,9 @@ platform. .. data:: RLIMIT_VMEM The largest area of mapped memory which the process may occupy. + Usually an alias of :const:`RLIMIT_AS`. - .. availability:: Solaris, FreeBSD. + .. availability:: Solaris, FreeBSD, NetBSD. .. data:: RLIMIT_AS @@ -254,7 +253,7 @@ platform. This limits the amount of network memory, and hence the amount of mbufs, that this user may hold at any time. - .. availability:: FreeBSD. + .. availability:: FreeBSD, NetBSD. .. versionadded:: 3.4 @@ -301,6 +300,16 @@ platform. .. versionadded:: next +.. data:: RLIMIT_PIPEBUF + + The maximum total size of in-kernel buffers for bi-directional pipes/fifos + that this user id is allowed to consume. + + .. availability:: FreeBSD >= 14.2. + + .. versionadded:: next + + .. data:: RLIMIT_THREADS The maximum number of threads each process can create. diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py index 4a7159b9215917..f73914b9b92c83 100644 --- a/Lib/test/test_resource.py +++ b/Lib/test/test_resource.py @@ -216,7 +216,7 @@ def test_linux_constants(self): self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int) def test_freebsd_contants(self): - for attr in ['SWAP', 'SBSIZE', 'NPTS', 'UMTXP', 'VMEM']: + for attr in ['SWAP', 'SBSIZE', 'NPTS', 'UMTXP', 'VMEM', 'PIPEBUF']: with contextlib.suppress(AttributeError): self.assertIsInstance(getattr(resource, 'RLIMIT_' + attr), int) diff --git a/Misc/NEWS.d/next/Library/2025-08-07-15-07-44.gh-issue-137512.j2or5h.rst b/Misc/NEWS.d/next/Library/2025-08-07-15-07-44.gh-issue-137512.j2or5h.rst index 19b95e03c303bc..fc67913b7e9055 100644 --- a/Misc/NEWS.d/next/Library/2025-08-07-15-07-44.gh-issue-137512.j2or5h.rst +++ b/Misc/NEWS.d/next/Library/2025-08-07-15-07-44.gh-issue-137512.j2or5h.rst @@ -1,4 +1,4 @@ Add new constants in the :mod:`resource` module: :data:`~resource.RLIMIT_NTHR`, :data:`~resource.RLIMIT_UMTXP`, -:data:`~resource.RLIMIT_THREADS`, :data:`~resource.RLIM_SAVED_CUR`, and -:data:`~resource.RLIM_SAVED_MAX`. +:data:`~resource.RLIMIT_PIPEBUF`, :data:`~resource.RLIMIT_THREADS`, +:data:`~resource.RLIM_SAVED_CUR`, and :data:`~resource.RLIM_SAVED_MAX`.