From: Victor Stinner Date: Sat, 22 May 2010 02:11:07 +0000 (+0000) Subject: Issue #6268: Fix seek() method of codecs.open(), don't read the BOM twice X-Git-Tag: v2.7rc1~108 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=262be5e70bd75520d3d95a19ceda3eb97bd7045c;p=thirdparty%2FPython%2Fcpython.git Issue #6268: Fix seek() method of codecs.open(), don't read the BOM twice after seek(0) --- diff --git a/Lib/codecs.py b/Lib/codecs.py index a67240a20fa6..e903f2e9148b 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -694,6 +694,10 @@ class StreamReaderWriter: self.reader.reset() self.writer.reset() + def seek(self, offset, whence=0): + self.reader.seek(offset, whence) + self.writer.seek(offset, whence) + def __getattr__(self, name, getattr=getattr): diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index f8563f61ae65..306a3b2ee68b 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -1496,6 +1496,26 @@ class WithStmtTest(unittest.TestCase): self.assertEquals(srw.read(), u"\xfc") +class BomTest(unittest.TestCase): + def test_seek0(self): + data = "1234567890" + tests = ("utf-16", + "utf-16-le", + "utf-16-be", + "utf-32", + "utf-32-le", + "utf-32-be") + for encoding in tests: + with codecs.open('foo', 'wt+', encoding=encoding) as f: + # Check if the BOM is written only once + f.write(data) + f.write(data) + f.seek(0) + self.assertEquals(f.read(), data * 2) + f.seek(0) + self.assertEquals(f.read(), data * 2) + + def test_main(): test_support.run_unittest( UTF32Test, @@ -1524,6 +1544,7 @@ def test_main(): BasicStrTest, CharmapTest, WithStmtTest, + BomTest, ) diff --git a/Misc/NEWS b/Misc/NEWS index 3656def6cab2..021cac025b5a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -29,6 +29,9 @@ C-API Library ------- +- Issue #6268: Fix seek() method of codecs.open(), don't read the BOM twice + after seek(0) + - Issue #5640: Fix Shift-JIS incremental encoder for error handlers different than strict