[pypy-commit] pypy cffi-embedding-win32: Do this patching only once

arigo pypy.commits at gmail.com
Thu Feb 11 10:12:22 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-embedding-win32
Changeset: r82161:bea8f2df95f8
Date: 2016-02-11 16:02 +0100
http://bitbucket.org/pypy/pypy/changeset/bea8f2df95f8/

Log:	Do this patching only once

diff --git a/pypy/module/_cffi_backend/__init__.py b/pypy/module/_cffi_backend/__init__.py
--- a/pypy/module/_cffi_backend/__init__.py
+++ b/pypy/module/_cffi_backend/__init__.py
@@ -69,6 +69,7 @@
     def startup(self, space):
         from pypy.module._cffi_backend import embedding
         embedding.glob.space = space
+        embedding.glob.patched_sys = False
 
 
 def get_dict_rtld_constants():
diff --git a/pypy/module/_cffi_backend/embedding.py b/pypy/module/_cffi_backend/embedding.py
--- a/pypy/module/_cffi_backend/embedding.py
+++ b/pypy/module/_cffi_backend/embedding.py
@@ -45,6 +45,26 @@
     pass
 glob = Global()
 
+def patch_sys(space):
+    # Annoying: CPython would just use the C-level std{in,out,err} as
+    # configured by the main application, for example in binary mode
+    # on Windows or with buffering turned off.  We can't easily do the
+    # same.  Instead, go for the safest bet (but possibly bad for
+    # performance) and open sys.std{in,out,err} unbuffered.  On
+    # Windows I guess binary mode is a better default choice.
+    #
+    # XXX if needed, we could add support for a flag passed to
+    # pypy_init_embedded_cffi_module().
+    if not glob.patched_sys:
+        space.appexec([], """():
+            import os
+            sys.stdin  = sys.__stdin__  = os.fdopen(0, 'rb', 0)
+            sys.stdout = sys.__stdout__ = os.fdopen(1, 'wb', 0)
+            sys.stderr = sys.__stderr__ = os.fdopen(2, 'wb', 0)
+        """)
+        glob.patched_sys = True
+
+
 def pypy_init_embedded_cffi_module(version, init_struct):
     # called from __init__.py
     name = "?"
@@ -56,24 +76,7 @@
         must_leave = False
         try:
             must_leave = space.threadlocals.try_enter_thread(space)
-
-            # Annoying: CPython would just use the C-level
-            # std{in,out,err} as configured by the main application,
-            # for example in binary mode on Windows or with buffering
-            # turned off.  We can't easily do the same.  Instead, go
-            # for the safest bet (but possibly bad for performance)
-            # and open sys.std{in,out,err} unbuffered.  On Windows I
-            # guess binary mode is a better default choice.
-            #
-            # XXX if needed, we could add support for a flag passed to
-            # this function.
-            space.appexec([], """():
-                import os
-                sys.stdin  = sys.__stdin__  = os.fdopen(0, 'rb', 0)
-                sys.stdout = sys.__stdout__ = os.fdopen(1, 'wb', 0)
-                sys.stderr = sys.__stderr__ = os.fdopen(2, 'wb', 0)
-            """)
-
+            patch_sys(space)
             load_embedded_cffi_module(space, version, init_struct)
             res = 0
         except OperationError, operr:


More information about the pypy-commit mailing list