diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 035c71dcd6bf4a..46ee5fb9322819 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -2229,6 +2229,13 @@ features: Return a string representing the current working directory. +.. function:: getdtablesize() + + Return the maximum number of files a process can have open. + + .. versionadded:: 3.14 + + .. availability:: Unix. .. function:: getcwdb() diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 1e570c757fccbc..fae2bdb9757fb4 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -113,6 +113,12 @@ def test_getcwd(self): cwd = os.getcwd() self.assertIsInstance(cwd, str) + @unittest.skipUnless(hasattr(os, 'getdtablesize'), 'need os.getdtablesize()') + def test_getdtablesize(self): + size = os.getdtablesize() + self.assertIsInstance(size, int) + self.assertGreaterEqual(size, 1) + def test_getcwd_long_path(self): # bpo-37412: On Linux, PATH_MAX is usually around 4096 bytes. On # Windows, MAX_PATH is defined as 260 characters, but Windows supports diff --git a/Misc/NEWS.d/next/Library/2024-09-24-14-15-34.gh-issue-83923.YunOLX.rst b/Misc/NEWS.d/next/Library/2024-09-24-14-15-34.gh-issue-83923.YunOLX.rst new file mode 100644 index 00000000000000..0aeacf18826084 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-09-24-14-15-34.gh-issue-83923.YunOLX.rst @@ -0,0 +1,2 @@ +Added :func:`os.getdtablesize` to :mod:`os` (supported only on unix-like +systems) diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h index 4b9dbac9af031f..79a126592c6ed6 100644 --- a/Modules/clinic/posixmodule.c.h +++ b/Modules/clinic/posixmodule.c.h @@ -1419,6 +1419,24 @@ os_getcwdb(PyObject *module, PyObject *Py_UNUSED(ignored)) return os_getcwdb_impl(module); } +PyDoc_STRVAR(os_getdtablesize__doc__, +"getdtablesize($module, /)\n" +"--\n" +"\n" +"Return the maximum number of files a process can have open."); + +#define OS_GETDTABLESIZE_METHODDEF \ + {"getdtablesize", (PyCFunction)os_getdtablesize, METH_NOARGS, os_getdtablesize__doc__}, + +static PyObject * +os_getdtablesize_impl(PyObject *module); + +static PyObject * +os_getdtablesize(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return os_getdtablesize_impl(module); +} + #if defined(HAVE_LINK) PyDoc_STRVAR(os_link__doc__, diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index f24ab81cbcb77b..d44b06d350d87c 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4218,6 +4218,22 @@ os_getcwdb_impl(PyObject *module) return posix_getcwd(1); } +#ifndef MS_WINDOWS +/*[clinic input] +os.getdtablesize + +Return the maximum number of files a process can have open. +[clinic start generated code]*/ + +static PyObject * +os_getdtablesize_impl(PyObject *module) +{ + int size; + + size = getdtablesize(); + return PyLong_FromLong(size); +} +#endif /* !MS_WINDOWS */ #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) #define HAVE_LINK 1 @@ -16843,6 +16859,7 @@ static PyMethodDef posix_methods[] = { OS_CTERMID_METHODDEF OS_GETCWD_METHODDEF OS_GETCWDB_METHODDEF + OS_GETDTABLESIZE_METHODDEF OS_LINK_METHODDEF OS_LISTDIR_METHODDEF OS_LISTDRIVES_METHODDEF diff --git a/configure b/configure index 2c58af3eace84a..c3ca65615b5237 100755 --- a/configure +++ b/configure @@ -18405,6 +18405,12 @@ if test "x$ac_cv_func__getpty" = xyes then : printf "%s\n" "#define HAVE__GETPTY 1" >>confdefs.h +fi +ac_fn_c_check_func "$LINENO" "getdtablesize" "ac_cv_func_getdtablesize" +if test "x$ac_cv_func_getdtablesize" = xyes +then : + printf "%s\n" "#define HAVE_GETDTABLESIZE 1" >>confdefs.h + fi ac_fn_c_check_func "$LINENO" "getpwent" "ac_cv_func_getpwent" if test "x$ac_cv_func_getpwent" = xyes diff --git a/configure.ac b/configure.ac index 3c1dc1cc5018d4..271a4a7fe31daa 100644 --- a/configure.ac +++ b/configure.ac @@ -5149,7 +5149,7 @@ AC_CHECK_FUNCS([ \ fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \ gai_strerror getegid geteuid getgid getgrent getgrgid getgrgid_r \ getgrnam_r getgrouplist gethostname getitimer getloadavg getlogin \ - getpeername getpgid getpid getppid getpriority _getpty \ + getpeername getpgid getpid getppid getpriority _getpty getdtablesize \ getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid getspent \ getspnam getuid getwd grantpt if_nameindex initgroups kill killpg lchown linkat \ lockf lstat lutimes madvise mbrtowc memrchr mkdirat mkfifo mkfifoat \ diff --git a/pyconfig.h.in b/pyconfig.h.in index a5946f3547b35c..9042bc09a82c04 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -471,6 +471,9 @@ /* Define this if you have flockfile(), getc_unlocked(), and funlockfile() */ #undef HAVE_GETC_UNLOCKED +/* Define to 1 if you have the `getdtablesize' function. */ +#undef HAVE_GETDTABLESIZE + /* Define to 1 if you have the `getegid' function. */ #undef HAVE_GETEGID