[pypy-svn] pypy default: Fixes for io.StringIO.{tell,seek}.
alex_gaynor
commits-noreply at bitbucket.org
Sat Jan 29 19:49:46 CET 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch:
Changeset: r41461:eb35134b52d5
Date: 2011-01-29 13:48 -0500
http://bitbucket.org/pypy/pypy/changeset/eb35134b52d5/
Log: Fixes for io.StringIO.{tell,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
@@ -191,7 +191,7 @@
setstate = interp2app(W_IncrementalNewlineDecoder.setstate_w),
newlines = GetSetProperty(W_IncrementalNewlineDecoder.newlines_get_w),
- )
+)
class W_TextIOBase(W_IOBase):
w_encoding = None
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
@@ -52,6 +52,27 @@
exc_info = raises(ValueError, sio.seek, -3)
assert exc_info.value.args[0] == "negative seek position: -3"
+ raises(ValueError, sio.seek, 3, -1)
+ raises(ValueError, sio.seek, 3, -3)
+
+ sio.close()
+ raises(ValueError, sio.seek, 0)
+
+ def test_tell(self):
+ import io
+
+ s = u"1234567890"
+ sio = io.StringIO(s)
+
+ assert sio.tell() == 0
+ sio.seek(5)
+ assert sio.tell() == 5
+ sio.seek(10000)
+ assert sio.tell() == 10000
+
+ sio.close()
+ raises(ValueError, sio.tell)
+
def test_write_error(self):
import io
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
@@ -6,6 +6,7 @@
from pypy.module._io.interp_textio import W_TextIOBase
from pypy.module._io.interp_iobase import convert_size
+
class W_StringIO(W_TextIOBase):
def __init__(self, space):
W_TextIOBase.__init__(self, space)
@@ -74,13 +75,30 @@
self.pos = end
return space.wrap(u''.join(self.buf[start:end]))
- @unwrap_spec('self', ObjSpace, int)
- def seek_w(self, space, pos):
- if pos < 0:
+ @unwrap_spec('self', ObjSpace, int, int)
+ def seek_w(self, space, pos, mode=0):
+ self._check_closed(space)
+
+ if not 0 <= mode <= 2:
+ raise operationerrfmt(space.w_ValueError,
+ "Invalid whence (%d, should be 0, 1 or 2)", mode
+ )
+ elif mode == 0 and pos < 0:
raise operationerrfmt(space.w_ValueError,
"negative seek position: %d", pos
)
+ elif mode != 0 and pos != 0:
+ raise OperationError(space.w_IOError,
+ space.wrap("Can't do nonzero cur-relative seeks")
+ )
+
+ # XXX: this makes almost no sense, but its how CPython does it.
+ if mode == 1:
+ pos = self.pos
+ elif mode == 2:
+ pos = len(self.buf)
self.pos = pos
+ return space.wrap(pos)
@unwrap_spec('self', ObjSpace)
def getvalue_w(self, space):
More information about the Pypy-commit
mailing list