Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a80987f

Browse files
author
Victor Stinner
committed
Issue #12175: RawIOBase.readall() now returns None if read() returns None.
1 parent b79f28c commit a80987f

4 files changed

Lines changed: 20 additions & 3 deletions

File tree

Lib/_pyio.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,11 @@ def readall(self):
556556
if not data:
557557
break
558558
res += data
559-
return bytes(res)
559+
if res:
560+
return bytes(res)
561+
else:
562+
# b'' or None
563+
return data
560564

561565
def readinto(self, b):
562566
"""Read up to len(b) bytes into b.

Lib/test/test_io.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,14 +790,17 @@ def test_read_non_blocking(self):
790790
# Inject some None's in there to simulate EWOULDBLOCK
791791
rawio = self.MockRawIO((b"abc", b"d", None, b"efg", None, None, None))
792792
bufio = self.tp(rawio)
793-
794793
self.assertEqual(b"abcd", bufio.read(6))
795794
self.assertEqual(b"e", bufio.read(1))
796795
self.assertEqual(b"fg", bufio.read())
797796
self.assertEqual(b"", bufio.peek(1))
798-
self.assertTrue(None is bufio.read())
797+
self.assertIsNone(bufio.read())
799798
self.assertEqual(b"", bufio.read())
800799

800+
rawio = self.MockRawIO((b"a", None, None))
801+
self.assertEqual(b"a", rawio.readall())
802+
self.assertIsNone(rawio.readall())
803+
801804
def test_read_past_eof(self):
802805
rawio = self.MockRawIO((b"abc", b"d", b"efg"))
803806
bufio = self.tp(rawio)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Core and Builtins
7575
Library
7676
-------
7777

78+
- Issue #12175: RawIOBase.readall() now returns None if read() returns None.
79+
7880
- Issue #12175: FileIO.readall() now raises a ValueError instead of an IOError
7981
if the file is closed.
8082

Modules/_io/iobase.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,14 @@ rawiobase_readall(PyObject *self, PyObject *args)
814814
Py_DECREF(chunks);
815815
return NULL;
816816
}
817+
if (data == Py_None) {
818+
if (PyList_GET_SIZE(chunks) == 0) {
819+
Py_DECREF(chunks);
820+
return data;
821+
}
822+
Py_DECREF(data);
823+
break;
824+
}
817825
if (!PyBytes_Check(data)) {
818826
Py_DECREF(chunks);
819827
Py_DECREF(data);

0 commit comments

Comments
 (0)