Skip to content

Commit 925ebfb

Browse files
miss-islingtonAlexey Izbyshevkumaraditya303
authored
[3.10] gh-102179: Fix os.dup2 error reporting for negative fds (GH-102180) (#102419)
* gh-102179: Fix `os.dup2` error reporting for negative fds (GH-102180) (cherry picked from commit c2bd55d) Co-authored-by: Alexey Izbyshev <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
1 parent fe36778 commit 925ebfb

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Lib/test/test_os.py

+16
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,22 @@ def test_closerange(self):
21432143
def test_dup2(self):
21442144
self.check(os.dup2, 20)
21452145

2146+
@unittest.skipUnless(hasattr(os, 'dup2'), 'test needs os.dup2()')
2147+
def test_dup2_negative_fd(self):
2148+
valid_fd = os.open(__file__, os.O_RDONLY)
2149+
self.addCleanup(os.close, valid_fd)
2150+
fds = [
2151+
valid_fd,
2152+
-1,
2153+
-2**31,
2154+
]
2155+
for fd, fd2 in itertools.product(fds, repeat=2):
2156+
if fd != fd2:
2157+
with self.subTest(fd=fd, fd2=fd2):
2158+
with self.assertRaises(OSError) as ctx:
2159+
os.dup2(fd, fd2)
2160+
self.assertEqual(ctx.exception.errno, errno.EBADF)
2161+
21462162
@unittest.skipUnless(hasattr(os, 'fchmod'), 'test needs os.fchmod()')
21472163
def test_fchmod(self):
21482164
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
@@ -9282,11 +9282,6 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
92829282
static int dup3_works = -1;
92839283
#endif
92849284

9285-
if (fd < 0 || fd2 < 0) {
9286-
posix_error();
9287-
return -1;
9288-
}
9289-
92909285
/* dup2() can fail with EINTR if the target FD is already open, because it
92919286
* then has to be closed. See os_close_impl() for why we don't handle EINTR
92929287
* upon close(), and therefore below.

0 commit comments

Comments
 (0)