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

afa at codespeak.net afa at codespeak.net
Fri Oct 8 09:02:19 CEST 2010


Author: afa
Date: Fri Oct  8 09:02:16 2010
New Revision: 77707

Modified:
   pypy/branch/fast-forward/pypy/module/_io/interp_io.py
   pypy/branch/fast-forward/pypy/module/_io/test/test_io.py
Log:
Allow the creation of io.xxxBase objects.

CPython can inherit tp_new from the base class, because
it assumes that zeroing the memory is a sane default for most types.
The closest thing we could do in PyPy is to call the __init__ function
with no argument, but it's difficult since the typedef has no reference
to the W_Class. Meanwhile,
    __new__ = GenericNew(W_Class)
is not too bad. If it works, I'll move it to typedef.py.


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	Fri Oct  8 09:02:16 2010
@@ -5,18 +5,19 @@
 
 DEFAULT_BUFFER_SIZE = 8192
 
+def GenericNew(W_Type):
+    def descr_new(space, w_subtype, __args__):
+        self = space.allocate_instance(W_Type, w_subtype)
+        W_Type.__init__(self, space)
+        return space.wrap(self)
+    descr_new.unwrap_spec = [ObjSpace, W_Root, Arguments]
+    return interp2app(descr_new)
 
 class W_BlockingIOError(W_IOError):
     def __init__(self, space):
         W_IOError.__init__(self, space)
         self.written = 0
 
-    def descr_new(space, w_subtype, __args__):
-        self = space.allocate_instance(W_BlockingIOError, w_subtype)
-        W_BlockingIOError.__init__(self, space)
-        return space.wrap(self)
-    descr_new.unwrap_spec = [ObjSpace, W_Root, Arguments]
-
     def descr_init(self, space, w_errno, w_strerror, written=0):
         W_IOError.descr_init(self, space, [w_errno, w_strerror])
         self.written = written
@@ -26,33 +27,40 @@
     'BlockingIOError',
     __doc__ = ("Exception raised when I/O would block "
                "on a non-blocking I/O stream"),
-    __new__  = interp2app(W_BlockingIOError.descr_new.im_func),
+    __new__  = GenericNew(W_BlockingIOError),
     __init__ = interp2app(W_BlockingIOError.descr_init),
     characters_written = interp_attrproperty('written', W_BlockingIOError),
     )
 
 class W_IOBase(Wrappable):
-    pass
+    def __init__(self, space):
+        pass
+
 W_IOBase.typedef = TypeDef(
     '_IOBase',
+    __new__ = GenericNew(W_IOBase),
     )
 
 class W_RawIOBase(W_IOBase):
     pass
 W_RawIOBase.typedef = TypeDef(
     '_RawIOBase', W_IOBase.typedef,
+    __new__ = GenericNew(W_RawIOBase),
     )
 
 class W_BufferedIOBase(W_IOBase):
     pass
+
 W_BufferedIOBase.typedef = TypeDef(
     '_BufferedIOBase', W_IOBase.typedef,
+    __new__ = GenericNew(W_BufferedIOBase),
     )
 
 class W_TextIOBase(W_IOBase):
     pass
 W_TextIOBase.typedef = TypeDef(
     '_TextIOBase', W_IOBase.typedef,
+    __new__ = GenericNew(W_TextIOBase),
     )
 
 class W_FileIO(W_RawIOBase):

Modified: pypy/branch/fast-forward/pypy/module/_io/test/test_io.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/test/test_io.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/test/test_io.py	Fri Oct  8 09:02:16 2010
@@ -6,3 +6,13 @@
 
     def test_import(self):
         import io
+
+    def test_iobase(self):
+        import io
+        io.IOBase()
+
+        class MyFile(io.BufferedIOBase):
+            def __init__(self, filename):
+                pass
+        MyFile("file")
+



More information about the Pypy-commit mailing list