[pypy-svn] pypy default: io.TextIOWrapper.truncate

alex_gaynor commits-noreply at bitbucket.org
Mon Jan 31 03:52:50 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r41482:35dfbbd9e8f3
Date: 2011-01-30 21:23 -0500
http://bitbucket.org/pypy/pypy/changeset/35dfbbd9e8f3/

Log:	io.TextIOWrapper.truncate

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
@@ -419,6 +419,13 @@
         self._writeflush(space)
         space.call_method(self.w_buffer, "flush")
 
+    @unwrap_spec('self', ObjSpace, W_Root)
+    def truncate_w(self, space, w_pos=None):
+        self._check_init(space)
+
+        space.call_method(self, "flush")
+        return space.call_method(self.w_buffer, "truncate", w_pos)
+
     @unwrap_spec('self', ObjSpace)
     def close_w(self, space):
         self._check_init(space)
@@ -965,6 +972,7 @@
     tell = interp2app(W_TextIOWrapper.tell_w),
     detach = interp2app(W_TextIOWrapper.detach_w),
     flush = interp2app(W_TextIOWrapper.flush_w),
+    truncate = interp2app(W_TextIOWrapper.truncate_w),
     close = interp2app(W_TextIOWrapper.close_w),
 
     line_buffering = interp_attrproperty("line_buffering", W_TextIOWrapper),

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
@@ -254,3 +254,16 @@
             f._CHUNK_SIZE = 4096
             assert f._CHUNK_SIZE == 4096
             raises(ValueError, setattr, f, "_CHUNK_SIZE", 0)
+
+    def test_multi_line(self):
+        import _io
+
+        with _io.open(self.tmpfile, "w+") as f:
+            f.write("abc")
+
+        with _io.open(self.tmpfile, "w+") as f:
+            f.truncate()
+
+        with _io.open(self.tmpfile, "r+") as f:
+            res = f.read()
+            assert res == ""


More information about the Pypy-commit mailing list