[Python-checkins] bpo-32110: codecs.StreamReader.read(n) now returns not more than n (GH-4499) (#4622)
Serhiy Storchaka
webhook-mailer at python.org
Tue Nov 28 19:06:56 EST 2017
https://github.com/python/cpython/commit/230ffeae0a3961b1769806bd722c26227c84e8da
commit: 230ffeae0a3961b1769806bd722c26227c84e8da
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2017-11-29T02:06:53+02:00
summary:
bpo-32110: codecs.StreamReader.read(n) now returns not more than n (GH-4499) (#4622)
characters/bytes for non-negative n. This makes it compatible with
read() methods of other file-like objects.
(cherry picked from commit 219c2de5ad0fdac825298bed1bb251f16956c04a)
files:
A Misc/NEWS.d/next/Library/2017-11-22-09-44-15.bpo-32110.VJa9bo.rst
M Lib/codecs.py
M Lib/test/test_codecs.py
diff --git a/Lib/codecs.py b/Lib/codecs.py
index 39ec8454acd..fd6c6f59d42 100644
--- a/Lib/codecs.py
+++ b/Lib/codecs.py
@@ -479,15 +479,17 @@ def read(self, size=-1, chars=-1, firstline=False):
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()
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index de6868a46c4..eb21a3915b9 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -196,19 +196,33 @@ def getreader():
self.assertEqual(f.read(), ''.join(lines[1:]))
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()
f = getreader()
self.assertEqual(f.readline(), lines[0])
self.assertEqual(f.readlines(), lines[1:])
self.assertEqual(f.read(), '')
- # Test read() followed by read()
+ # Test read(n) followed by read()
f = getreader()
self.assertEqual(f.read(size=40, chars=5), data[:5])
self.assertEqual(f.read(), data[5:])
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()
self.assertEqual(f.read(size=40, chars=5), data[:5])
self.assertEqual(f.readlines(), [lines[0][5:]] + lines[1:])
diff --git a/Misc/NEWS.d/next/Library/2017-11-22-09-44-15.bpo-32110.VJa9bo.rst b/Misc/NEWS.d/next/Library/2017-11-22-09-44-15.bpo-32110.VJa9bo.rst
new file mode 100644
index 00000000000..b57ff1acaff
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-11-22-09-44-15.bpo-32110.VJa9bo.rst
@@ -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.
More information about the Python-checkins
mailing list