diff -r 5b5ef012cd4e Doc/using/cmdline.rst --- a/Doc/using/cmdline.rst Thu Aug 22 17:53:06 2013 +0300 +++ b/Doc/using/cmdline.rst Fri Aug 23 22:10:10 2013 +0300 @@ -538,13 +538,16 @@ .. envvar:: PYTHONIOENCODING If this is set before running the interpreter, it overrides the encoding used - for stdin/stdout/stderr, in the syntax ``encodingname:errorhandler``. The - ``:errorhandler`` part is optional and has the same meaning as in - :func:`str.encode`. + for stdin/stdout/stderr, in the syntax ``encodingname:errorhandler``. Both + ``encodingname`` and ``:errorhandler`` parts are optional and has the same + meaning as in :func:`str.encode`. For stderr, the ``:errorhandler`` part is ignored; the handler will always be ``'backslashreplace'``. + .. versionchanged:: 3.4 + The ``encodingname`` part is optional now. + .. envvar:: PYTHONNOUSERSITE diff -r 5b5ef012cd4e Lib/test/test_sys.py --- a/Lib/test/test_sys.py Thu Aug 22 17:53:06 2013 +0300 +++ b/Lib/test/test_sys.py Fri Aug 23 22:10:10 2013 +0300 @@ -544,6 +544,38 @@ out = p.communicate()[0].strip() self.assertEqual(out, b'?') + env["PYTHONIOENCODING"] = "ascii" + p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + env=env) + out, err = p.communicate() + self.assertEqual(out, b'') + self.assertIn(b'UnicodeEncodeError:', err) + self.assertIn(rb"'\xa2'", err) + + env["PYTHONIOENCODING"] = "ascii:" + p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + env=env) + out, err = p.communicate() + self.assertEqual(out, b'') + self.assertIn(b'UnicodeEncodeError:', err) + self.assertIn(rb"'\xa2'", err) + + if test.support.FS_NONASCII: + env["PYTHONIOENCODING"] = "" + p = subprocess.Popen([sys.executable, "-c", + 'print(%a)' % test.support.FS_NONASCII], + stdout=subprocess.PIPE, env=env) + out = p.communicate()[0].strip() + self.assertEqual(out, os.fsencode(test.support.FS_NONASCII)) + + env["PYTHONIOENCODING"] = ":surrogateescape" + p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xdcbd))'], + stdout=subprocess.PIPE, env=env) + out = p.communicate()[0].strip() + self.assertEqual(out, b'\xbd') + @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') def test_executable(self): diff -r 5b5ef012cd4e Python/pythonrun.c --- a/Python/pythonrun.c Thu Aug 22 17:53:06 2013 +0300 +++ b/Python/pythonrun.c Fri Aug 23 22:10:10 2013 +0300 @@ -1052,7 +1052,7 @@ PyObject *std = NULL; int status = 0, fd; PyObject * encoding_attr; - char *encoding = NULL, *errors; + char *pythonioencoding = NULL, *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 */ @@ -1084,19 +1084,23 @@ } Py_DECREF(wrapper); - encoding = Py_GETENV("PYTHONIOENCODING"); - errors = NULL; - if (encoding) { - encoding = _PyMem_Strdup(encoding); - if (encoding == NULL) { + pythonioencoding = Py_GETENV("PYTHONIOENCODING"); + encoding = errors = NULL; + if (pythonioencoding) { + pythonioencoding = _PyMem_Strdup(pythonioencoding); + if (pythonioencoding == NULL) { PyErr_NoMemory(); goto error; } - errors = strchr(encoding, ':'); + errors = strchr(pythonioencoding, ':'); if (errors) { *errors = '\0'; errors++; + if (!*errors) + errors = NULL; } + if (*pythonioencoding) + encoding = pythonioencoding; } /* Set sys.stdin */ @@ -1176,7 +1180,7 @@ status = -1; } - PyMem_Free(encoding); + PyMem_Free(pythonioencoding); Py_XDECREF(bimod); Py_XDECREF(iomod); return status;