[pypy-commit] pypy py3k: Make sure sys.stdout or sys.stderr are initialized when app_main prints

amauryfa noreply at buildbot.pypy.org
Mon Nov 7 00:24:24 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r48854:1bab2dd470bf
Date: 2011-11-07 00:23 +0100
http://bitbucket.org/pypy/pypy/changeset/1bab2dd470bf/

Log:	Make sure sys.stdout or sys.stderr are initialized when app_main
	prints
	--version or --help

diff --git a/pypy/translator/goal/app_main.py b/pypy/translator/goal/app_main.py
--- a/pypy/translator/goal/app_main.py
+++ b/pypy/translator/goal/app_main.py
@@ -114,6 +114,7 @@
 # Option parsing
 
 def print_info(*args):
+    initstdio()
     try:
         options = sys.pypy_translation_info
     except AttributeError:
@@ -126,6 +127,7 @@
     raise SystemExit
 
 def print_help(*args):
+    initstdio()
     print('usage: %s [options] [-c cmd|-m mod|file.py|-] [arg...]' % (
         sys.executable,))
     print(__doc__.rstrip())
@@ -144,11 +146,13 @@
     print('  --jit off                  turn off the JIT')
 
 def print_version(*args):
+    initstdio()
     print ("Python", sys.version, file=sys.stderr)
     raise SystemExit
 
 def set_jit_option(options, jitparam, *args):
     if 'pypyjit' not in sys.builtin_module_names:
+        initstdio()
         print("Warning: No jit support in %s" % (sys.executable,),
               file=sys.stderr)
     else:
@@ -249,7 +253,9 @@
             sys.path.append(dir)
             _seen[dir] = True
 
-def initstdio(encoding, unbuffered):
+def initstdio(encoding=None, unbuffered=False):
+    if not encoding:
+        encoding = sys.getfilesystemencoding()
     if ':' in encoding:
         encoding, errors = encoding.split(':', 1)
     else:
@@ -510,8 +516,7 @@
         sys.setrecursionlimit(5000)
 
     readenv = not ignore_environment
-    io_encoding = ((readenv and os.getenv("PYTHONIOENCODING"))
-                   or sys.getfilesystemencoding())
+    io_encoding = readenv and os.getenv("PYTHONIOENCODING")
     initstdio(io_encoding, unbuffered)
 
     mainmodule = type(sys)('__main__')
@@ -694,6 +699,7 @@
     try:
         cmdline = parse_command_line(argv)
     except CommandLineError as e:
+        initstdio()
         print_error(str(e))
         return 2
     except SystemExit as e:


More information about the pypy-commit mailing list