[Python-checkins] r42872 - in python/trunk/Lib: codecs.py test/test_codecs.py

walter.doerwald python-checkins at python.org
Mon Mar 6 23:39:13 CET 2006


Author: walter.doerwald
Date: Mon Mar  6 23:39:12 2006
New Revision: 42872

Modified:
   python/trunk/Lib/codecs.py
   python/trunk/Lib/test/test_codecs.py
Log:
If size is specified, try to read at least size characters.
This is a alternative version of patch #1379332.


Modified: python/trunk/Lib/codecs.py
==============================================================================
--- python/trunk/Lib/codecs.py	(original)
+++ python/trunk/Lib/codecs.py	Mon Mar  6 23:39:12 2006
@@ -274,7 +274,10 @@
         while True:
             # can the request can be satisfied from the character buffer?
             if chars < 0:
-                if self.charbuffer:
+                if size < 0:
+                    if self.charbuffer:
+                        break
+                elif len(self.charbuffer) >= size:
                     break
             else:
                 if len(self.charbuffer) >= chars:

Modified: python/trunk/Lib/test/test_codecs.py
==============================================================================
--- python/trunk/Lib/test/test_codecs.py	(original)
+++ python/trunk/Lib/test/test_codecs.py	Mon Mar  6 23:39:12 2006
@@ -46,19 +46,23 @@
             stream = StringIO.StringIO(input.encode(self.encoding))
             return codecs.getreader(self.encoding)(stream)
 
-        def readalllines(input, keepends=True):
+        def readalllines(input, keepends=True, size=None):
             reader = getreader(input)
             lines = []
             while True:
-                line = reader.readline(keepends=keepends)
+                line = reader.readline(size=size, keepends=keepends)
                 if not line:
                     break
                 lines.append(line)
-            return "".join(lines)
+            return "|".join(lines)
 
         s = u"foo\nbar\r\nbaz\rspam\u2028eggs"
-        self.assertEqual(readalllines(s, True), s)
-        self.assertEqual(readalllines(s, False), u"foobarbazspameggs")
+        sexpected = u"foo\n|bar\r\n|baz\r|spam\u2028|eggs"
+        sexpectednoends = u"foo|bar|baz|spam|eggs"
+        self.assertEqual(readalllines(s, True), sexpected)
+        self.assertEqual(readalllines(s, False), sexpectednoends)
+        self.assertEqual(readalllines(s, True, 10), sexpected)
+        self.assertEqual(readalllines(s, False, 10), sexpectednoends)
 
         # Test long lines (multiple calls to read() in readline())
         vw = []


More information about the Python-checkins mailing list