[pypy-svn] pypy default: Fix test_append_bom for _io.

alex_gaynor commits-noreply at bitbucket.org
Mon Jan 31 05:06:55 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r41485:a38d074fa257
Date: 2011-01-30 23:06 -0500
http://bitbucket.org/pypy/pypy/changeset/a38d074fa257/

Log:	Fix test_append_bom for _io.

diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -359,6 +359,13 @@
         self.seekable = space.is_true(space.call_method(w_buffer, "seekable"))
         self.telling = self.seekable
 
+        if self.seekable and self.w_encoder:
+            self.encoding_start_of_stream = True
+            w_cookie = space.call_method(self.w_buffer, "tell")
+            if not space.eq_w(w_cookie, space.wrap(0)):
+                self.encoding_start_of_stream = False
+                space.call_method(self.w_encoder, "setstate", space.wrap(0))
+
         self.state = STATE_OK
 
     def _check_init(self, space):
@@ -738,6 +745,7 @@
 
         pending_bytes = ''.join(self.pending_bytes)
         self.pending_bytes = None
+        self.pending_bytes_count = 0
 
         space.call_method(self.w_buffer, "write", space.wrap(pending_bytes))
 

diff --git a/pypy/module/_io/test/test_io.py b/pypy/module/_io/test/test_io.py
--- a/pypy/module/_io/test/test_io.py
+++ b/pypy/module/_io/test/test_io.py
@@ -280,3 +280,20 @@
             assert f.errors == "strict"
         with _io.open(self.tmpfile, "w", errors="replace") as f:
             assert f.errors == "replace"
+
+    def test_append_bom(self):
+        import _io
+
+        # The BOM is not written again when appending to a non-empty file
+        for charset in ["utf-8-sig", "utf-16", "utf-32"]:
+            with _io.open(self.tmpfile, "w", encoding=charset) as f:
+                f.write("aaa")
+                pos = f.tell()
+            with _io.open(self.tmpfile, "rb") as f:
+                res = f.read()
+                assert res == "aaa".encode(charset)
+            with _io.open(self.tmpfile, "a", encoding=charset) as f:
+                f.write("xxx")
+            with _io.open(self.tmpfile, "rb") as f:
+                res = f.read()
+                assert res == "aaaxxx".encode(charset)


More information about the Pypy-commit mailing list