Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-102179: Fix os.dup2() error reporting for negative fds #102180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-102179: Fix os.dup2() error reporting for negative fds
A remnant of old code manually checks the sign of fds, but doesn't set
errno before raising OSError:
```
>>> os.dup2(-1, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  OSError: [Errno 0] Error
```
Remove the manual check altogether: the C library functions used to
implement os.dup2() check fd validity anyway.
  • Loading branch information
Alexey Izbyshev committed Feb 23, 2023
commit 411107d5a8df5fa11a2cd89d89811c8811d77cd4
16 changes: 16 additions & 0 deletions 16 Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``os.dup2()`` error reporting for negative fds.
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 0 additions & 5 deletions 5 Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9829,11 +9829,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.
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.