From: Victor Stinner Date: Thu, 16 Jul 2015 20:17:31 +0000 (+0200) Subject: Closes #23247: Fix a crash in the StreamWriter.reset() of CJK codecs X-Git-Tag: v3.5.0b4~50^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=579db160b392404b6dd9608ce5f5bef29a9885a4;p=python Closes #23247: Fix a crash in the StreamWriter.reset() of CJK codecs --- diff --git a/Lib/test/multibytecodec_support.py b/Lib/test/multibytecodec_support.py index bc1cfc8518..f9884c68eb 100644 --- a/Lib/test/multibytecodec_support.py +++ b/Lib/test/multibytecodec_support.py @@ -270,6 +270,13 @@ class TestBase: self.assertEqual(ostream.getvalue(), self.tstring[0]) + def test_streamwriter_reset_no_pending(self): + # Issue #23247: Calling reset() on a fresh StreamWriter instance + # (without pending data) must not crash + stream = BytesIO() + writer = self.writer(stream) + writer.reset() + class TestBase_Mapping(unittest.TestCase): pass_enctest = [] diff --git a/Misc/NEWS b/Misc/NEWS index 874bbd95ed..c0a5edf995 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -66,6 +66,8 @@ Core and Builtins Library ------- +- Issue #23247: Fix a crash in the StreamWriter.reset() of CJK codecs. + - Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely. Patch from Nicola Palumbo and Laurent De Buyst. diff --git a/Modules/cjkcodecs/multibytecodec.c b/Modules/cjkcodecs/multibytecodec.c index 435529fb0e..e0d9b678c2 100644 --- a/Modules/cjkcodecs/multibytecodec.c +++ b/Modules/cjkcodecs/multibytecodec.c @@ -1629,6 +1629,9 @@ mbstreamwriter_reset(MultibyteStreamWriterObject *self) { PyObject *pwrt; + if (!self->pending) + Py_RETURN_NONE; + pwrt = multibytecodec_encode(self->codec, &self->state, self->pending, NULL, self->errors, MBENC_FLUSH | MBENC_RESET);