[Python-checkins] r42873 - in python/branches/release24-maint/Lib: codecs.py test/test_codecs.py

walter.doerwald python-checkins at python.org
Mon Mar 6 23:44:03 CET 2006


Author: walter.doerwald
Date: Mon Mar 6 23:44:03 2006
New Revision: 42873
Modified:
 python/branches/release24-maint/Lib/codecs.py
 python/branches/release24-maint/Lib/test/test_codecs.py
Log:
Backport revision 42872:
If size is specified, try to read at least size characters.
This is a alternative version of patch #1379332.
Modified: python/branches/release24-maint/Lib/codecs.py
==============================================================================
--- python/branches/release24-maint/Lib/codecs.py	(original)
+++ python/branches/release24-maint/Lib/codecs.py	Mon Mar 6 23:44:03 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/branches/release24-maint/Lib/test/test_codecs.py
==============================================================================
--- python/branches/release24-maint/Lib/test/test_codecs.py	(original)
+++ python/branches/release24-maint/Lib/test/test_codecs.py	Mon Mar 6 23:44:03 2006
@@ -57,19 +57,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

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