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

afa at codespeak.net afa at codespeak.net
Thu Oct 14 23:50:01 CEST 2010


Author: afa
Date: Thu Oct 14 23:49:58 2010
New Revision: 77960

Added:
   pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py   (contents, props changed)
Modified:
   pypy/branch/fast-forward/pypy/module/_io/__init__.py
   pypy/branch/fast-forward/pypy/module/_io/interp_io.py
   pypy/branch/fast-forward/pypy/module/_io/test/test_io.py
Log:
Start implementing _io.FileIO.
cheat and have io.open() use _pyio.open()...


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	Thu Oct 14 23:49:58 2010
@@ -14,7 +14,7 @@
         '_BufferedIOBase': 'interp_io.W_BufferedIOBase',
         '_TextIOBase': 'interp_io.W_TextIOBase',
 
-        'FileIO': 'interp_io.W_FileIO',
+        'FileIO': 'interp_fileio.W_FileIO',
         'BytesIO': 'interp_io.W_BytesIO',
         'StringIO': 'interp_stringio.W_StringIO',
         'BufferedReader': 'interp_io.W_BufferedReader',
@@ -23,7 +23,7 @@
         'BufferedRandom': 'interp_io.W_BufferedRandom',
         'TextIOWrapper': 'interp_io.W_TextIOWrapper',
 
-        'open': 'space.w_None',
+        'open': 'interp_io.open',
         'UnsupportedOperation': 'space.w_None',
         'IncrementalNewlineDecoder': 'space.w_None',
         }

Added: pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py
==============================================================================
--- (empty file)
+++ pypy/branch/fast-forward/pypy/module/_io/interp_fileio.py	Thu Oct 14 23:49:58 2010
@@ -0,0 +1,129 @@
+from pypy.module._io.interp_io import W_RawIOBase
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.gateway import interp2app, unwrap_spec, Arguments
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root
+from pypy.interpreter.error import OperationError, wrap_oserror2
+import os
+
+def _bad_mode(space):
+    raise OperationError(space.w_ValueError, space.wrap(
+        "Must have exactly one of read/write/append mode"))
+
+def decode_mode(spac, mode):
+    flags = 0
+    rwa = False
+    readable = False
+    writable = False
+    append = False
+    plus = False
+
+    for s in mode:
+        if s == 'r':
+            if rwa:
+                _bad_mode(space)
+            rwa = True
+            readable = True
+        elif s == 'w':
+            if rwa:
+                _bad_mode(space)
+            rwa = True
+            writable = True
+            flags |= os.O_CREAT | os.O_TRUNC
+        elif s == 'a':
+            if rwa:
+                _bad_mode(space)
+            rwa = 1
+            writable = True
+            flags |= os.O_CREAT
+            append = True
+        elif s == 'b':
+            pass
+        elif s == '+':
+            if plus:
+                _bad_mode(space)
+            readable = writable = True
+            plus = True
+        else:
+            raise OperationError(space.w_ValueError, space.wrap(
+                "invalid mode: %s" % (mode,)))
+
+    if not rwa:
+        _bad_mode(space)
+
+    if readable and writable:
+        flags |= os.O_RDWR
+    elif readable:
+        flags |= os.O_RDONLY
+    else:
+        flags |= os.O_WRONLY
+
+    if hasattr(os, 'O_BINARY'):
+        flags |= os.O_BINARY
+
+    if hasattr(os, 'O_APPEND') and append:
+        flags |= os.O_APPEND
+
+    return readable, writable, flags
+
+class W_FileIO(W_RawIOBase):
+    def __init__(self, space):
+        W_RawIOBase.__init__(self, space)
+        self.fd = -1
+        self.readable = False
+        self.writable = False
+        self.seekable = -1
+        self.closefd = True
+
+    @unwrap_spec(ObjSpace, W_Root, Arguments)
+    def descr_new(space, w_subtype, __args__):
+        self = space.allocate_instance(W_FileIO, w_subtype)
+        W_FileIO.__init__(self, space)
+        return space.wrap(self)
+
+    @unwrap_spec('self', ObjSpace, W_Root, str, int)
+    def descr_init(self, space, w_name, mode, closefd):
+        if space.isinstance_w(w_name, space.w_float):
+            raise OperationError(space.w_TypeError, space.wrap(
+                "integer argument expected, got float"))
+        try:
+            fd = space.int_w(w_name)
+        except OperationError, e:
+            pass
+        else:
+            if fd < 0:
+                raise OperationError(space.w_ValueError, space.wrap(
+                    "negative file descriptor"))
+
+        self.readable, self.writable, flags = decode_mode(space, mode)
+
+        from pypy.module.posix.interp_posix import dispatch_filename, rposix
+        try:
+            self.fd = dispatch_filename(rposix.open)(
+                space, w_name, flags, 0666)
+        except OSError, e:
+            raise wrap_oserror2(space, e, w_fname)
+        self.closefd = bool(closefd)
+
+    def _check_closed(self, space):
+        if self.fd < 0:
+            raise OperationError(space.w_ValueError, space.wrap(
+                "I/O operation on closed file"))
+
+    @unwrap_spec('self', ObjSpace)
+    def readable_w(self, space):
+        self._check_closed(space)
+        return space.wrap(self.readable)
+
+    @unwrap_spec('self', ObjSpace)
+    def writable_w(self, space):
+        self._check_closed(space)
+        return space.wrap(self.writable)
+
+W_FileIO.typedef = TypeDef(
+    'FileIO', W_RawIOBase.typedef,
+    __new__  = interp2app(W_FileIO.descr_new.im_func),
+    __init__  = interp2app(W_FileIO.descr_init),
+    readable = interp2app(W_FileIO.readable_w),
+    writable = interp2app(W_FileIO.writable_w),
+    )
+

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	Thu Oct 14 23:49:58 2010
@@ -94,6 +94,22 @@
             raise OperationError(space.w_StopIteration, space.w_None)
         return w_line
 
