]> granicus.if.org Git - python/commitdiff
Forward-port PYTHONIOENCODING.
authorMartin v. Löwis <martin@v.loewis.de>
Mon, 2 Jun 2008 11:13:03 +0000 (11:13 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Mon, 2 Jun 2008 11:13:03 +0000 (11:13 +0000)
Doc/using/cmdline.rst
Lib/test/test_sys.py
Modules/main.c
Python/pythonrun.c

index ae661ce35be7196bc9350c533389bc49f352e68e..a6a6ecf909054427ba25b6a0a4d39cbb450f52e2 100644 (file)
@@ -467,6 +467,13 @@ if Python was configured with the :option:`--with-pydebug` build option.
    If set, Python will dump objects and reference counts still alive after
    shutting down the interpreter.
 
+.. envvar:: PYTHONIOENCODING
+
+   Overrides the encoding used for stdin/stdout/stderr, in the syntax
+   encodingname:errorhandler, with the :errors part being optional.
+
+   .. versionadded:: 2.6
+
 
 .. envvar:: PYTHONMALLOCSTATS
 
index 231b4603829c234f9e24f3a9ace0cda30f69df60..e1bc98d0974501de639c3281f7c81b4d97b623f6 100644 (file)
@@ -349,6 +349,26 @@ class SysModuleTest(unittest.TestCase):
         #self.assert_(r[0][1] > 100, r[0][1])
         #self.assert_(r[0][2] > 100, r[0][2])
 
+    def test_ioencoding(self):
+        import subprocess,os
+        env = dict(os.environ)
+
+        # Test character: cent sign, encoded as 0x4A (ASCII J) in CP424,
+        # not representable in ASCII.
+
+        env["PYTHONIOENCODING"] = "cp424"
+        p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
+                             stdout = subprocess.PIPE, env=env)
+        out = p.stdout.read()
+        self.assertEqual(out, "\xa2\n".encode("cp424"))
+
+        env["PYTHONIOENCODING"] = "ascii:replace"
+        p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
+                             stdout = subprocess.PIPE, env=env)
+        out = p.stdout.read().strip()
+        self.assertEqual(out, b'?')
+
+
 def test_main():
     test.support.run_unittest(SysModuleTest)
 
index 27b8493941021f1c0e07f847135110b4356a18a2..b762b0f2963c7e2db3be7ade2ddf18d8c9b29fcc 100644 (file)
@@ -96,6 +96,7 @@ static char *usage_5 = "\
 PYTHONHOME   : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
                The default module search path uses %s.\n\
 PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
+PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
 ";
 
 #ifndef MS_WINDOWS
index 2e93a3a1991cea5fea6d50dd6d70430184b42877..f2f14edb99eb8ff46e84c91e303054fb5b452799 100644 (file)
@@ -701,6 +701,7 @@ initstdio(void)
        PyObject *std = NULL;
        int status = 0, fd;
        PyObject * encoding_attr;
+       char *encoding, *errors;
 
        /* Hack to avoid a nasty recursion issue when Python is invoked
           in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
@@ -730,6 +731,16 @@ initstdio(void)
                goto error;
        }
 
+       encoding = Py_GETENV("PYTHONIOENCODING");
+       if (encoding) {
+               encoding = strdup(encoding);
+               errors = strchr(encoding, ':');
+               if (errors) {
+                       *errors = '\0';
+                       errors++;
+               }
+       }
+
        /* Set sys.stdin */
        fd = fileno(stdin);
        /* Under some conditions stdin, stdout and stderr may not be connected
@@ -745,8 +756,8 @@ initstdio(void)
 #endif
        }
        else {
-               if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL,
-                                         "\n", 0))) {
+               if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, encoding, 
+                                         errors, "\n", 0))) {
                        goto error;
                }
        } /* if (fd < 0) */
@@ -765,8 +776,8 @@ initstdio(void)
 #endif
        }
        else {
-               if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, NULL,
-                                         "\n", 0))) {
+               if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, encoding, 
+                                         errors, "\n", 0))) {
                        goto error;
                }
        } /* if (fd < 0) */
@@ -786,8 +797,8 @@ initstdio(void)
 #endif
        }
        else {
-               if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, NULL,
-                                         "\n", 0))) {
+               if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, encoding,
+                                         errors, "\n", 0))) {
                        goto error;
                }
        } /* if (fd < 0) */