Skip to content

Commit fd6019c

Browse files
Add some tests.
1 parent 4a50dcd commit fd6019c

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

Lib/test/test_builtin.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,29 @@ def test_input(self):
17101710
sys.stdout = savestdout
17111711
fp.close()
17121712

1713+
def test_input_gh130163(self):
1714+
class X(io.StringIO):
1715+
def __getattribute__(self, name):
1716+
nonlocal patch
1717+
if patch:
1718+
patch = False
1719+
sys.stdout = X()
1720+
sys.stderr = X()
1721+
sys.stdin = X('input\n')
1722+
support.gc_collect()
1723+
return StringIO.__getattribute__(self, name)
1724+
1725+
with (support.swap_attr(sys, 'stdout', None),
1726+
support.swap_attr(sys, 'stderr', None),
1727+
support.swap_attr(sys, 'stdin', None)):
1728+
patch = False
1729+
# the only references:
1730+
sys.stdout = X()
1731+
sys.stderr = X()
1732+
sys.stdin = X('input\n')
1733+
patch = True
1734+
input() # should not crash
1735+
17131736
# test_int(): see test_int.py for tests of built-in function int().
17141737

17151738
def test_repr(self):

Lib/test/test_print.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ def flush(self):
129129
raise RuntimeError
130130
self.assertRaises(RuntimeError, print, 1, file=noflush(), flush=True)
131131

132+
def test_gh130163(self):
133+
class X:
134+
def __str__(self):
135+
sys.stdout = StringIO()
136+
support.gc_collect()
137+
return 'foo'
138+
139+
with support.swap_attr(sys, 'stdout', None):
140+
sys.stdout = StringIO() # the only reference
141+
print(X()) # should not crash
142+
132143

133144
class TestPy2MigrationHint(unittest.TestCase):
134145
"""Test that correct hint is produced analogous to Python3 syntax,

Lib/test/test_sys.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import codecs
33
import _datetime
44
import gc
5+
import io
56
import locale
67
import operator
78
import os
@@ -80,6 +81,18 @@ def baddisplayhook(obj):
8081
code = compile("42", "<string>", "single")
8182
self.assertRaises(ValueError, eval, code)
8283

84+
def test_gh130163(self):
85+
class X:
86+
def __repr__(self):
87+
sys.stdout = io.StringIO()
88+
support.gc_collect()
89+
return 'foo'
90+
91+
with support.swap_attr(sys, 'stdout', None):
92+
sys.stdout = io.StringIO() # the only reference
93+
sys.displayhook(X()) # should not crash
94+
95+
8396
class ActiveExceptionTests(unittest.TestCase):
8497
def test_exc_info_no_exception(self):
8598
self.assertEqual(sys.exc_info(), (None, None, None))

0 commit comments

Comments
 (0)