summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorsobolevn <mail@sobolevn.me>2024-11-25 17:32:02 (GMT)
committerGitHub <noreply@github.com>2024-11-25 17:32:02 (GMT)
commita2ee89968299fc4f0da4b5a4165025b941213ba5 (patch)
tree081541a20bc25965fde2f1e4feb2aba6493b35d8 /Lib
parentd3da04bfc91ec065fe587451409102213af0e57c (diff)
downloadcpython-a2ee89968299fc4f0da4b5a4165025b941213ba5.zip
cpython-a2ee89968299fc4f0da4b5a4165025b941213ba5.tar.gz
cpython-a2ee89968299fc4f0da4b5a4165025b941213ba5.tar.bz2
gh-127182: Fix `io.StringIO.__setstate__` crash when `None` is the first value (#127219)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_io.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index aa1b826..f1f8ce5 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -1148,6 +1148,21 @@ class TestIOCTypes(unittest.TestCase):
_io = self._io
support.check_disallow_instantiation(self, _io._BytesIOBuffer)
+ def test_stringio_setstate(self):
+ # gh-127182: Calling __setstate__() with invalid arguments must not crash
+ obj = self._io.StringIO()
+ with self.assertRaisesRegex(
+ TypeError,
+ 'initial_value must be str or None, not int',
+ ):
+ obj.__setstate__((1, '', 0, {}))
+
+ obj.__setstate__((None, '', 0, {})) # should not crash
+ self.assertEqual(obj.getvalue(), '')
+
+ obj.__setstate__(('', '', 0, {}))
+ self.assertEqual(obj.getvalue(), '')
+
class PyIOTest(IOTest):
pass