Alex Gaynor avatar 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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.