(Merge 3.2) Issue #12100: Don't reset incremental encoders of CJK codecs at

each call to their encode() method anymore, but continue to call the reset()
method if the final argument is True.
This commit is contained in:
Victor Stinner 2011-05-24 22:24:11 +02:00
commit eb734f77ad
3 changed files with 38 additions and 4 deletions

View File

@ -256,6 +256,36 @@ class Test_ISO2022(unittest.TestCase):
# Any ISO 2022 codec will cause the segfault # Any ISO 2022 codec will cause the segfault
myunichr(x).encode('iso_2022_jp', 'ignore') myunichr(x).encode('iso_2022_jp', 'ignore')
class TestStateful(unittest.TestCase):
text = '\u4E16\u4E16'
encoding = 'iso-2022-jp'
expected = b'\x1b$B@$@$'
expected_reset = b'\x1b$B@$@$\x1b(B'
def test_encode(self):
self.assertEqual(self.text.encode(self.encoding), self.expected_reset)
def test_incrementalencoder(self):
encoder = codecs.getincrementalencoder(self.encoding)()
output = b''.join(
encoder.encode(char)
for char in self.text)
self.assertEqual(output, self.expected)
def test_incrementalencoder_final(self):
encoder = codecs.getincrementalencoder(self.encoding)()
last_index = len(self.text) - 1
output = b''.join(
encoder.encode(char, index == last_index)
for index, char in enumerate(self.text))
self.assertEqual(output, self.expected_reset)
class TestHZStateful(TestStateful):
text = '\u804a\u804a'
encoding = 'hz'
expected = b'~{ADAD'
expected_reset = b'~{ADAD~}'
def test_main(): def test_main():
support.run_unittest(__name__) support.run_unittest(__name__)

View File

@ -161,6 +161,10 @@ Core and Builtins
Library Library
------- -------
- Issue #12100: Don't reset incremental encoders of CJK codecs at each call to
their encode() method anymore, but continue to call the reset() method if the
final argument is True.
- Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl - Issue #12049: Add RAND_bytes() and RAND_pseudo_bytes() functions to the ssl
module. module.

View File

@ -479,7 +479,7 @@ multibytecodec_encode(MultibyteCodec *codec,
MultibyteEncodeBuffer buf; MultibyteEncodeBuffer buf;
Py_ssize_t finalsize, r = 0; Py_ssize_t finalsize, r = 0;
if (datalen == 0) if (datalen == 0 && !(flags & MBENC_RESET))
return PyBytes_FromStringAndSize(NULL, 0); return PyBytes_FromStringAndSize(NULL, 0);
buf.excobj = NULL; buf.excobj = NULL;
@ -515,7 +515,7 @@ multibytecodec_encode(MultibyteCodec *codec,
break; break;
} }
if (codec->encreset != NULL) if (codec->encreset != NULL && (flags & MBENC_RESET))
for (;;) { for (;;) {
Py_ssize_t outleft; Py_ssize_t outleft;
@ -785,8 +785,8 @@ encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,
inbuf_end = inbuf + datalen; inbuf_end = inbuf + datalen;
r = multibytecodec_encode(ctx->codec, &ctx->state, r = multibytecodec_encode(ctx->codec, &ctx->state,
(const Py_UNICODE **)&inbuf, (const Py_UNICODE **)&inbuf, datalen,
datalen, ctx->errors, final ? MBENC_FLUSH : 0); ctx->errors, final ? MBENC_FLUSH | MBENC_RESET : 0);
if (r == NULL) { if (r == NULL) {
/* recover the original pending buffer */ /* recover the original pending buffer */
if (origpending > 0) if (origpending > 0)