+    @unwrap_spec('self', ObjSpace)
+    def isatty_w(self, space):
+        return space.w_False
+
+    @unwrap_spec('self', ObjSpace)
+    def readable_w(self, space):
+        return space.w_False
+
+    @unwrap_spec('self', ObjSpace)
+    def writable_w(self, space):
+        return space.w_False
+
+    @unwrap_spec('self', ObjSpace)
+    def seekable_w(self, space):
+        return space.w_False
+
 W_IOBase.typedef = TypeDef(
     '_IOBase',
     __new__ = generic_new_descr(W_IOBase),
@@ -103,6 +119,10 @@
     next = interp2app(W_IOBase.next_w),
     close = interp2app(W_IOBase.close_w),
     flush = interp2app(W_IOBase.flush_w),
+    isatty = interp2app(W_IOBase.isatty_w),
+    readable = interp2app(W_IOBase.readable_w),
+    writable = interp2app(W_IOBase.writable_w),
+    seekable = interp2app(W_IOBase.seekable_w),
     closed = GetSetProperty(W_IOBase.closed_get_w),
     )
 
@@ -128,12 +148,6 @@
     __new__ = generic_new_descr(W_TextIOBase),
     )
 
-class W_FileIO(W_RawIOBase):
-    pass
-W_FileIO.typedef = TypeDef(
-    'FileIO', W_RawIOBase.typedef,
-    )
-
 class W_BytesIO(W_BufferedIOBase):
     pass
 W_BytesIO.typedef = TypeDef(
@@ -169,3 +183,12 @@
 W_TextIOWrapper.typedef = TypeDef(
     'TextIOWrapper', W_TextIOBase.typedef,
     )
+
+ at unwrap_spec(ObjSpace, Arguments)
+def open(space, __args__):
+    # XXX cheat!
+    w_pyio = space.call_method(space.builtin, '__import__',
+                             space.wrap("_pyio"))
+    w_func = space.getattr(w_pyio, space.wrap("open"))
+    return space.call_args(w_func, __args__)
+

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	Thu Oct 14 23:49:58 2010
@@ -36,3 +36,14 @@
                 return ""
 
         assert list(MyFile()) == ["line1", "line2"]
+
+class AppTestOpen:
+    def setup_class(cls):
+        from pypy.tool.udir import udir
+        tmpfile = udir.join('tmpfile').ensure()
+        cls.w_tmpfile = cls.space.wrap(str(tmpfile))
+
+    def test_open(self):
+        import io
+        f = io.open(self.tmpfile, "rb")
+        f.close()



More information about the Pypy-commit mailing list