[pypy-svn] r78132 - in pypy/branch/fast-forward/pypy/module/_io: . test
afa at codespeak.net
afa at codespeak.net
Wed Oct 20 15:18:59 CEST 2010
Author: afa
Date: Wed Oct 20 15:18:58 2010
New Revision: 78132
Modified:
pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py
pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py
Log:
FileIO.readinto()
Modified: pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py (original)
+++ pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py Wed Oct 20 15:18:58 2010
@@ -321,6 +321,21 @@
return space.wrap(s)
+ @unwrap_spec('self', ObjSpace, W_Root)
+ def readinto_w(self, space, w_buffer):
+ self._check_closed(space)
+
+ # XXX check readable
+ rwbuffer = space.rwbuffer_w(w_buffer)
+ length = rwbuffer.getlength()
+ try:
+ buf = os.read(self.fd, length)
+ except OSError, e:
+ raise wrap_oserror(space, e,
+ exception_name='w_IOError')
+ rwbuffer.setslice(0, buf)
+ return space.wrap(len(buf))
+
@unwrap_spec('self', ObjSpace)
def readall_w(self, space):
self._check_closed(space)
@@ -376,6 +391,7 @@
tell = interp2app(W_FileIO.tell_w),
write = interp2app(W_FileIO.write_w),
read = interp2app(W_FileIO.read_w),
+ readinto = interp2app(W_FileIO.readinto_w),
readall = interp2app(W_FileIO.readall_w),
truncate = interp2app(W_FileIO.truncate_w),
close = interp2app(W_FileIO.close_w),
Modified: pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py (original)
+++ pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py Wed Oct 20 15:18:58 2010
@@ -85,17 +85,32 @@
def test_truncate(self):
import _io
- f = _io.FileIO(self.tmpfile, 'wb')
+ f = _io.FileIO(self.tmpfile, 'r+b')
assert f.truncate(100) == 100 # grow the file
f.close()
f = _io.FileIO(self.tmpfile)
assert len(f.read()) == 100
f.close()
#
- f = _io.FileIO(self.tmpfile, 'wb')
+ f = _io.FileIO(self.tmpfile, 'r+b')
f.seek(50)
assert f.truncate() == 50
f.close()
f = _io.FileIO(self.tmpfile)
assert len(f.read()) == 50
f.close()
+
+ def test_readinto(self):
+ import _io
+ a = bytearray('x' * 10)
+ f = _io.FileIO(self.tmpfile, 'r+')
+ assert f.readinto(a) == 10
+ f.close()
+ assert a == 'a\nb\nc\0\0\0\0\0'
+ #
+ a = bytearray('x' * 10)
+ f = _io.FileIO(self.tmpfile, 'r+')
+ f.truncate(3)
+ assert f.readinto(a) == 3
+ f.close()
+ assert a == 'a\nbxxxxxxx'
More information about the Pypy-commit
mailing list