[pypy-svn] pypy default: Implement io.StringIO.seek

alex_gaynor commits-noreply at bitbucket.org
Fri Jan 28 23:27:12 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r41436:810b67f3b243
Date: 2011-01-28 17:21 -0500
http://bitbucket.org/pypy/pypy/changeset/810b67f3b243/

Log:	Implement io.StringIO.seek

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
@@ -228,7 +228,7 @@
     readline = interp2app(W_TextIOBase.readline_w),
     detach = interp2app(W_TextIOBase.detach_w),
     encoding = interp_attrproperty_w("w_encoding", W_TextIOBase)
-    )
+)
 
 class PositionCookie:
     def __init__(self, bigint):

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
@@ -33,3 +33,18 @@
         assert buf[5:] == sio.read(900)
         assert u"" == sio.read()
 
+    def test_seek(self):
+        import io
+
+        s = u"1234567890"
+        sio = io.StringIO(s)
+
+        sio.read(5)
+        sio.seek(0)
+        r = sio.read()
+        assert r == s
+
+        sio.seek(3)
+        r = sio.read()
+        assert r == s[3:]
+        raises(TypeError, sio.seek, 0.0)
\ No newline at end of file

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
@@ -74,6 +74,10 @@
         self.pos = end
         return space.wrap(u''.join(self.buf[start:end]))
 
+    @unwrap_spec('self', ObjSpace, int)
+    def seek_w(self, space, pos):
+        self.pos = pos
+
     @unwrap_spec('self', ObjSpace)
     def getvalue_w(self, space):
         self._check_closed(space)
@@ -104,11 +108,12 @@
     __init__ = interp2app(W_StringIO.descr_init),
     write=interp2app(W_StringIO.write_w),
     read=interp2app(W_StringIO.read_w),
+    seek=interp2app(W_StringIO.seek_w),
     getvalue=interp2app(W_StringIO.getvalue_w),
     readable = interp2app(W_StringIO.readable_w),
     writable = interp2app(W_StringIO.writable_w),
     seekable = interp2app(W_StringIO.seekable_w),
     close = interp2app(W_StringIO.close_w),
     closed = GetSetProperty(W_StringIO.closed_get_w),
-    )
+)
 


More information about the Pypy-commit mailing list