]> granicus.if.org Git - python/commitdiff
Backport trunk revision 53527:
authorThomas Wouters <thomas@python.org>
Tue, 23 Jan 2007 13:54:30 +0000 (13:54 +0000)
committerThomas Wouters <thomas@python.org>
Tue, 23 Jan 2007 13:54:30 +0000 (13:54 +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 9876af9c9398061bab9839a715c329b6e78382e8..f30f50b6a5584039c13bc7b706b09cb7f859d124 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.5.1c1?
 Core and builtins
 -----------------
 
+- patch #1630975: Fix crash when replacing sys.stdout in sitecustomize.py
+
 - Bug #1637022: Prefix AST symbols with _Py_.
 
 - Prevent seg fault on shutdown which could occur if an object
index ced07684fd31222f5ddc31d0555dd79770196456..869ded916cb56606efca1b087353022a08af3041 100644 (file)
@@ -354,6 +354,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 e8f4fa2e5625b7bec1a85d957af1893c63783c5c..e83c4cb4233a49a68fdccada1e69abe703ad32c2 100644 (file)
@@ -275,7 +275,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");
                }
@@ -285,7 +286,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");
                }
@@ -295,7 +297,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 stderr");
                }
index 4970adff501278e5100ad44847e006d86c7ebbd5..59f6cfc684d52bf4bc676942af12fc75e02d1f21 100644 (file)
@@ -1087,17 +1087,17 @@ _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;
        }
-       if(isatty(_fileno(stderr))) {
+       if(isatty(_fileno(stderr)) && PyFile_Check(syserr)) {
                sprintf(buf, "cp%d", GetConsoleOutputCP());
                if (!PyFile_SetEncoding(syserr, buf))
                        return NULL;