[pypy-svn] pypy default: Implement io.StringIO.truncate.
alex_gaynor
commits-noreply at bitbucket.org
Sat Jan 29 20:02:07 CET 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch:
Changeset: r41463:3f1f650b2424
Date: 2011-01-29 14:01 -0500
http://bitbucket.org/pypy/pypy/changeset/3f1f650b2424/
Log: Implement io.StringIO.truncate.
diff --git a/pypy/module/_io/test/test_stringio.py b/pypy/module/_io/test/test_stringio.py
--- a/pypy/module/_io/test/test_stringio.py
+++ b/pypy/module/_io/test/test_stringio.py
@@ -73,6 +73,36 @@
sio.close()
raises(ValueError, sio.tell)
+ def test_truncate(self):
+ import io
+
+ s = u"1234567890"
+ sio = io.StringIO(s)
+
+ raises(ValueError, sio.truncate, -1)
+ sio.seek(6)
+ res = sio.truncate()
+ assert res == 6
+ assert sio.getvalue() == s[:6]
+ res = sio.truncate(4)
+ assert res == 4
+ assert sio.getvalue() == s[:4]
+ # truncate() accepts long objects
+ res = sio.truncate(4L)
+ assert res == 4
+ assert sio.getvalue() == s[:4]
+ assert sio.tell() == 6
+ sio.seek(0, 2)
+ sio.write(s)
+ assert sio.getvalue() == s[:4] + s
+ pos = sio.tell()
+ res = sio.truncate(None)
+ assert res == pos
+ assert sio.tell() == pos
+ raises(TypeError, sio.truncate, '0')
+ sio.close()
+ raises(ValueError, sio.truncate, 0)
+
def test_write_error(self):
import io
diff --git a/pypy/module/_io/interp_stringio.py b/pypy/module/_io/interp_stringio.py
--- a/pypy/module/_io/interp_stringio.py
+++ b/pypy/module/_io/interp_stringio.py
@@ -100,6 +100,24 @@
self.pos = pos
return space.wrap(pos)
+ @unwrap_spec('self', ObjSpace, W_Root)
+ def truncate_w(self, space, w_size=None):
+ self._check_closed(space)
+ if space.is_w(w_size, space.w_None):
+ size = self.pos
+ else:
+ size = space.int_w(w_size)
+
+ if size < 0:
+ raise operationerrfmt(space.w_ValueError,
+ "Negative size value %d", size
+ )
+
+ if size < len(self.buf):
+ self.resize_buffer(size)
+
+ return space.wrap(size)
+
@unwrap_spec('self', ObjSpace)
def getvalue_w(self, space):
self._check_closed(space)
@@ -131,6 +149,7 @@
write = interp2app(W_StringIO.write_w),
read = interp2app(W_StringIO.read_w),
seek = interp2app(W_StringIO.seek_w),
+ truncate = interp2app(W_StringIO.truncate_w),
getvalue = interp2app(W_StringIO.getvalue_w),
readable = interp2app(W_StringIO.readable_w),
writable = interp2app(W_StringIO.writable_w),
More information about the Pypy-commit
mailing list