mirror of
https://github.com/python/cpython.git
synced 2025-11-01 14:11:41 +00:00
#3242: fix a crash in "print", if sys.stdout is set to a custom object,
whose write() method installs another sys.stdout. Will backport.
This commit is contained in:
parent
dbd0ae383d
commit
bdd941fac3
3 changed files with 25 additions and 1 deletions
|
|
@ -503,13 +503,31 @@ def io_func():
|
||||||
self._test_close_open_io(io_func)
|
self._test_close_open_io(io_func)
|
||||||
|
|
||||||
|
|
||||||
|
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():
|
def test_main():
|
||||||
# Historically, these tests have been sloppy about removing TESTFN.
|
# Historically, these tests have been sloppy about removing TESTFN.
|
||||||
# So get rid of it no matter what.
|
# So get rid of it no matter what.
|
||||||
try:
|
try:
|
||||||
run_unittest(AutoFileTests, OtherFileTests, FileSubclassTests,
|
run_unittest(AutoFileTests, OtherFileTests, FileSubclassTests,
|
||||||
FileThreadingTests)
|
FileThreadingTests, StdoutTests)
|
||||||
finally:
|
finally:
|
||||||
if os.path.exists(TESTFN):
|
if os.path.exists(TESTFN):
|
||||||
os.unlink(TESTFN)
|
os.unlink(TESTFN)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ What's New in Python 2.6 beta 2?
|
||||||
Core and Builtins
|
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 #3088: Corrected a race condition in classes derived from
|
- Issue #3088: Corrected a race condition in classes derived from
|
||||||
threading.local: the first member set by a thread could be saved in
|
threading.local: the first member set by a thread could be saved in
|
||||||
another thread's dictionary.
|
another thread's dictionary.
|
||||||
|
|
|
||||||
|
|
@ -1617,9 +1617,11 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
|
||||||
"lost sys.stdout");
|
"lost sys.stdout");
|
||||||
}
|
}
|
||||||
if (w != NULL) {
|
if (w != NULL) {
|
||||||
|
Py_INCREF(w);
|
||||||
err = PyFile_WriteString("\n", w);
|
err = PyFile_WriteString("\n", w);
|
||||||
if (err == 0)
|
if (err == 0)
|
||||||
PyFile_SoftSpace(w, 0);
|
PyFile_SoftSpace(w, 0);
|
||||||
|
Py_DECREF(w);
|
||||||
}
|
}
|
||||||
Py_XDECREF(stream);
|
Py_XDECREF(stream);
|
||||||
stream = NULL;
|
stream = NULL;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue