File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -323,11 +323,30 @@ def testIteration(self):
323323 os .unlink (TESTFN )
324324
325325
326+ class StdoutTests (unittest .TestCase ):
327+
328+ def test_move_stdout_on_write (self ):
329+ # Issue 3242: sys.stdout can be replaced (and freed) during a
330+ # print statement; prevent a segfault in this case
331+ save_stdout = sys .stdout
332+
333+ class File :
334+ def write (self , data ):
335+ if '\n ' in data :
336+ sys .stdout = save_stdout
337+
338+ try :
339+ sys .stdout = File ()
340+ print "some text"
341+ finally :
342+ sys .stdout = save_stdout
343+
344+
326345def test_main ():
327346 # Historically, these tests have been sloppy about removing TESTFN.
328347 # So get rid of it no matter what.
329348 try :
330- run_unittest (AutoFileTests , OtherFileTests )
349+ run_unittest (AutoFileTests , OtherFileTests , StdoutTests )
331350 finally :
332351 if os .path .exists (TESTFN ):
333352 os .unlink (TESTFN )
Original file line number Diff line number Diff line change @@ -12,6 +12,10 @@ What's New in Python 2.5.3?
1212Core and builtins
1313-----------------
1414
15+ - Issue #3242: Fix a crash inside the print statement, if sys.stdout is
16+ set to a custom object whose write() method happens to install
17+ another file in sys.stdout.
18+
1519- Issue #3100: Corrected a crash on deallocation of a subclassed weakref which
1620 holds the last (strong) reference to its referent.
1721
Original file line number Diff line number Diff line change @@ -1603,9 +1603,11 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
16031603 "lost sys.stdout" );
16041604 }
16051605 if (w != NULL ) {
1606+ Py_INCREF (w );
16061607 err = PyFile_WriteString ("\n" , w );
16071608 if (err == 0 )
16081609 PyFile_SoftSpace (w , 0 );
1610+ Py_DECREF (w );
16091611 }
16101612 Py_XDECREF (stream );
16111613 stream = NULL ;
You can’t perform that action at this time.
0 commit comments