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

Commit 9f6df8c

Browse filesBrowse files
Alexey Izbyshevmiss-islington
Alexey Izbyshev
authored andcommitted
pythongh-102179: Fix os.dup2 error reporting for negative fds (pythonGH-102180)
(cherry picked from commit c2bd55d) Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
1 parent 6c2e052 commit 9f6df8c
Copy full SHA for 9f6df8c

File tree

Expand file treeCollapse file tree

3 files changed

+21
-5
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+21
-5
lines changed

‎Lib/test/test_os.py

Copy file name to clipboardExpand all lines: Lib/test/test_os.py
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,26 @@ 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+
@unittest.skipIf(
2148+
support.is_emscripten,
2149+
"dup2() with negative fds is broken on Emscripten (see gh-102179)"
2150+
)
2151+
def test_dup2_negative_fd(self):
2152+
valid_fd = os.open(__file__, os.O_RDONLY)
2153+
self.addCleanup(os.close, valid_fd)
2154+
fds = [
2155+
valid_fd,
2156+
-1,
2157+
-2**31,
2158+
]
2159+
for fd, fd2 in itertools.product(fds, repeat=2):
2160+
if fd != fd2:
2161+
with self.subTest(fd=fd, fd2=fd2):
2162+
with self.assertRaises(OSError) as ctx:
2163+
os.dup2(fd, fd2)
2164+
self.assertEqual(ctx.exception.errno, errno.EBADF)
2165+
21462166
@unittest.skipUnless(hasattr(os, 'fchmod'), 'test needs os.fchmod()')
21472167
def test_fchmod(self):
21482168
self.check(os.fchmod, 0)
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :func:`os.dup2` error message for negative fds.

‎Modules/posixmodule.c

Copy file name to clipboardExpand all lines: Modules/posixmodule.c
-5Lines changed: 0 additions & 5 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.