summaryrefslogtreecommitdiffstats
path: root/Lib/io.py
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2008-01-07 18:30:48 (GMT)
committerAlexandre Vassalotti <alexandre@peadrop.com>2008-01-07 18:30:48 (GMT)
commita38f73b1bb327ceb1aad3a3001255ab81da91c22 (patch)
treee1bc52230da9265f8b809b049f2499819047942f /Lib/io.py
parent52d168a9950cc0933b6f650e5fdebfad13347e88 (diff)
downloadcpython-a38f73b1bb327ceb1aad3a3001255ab81da91c22.zip
cpython-a38f73b1bb327ceb1aad3a3001255ab81da91c22.tar.gz
cpython-a38f73b1bb327ceb1aad3a3001255ab81da91c22.tar.bz2
Fix issue1753: TextIOWrapper.write writes utf BOM for every string.
Patch by Erick Tryzelaar, with slight modifications by me.
Diffstat (limited to 'Lib/io.py')
-rw-r--r--Lib/io.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/Lib/io.py b/Lib/io.py
index 2a5348d..e427fe6 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -1182,6 +1182,7 @@ class TextIOWrapper(TextIOBase):
self._readnl = newline
self._writetranslate = newline != ''
self._writenl = newline or os.linesep
+ self._encoder = None
self._decoder = None
self._pending = ""
self._snapshot = None
@@ -1240,8 +1241,9 @@ class TextIOWrapper(TextIOBase):
haslf = (self._writetranslate or self._line_buffering) and "\n" in s
if haslf and self._writetranslate and self._writenl != "\n":
s = s.replace("\n", self._writenl)
+ encoder = self._encoder or self._get_encoder()
# XXX What if we were just reading?
- b = s.encode(self._encoding, self._errors)
+ b = encoder.encode(s)
self.buffer.write(b)
if self._line_buffering and (haslf or "\r" in s):
self.flush()
@@ -1250,11 +1252,13 @@ class TextIOWrapper(TextIOBase):
self._decoder.reset()
return length
+ def _get_encoder(self):
+ make_encoder = codecs.getincrementalencoder(self._encoding)
+ self._encoder = make_encoder(self._errors)
+ return self._encoder
+
def _get_decoder(self):
make_decoder = codecs.getincrementaldecoder(self._encoding)
- if make_decoder is None:
- raise IOError("Can't find an incremental decoder for encoding %s" %
- self._encoding)
decoder = make_decoder(self._errors)
if self._readuniversal:
decoder = IncrementalNewlineDecoder(decoder, self._readtranslate)