[pypy-commit] pypy winconsoleio: Added testconsole, further implementation of winconsoleio.

andrewjlawrence pypy.commits at gmail.com
Sat Jun 1 12:42:52 EDT 2019


Author: andrewjlawrence
Branch: winconsoleio
Changeset: r96725:64a78c073e0b
Date: 2019-06-01 17:40 +0100
http://bitbucket.org/pypy/pypy/changeset/64a78c073e0b/

Log:	Added testconsole, further 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,25 +1,44 @@
 import sys
 
-from pypy.module._io.interp_iobase import W_RawIOBase
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
+from pypy.module._io.interp_iobase import (W_RawIOBase, DEFAULT_BUFFER_SIZE)
 from rpython.rlib import rwin32
 
+def _get_console_type():
+    pass
+
 def _pyio_get_console_type():
     pass
 
 class W_WinConsoleIO(W_RawIOBase):
+    SMALLBUF = 4
+    
     def __init__(self, space):
         W_RawIOBase.__init__(self, space)
+        self.handle = rwin32.INVALID_HANDLE_VALUE
+        self.fd = -1
+        self.created = 0
+        self.readable = 0
+        self.writable = 0
+        self.closehandle = 0
+        self.blksize = 0
 
-    def descr_init(self, space, w_nameobj, w_mode="r", w_closefd=True, w_opener=None):
+    @unwrap_spec(w_mode=WrappedDefault("r"), w_closefd=WrappedDefault(True), w_opener=WrappedDefault(None))
+    def descr_init(self, space, w_nameobj, w_mode, w_closefd, w_opener):
         #self.fd = -1
         #self.created = 0
         self.readable = False
         self.writable = False
         #self.closehandle = 0;
-        #self.blksize = 0
+        self.blksize = 0
         rwa = False
         console_type = '\0'
+        self.buf = lltype.malloc(rffi.CCHARPP.TO,SMALLBUF,flavor='raw')
+
         self.fd = space.int_w(w_nameobj)
+        closefd = space.bool_w(w_closefd)
+
         if self.fd < 0:
             decodedname = space.fsdecode_w(w_nameobj)
             name = rffi.cast(rffi.CWCHARP, decodedname)
@@ -36,7 +55,7 @@
             if char in "+abx":
                 # OK do nothing
                 pass
-            else if char == "r":
+            elif char == "r":
                 if rwa:
                     raise oefmt(space.w_ValueError,
                             "invalid mode: %.200s", mode)
@@ -44,7 +63,7 @@
                 self.readable = True
                 if console_type == "x":
                     console_type = "r"
-            else if char == "w":
+            elif char == "w":
                 if rwa:
                     raise oefmt(space.w_ValueError,
                             "invalid mode: %.200s", mode)
@@ -64,3 +83,53 @@
             self.closehandle = False
         else:
             access = rwin32.GENERIC_READ
+            self.closehandle = True
+            if not closefd:
+                raise oefmt(space.w_ValueError,
+                        "Cannot use closefd=False with a file name")
+            if self.writeable:
+                access = rwin32.GENERIC_WRITE
+        
+            from rpython.rlib._os_support import _preferred_traits, string_trait
+            traits = _preferred_traits(name)
+            if not (traits.str is unicode):
+                raise oefmt(space.w_ValueError,
+                            "Non-unicode string name %s", traits.str)
+            win32traits = make_win32_traits(traits)
+            self.handle = win32traits.CreateFile(name, 
+                rwin32.GENERIC_READ | rwin32.GENERIC_WRITE,
+                rwin32.FILE_SHARE_READ | rwin32.FILE_SHARE_WRITE,
+                rffi.NULL, win32traits.OPEN_EXISTING, 0, rffi.NULL)
+            if self.handle == rwin32.INVALID_HANDLE_VALUE:
+                self.handle = win32traits.CreateFile(name, 
+                    access,
+                    rwin32.FILE_SHARE_READ | rwin32.FILE_SHARE_WRITE,
+                    rffi.NULL, win32traits.OPEN_EXISTING, 0, rffi.NULL)
+
+            if self.handle == rwin32.INVALID_HANDLE_VALUE:
+                raise WindowsError(rwin32.GetLastError_saved(),
+                                   "Failed to open handle")
+        
+        if console_type == '\0':
+            console_type = _get_console_type(self.handle)
+
+        if console_type == '\0': 
+            raise oefmt(space.w_ValueError,
+                        "Cannot open non-console file")
+        
+        if self.writable and console_type != 'w':
+            raise oefmt(space.w_ValueError,
+                        "Cannot open input buffer for writing")
+
+        if self.readable and console_type != 'r':
+            raise oefmt(space.w_ValueError,
+                        "Cannot open output buffer for reading")
+
+        self.blksize = DEFAULT_BUFFER_SIZE
+        rffi.c_memset(self.buf, 0, SMALLBUF)
+
+W_WinConsoleIO.typedef = TypeDef(
+    '_io.WinConsoleIO', W_WinConsoleIO.typedef,
+    #__new__  = interp2app(W_FileIO.descr_new.im_func),
+    __init__  = interp2app(W_WinConsoleIO.descr_init),
+    )
diff --git a/pypy/module/_testconsole/__init__.py b/pypy/module/_testconsole/__init__.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/_testconsole/__init__.py
@@ -0,0 +1,11 @@
+from pypy.interpreter.mixedmodule import MixedModule
+
+class Module(MixedModule):
+
+    appleveldefs = {
+    }
+
+    interpleveldefs = {
+        'write_input': 'interp_testconsole.write_input',
+        'read_output': 'interp_testconsole.read_output',
+    }
diff --git a/pypy/module/_testconsole/interp_testconsole.py b/pypy/module/_testconsole/interp_testconsole.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/_testconsole/interp_testconsole.py
@@ -0,0 +1,10 @@
+import sys
+
+from pypy.module._io.interp_win32consoleio import W_WinConsoleIO
+from rpython.rlib import rwin32
+
+def write_input():
+    pass
+
+def read_output():
+    pass
diff --git a/rpython/rlib/rwin32.py b/rpython/rlib/rwin32.py
--- a/rpython/rlib/rwin32.py
+++ b/rpython/rlib/rwin32.py
@@ -137,10 +137,12 @@
     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)
+    GENERIC_READ     = rffi.cast(DWORD, 0x80000000)
+    GENERIC_WRITE    = rffi.cast(DWORD, 0x40000000)
+    GENERIC_EXECUTE  = rffi.cast(DWORD, 0x20000000)
+    GENERIC_ALL      = rffi.cast(DWORD, 0x10000000)
+    FILE_SHARE_READ  = rffi.cast(DWORD, 0x00000001)
+    FILE_SHARE_WRITE = rffi.cast(DWORD, 0x00000002)
 
     PFILETIME = rffi.CArrayPtr(FILETIME)
 


More information about the pypy-commit mailing list