Skip to content

Commit 29273c8

Browse files
committed
Fix for [ 543344 ] Interpreter crashes when recoding; suggested
by Michael Stone (mbrierst). Python 2.1.4, 2.2.2 candidate.
1 parent 604ade4 commit 29273c8

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/test_codecs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,20 @@ class EscapeDecodeTest(unittest.TestCase):
2727
def test_empty_escape_decode(self):
2828
self.assertEquals(codecs.escape_decode(""), ("", 0))
2929

30+
class RecodingTest(unittest.TestCase):
31+
def test_recoding(self):
32+
f = StringIO.StringIO()
33+
f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
34+
f2.write(u"a")
35+
f2.close()
36+
# Python used to crash on this at exit because of a refcount
37+
# bug in _codecsmodule.c
3038

3139
def test_main():
3240
suite = unittest.TestSuite()
3341
suite.addTest(unittest.makeSuite(UTF16Test))
3442
suite.addTest(unittest.makeSuite(EscapeDecodeTest))
43+
suite.addTest(unittest.makeSuite(RecodingTest))
3544
test_support.run_suite(suite)
3645

3746

Modules/_codecsmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ unicode_internal_decode(PyObject *self,
167167
&obj, &errors))
168168
return NULL;
169169

170-
if (PyUnicode_Check(obj))
170+
if (PyUnicode_Check(obj)) {
171+
Py_INCREF(obj);
171172
return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
173+
}
172174
else {
173175
if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
174176
return NULL;

0 commit comments

Comments
 (0)