[Python-checkins] r64639 - in python/branches/release25-maint: Lib/test/test_file.py Misc/NEWS Python/ceval.c

amaury.forgeotdarc python-checkins at python.org
Tue Jul 1 22:52:56 CEST 2008


Author: amaury.forgeotdarc
Date: Tue Jul  1 22:52:56 2008
New Revision: 64639

Log:
#3242: fix a crash in "print", if sys.stdout is set to a custom object,
whose write() method installs another sys.stdout.

Backport of r64633


Modified:
   python/branches/release25-maint/Lib/test/test_file.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Python/ceval.c

Modified: python/branches/release25-maint/Lib/test/test_file.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_file.py	(original)
+++ python/branches/release25-maint/Lib/test/test_file.py	Tue Jul  1 22:52:56 2008
@@ -323,11 +323,30 @@
             os.unlink(TESTFN)
 
 
+class StdoutTests(unittest.TestCase):
+
+    def test_move_stdout_on_write(self):
+        # Issue 3242: sys.stdout can be replaced (and freed) during a
+        # print statement; prevent a segfault in this case
+        save_stdout = sys.stdout
+
+        class File:
+            def write(self, data):
+                if '\n' in data:
+                    sys.stdout = save_stdout
+
+        try:
+            sys.stdout = File()
+            print "some text"
+        finally:
+            sys.stdout = save_stdout
+
+
 def test_main():
     # Historically, these tests have been sloppy about removing TESTFN.
     # So get rid of it no matter what.
     try:
-        run_unittest(AutoFileTests, OtherFileTests)
+        run_unittest(AutoFileTests, OtherFileTests, StdoutTests)
     finally:
         if os.path.exists(TESTFN):
             os.unlink(TESTFN)

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Tue Jul  1 22:52:56 2008
@@ -12,6 +12,10 @@
 Core and builtins
 -----------------
 
+- Issue #3242: Fix a crash inside the print statement, if sys.stdout is
+  set to a custom object whose write() method happens to install
+  another file in sys.stdout.
+
 - Issue #3100: Corrected a crash on deallocation of a subclassed weakref which
   holds the last (strong) reference to its referent.
 

Modified: python/branches/release25-maint/Python/ceval.c
==============================================================================
--- python/branches/release25-maint/Python/ceval.c	(original)
+++ python/branches/release25-maint/Python/ceval.c	Tue Jul  1 22:52:56 2008
@@ -1603,9 +1603,11 @@
 							"lost sys.stdout");
 			}
 			if (w != NULL) {
+				Py_INCREF(w);
 				err = PyFile_WriteString("\n", w);
 				if (err == 0)
 					PyFile_SoftSpace(w, 0);
+				Py_DECREF(w);
 			}
 			Py_XDECREF(stream);
 			stream = NULL;


More information about the Python-checkins mailing list