Commits

Alex Gaynor committed 810b67f

Implement io.StringIO.seek

Comments (0)

Files changed (3)

pypy/module/_io/interp_stringio.py

         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)
     __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),
-    )
+)
 

pypy/module/_io/interp_textio.py

     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):

pypy/module/_io/test/test_stringio.py

         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)