"""
pass
+ def seek(self, offset, whence=0):
+ self.stream.seek(offset, whence)
+ if whence == 0 and offset == 0:
+ self.reset()
+
def __getattr__(self, name,
getattr=getattr):
Resets the codec buffers used for keeping state.
"""
- self.reset()
self.stream.seek(offset, whence)
+ self.reset()
def __next__(self):
self.writer.reset()
def seek(self, offset, whence=0):
- self.reader.seek(offset, whence)
- self.writer.seek(offset, whence)
+ self.stream.seek(offset, whence)
+ self.reader.reset()
+ if whence == 0 and offset == 0:
+ self.writer.reset()
def __getattr__(self, name,
getattr=getattr):
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_16_encode(input, errors)
+ if sys.byteorder == 'little':
+ self.encoder = codecs.utf_16_le_encode
+ else:
+ self.encoder = codecs.utf_16_be_encode
+ return result
else:
- self.encode = codecs.utf_16_be_encode
- return result
+ return self.encoder(input, errors)
class StreamReader(codecs.StreamReader):
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_32_encode(input, errors)
+ if sys.byteorder == 'little':
+ self.encoder = codecs.utf_32_le_encode
+ else:
+ self.encoder = codecs.utf_32_be_encode
+ return result
else:
- self.encode = codecs.utf_32_be_encode
- return result
+ return self.encoder(input, errors)
class StreamReader(codecs.StreamReader):
"utf-32-le",
"utf-32-be")
for encoding in tests:
- with codecs.open('foo', 'w+', encoding=encoding) as f:
- # Check if the BOM is written only once
+ # Check if the BOM is written only once
+ with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
f.write(data)
f.write(data)
f.seek(0)
f.seek(0)
self.assertEquals(f.read(), data * 2)
+ # Check that the BOM is written after a seek(0)
+ with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
+ f.write(data[0])
+ self.assertNotEquals(f.tell(), 0)
+ f.seek(0)
+ f.write(data)
+ f.seek(0)
+ self.assertEquals(f.read(), data)
+
+ # (StreamWriter) Check that the BOM is written after a seek(0)
+ with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
+ f.writer.write(data[0])
+ self.assertNotEquals(f.writer.tell(), 0)
+ f.writer.seek(0)
+ f.writer.write(data)
+ f.seek(0)
+ self.assertEquals(f.read(), data)
+
+ # Check that the BOM is not written after a seek() at a position
+ # different than the start
+ with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
+ f.write(data)
+ f.seek(f.tell())
+ f.write(data)
+ f.seek(0)
+ self.assertEquals(f.read(), data * 2)
+
+ # (StreamWriter) Check that the BOM is not written after a seek()
+ # at a position different than the start
+ with codecs.open(support.TESTFN, 'w+', encoding=encoding) as f:
+ f.writer.write(data)
+ f.writer.seek(f.writer.tell())
+ f.writer.write(data)
+ f.seek(0)
+ self.assertEquals(f.read(), data * 2)
+
def test_main():
support.run_unittest(
Library
-------
-- Issue #6268: Fix seek() method of codecs.open(), don't read the BOM twice
- after seek(0)
+- Issue #6268: Fix seek() method of codecs.open(), don't read or write the BOM
+ twice after seek(0). Fix also reset() method of codecs, UTF-16, UTF-32 and
+ StreamWriter classes.
- Issue #8782: Add a trailing newline in linecache.updatecache to the last line
of files without one.