summaryrefslogtreecommitdiffstats
path: root/Modules/posixmodule.c
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2017-12-29 21:13:06 (GMT)
committerGitHub <noreply@github.com>2017-12-29 21:13:06 (GMT)
commitbbdb17d19bb1d5443ca4417254e014ad64c04540 (patch)
treefc518799f5fedc84812666d3994fa19aec5f2f8d /Modules/posixmodule.c
parent03220fdb26c0b6a50ce5ed1fdfbf232094b66db6 (diff)
downloadcpython-bbdb17d19bb1d5443ca4417254e014ad64c04540.zip
cpython-bbdb17d19bb1d5443ca4417254e014ad64c04540.tar.gz
cpython-bbdb17d19bb1d5443ca4417254e014ad64c04540.tar.bz2
return the new file descriptor from os.dup2 (closes bpo-32441) (#5041)
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r--Modules/posixmodule.c43
1 files changed, 27 insertions, 16 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 38b6c80..47b79fc 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -7770,7 +7770,7 @@ os_dup_impl(PyObject *module, int fd)
/*[clinic input]
-os.dup2
+os.dup2 -> int
fd: int
fd2: int
inheritable: bool=True
@@ -7778,9 +7778,9 @@ os.dup2
Duplicate file descriptor.
[clinic start generated code]*/
-static PyObject *
+static int
os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
-/*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f]*/
+/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
{
int res;
#if defined(HAVE_DUP3) && \
@@ -7789,8 +7789,10 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
int dup3_works = -1;
#endif
- if (fd < 0 || fd2 < 0)
- return posix_error();
+ if (fd < 0 || fd2 < 0) {
+ posix_error();
+ return -1;
+ }
/* dup2() can fail with EINTR if the target FD is already open, because it
* 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)
res = dup2(fd, fd2);
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
- if (res < 0)
- return posix_error();
+ if (res < 0) {
+ posix_error();
+ return -1;
+ }
+ res = fd2; // msvcrt dup2 returns 0 on success.
/* Character files like console cannot be make non-inheritable */
if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
close(fd2);
- return NULL;
+ return -1;
}
#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
@@ -7818,8 +7823,10 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
else
res = dup2(fd, fd2);
Py_END_ALLOW_THREADS
- if (res < 0)
- return posix_error();
+ if (res < 0) {
+ posix_error();
+ return -1;
+ }
#else
@@ -7831,8 +7838,10 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
if (res < 0) {
if (dup3_works == -1)
dup3_works = (errno != ENOSYS);
- if (dup3_works)
- return posix_error();
+ if (dup3_works) {
+ posix_error();
+ return -1;
+ }
}
}
@@ -7842,12 +7851,14 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Py_BEGIN_ALLOW_THREADS
res = dup2(fd, fd2);
Py_END_ALLOW_THREADS
- if (res < 0)
- return posix_error();
+ if (res < 0) {
+ posix_error();
+ return -1;
+ }
if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
close(fd2);
- return NULL;
+ return -1;
}
#ifdef HAVE_DUP3
}
@@ -7855,7 +7866,7 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
#endif
- Py_RETURN_NONE;
+ return res;
}