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.charbuffer = self._empty_charbuffer.join(self.linebuffer)
self.linebuffer = None 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) # read until we get the required number of characters (if available)
while True: while True:
# can the request be satisfied from the character buffer? # can the request be satisfied from the character buffer?
if chars >= 0: if chars >= 0:
if len(self.charbuffer) >= chars: if len(self.charbuffer) >= chars:
break break
elif size >= 0:
if len(self.charbuffer) >= size:
break
# we need more data # we need more data
if size < 0: if size < 0:
newdata = self.stream.read() newdata = self.stream.read()

View File

@ -196,19 +196,33 @@ class ReadTest(MixInCheckStateHandling):
self.assertEqual(f.read(), ''.join(lines[1:])) self.assertEqual(f.read(), ''.join(lines[1:]))
self.assertEqual(f.read(), '') self.assertEqual(f.read(), '')
# Issue #32110: Test readline() followed by read(n)
f = getreader()
self.assertEqual(f.readline(), lines[0])
self.assertEqual(f.read(1), lines[1][0])
self.assertEqual(f.read(0), '')
self.assertEqual(f.read(100), data[len(lines[0]) + 1:][:100])
# Issue #16636: Test readline() followed by readlines() # Issue #16636: Test readline() followed by readlines()
f = getreader() f = getreader()
self.assertEqual(f.readline(), lines[0]) self.assertEqual(f.readline(), lines[0])
self.assertEqual(f.readlines(), lines[1:]) self.assertEqual(f.readlines(), lines[1:])
self.assertEqual(f.read(), '') self.assertEqual(f.read(), '')
# Test read() followed by read() # Test read(n) followed by read()
f = getreader() f = getreader()
self.assertEqual(f.read(size=40, chars=5), data[:5]) self.assertEqual(f.read(size=40, chars=5), data[:5])
self.assertEqual(f.read(), data[5:]) self.assertEqual(f.read(), data[5:])
self.assertEqual(f.read(), '') self.assertEqual(f.read(), '')
# Issue #12446: Test read() followed by readlines() # Issue #32110: Test read(n) followed by read(n)
f = getreader()
self.assertEqual(f.read(size=40, chars=5), data[:5])
self.assertEqual(f.read(1), data[5])
self.assertEqual(f.read(0), '')
self.assertEqual(f.read(100), data[6:106])
# Issue #12446: Test read(n) followed by readlines()
f = getreader() f = getreader()
self.assertEqual(f.read(size=40, chars=5), data[:5]) self.assertEqual(f.read(size=40, chars=5), data[:5])
self.assertEqual(f.readlines(), [lines[0][5:]] + lines[1:]) self.assertEqual(f.readlines(), [lines[0][5:]] + lines[1:])

View File

@ -0,0 +1,3 @@
``codecs.StreamReader.read(n)`` now returns not more than *n*
characters/bytes for non-negative *n*. This makes it compatible with
``read()`` methods of other file-like objects.