]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Backport trunk revision 53527:
authorThomas Wouters <thomas@python.org>
Tue, 23 Jan 2007 15:09:19 +0000 (15:09 +0000)
committerThomas Wouters <thomas@python.org>
Tue, 23 Jan 2007 15:09:19 +0000 (15:09 +0000)
SF patch #1630975: Fix crash when replacing sys.stdout in sitecustomize

When running the interpreter in an environment that would cause it to set
stdout/stderr/stdin's encoding, having a sitecustomize that would replace
them with something other than PyFile objects would crash the interpreter.
Fix it by simply ignoring the encoding-setting for non-files.

This could do with a test, but I can think of no maintainable and portable
way to test this bug, short of adding a sitecustomize.py to the buildsystem
and have it always run with it (hmmm....)

Misc/NEWS
Objects/fileobject.c
Python/pythonrun.c
Python/sysmodule.c

index ff711adf745ceea1294913638add27a16912b5a6..8aa13bad0bc6f285e1e598e318396ad56874d817 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,8 @@ What's New in Python 2.4.5c1?
 Core and builtins
 -----------------
 
+- patch #1630975: Fix crash when replacing sys.stdout in sitecustomize.py
+
 - Bug #1590891: random.randrange don't return correct value for big number
 
 - Bug #1542016: make sys.callstats() match its docstring and return an
index 0fcad1210beb878d8f10be1c1ae9735be4fb6cbf..ee7bc4813ebae5fceefd8089b9a2f8d490801160 100644 (file)
@@ -282,6 +282,8 @@ PyFile_SetEncoding(PyObject *f, const char *enc)
 {
        PyFileObject *file = (PyFileObject*)f;
        PyObject *str = PyString_FromString(enc);
+
+       assert(PyFile_Check(f));
        if (!str)
                return 0;
        Py_DECREF(file->f_encoding);
index e8392eee4f3950446e1c17067717c98fa9680d66..ddf714af72163bb70c7b1f1b2c987063db58011f 100644 (file)
@@ -257,7 +257,8 @@ Py_InitializeEx(int install_sigs)
                sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
                if (!sys_isatty)
                        PyErr_Clear();
-               if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
+               if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
+                  PyFile_Check(sys_stream)) {
                        if (!PyFile_SetEncoding(sys_stream, codeset))
                                Py_FatalError("Cannot set codeset of stdin");
                }
@@ -267,7 +268,8 @@ Py_InitializeEx(int install_sigs)
                sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
                if (!sys_isatty)
                        PyErr_Clear();
-               if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
+               if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
+                  PyFile_Check(sys_stream)) {
                        if (!PyFile_SetEncoding(sys_stream, codeset))
                                Py_FatalError("Cannot set codeset of stdout");
                }
index 08831786fe73962bd623cd09ffabcb0cd5f3bb54..d2229a6728e04868366f6a82bdc9b42bdef99ba8 100644 (file)
@@ -972,12 +972,12 @@ _PySys_Init(void)
        if (PyErr_Occurred())
                return NULL;
 #ifdef MS_WINDOWS
-       if(isatty(_fileno(stdin))){
+       if(isatty(_fileno(stdin)) && PyFile_Check(sysin)) {
                sprintf(buf, "cp%d", GetConsoleCP());
                if (!PyFile_SetEncoding(sysin, buf))
                        return NULL;
        }
-       if(isatty(_fileno(stdout))) {
+       if(isatty(_fileno(stdout)) && PyFile_Check(sysout)) {
                sprintf(buf, "cp%d", GetConsoleOutputCP());
                if (!PyFile_SetEncoding(sysout, buf))
                        return NULL;