summaryrefslogtreecommitdiff
path: root/Lib/io.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2008-12-14 18:08:37 +0000
committerAntoine Pitrou <solipsis@pitrou.net>2008-12-14 18:08:37 +0000
commitf8638a8d2181e7b021be7e838a9400e2319a9b4e (patch)
tree6ec1be62552ab25aaf5e20d6e02d9bc15b25dd15 /Lib/io.py
parent30327242b3c83cfbedb21b5b869c52b855573221 (diff)
downloadcpython-git-f8638a8d2181e7b021be7e838a9400e2319a9b4e.tar.gz
Merged revisions 67762 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r67762 | antoine.pitrou | 2008-12-14 18:40:51 +0100 (dim., 14 déc. 2008) | 3 lines Backport r67759 (fix io.IncrementalNewlineDecoder for UTF-16 et al.). ........
Diffstat (limited to 'Lib/io.py')
-rw-r--r--Lib/io.py27
1 files changed, 12 insertions, 15 deletions
diff --git a/Lib/io.py b/Lib/io.py
index 110804e607..7f938987b7 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -1292,25 +1292,23 @@ class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
"""
def __init__(self, decoder, translate, errors='strict'):
codecs.IncrementalDecoder.__init__(self, errors=errors)
- self.buffer = b''
self.translate = translate
self.decoder = decoder
self.seennl = 0
+ self.pendingcr = False
def decode(self, input, final=False):
# decode input (with the eventual \r from a previous pass)
- if self.buffer:
- input = self.buffer + input
-
output = self.decoder.decode(input, final=final)
+ if self.pendingcr and (output or final):
+ output = "\r" + output
+ self.pendingcr = False
# retain last \r even when not translating data:
# then readline() is sure to get \r\n in one pass
if output.endswith("\r") and not final:
output = output[:-1]
- self.buffer = b'\r'
- else:
- self.buffer = b''
+ self.pendingcr = True
# Record which newlines are read
crlf = output.count('\r\n')
@@ -1329,20 +1327,19 @@ class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
def getstate(self):
buf, flag = self.decoder.getstate()
- return buf + self.buffer, flag
+ flag <<= 1
+ if self.pendingcr:
+ flag |= 1
+ return buf, flag
def setstate(self, state):
buf, flag = state
- if buf.endswith(b'\r'):
- self.buffer = b'\r'
- buf = buf[:-1]
- else:
- self.buffer = b''
- self.decoder.setstate((buf, flag))
+ self.pendingcr = bool(flag & 1)
+ self.decoder.setstate((buf, flag >> 1))
def reset(self):
self.seennl = 0
- self.buffer = b''
+ self.pendingcr = False
self.decoder.reset()
_LF = 1