Skip to content

Commit cc6ce90

Browse files
miss-islingtonAlexey Izbyshev
and
Alexey Izbyshev
authored
gh-102179: Fix os.dup2 error reporting for negative fds (GH-102180)
(cherry picked from commit c2bd55d) Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
1 parent 06a3bb8 commit cc6ce90

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

Lib/test/test_os.py

+20
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,26 @@ def test_closerange(self):
21762176
def test_dup2(self):
21772177
self.check(os.dup2, 20)
21782178

2179+
@unittest.skipUnless(hasattr(os, 'dup2'), 'test needs os.dup2()')
2180+
@unittest.skipIf(
2181+
support.is_emscripten,
2182+
"dup2() with negative fds is broken on Emscripten (see gh-102179)"
2183+
)
2184+
def test_dup2_negative_fd(self):
2185+
valid_fd = os.open(__file__, os.O_RDONLY)
2186+
self.addCleanup(os.close, valid_fd)
2187+
fds = [
2188+
valid_fd,
2189+
-1,
2190+
-2**31,
2191+
]
2192+
for fd, fd2 in itertools.product(fds, repeat=2):
2193+
if fd != fd2:
2194+
with self.subTest(fd=fd, fd2=fd2):
2195+
with self.assertRaises(OSError) as ctx:
2196+
os.dup2(fd, fd2)
2197+
self.assertEqual(ctx.exception.errno, errno.EBADF)
2198+
21792199
@unittest.skipUnless(hasattr(os, 'fchmod'), 'test needs os.fchmod()')
21802200
def test_fchmod(self):
21812201
self.check(os.fchmod, 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :func:`os.dup2` error message for negative fds.

Modules/posixmodule.c

-5
Original file line numberDiff line numberDiff line change
@@ -9387,11 +9387,6 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
93879387
static int dup3_works = -1;
93889388
#endif
93899389

9390-
if (fd < 0 || fd2 < 0) {
9391-
posix_error();
9392-
return -1;
9393-
}
9394-
93959390
/* dup2() can fail with EINTR if the target FD is already open, because it
93969391
* then has to be closed. See os_close_impl() for why we don't handle EINTR
93979392
* upon close(), and therefore below.

0 commit comments

Comments
 (0)