@@ -989,6 +989,7 @@ typedef struct {
989989 PyObject * struct_rusage ;
990990#endif
991991 PyObject * st_mode ;
992+ int dup3_works ;
992993} _posixstate ;
993994
994995
@@ -9407,11 +9408,6 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
94079408/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
94089409{
94099410 int res = 0 ;
9410- #if defined(HAVE_DUP3 ) && \
9411- !(defined(HAVE_FCNTL_H ) && defined(F_DUP2FD_CLOEXEC ))
9412- /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
9413- static int dup3_works = -1 ;
9414- #endif
94159411
94169412 if (fd < 0 || fd2 < 0 ) {
94179413 posix_error ();
@@ -9455,21 +9451,22 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
94559451#else
94569452
94579453#ifdef HAVE_DUP3
9458- if (!inheritable && dup3_works != 0 ) {
9454+ _posixstate * state = get_posix_state (module );
9455+ if (!inheritable && state -> dup3_works != 0 ) {
94599456 Py_BEGIN_ALLOW_THREADS
94609457 res = dup3 (fd , fd2 , O_CLOEXEC );
94619458 Py_END_ALLOW_THREADS
94629459 if (res < 0 ) {
9463- if (dup3_works == -1 )
9460+ if (state -> dup3_works == -1 )
94649461 dup3_works = (errno != ENOSYS );
9465- if (dup3_works ) {
9462+ if (state -> dup3_works ) {
94669463 posix_error ();
94679464 return -1 ;
94689465 }
94699466 }
94709467 }
94719468
9472- if (inheritable || dup3_works == 0 )
9469+ if (inheritable || state -> dup3_works == 0 )
94739470 {
94749471#endif
94759472 Py_BEGIN_ALLOW_THREADS
@@ -15872,6 +15869,16 @@ posixmodule_exec(PyObject *m)
1587215869{
1587315870 _posixstate * state = get_posix_state (m );
1587415871
15872+ #if defined(HAVE_DUP3 ) && \
15873+ !(defined(HAVE_FCNTL_H ) && defined(F_DUP2FD_CLOEXEC ))
15874+ /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
15875+ state -> dup3_works = -1 ;
15876+ #elif !defined(HAVE_DUP3 )
15877+ state -> dup3_works = -1 ;
15878+ #else
15879+ state -> dup3_works = 0 ;
15880+ #endif
15881+
1587515882#if defined(HAVE_PWRITEV )
1587615883 if (HAVE_PWRITEV_RUNTIME ) {} else {
1587715884 PyObject * dct = PyModule_GetDict (m );
0 commit comments