From 64d10a9021754a7d02959e68af4e7d472efa7de7 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 14:25:15 +0800 Subject: [PATCH 01/15] Added getdtablesize function to os module --- ...4-09-24-14-15-34.gh-issue-83923.YunOLX.rst | 2 ++ Modules/clinic/posixmodule.c.h | 18 ++++++++++++ Modules/posixmodule.c | 28 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-09-24-14-15-34.gh-issue-83923.YunOLX.rst 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..5b1a03c188aecc --- /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:`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..e705bd67fa2e8f 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4218,6 +4218,33 @@ os_getcwdb_impl(PyObject *module) return posix_getcwd(1); } +/*[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) +{ + PyObject *max_fptable_size; + int size; + +#ifdef MS_WINDOWS + PyErr_SetString(PyExc_NotImplementedError, + "this function is not supported on windows"); + if (PyErr_Occurred()) { + return NULL; + } +#else /* !Unix-like */ + size = getdtablesize(); +#endif /* !MS_WINDOWS */ + max_fptable_size = PyLong_FromLong(size); + if (max_fptable_size == NULL) + return NULL; + + return max_fptable_size; +} #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) #define HAVE_LINK 1 @@ -16843,6 +16870,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 From aa6fae8a22c0a1c53b3a41e9afd1ee6b1f62544d Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 14:29:12 +0800 Subject: [PATCH 02/15] getdtablesize change to os.getdtablesize --- .../next/Library/2024-09-24-14-15-34.gh-issue-83923.YunOLX.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 5b1a03c188aecc..0aeacf18826084 100644 --- 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 @@ -1,2 +1,2 @@ -Added :func:`getdtablesize` to :mod:`os` (supported only on unix-like +Added :func:`os.getdtablesize` to :mod:`os` (supported only on unix-like systems) From 5344b10e8da8a5d08ce00f071c33167eadbf4e4a Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 14:40:06 +0800 Subject: [PATCH 03/15] Added a blank line before clinic --- Modules/posixmodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index e705bd67fa2e8f..dd198b3e4fc5d0 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4218,6 +4218,7 @@ os_getcwdb_impl(PyObject *module) return posix_getcwd(1); } + /*[clinic input] os.getdtablesize From c7f0cfda4a4c87778848a673834cbe0ebff8f221 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 15:08:21 +0800 Subject: [PATCH 04/15] Add os.getdtablesize() unittest file --- Lib/test/test_os.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 1e570c757fccbc..e58cfc92ded22c 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -113,6 +113,18 @@ def test_getcwd(self): cwd = os.getcwd() self.assertIsInstance(cwd, str) + def test_getdtablesize(self): + curr_system = platform.system() + if curr_system == 'Windows': + try: + size = os.getdtablesize() + except NotImplementedError as e: + self.assertIsInstance(e, NotImplementedError) + else: + self.fail('No NotImplementedError is thrown') + else: + self.assertIsInstance(size, int) + 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 From a105d811b5fb45c8d404efeedbba5ce092702379 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 15:38:19 +0800 Subject: [PATCH 05/15] Change unittest --- Lib/test/test_os.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index e58cfc92ded22c..5bc2f6c3b26321 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -123,6 +123,7 @@ def test_getdtablesize(self): else: self.fail('No NotImplementedError is thrown') else: + size = os.getdtablesize() self.assertIsInstance(size, int) def test_getcwd_long_path(self): From f32ddb8c7ed3732604cc2765c9829808ea04a9be Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 17:29:57 +0800 Subject: [PATCH 06/15] Added os.getdtablesize() document --- Doc/library/os.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 035c71dcd6bf4a..b9009bcb1c1f48 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. + + .. warning:: + + This function currently only supports unix-like system. .. function:: getcwdb() From d65716d16ddb1c4fa596b6c8606f554f1123008d Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 18:53:55 +0800 Subject: [PATCH 07/15] Change os.getdtablesize document --- Doc/library/os.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index b9009bcb1c1f48..7dc85102b97f15 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -2233,9 +2233,9 @@ features: Return the maximum number of files a process can have open. - .. warning:: - - This function currently only supports unix-like system. + .. versionadded:: 3.14 + + .. availability:: Unix. .. function:: getcwdb() From fd7b35a233814f3a4f8aad1cf8facc7bf2a382e7 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 18:56:58 +0800 Subject: [PATCH 08/15] Change os.getdtablesize unittest --- Lib/test/test_os.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 5bc2f6c3b26321..43be3c45419420 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -113,18 +113,11 @@ def test_getcwd(self): cwd = os.getcwd() self.assertIsInstance(cwd, str) + @unittest.skipUnless(hasattr(os, 'getdtablesize'), 'need os.getdtablesize()') def test_getdtablesize(self): - curr_system = platform.system() - if curr_system == 'Windows': - try: - size = os.getdtablesize() - except NotImplementedError as e: - self.assertIsInstance(e, NotImplementedError) - else: - self.fail('No NotImplementedError is thrown') - else: - size = os.getdtablesize() - self.assertIsInstance(size, int) + size = os.getdtablesize() + self.assertIsInstance(size, int) + self.assertGreaterEqual(size, 0) def test_getcwd_long_path(self): # bpo-37412: On Linux, PATH_MAX is usually around 4096 bytes. On From ade11c79b72362286a8d3370713e07aabb64bee7 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 19:00:20 +0800 Subject: [PATCH 09/15] Add ifndef MS_WINDOWS to function --- Modules/posixmodule.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index dd198b3e4fc5d0..d367c5f084d9fa 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4218,7 +4218,7 @@ os_getcwdb_impl(PyObject *module) return posix_getcwd(1); } - +#ifndef MS_WINDOWS /*[clinic input] os.getdtablesize @@ -4228,24 +4228,12 @@ Return the maximum number of files a process can have open. static PyObject * os_getdtablesize_impl(PyObject *module) { - PyObject *max_fptable_size; - int size; + PyObject *size; -#ifdef MS_WINDOWS - PyErr_SetString(PyExc_NotImplementedError, - "this function is not supported on windows"); - if (PyErr_Occurred()) { - return NULL; - } -#else /* !Unix-like */ size = getdtablesize(); -#endif /* !MS_WINDOWS */ - max_fptable_size = PyLong_FromLong(size); - if (max_fptable_size == NULL) - return NULL; - - return max_fptable_size; + return PyLong_FromLong(size); } +#endif /* !MS_WINDOWS */ #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) #define HAVE_LINK 1 From 9d6d75673aec955ebf9b1a39084f0853897f336c Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 19:05:30 +0800 Subject: [PATCH 10/15] Delete skipUnless after space --- Lib/test/test_os.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 43be3c45419420..a3ae765ec10a53 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -113,7 +113,7 @@ def test_getcwd(self): cwd = os.getcwd() self.assertIsInstance(cwd, str) - @unittest.skipUnless(hasattr(os, 'getdtablesize'), 'need os.getdtablesize()') + @unittest.skipUnless(hasattr(os, 'getdtablesize'), 'need os.getdtablesize()') def test_getdtablesize(self): size = os.getdtablesize() self.assertIsInstance(size, int) From 4c1658b9b206124382f695b97ca9865c45941a99 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 19:07:54 +0800 Subject: [PATCH 11/15] getdtablesize added to AC_CHECK_FUNCS --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 \ From 7e77c2bed0e81e9bfee4b81e4e8e886a57af5261 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 19:10:02 +0800 Subject: [PATCH 12/15] Remove os.rst extra space --- Doc/library/os.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 7dc85102b97f15..46ee5fb9322819 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -2234,7 +2234,7 @@ features: Return the maximum number of files a process can have open. .. versionadded:: 3.14 - + .. availability:: Unix. .. function:: getcwdb() From ebb53882650b160fead9a3855106a81688622459 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 19:13:26 +0800 Subject: [PATCH 13/15] Change posixmodule.c --- Modules/posixmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index d367c5f084d9fa..d44b06d350d87c 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4228,7 +4228,7 @@ Return the maximum number of files a process can have open. static PyObject * os_getdtablesize_impl(PyObject *module) { - PyObject *size; + int size; size = getdtablesize(); return PyLong_FromLong(size); From 6cd7b658fae02dbba9a8e4894d4d8124746530f6 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 19:52:04 +0800 Subject: [PATCH 14/15] 0 change to 1 --- Lib/test/test_os.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index a3ae765ec10a53..fae2bdb9757fb4 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -117,7 +117,7 @@ def test_getcwd(self): def test_getdtablesize(self): size = os.getdtablesize() self.assertIsInstance(size, int) - self.assertGreaterEqual(size, 0) + self.assertGreaterEqual(size, 1) def test_getcwd_long_path(self): # bpo-37412: On Linux, PATH_MAX is usually around 4096 bytes. On From 83b7d56572d9a581a5faae9333d4627e858ceda1 Mon Sep 17 00:00:00 2001 From: ruang Date: Tue, 24 Sep 2024 21:32:31 +0800 Subject: [PATCH 15/15] Regen configure, pyconfig.h.in --- configure | 6 ++++++ pyconfig.h.in | 3 +++ 2 files changed, 9 insertions(+) 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/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