[Python-checkins] r53529 - in python/branches/release25-maint: Misc/NEWS Objects/fileobject.c Python/pythonrun.c Python/sysmodule.c

thomas.wouters python-checkins at python.org
Tue Jan 23 14:54:31 CET 2007


Author: thomas.wouters
Date: Tue Jan 23 14:54:30 2007
New Revision: 53529
Modified:
 python/branches/release25-maint/Misc/NEWS
 python/branches/release25-maint/Objects/fileobject.c
 python/branches/release25-maint/Python/pythonrun.c
 python/branches/release25-maint/Python/sysmodule.c
Log:
Backport trunk revision 53527:
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....)
Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Tue Jan 23 14:54:30 2007
@@ -12,6 +12,8 @@
 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
Modified: python/branches/release25-maint/Objects/fileobject.c
==============================================================================
--- python/branches/release25-maint/Objects/fileobject.c	(original)
+++ python/branches/release25-maint/Objects/fileobject.c	Tue Jan 23 14:54:30 2007
@@ -354,6 +354,8 @@
 {
 	PyFileObject *file = (PyFileObject*)f;
 	PyObject *str = PyString_FromString(enc);
+
+	assert(PyFile_Check(f));
 	if (!str)
 		return 0;
 	Py_DECREF(file->f_encoding);
Modified: python/branches/release25-maint/Python/pythonrun.c
==============================================================================
--- python/branches/release25-maint/Python/pythonrun.c	(original)
+++ python/branches/release25-maint/Python/pythonrun.c	Tue Jan 23 14:54:30 2007
@@ -275,7 +275,8 @@
 		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 @@
 		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 @@
 		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");
 		}
Modified: python/branches/release25-maint/Python/sysmodule.c
==============================================================================
--- python/branches/release25-maint/Python/sysmodule.c	(original)
+++ python/branches/release25-maint/Python/sysmodule.c	Tue Jan 23 14:54:30 2007
@@ -1087,17 +1087,17 @@
 	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;


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /