Skip to content

Commit ea24b71

Browse files
author
martin.v.loewis
committed
Forward-port PYTHONIOENCODING.
git-svn-id: http://svn.python.org/projects/python/branches/py3k@63891 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 1769ef8 commit ea24b71

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

Doc/using/cmdline.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,13 @@ if Python was configured with the :option:`--with-pydebug` build option.
467467
If set, Python will dump objects and reference counts still alive after
468468
shutting down the interpreter.
469469

470+
.. envvar:: PYTHONIOENCODING
471+
472+
Overrides the encoding used for stdin/stdout/stderr, in the syntax
473+
encodingname:errorhandler, with the :errors part being optional.
474+
475+
.. versionadded:: 2.6
476+
470477

471478
.. envvar:: PYTHONMALLOCSTATS
472479

Lib/test/test_sys.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,26 @@ def test_compact_freelists(self):
349349
#self.assert_(r[0][1] > 100, r[0][1])
350350
#self.assert_(r[0][2] > 100, r[0][2])
351351

352+
def test_ioencoding(self):
353+
import subprocess,os
354+
env = dict(os.environ)
355+
356+
# Test character: cent sign, encoded as 0x4A (ASCII J) in CP424,
357+
# not representable in ASCII.
358+
359+
env["PYTHONIOENCODING"] = "cp424"
360+
p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
361+
stdout = subprocess.PIPE, env=env)
362+
out = p.stdout.read()
363+
self.assertEqual(out, "\xa2\n".encode("cp424"))
364+
365+
env["PYTHONIOENCODING"] = "ascii:replace"
366+
p = subprocess.Popen([sys.executable, "-c", 'print(chr(0xa2))'],
367+
stdout = subprocess.PIPE, env=env)
368+
out = p.stdout.read().strip()
369+
self.assertEqual(out, b'?')
370+
371+
352372
def test_main():
353373
test.support.run_unittest(SysModuleTest)
354374

Modules/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static char *usage_5 = "\
9696
PYTHONHOME : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
9797
The default module search path uses %s.\n\
9898
PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\
99+
PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\
99100
";
100101

101102
#ifndef MS_WINDOWS

Python/pythonrun.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ initstdio(void)
701701
PyObject *std = NULL;
702702
int status = 0, fd;
703703
PyObject * encoding_attr;
704+
char *encoding, *errors;
704705

705706
/* Hack to avoid a nasty recursion issue when Python is invoked
706707
in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
@@ -730,6 +731,16 @@ initstdio(void)
730731
goto error;
731732
}
732733

734+
encoding = Py_GETENV("PYTHONIOENCODING");
735+
if (encoding) {
736+
encoding = strdup(encoding);
737+
errors = strchr(encoding, ':');
738+
if (errors) {
739+
*errors = '\0';
740+
errors++;
741+
}
742+
}
743+
733744
/* Set sys.stdin */
734745
fd = fileno(stdin);
735746
/* Under some conditions stdin, stdout and stderr may not be connected
@@ -745,8 +756,8 @@ initstdio(void)
745756
#endif
746757
}
747758
else {
748-
if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL,
749-
"\n", 0))) {
759+
if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, encoding,
760+
errors, "\n", 0))) {
750761
goto error;
751762
}
752763
} /* if (fd < 0) */
@@ -765,8 +776,8 @@ initstdio(void)
765776
#endif
766777
}
767778
else {
768-
if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, NULL,
769-
"\n", 0))) {
779+
if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, encoding,
780+
errors, "\n", 0))) {
770781
goto error;
771782
}
772783
} /* if (fd < 0) */
@@ -786,8 +797,8 @@ initstdio(void)
786797
#endif
787798
}
788799
else {
789-
if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, NULL,
790-
"\n", 0))) {
800+
if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, encoding,
801+
errors, "\n", 0))) {
791802
goto error;
792803
}
793804
} /* if (fd < 0) */

0 commit comments

Comments
 (0)