changeset: 88727:e24265eb2271 branch: 2.7 parent: 88707:25f6da4c49c2 user: Serhiy Storchaka date: Sun Jan 26 19:20:24 2014 +0200 files: Lib/codecs.py Lib/test/test_codecs.py Misc/NEWS description: Issue #8260: The read(), readline() and readlines() methods of codecs.StreamReader returned incomplete data when were called after readline() or read(size). Based on patch by Amaury Forgeot d'Arc. diff -r 25f6da4c49c2 -r e24265eb2271 Lib/codecs.py --- a/Lib/codecs.py Sat Jan 25 13:27:06 2014 -0500 +++ b/Lib/codecs.py Sun Jan 26 19:20:24 2014 +0200 @@ -456,15 +456,12 @@ # read until we get the required number of characters (if available) while True: - # can the request can be satisfied from the character buffer? - if chars < 0: - if size < 0: - if self.charbuffer: - break - elif len(self.charbuffer)>= size: + # can the request be satisfied from the character buffer? + if chars>= 0: + if len(self.charbuffer)>= chars: break - else: - if len(self.charbuffer)>= chars: + elif size>= 0: + if len(self.charbuffer)>= size: break # we need more data if size < 0: @@ -473,6 +470,8 @@ newdata = self.stream.read(size) # decode bytes (those remaining from the last call included) data = self.bytebuffer + newdata + if not data: + break try: newchars, decodedbytes = self.decode(data, self.errors) except UnicodeDecodeError, exc: diff -r 25f6da4c49c2 -r e24265eb2271 Lib/test/test_codecs.py --- a/Lib/test/test_codecs.py Sat Jan 25 13:27:06 2014 -0500 +++ b/Lib/test/test_codecs.py Sun Jan 26 19:20:24 2014 +0200 @@ -124,6 +124,40 @@ size*u"a", ) + def test_mixed_readline_and_read(self): + lines = ["Humpty Dumpty sat on a wall,\n", + "Humpty Dumpty had a great fall.\r\n", + "All the king's horses and all the king's men\r", + "Couldn't put Humpty together again."] + data = ''.join(lines) + def getreader(): + stream = StringIO.StringIO(data.encode(self.encoding)) + return codecs.getreader(self.encoding)(stream) + + # Issue #8260: Test readline() followed by read() + f = getreader() + self.assertEqual(f.readline(), lines[0]) + self.assertEqual(f.read(), ''.join(lines[1:])) + self.assertEqual(f.read(), '') + + # 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() + 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() + f = getreader() + self.assertEqual(f.read(size=40, chars=5), data[:5]) + self.assertEqual(f.readlines(), [lines[0][5:]] + lines[1:]) + self.assertEqual(f.read(), '') + def test_bug1175396(self): s = [ '<%!--===================================================\r\n', diff -r 25f6da4c49c2 -r e24265eb2271 Misc/NEWS --- a/Misc/NEWS Sat Jan 25 13:27:06 2014 -0500 +++ b/Misc/NEWS Sun Jan 26 19:20:24 2014 +0200 @@ -38,6 +38,10 @@ Library ------- +- Issue #8260: The read(), readline() and readlines() methods of + codecs.StreamReader returned incomplete data when were called after + readline() or read(size). Based on patch by Amaury Forgeot d'Arc. + - Issue #20374: Fix build with GNU readline>= 6.3. - Issue #14548: Make multiprocessing finalizers check pid before

AltStyle によって変換されたページ (->オリジナル) /