bpo-32110: codecs.StreamReader.read(n) now returns not more than n (#4499)

characters/bytes for non-negative n.  This makes it compatible with
read() methods of other file-like objects.
This commit is contained in:
Serhiy Storchaka 2017-11-29 01:30:00 +02:00 committed by GitHub
parent 23df2d1304
commit 219c2de5ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View file

@ -480,15 +480,17 @@ class StreamReader(Codec):
self.charbuffer = self._empty_charbuffer.join(self.linebuffer)
self.linebuffer = None
if chars < 0:
# For compatibility with other read() methods that take a
# single argument
chars = size
# read until we get the required number of characters (if available)
while True:
# can the request be satisfied from the character buffer?
if chars >= 0:
if len(self.charbuffer) >= chars:
break
elif size >= 0:
if len(self.charbuffer) >= size:
break
# we need more data
if size < 0:
newdata = self.stream.read()