8000 gh-110673: test_pty raises on short write · python/cpython@b5ebb45 · GitHub
[go: up one dir, main page]

Skip to content

Commit b5ebb45

Browse files
committed
gh-110673: test_pty raises on short write
Add write_all() helper function to test_pty to raise an exception on short write: if os.writes() does not write all bytes. It should not happen for a PTY.
1 parent c523ce0 commit b5ebb45

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Lib/test/test_pty.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ def expectedFailureIfStdinIsTTY(fun):
7676
pass
7777
return fun
7878

79+
80+
def write_all(fd, data):
81+
written = os.write(fd, data)
82+
if written != len(data):
83+
# gh-73256, gh-110673: It should never happen, but check just in case
84+
raise Exception(f"short write: os.write({fd}, {len(data)} bytes) "
85+
f"wrote {written} bytes")
86+
87+
7988
# Marginal testing of pty suite. Cannot do extensive 'do or fail' testing
8089
# because pty code is not too portable.
8190
class PtyTest(unittest.TestCase):
@@ -170,14 +179,14 @@ def test_openpty(self):
170179
os.set_blocking(master_fd, blocking)
171180

172181
debug("Writing to slave_fd")
173-
os.write(slave_fd, TEST_STRING_1)
182+
write_all(slave_fd, TEST_STRING_1)
174183
s1 = _readline(master_fd)
175184
self.assertEqual(b'I wish to buy a fish license.\n',
176185
normalize_output(s1))
177186

178187
debug("Writing chunked output")
179-
os.write(slave_fd, TEST_STRING_2[:5])
180-
os.write(slave_fd, TEST_STRING_2[5:])
188+
write_all(slave_fd, TEST_STRING_2[:5])
189+
write_all(slave_fd, TEST_STRING_2[5:])
181190
s2 = _readline(master_fd)
182191
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
183192

@@ -360,8 +369,8 @@ def test__copy_to_each(self):
360369
masters = [s.fileno() for s in socketpair]
361370

362371
# Feed data. Smaller than PIPEBUF. These writes will not block.
363-
os.write(masters[1], b'from master')
364-
os.write(write_to_stdin_fd, b'from stdin')
372+
write_all(masters[1], b'from master')
373+
write_all(write_to_stdin_fd, b'from stdin')
365374

366375
# Expect three select calls, the last one will cause IndexError
367376
pty.select = self._mock_select

0 commit comments

Comments
 (0)
0