[pypy-commit] pypy winconsoleio: Initial implementation of winconsoleio

andrewjlawrence pypy.commits at gmail.com
Mon May 27 18:35:53 EDT 2019


Author: andrewjlawrence
Branch: winconsoleio
Changeset: r96706:c795c20ce4b8
Date: 2019-05-27 23:33 +0100
http://bitbucket.org/pypy/pypy/changeset/c795c20ce4b8/

Log:	Initial implementation of winconsoleio

diff --git a/pypy/module/_io/interp_win32consoleio.py b/pypy/module/_io/interp_win32consoleio.py
--- a/pypy/module/_io/interp_win32consoleio.py
+++ b/pypy/module/_io/interp_win32consoleio.py
@@ -1,6 +1,10 @@
 import sys
 
 from pypy.module._io.interp_iobase import W_RawIOBase
+from rpython.rlib import rwin32
+
+def _pyio_get_console_type():
+    pass
 
 class W_WinConsoleIO(W_RawIOBase):
     def __init__(self, space):
@@ -9,11 +13,54 @@
     def descr_init(self, space, w_nameobj, w_mode="r", w_closefd=True, w_opener=None):
         #self.fd = -1
         #self.created = 0
-        #self.readable = 0
-        #self.writable = 0
+        self.readable = False
+        self.writable = False
         #self.closehandle = 0;
         #self.blksize = 0
-        fd = space.int_w(w_nameobj)
+        rwa = False
+        console_type = '\0'
+        self.fd = space.int_w(w_nameobj)
         if self.fd < 0:
-            raise oefmt(space.w_ValueError, "negative file descriptor")
-        self.fd = fd
+            decodedname = space.fsdecode_w(w_nameobj)
+            name = rffi.cast(rffi.CWCHARP, decodedname)
+            console_type = _pyio_get_console_type(decodedname)
+            if not console_type:
+                raise oefmt(space.w_ValueError,
+                        "Invalid console type")
+            if console_type == '\0':
+                raise oefmt(space.w_ValueError,
+                        "Cannot open non-console file")
+        s = space.text_w(w_mode)
+        
+        for char in s:
+            if char in "+abx":
+                # OK do nothing
+                pass
+            else if char == "r":
+                if rwa:
+                    raise oefmt(space.w_ValueError,
+                            "invalid mode: %.200s", mode)
+                rwa = True
+                self.readable = True
+                if console_type == "x":
+                    console_type = "r"
+            else if char == "w":
+                if rwa:
+                    raise oefmt(space.w_ValueError,
+                            "invalid mode: %.200s", mode)
+                rwa = True
+                self.writable = True;
+                if console_type == 'x':
+                    console_type = 'w'
+            else:
+                raise oefmt(space.w_ValueError,
+                            "invalid mode: %.200s", mode)
+        if not rwa:
+            raise oefmt(space.w_ValueError,
+                        "Must have exactly one of read or write mode")
+        
+        if self.fd >= 0:
+            self.handle = rwin32.get_osfhandle(self.fd)
+            self.closehandle = False
+        else:
+            access = rwin32.GENERIC_READ
diff --git a/rpython/rlib/rwin32.py b/rpython/rlib/rwin32.py
--- a/rpython/rlib/rwin32.py
+++ b/rpython/rlib/rwin32.py
@@ -137,6 +137,11 @@
     HMODULE = HANDLE
     NULL_HANDLE = rffi.cast(HANDLE, 0)
     INVALID_HANDLE_VALUE = rffi.cast(HANDLE, -1)
+    GENERIC_READ   =  rffi.cast(DWORD, 0x80000000)
+    GENERIC_WRITE  =  rffi.cast(DWORD, 0x40000000)
+    GENERIC_EXECUTE=  rffi.cast(DWORD, 0x20000000)
+    GENERIC_ALL    =  rffi.cast(DWORD, 0x10000000)
+
     PFILETIME = rffi.CArrayPtr(FILETIME)
 
     _GetLastError = winexternal('GetLastError', [], DWORD,


More information about the pypy-commit mailing list