Skip to content

Commit 4668e15

Browse files
committed
gh-85393: Preserve compatibility with writers returning None
1 parent 32ca4f8 commit 4668e15

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎Lib/_pyio.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,8 @@ def write(self, s):
23112311
while True:
23122312
n = self.buffer.write(b)
23132313
if n is None:
2314-
raise BlockingIOError("write returned None")
2314+
# Some legacy writers return None after writing all the data.
2315+
break
23152316
if n < 0 or n > len(b) or (n == 0 and b):
23162317
raise OSError("write() returned incorrect number of bytes")
23172318
b = b[n:]

‎Lib/test/test_io/test_textio.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,10 +1032,16 @@ def write(self, b):
10321032
self.assertEqual(txt.write('a\u20acb'), 3)
10331033
self.assertEqual(b''.join(raw._write_stack), b'a\xe2\x82\xacb')
10341034

1035+
def test_rawio_write_none(self):
1036+
raw = self.MockRawIO()
1037+
raw.write = raw._write_stack.append
1038+
with self.TextIOWrapper(raw, encoding='ascii', write_through=True) as txt:
1039+
self.assertEqual(txt.write('abc'), 3)
1040+
self.assertEqual(raw._write_stack, [b'abc'])
1041+
10351042
def test_rawio_write_invalid_result(self):
10361043
for result, error in ((0, OSError), (-1, OSError), (4, OSError),
1037-
(None, BlockingIOError), ('1', TypeError),
1038-
(1.5, TypeError)):
1044+
('1', TypeError), (1.5, TypeError)):
10391045
with self.subTest(result=result):
10401046
raw = self.MockRawIO()
10411047
raw.write = lambda b: result

‎Modules/_io/textio.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,9 +1735,9 @@ _textiowrapper_writeflush(textio *self)
17351735
goto error;
17361736
}
17371737
if (ret == Py_None) {
1738+
/* Some legacy writers return None after writing all the data. */
17381739
Py_DECREF(ret);
1739-
PyErr_SetString(PyExc_BlockingIOError, "write returned None");
1740-
goto error;
1740+
break;
17411741
}
17421742
Py_ssize_t n = PyNumber_AsSsize_t(ret, PyExc_OverflowError);
17431743
Py_DECREF(ret);

0 commit comments

Comments
 (0)