@@ -7770,17 +7770,17 @@ os_dup_impl(PyObject *module, int fd)
77707770
77717771
77727772/*[clinic input]
7773- os.dup2
7773+ os.dup2 -> int
77747774 fd: int
77757775 fd2: int
77767776 inheritable: bool=True
77777777
77787778Duplicate file descriptor.
77797779[clinic start generated code]*/
77807780
7781- static PyObject *
7781+ static int
77827782os_dup2_impl (PyObject * module , int fd , int fd2 , int inheritable )
7783- /*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f ]*/
7783+ /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d ]*/
77847784{
77857785 int res ;
77867786#if defined(HAVE_DUP3 ) && \
@@ -7789,8 +7789,10 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
77897789 int dup3_works = -1 ;
77907790#endif
77917791
7792- if (fd < 0 || fd2 < 0 )
7793- return posix_error ();
7792+ if (fd < 0 || fd2 < 0 ) {
7793+ posix_error ();
7794+ return -1 ;
7795+ }
77947796
77957797 /* dup2() can fail with EINTR if the target FD is already open, because it
77967798 * then has to be closed. See os_close_impl() for why we don't handle EINTR
@@ -7802,13 +7804,16 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
78027804 res = dup2 (fd , fd2 );
78037805 _Py_END_SUPPRESS_IPH
78047806 Py_END_ALLOW_THREADS
7805- if (res < 0 )
7806- return posix_error ();
7807+ if (res < 0 ) {
7808+ posix_error ();
7809+ return -1 ;
7810+ }
7811+ res = fd2 ; // msvcrt dup2 returns 0 on success.
78077812
78087813 /* Character files like console cannot be make non-inheritable */
78097814 if (!inheritable && _Py_set_inheritable (fd2 , 0 , NULL ) < 0 ) {
78107815 close (fd2 );
7811- return NULL ;
7816+ return -1 ;
78127817 }
78137818
78147819#elif defined(HAVE_FCNTL_H ) && defined(F_DUP2FD_CLOEXEC )
@@ -7818,8 +7823,10 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
78187823 else
78197824 res = dup2 (fd , fd2 );
78207825 Py_END_ALLOW_THREADS
7821- if (res < 0 )
7822- return posix_error ();
7826+ if (res < 0 ) {
7827+ posix_error ();
7828+ return -1 ;
7829+ }
78237830
78247831#else
78257832
@@ -7831,8 +7838,10 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
78317838 if (res < 0 ) {
78327839 if (dup3_works == -1 )
78337840 dup3_works = (errno != ENOSYS );
7834- if (dup3_works )
7835- return posix_error ();
7841+ if (dup3_works ) {
7842+ posix_error ();
7843+ return -1 ;
7844+ }
78367845 }
78377846 }
78387847
@@ -7842,20 +7851,22 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
78427851 Py_BEGIN_ALLOW_THREADS
78437852 res = dup2 (fd , fd2 );
78447853 Py_END_ALLOW_THREADS
7845- if (res < 0 )
7846- return posix_error ();
7854+ if (res < 0 ) {
7855+ posix_error ();
7856+ return -1 ;
7857+ }
78477858
78487859 if (!inheritable && _Py_set_inheritable (fd2 , 0 , NULL ) < 0 ) {
78497860 close (fd2 );
7850- return NULL ;
7861+ return -1 ;
78517862 }
78527863#ifdef HAVE_DUP3
78537864 }
78547865#endif
78557866
78567867#endif
78577868
7858- Py_RETURN_NONE ;
7869+ return res ;
78597870}
78607871
78617872
0 commit comments