mirror of
https://github.com/python/cpython.git
synced 2025-08-19 08:11:46 +00:00
#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
This commit is contained in:
parent
6fa30f40b5
commit
ceda6a67ce
3 changed files with 26 additions and 1 deletions
|
@ -323,11 +323,30 @@ class OtherFileTests(unittest.TestCase):
|
|||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue