diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-12-15-15-46-30.gh-issue-100227.ae9uXR.rst b/Misc/NEWS.d/next/Core and Builtins/2022-12-15-15-46-30.gh-issue-100227.ae9uXR.rst new file mode 100644 index 00000000000000..501b1720e57677 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-12-15-15-46-30.gh-issue-100227.ae9uXR.rst @@ -0,0 +1,2 @@ +Port dup3_works of :mod:`posix` module to the module state variable. Patch +by Dong-hee Na. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 4817973262f484..2d05652a4be920 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -989,6 +989,7 @@ typedef struct { PyObject *struct_rusage; #endif PyObject *st_mode; + int dup3_works; } _posixstate; @@ -9407,11 +9408,6 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ { int res = 0; -#if defined(HAVE_DUP3) && \ - !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) - /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ - static int dup3_works = -1; -#endif if (fd < 0 || fd2 < 0) { posix_error(); @@ -9455,21 +9451,23 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) #else #ifdef HAVE_DUP3 - if (!inheritable && dup3_works != 0) { + _posixstate *state = get_posix_state(module); + if (!inheritable && state->dup3_works != 0) { Py_BEGIN_ALLOW_THREADS res = dup3(fd, fd2, O_CLOEXEC); Py_END_ALLOW_THREADS if (res < 0) { - if (dup3_works == -1) - dup3_works = (errno != ENOSYS); - if (dup3_works) { + if (state->dup3_works == -1) { + state->dup3_works = (errno != ENOSYS); + } + if (state->dup3_works) { posix_error(); return -1; } } } - if (inheritable || dup3_works == 0) + if (inheritable || state->dup3_works == 0) { #endif Py_BEGIN_ALLOW_THREADS @@ -15872,6 +15870,16 @@ posixmodule_exec(PyObject *m) { _posixstate *state = get_posix_state(m); +#if defined(HAVE_DUP3) && \ + !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) + /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ + state->dup3_works = -1; +#elif !defined(HAVE_DUP3) + state->dup3_works = -1; +#else + state->dup3_works = 0; +#endif + #if defined(HAVE_PWRITEV) if (HAVE_PWRITEV_RUNTIME) {} else { PyObject* dct = PyModule_GetDict(m); diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv index c71fc0d958216c..4de5fe30177c8b 100644 --- a/Tools/c-analyzer/cpython/ignored.tsv +++ b/Tools/c-analyzer/cpython/ignored.tsv @@ -18,9 +18,6 @@ filename funcname name reason Python/bootstrap_hash.c py_getrandom getrandom_works - Python/fileutils.c - _Py_open_cloexec_works - Python/fileutils.c set_inheritable ioctl_works - -# (set lazily, *after* first init) -# XXX Is this thread-safe? -Modules/posixmodule.c os_dup2_impl dup3_works - ## guards around resource init Python/thread_pthread.h PyThread__init_thread lib_initialized -