Author: Armin Rigo <[email protected]>
Branch: msvcrt-cffi
Changeset: r85732:2d2968de4cfd
Date: 2016-07-17 11:55 +0200
http://bitbucket.org/pypy/pypy/changeset/2d2968de4cfd/

Log:    Rewrite _msvcrt_build to use the out-of-line ABI mode. Seems to work
        with an extra hack in _pycparser to not import 'subprocess' eagerly.

diff --git a/lib_pypy/_msvcrt_build.py b/lib_pypy/_msvcrt_build.py
--- a/lib_pypy/_msvcrt_build.py
+++ b/lib_pypy/_msvcrt_build.py
@@ -1,35 +1,30 @@
+# Note: uses the CFFI out-of-line ABI mode.  We can't use the API
+# mode because ffi.compile() needs to run the compiler, which
+# needs 'subprocess', which needs 'msvcrt' already.
+
 from cffi import FFI
 
 ffi = FFI()
 
-ffi.set_source("_msvcrt_cffi", """
-#include <io.h>
-#include <sys/locking.h>
-#include <conio.h>
-""")
+ffi.set_source("_msvcrt_cffi", None)
 
 ffi.cdef("""
+typedef unsigned short wint_t;
+
 int _open_osfhandle(intptr_t osfhandle, int flags);
 intptr_t _get_osfhandle(int fd);
 int _setmode(int fd, int mode);
 int _locking(int fd, int mode, long nbytes);
 
-#define LK_UNLCK ...
-#define LK_LOCK ...
-#define LK_NBLCK ...
-#define LK_RLCK ...
-#define LK_NBRLCK ...
-
 int _kbhit(void);
-char _getch(void);
-wchar_t _getwch(void);
-char _getche(void);
-wchar_t _getwche(void);
-void _putch(char);
-void _putwch(wchar_t);
-int _ungetch(char);
-
-#define EOF ...
+int _getch(void);
+wint_t _getwch(void);
+int _getche(void);
+wint_t _getwche(void);
+int _putch(int);
+wint_t _putwch(wchar_t);
+int _ungetch(int);
+wint_t _ungetwch(wint_t);
 """)
 
 if __name__ == "__main__":
diff --git a/lib_pypy/cffi/_pycparser/__init__.py 
b/lib_pypy/cffi/_pycparser/__init__.py
--- a/lib_pypy/cffi/_pycparser/__init__.py
+++ b/lib_pypy/cffi/_pycparser/__init__.py
@@ -10,7 +10,6 @@
 __all__ = ['c_lexer', 'c_parser', 'c_ast']
 __version__ = '2.14'
 
-from subprocess import Popen, PIPE
 from .c_parser import CParser
 
 
@@ -28,6 +27,7 @@
         When successful, returns the preprocessed file's contents.
         Errors from cpp will be printed out.
     """
+    from subprocess import Popen, PIPE
     path_list = [cpp_path]
     if isinstance(cpp_args, list):
         path_list += cpp_args
diff --git a/lib_pypy/msvcrt.py b/lib_pypy/msvcrt.py
--- a/lib_pypy/msvcrt.py
+++ b/lib_pypy/msvcrt.py
@@ -13,7 +13,9 @@
 if sys.platform != 'win32':
     raise ImportError("The 'msvcrt' module is only available on Windows")
 
-from _msvcrt_cffi import ffi, lib
+from _msvcrt_cffi import ffi
+lib = ffi.dlopen('msvcrt')
+
 import errno
 
 try: from __pypy__ import builtinify, validate_fd
@@ -65,11 +67,7 @@
         _ioerr()
     return flags
 
-LK_UNLCK = lib.LK_UNLCK
-LK_LOCK = lib.LK_LOCK
-LK_NBLCK = lib.LK_NBLCK
-LK_RLCK = lib.RLCK
-LK_NBRLCK = lib.LK_NBRLCK
+LK_UNLCK, LK_LOCK, LK_NBLCK, LK_RLCK, LK_NBRLCK = range(5)
 
 @builtinify
 def locking(fd, mode, nbytes):
@@ -89,20 +87,37 @@
 # Console I/O routines
 
 kbhit = lib._kbhit
-getch = lib._getch
-getwch = lib._getwch
-getche = lib._getche
-getwche = lib._getwche
-putch = lib._putch
-putwch = lib._putwch      # xxx CPython accepts a unicode str of length > 1 and
-                          # discards the other characters, but only in putwch()
+
+@builtinify
+def getch():
+    return chr(lib._getch())
+
+@builtinify
+def getwch():
+    return unichr(lib._getwch())
+
+@builtinify
+def getche():
+    return chr(lib._getche())
+
+@builtinify
+def getwche():
+    return unichr(lib._getwche())
+
+@builtinify
+def putch(ch):
+    lib._putch(ord(ch))
+
+@builtinify
+def putwch(ch):
+    lib._putwch(ord(ch))
 
 @builtinify
 def ungetch(ch):
-    if lib._ungetch(ch) == lib.EOF:
+    if lib._ungetch(ord(ch)) == -1:   # EOF
         _ioerr()
 
 @builtinify
 def ungetwch(ch):
-    if lib._ungetwch(ch) == lib.EOF:
+    if lib._ungetwch(ord(ch)) == -1:   # EOF
         _ioerr()
diff --git a/pypy/tool/build_cffi_imports.py b/pypy/tool/build_cffi_imports.py
--- a/pypy/tool/build_cffi_imports.py
+++ b/pypy/tool/build_cffi_imports.py
@@ -14,7 +14,6 @@
     "gdbm": "_gdbm_build.py"  if sys.platform != "win32" else None,
     "pwdgrp": "_pwdgrp_build.py" if sys.platform != "win32" else None,
     "resource": "_resource_build.py" if sys.platform != "win32" else None,
-    "msvcrt": "_msvcrt_build.py" if sys.platform == "win32" else None,
     "xx": None,    # for testing: 'None' should be completely ignored
     }
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to