diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-05-22 13:37:56 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-05-22 13:37:56 (GMT) |
commit | 7df55dad3bd130992f5d978e8baf1119a7f2090b (patch) | |
tree | 977dc8c6e618ffdc1fb9a3ba67409b644217297b /Lib/encodings | |
parent | 54d2898ef85271f8fba369826469c16d59267a1a (diff) | |
download | cpython-7df55dad3bd130992f5d978e8baf1119a7f2090b.zip cpython-7df55dad3bd130992f5d978e8baf1119a7f2090b.tar.gz cpython-7df55dad3bd130992f5d978e8baf1119a7f2090b.tar.bz2 |
Issue #6268: More bugfixes about BOM, UTF-16 and UTF-32
* Fix seek() method of codecs.open(), don't write the BOM twice after seek(0)
* Fix reset() method of codecs, UTF-16, UTF-32 and StreamWriter classes
* test_codecs: use "w+" mode instead of "wt+". "t" mode is not supported by
Solaris or Windows, but does it really exist? I found it the in the issue.
Diffstat (limited to 'Lib/encodings')
-rw-r--r-- | Lib/encodings/utf_16.py | 20 | ||||
-rw-r--r-- | Lib/encodings/utf_32.py | 20 |
2 files changed, 26 insertions, 14 deletions
diff --git a/Lib/encodings/utf_16.py b/Lib/encodings/utf_16.py index eff08f3..61bb041 100644 --- a/Lib/encodings/utf_16.py +++ b/Lib/encodings/utf_16.py @@ -58,17 +58,23 @@ class IncrementalDecoder(codecs.BufferedIncrementalDecoder): class StreamWriter(codecs.StreamWriter): def __init__(self, stream, errors='strict'): - self.bom_written = False codecs.StreamWriter.__init__(self, stream, errors) + self.encoder = None + + def reset(self): + codecs.StreamWriter.reset(self) + self.encoder = None def encode(self, input, errors='strict'): - self.bom_written = True - result = codecs.utf_16_encode(input, errors) - if sys.byteorder == 'little': - self.encode = codecs.utf_16_le_encode + if self.encoder is None: + result = codecs.utf_16encoder(input, errors) + if sys.byteorder == 'little': + self.encoder = codecs.utf_16_leencoder + else: + self.encoder = codecs.utf_16_beencoder + return result else: - self.encode = codecs.utf_16_be_encode - return result + return self.encoder(input, errors) class StreamReader(codecs.StreamReader): diff --git a/Lib/encodings/utf_32.py b/Lib/encodings/utf_32.py index 4bbd22a..ed73809 100644 --- a/Lib/encodings/utf_32.py +++ b/Lib/encodings/utf_32.py @@ -98,17 +98,23 @@ class IncrementalDecoder(codecs.BufferedIncrementalDecoder): class StreamWriter(codecs.StreamWriter): def __init__(self, stream, errors='strict'): - self.bom_written = False + self.encoder = None codecs.StreamWriter.__init__(self, stream, errors) + def reset(self): + codecs.StreamWriter.reset(self) + self.encoder = None + def encode(self, input, errors='strict'): - self.bom_written = True - result = codecs.utf_32_encode(input, errors) - if sys.byteorder == 'little': - self.encode = codecs.utf_32_le_encode + if self.encoder is None: + result = codecs.utf_32encoder(input, errors) + if sys.byteorder == 'little': + self.encoder = codecs.utf_32_leencoder + else: + self.encoder = codecs.utf_32_beencoder + return result else: - self.encode = codecs.utf_32_be_encode - return result + return self.encoder(input, errors) class StreamReader(codecs.StreamReader): |