From 29273c87da0ef361f84fee41385f7c991536f9cd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc-Andr=C3=A9=20Lemburg?= Date: Tue, 4 Feb 2003 19:35:03 +0000 Subject: [PATCH] Fix for [ 543344 ] Interpreter crashes when recoding; suggested by Michael Stone (mbrierst). Python 2.1.4, 2.2.2 candidate. --- Lib/test/test_codecs.py | 9 +++++++++ Modules/_codecsmodule.c | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 36cebd5535..9a4f35f216 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -27,11 +27,20 @@ class EscapeDecodeTest(unittest.TestCase): def test_empty_escape_decode(self): self.assertEquals(codecs.escape_decode(""), ("", 0)) +class RecodingTest(unittest.TestCase): + def test_recoding(self): + f = StringIO.StringIO() + f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8") + f2.write(u"a") + f2.close() + # Python used to crash on this at exit because of a refcount + # bug in _codecsmodule.c def test_main(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(UTF16Test)) suite.addTest(unittest.makeSuite(EscapeDecodeTest)) + suite.addTest(unittest.makeSuite(RecodingTest)) test_support.run_suite(suite) diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c index cd19ab5954..210be516f9 100644 --- a/Modules/_codecsmodule.c +++ b/Modules/_codecsmodule.c @@ -167,8 +167,10 @@ unicode_internal_decode(PyObject *self, &obj, &errors)) return NULL; - if (PyUnicode_Check(obj)) + if (PyUnicode_Check(obj)) { + Py_INCREF(obj); return codec_tuple(obj, PyUnicode_GET_SIZE(obj)); + } else { if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) return NULL; -- 2.50.1