diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 47b79fcc79..c8cd0d54e5 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -7783,11 +7783,6 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ { int res; -#if defined(HAVE_DUP3) && \ - !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) - /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ - int dup3_works = -1; -#endif if (fd < 0 || fd2 < 0) { posix_error(); @@ -7831,38 +7826,35 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) #else #ifdef HAVE_DUP3 - if (!inheritable && dup3_works != 0) { + if (!inheritable) { 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) { + /* Use dup2() if dup3() fails with ENOSYS, dup3() is available on + * Linux 2.6.27+ and glibc 2.9. */ + if (errno != ENOSYS) { posix_error(); return -1; } + } else { + return res; } } - if (inheritable || dup3_works == 0) - { #endif - Py_BEGIN_ALLOW_THREADS - res = dup2(fd, fd2); - Py_END_ALLOW_THREADS - if (res < 0) { - posix_error(); - return -1; - } + Py_BEGIN_ALLOW_THREADS + res = dup2(fd, fd2); + Py_END_ALLOW_THREADS + if (res < 0) { + posix_error(); + return -1; + } - if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { - close(fd2); - return -1; - } -#ifdef HAVE_DUP3 + if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { + close(fd2); + return -1; } -#endif #endif