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

Skip to content

Commit 9b3d056

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. Let's check if this assumption is correct.
1 parent 790ecf6 commit 9b3d056

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Lib/test/test_pty.py

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

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

172183
debug("Writing to slave_fd")
173-
os.write(slave_fd, TEST_STRING_1)
184+
write_all(slave_fd, TEST_STRING_1)
174185
s1 = _readline(master_fd)
175186
self.assertEqual(b'I wish to buy a fish license.\n',
176187
normalize_output(s1))
177188

178189
debug("Writing chunked output")
179-
os.write(slave_fd, TEST_STRING_2[:5])
180-
os.write(slave_fd, TEST_STRING_2[5:])
190+
write_all(slave_fd, TEST_STRING_2[:5])
191+
write_all(slave_fd, TEST_STRING_2[5:])
181192
s2 = _readline(master_fd)
182193
self.assertEqual(b'For my pet fish, Eric.\n', normalize_output(s2))
183194

@@ -360,8 +371,8 @@ def test__copy_to_each(self):
360371
masters = [s.fileno() for s in socketpair]
361372

362373
# 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')
374+
write_all(masters[1], b'from master')
375+
write_all(write_to_stdin_fd, b'from stdin')
365376

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

0 commit comments

Comments
 (0)
0