mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
[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 c2bd55d26f)
Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
This commit is contained in:
parent
fe36778968
commit
925ebfbfd2
3 changed files with 17 additions and 5 deletions
|
|
@ -2143,6 +2143,22 @@ def test_closerange(self):
|
|||
def test_dup2(self):
|
||||
self.check(os.dup2, 20)
|
||||
|
||||
@unittest.skipUnless(hasattr(os, 'dup2'), 'test needs os.dup2()')
|
||||
def test_dup2_negative_fd(self):
|
||||
valid_fd = os.open(__file__, os.O_RDONLY)
|
||||
self.addCleanup(os.close, valid_fd)
|
||||
fds = [
|
||||
valid_fd,
|
||||
-1,
|
||||
-2**31,
|
||||
]
|
||||
for fd, fd2 in itertools.product(fds, repeat=2):
|
||||
if fd != fd2:
|
||||
with self.subTest(fd=fd, fd2=fd2):
|
||||
with self.assertRaises(OSError) as ctx:
|
||||
os.dup2(fd, fd2)
|
||||
self.assertEqual(ctx.exception.errno, errno.EBADF)
|
||||
|
||||
@unittest.skipUnless(hasattr(os, 'fchmod'), 'test needs os.fchmod()')
|
||||
def test_fchmod(self):
|
||||
self.check(os.fchmod, 0)
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fix :func:`os.dup2` error message for negative fds.
|
||||
|
|
@ -9282,11 +9282,6 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
|
|||
static int dup3_works = -1;
|
||||
#endif
|
||||
|
||||
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
|
||||
* upon close(), and therefore below.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue