Author: Armin Rigo <[email protected]>
Branch: py3k
Changeset: r86327:26dfa2f67afd
Date: 2016-08-19 10:24 +0200
http://bitbucket.org/pypy/pypy/changeset/26dfa2f67afd/

Log:    Load sys.filesystemencoding and the app-level sys.std{in,out,err}
        lazily. This gives a big reduction over the time it takes to build a
        space.

diff --git a/pypy/module/sys/__init__.py b/pypy/module/sys/__init__.py
--- a/pypy/module/sys/__init__.py
+++ b/pypy/module/sys/__init__.py
@@ -14,6 +14,13 @@
         """NOT_RPYTHON""" # because parent __init__ isn't
         if space.config.translating:
             del self.__class__.interpleveldefs['pypy_getudir']
+            del self.__class__.appleveldefs['stdin']
+            del self.__class__.appleveldefs['__stdin__']
+            del self.__class__.appleveldefs['stdout']
+            del self.__class__.appleveldefs['__stdout__']
+            del self.__class__.appleveldefs['stderr']
+            del self.__class__.appleveldefs['__stderr__']
+
         super(Module, self).__init__(space, w_name)
         self.recursionlimit = 100
         self.defaultencoding = "utf-8"
@@ -99,6 +106,15 @@
         'flags'                 : 'app.null_sysflags',
         '_xoptions'             : 'app.null__xoptions',
         'implementation'        : 'app.implementation',
+
+        # these six attributes are here only during tests;
+        # they are removed before translation
+        'stdin'                 : 'std_test.stdin',
+        '__stdin__'             : 'std_test.stdin',
+        'stdout'                : 'std_test.stdout',
+        '__stdout__'            : 'std_test.stdout',
+        'stderr'                : 'std_test.stderr',
+        '__stderr__'            : 'std_test.stderr',
     }
 
     def startup(self, space):
@@ -123,28 +139,12 @@
         space = self.space
 
         if not space.config.translating:
-            from pypy.module.sys.interp_encoding import _getfilesystemencoding
-            self.filesystemencoding = _getfilesystemencoding(space)
-
-        if not space.config.translating:
-            # Install standard streams for tests that don't call app_main.
-            # Always use line buffering, even for tests that capture
-            # standard descriptors.
-            space.appexec([], """():
-                import sys, io
-                sys.stdin = sys.__stdin__ = io.open(0, "r", encoding="ascii",
-                                                    closefd=False)
-                sys.stdin.buffer.raw.name = "<stdin>"
-                sys.stdout = sys.__stdout__ = io.open(1, "w", encoding="ascii",
-                                                      buffering=1,
-                                                      closefd=False)
-                sys.stdout.buffer.raw.name = "<stdout>"
-                sys.stderr = sys.__stderr__ = io.open(2, "w", encoding="ascii",
-                                                      
errors="backslashreplace",
-                                                      buffering=1,
-                                                      closefd=False)
-                sys.stderr.buffer.raw.name = "<stderr>"
-               """)
+            ##from pypy.module.sys.interp_encoding import 
_getfilesystemencoding
+            ##self.filesystemencoding = _getfilesystemencoding(space)
+            # XXX the two lines above take a few seconds to run whenever
+            # we initialize the space; for tests, use a simpler version
+            from pypy.module.sys.interp_encoding import base_encoding
+            self.filesystemencoding = space.wrap(base_encoding)
 
     def flush_std_files(self, space):
         w_stdout = space.sys.getdictvalue(space, 'stdout')
diff --git a/pypy/module/sys/std_test.py b/pypy/module/sys/std_test.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/sys/std_test.py
@@ -0,0 +1,19 @@
+# Install standard streams for tests that don't call app_main.  Always
+# use line buffering, even for tests that capture standard descriptors.
+
+import io
+
+stdin = io.open(0, "r", encoding="ascii",
+                closefd=False)
+stdin.buffer.raw.name = "<stdin>"
+
+stdout = io.open(1, "w", encoding="ascii",
+                 buffering=1,
+                 closefd=False)
+stdout.buffer.raw.name = "<stdout>"
+
+stderr = io.open(2, "w", encoding="ascii",
+                 errors="backslashreplace",
+                 buffering=1,
+                 closefd=False)
+stderr.buffer.raw.name = "<stderr>"
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to