[pypy-svn] r77587 - in pypy/branch/fast-forward/pypy/module/_io: . test

afa at codespeak.net afa at codespeak.net
Mon Oct 4 23:11:00 CEST 2010


Author: afa
Date: Mon Oct  4 23:10:58 2010
New Revision: 77587

Added:
   pypy/branch/fast-forward/pypy/module/_io/interp_stringio.py
   pypy/branch/fast-forward/pypy/module/_io/test/test_stringio.py
Modified:
   pypy/branch/fast-forward/pypy/module/_io/__init__.py
   pypy/branch/fast-forward/pypy/module/_io/interp_io.py
Log:
Start to implement io.StringIO,
enough for py/_plugin/pytest_helpconfig.py to use it.


Modified: pypy/branch/fast-forward/pypy/module/_io/__init__.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/__init__.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/__init__.py	Mon Oct  4 23:10:58 2010
@@ -16,7 +16,7 @@
 
         'FileIO': 'interp_io.W_FileIO',
         'BytesIO': 'interp_io.W_BytesIO',
-        'StringIO': 'interp_io.W_StringIO',
+        'StringIO': 'interp_stringio.W_StringIO',
         'BufferedReader': 'interp_io.W_BufferedReader',
         'BufferedWriter': 'interp_io.W_BufferedWriter',
         'BufferedRWPair': 'interp_io.W_BufferedRWPair',

Modified: pypy/branch/fast-forward/pypy/module/_io/interp_io.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/interp_io.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/interp_io.py	Mon Oct  4 23:10:58 2010
@@ -67,12 +67,6 @@
     'BytesIO', W_BufferedIOBase.typedef,
     )
 
-class W_StringIO(W_TextIOBase):
-    pass
-W_StringIO.typedef = TypeDef(
-    'StringIO', W_TextIOBase.typedef,
-    )
-
 class W_BufferedReader(W_BufferedIOBase):
     pass
 W_BufferedReader.typedef = TypeDef(

Added: pypy/branch/fast-forward/pypy/module/_io/interp_stringio.py
==============================================================================
--- (empty file)
+++ pypy/branch/fast-forward/pypy/module/_io/interp_stringio.py	Mon Oct  4 23:10:58 2010
@@ -0,0 +1,68 @@
+from pypy.module._io.interp_io import W_TextIOBase
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.error import operationerrfmt
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root
+
+class W_StringIO(W_TextIOBase):
+    def __init__(self):
+        self.buf = []
+        self.pos = 0
+
+    def _check_closed(self):
+        pass
+    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)
+        return space.wrap(self)
+
+    def resize_buffer(self, newlength):
+        if len(self.buf) > newlength:
+            self.buf = self.buf[:newlength]
+        if len(self.buf) < newlength:
+            self.buf.extend([u'\0'] * (newlength - len(self.buf)))
+
+    def write(self, string):
+        # XXX self.decoder
+        decoded = string
+        # XXX writenl
+
+        length = len(decoded)
+        if self.pos + length > len(self.buf):
+            self.resize_buffer(self.pos + length)
+
+        for i in range(length):
+            self.buf[self.pos + i] = string[i]
+        self.pos += length
+
+    @unwrap_spec('self', ObjSpace, W_Root)
+    def write_w(self, space, w_obj):
+        self._check_initialized()
+        if not space.isinstance_w(w_obj, space.w_unicode):
+            raise operationerrfmt(space.w_TypeError,
+                                  "string argument expected, got '%s'",
+                                  space.type(self).getname(space, '?'))
+        self._check_closed()
+        string = space.unicode_w(w_obj)
+        size = len(string)
+        if size:
+            self.write(string)
+        return space.wrap(size)
+
+    @unwrap_spec('self', ObjSpace)
+    def getvalue_w(self, space):
+        self._check_initialized()
+        self._check_closed()
+        return space.wrap(u''.join(self.buf))
+
+W_StringIO.typedef = TypeDef(
+    'StringIO', W_TextIOBase.typedef,
+    __new__  = interp2app(W_StringIO.descr_new.im_func),
+    write=interp2app(W_StringIO.write_w),
+    getvalue=interp2app(W_StringIO.getvalue_w),
+    )
+

Added: pypy/branch/fast-forward/pypy/module/_io/test/test_stringio.py
==============================================================================
--- (empty file)
+++ pypy/branch/fast-forward/pypy/module/_io/test/test_stringio.py	Mon Oct  4 23:10:58 2010
@@ -0,0 +1,7 @@
+class AppTestStringIO:
+    def test_stringio(self):
+        import io
+        sio = io.StringIO()
+        sio.write(u'Hello ')
+        sio.write(u'world')
+        assert sio.getvalue() == u'Hello world'



More information about the Pypy-commit mailing list