[pypy-svn] r79184 - in pypy/branch/fast-forward/pypy/module/_io: . test
afa at codespeak.net
afa at codespeak.net
Wed Nov 17 12:05:09 CET 2010
Author: afa
Date: Wed Nov 17 12:05:07 2010
New Revision: 79184
Modified:
pypy/branch/fast-forward/pypy/module/_io/interp_bytesio.py
pypy/branch/fast-forward/pypy/module/_io/test/test_bytesio.py
Log:
BytesIO.truncate()
Modified: pypy/branch/fast-forward/pypy/module/_io/interp_bytesio.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/interp_bytesio.py (original)
+++ pypy/branch/fast-forward/pypy/module/_io/interp_bytesio.py Wed Nov 17 12:05:07 2010
@@ -82,6 +82,25 @@
return space.wrap(length)
+ @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.r_longlong_w(w_size)
+
+ if size < 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "negative size value"))
+
+ if size < self.string_size:
+ self.string_size = size
+ del self.buf[size:]
+
+ return space.wrap(size)
+
@unwrap_spec('self', ObjSpace)
def getvalue_w(self, space):
self._check_closed(space)
@@ -139,6 +158,7 @@
read = interp2app(W_BytesIO.read_w),
write = interp2app(W_BytesIO.write_w),
+ truncate = interp2app(W_BytesIO.truncate_w),
getvalue = interp2app(W_BytesIO.getvalue_w),
seek = interp2app(W_BytesIO.seek_w),
tell = interp2app(W_BytesIO.tell_w),
Modified: pypy/branch/fast-forward/pypy/module/_io/test/test_bytesio.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/test/test_bytesio.py (original)
+++ pypy/branch/fast-forward/pypy/module/_io/test/test_bytesio.py Wed Nov 17 12:05:07 2010
@@ -35,3 +35,10 @@
assert f.seek(-1, 2) == 4
assert f.tell() == 4
assert f.seek(0) == 0
+
+ def test_truncate(self):
+ import _io
+ f = _io.BytesIO("hello")
+ f.seek(3)
+ assert f.truncate() == 3
+ assert f.getvalue() == "hel"
More information about the Pypy-commit
mailing list