[pypy-svn] pypy fast-forward: support io.StringIO.read(), and initial value given in the constructor
amauryfa
commits-noreply at bitbucket.org
Fri Jan 7 17:56:02 CET 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40459:811b3d983d52
Date: 2011-01-07 13:31 +0100
http://bitbucket.org/pypy/pypy/changeset/811b3d983d52/
Log: support io.StringIO.read(), and initial value given in the
constructor
diff --git a/pypy/module/_io/interp_bytesio.py b/pypy/module/_io/interp_bytesio.py
--- a/pypy/module/_io/interp_bytesio.py
+++ b/pypy/module/_io/interp_bytesio.py
@@ -197,6 +197,9 @@
pos = space.int_w(w_pos)
self.buf = []
self.write_w(space, w_content)
+ if pos < 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "position value cannot be negative"))
self.pos = pos
if not space.is_w(w_dict, space.w_None):
space.call_method(self.getdict(), "update", w_dict)
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
@@ -5,3 +5,5 @@
sio.write(u'Hello ')
sio.write(u'world')
assert sio.getvalue() == u'Hello world'
+
+ assert io.StringIO(u"hello").read() == u'hello'
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
@@ -1,8 +1,9 @@
-from pypy.module._io.interp_textio import W_TextIOBase
-from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.typedef import TypeDef, generic_new_descr
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.error import operationerrfmt
from pypy.interpreter.baseobjspace import ObjSpace, W_Root
+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):
@@ -15,11 +16,15 @@
def _check_initialized(self):
pass
- @unwrap_spec(ObjSpace, W_Root)
- def descr_new(space, w_subtype):
- self = space.allocate_instance(W_StringIO, w_subtype)
- W_StringIO.__init__(self, space)
- return space.wrap(self)
+ @unwrap_spec('self', ObjSpace, W_Root)
+ def descr_init(self, space, w_initvalue=None):
+ # In case __init__ is called multiple times
+ self.buf = []
+ self.pos = 0
+
+ if not space.is_w(w_initvalue, space.w_None):
+ self.write_w(space, w_initvalue)
+ self.pos = 0
def resize_buffer(self, newlength):
if len(self.buf) > newlength:
@@ -54,6 +59,17 @@
self.write(string)
return space.wrap(size)
+ @unwrap_spec('self', ObjSpace, W_Root)
+ def read_w(self, space, w_size=None):
+ size = convert_size(space, w_size)
+ start = self.pos
+ if size >= 0:
+ end = start + size
+ else:
+ end = len(self.buf)
+ self.pos = end
+ return space.wrap(u''.join(self.buf[start:end]))
+
@unwrap_spec('self', ObjSpace)
def getvalue_w(self, space):
self._check_initialized()
@@ -62,8 +78,10 @@
W_StringIO.typedef = TypeDef(
'StringIO', W_TextIOBase.typedef,
- __new__ = interp2app(W_StringIO.descr_new.im_func),
+ __new__ = generic_new_descr(W_StringIO),
+ __init__ = interp2app(W_StringIO.descr_init),
write=interp2app(W_StringIO.write_w),
+ read=interp2app(W_StringIO.read_w),
getvalue=interp2app(W_StringIO.getvalue_w),
)
More information about the Pypy-commit
mailing list