[3.12] gh-127182: Fix io.StringIO.__setstate__ crash when None is the first value (GH-127219) (#127263)

gh-127182: Fix `io.StringIO.__setstate__` crash when `None` is the first value (GH-127219)
(cherry picked from commit a2ee899682)

Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2024-11-25 18:51:21 +01:00 committed by GitHub
parent f7e587d15c
commit a4d6b905dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 33 additions and 14 deletions

View file

@ -1154,6 +1154,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