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

Skip to content

Commit d2780ae

Browse files
author
Victor Stinner
committed
(Merge 3.2) Issue #12175: RawIOBase.readall() now returns None if read()
returns None.
2 parents af62c7d + 988512c commit d2780ae

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

Lib/_pyio.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,11 @@ def readall(self):
558558
if not data:
559559
break
560560
res += data
561-
return bytes(res)
561+
if res:
562+
return bytes(res)
563+
else:
564+
# b'' or None
565+
return data
562566

563567
def readinto(self, b):
564568
"""Read up to len(b) bytes into bytearray b.

Lib/test/test_io.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,14 +833,17 @@ def test_read_non_blocking(self):
833833
# Inject some None's in there to simulate EWOULDBLOCK
834834
rawio = self.MockRawIO((b"abc", b"d", None, b"efg", None, None, None))
835835
bufio = self.tp(rawio)
836-
837836
self.assertEqual(b"abcd", bufio.read(6))
838837
self.assertEqual(b"e", bufio.read(1))
839838
self.assertEqual(b"fg", bufio.read())
840839
self.assertEqual(b"", bufio.peek(1))
841-
self.assertTrue(None is bufio.read())
840+
self.assertIsNone(bufio.read())
842841
self.assertEqual(b"", bufio.read())
843842

843+
rawio = self.MockRawIO((b"a", None, None))
844+
self.assertEqual(b"a", rawio.readall())
845+
self.assertIsNone(rawio.readall())
846+
844847
def test_read_past_eof(self):
845848
rawio = self.MockRawIO((b"abc", b"d", b"efg"))
846849
bufio = self.tp(rawio)

Misc/NEWS

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,16 @@ Core and Builtins
161161
Library
162162
-------
163163

164-
- Issue #11109 - New service_action method for BaseServer, used by
165-
ForkingMixin class for cleanup. Initial Patch by Justin Wark.
166-
167-
- Issue #12045: Avoid duplicate execution of command in ctypes.util._get_soname().
168-
Patch by Sijin Joseph.
164+
- Issue #12175: RawIOBase.readall() now returns None if read() returns None.
165+
166+
- Issue #12175: FileIO.readall() now raises a ValueError instead of an IOError
167+
if the file is closed.
168+
169+
- Issue #11109: New service_action method for BaseServer, used by ForkingMixin
170+
class for cleanup. Initial Patch by Justin Wark.
171+
172+
- Issue #12045: Avoid duplicate execution of command in
173+
ctypes.util._get_soname(). Patch by Sijin Joseph.
169174

170175
- Issue #10818: Remove the Tk GUI and the serve() function of the pydoc module,
171176
pydoc -g has been deprecated in Python 3.2 and it has a new enhanced web
@@ -1062,9 +1067,6 @@ Core and Builtins
10621067
Library
10631068
-------
10641069

1065-
- Issue #12175: FileIO.readall() now raises a ValueError instead of an IOError
1066-
if the file is closed.
1067-
10681070
- Issue #10916: mmap should not segfault when a file is mapped using 0 as length
10691071
and a non-zero offset, and an attempt to read past the end of file is made
10701072
(IndexError is raised instead). Patch by Ross Lagerwall.

Modules/_io/iobase.c

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

0 commit comments

Comments
 (0)