Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e15dce3

Browse files
author
Victor Stinner
committed
Close #12171: IncrementalEncoder.reset() of CJK codecs (multibytecodec) calls
encreset() instead of decreset().
1 parent ebbb3b7 commit e15dce3

4 files changed

Lines changed: 23 additions & 8 deletions

File tree

Doc/library/codecs.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ define in order to be compatible with the Python codec registry.
458458

459459
.. method:: reset()
460460

461-
Reset the encoder to the initial state.
461+
Reset the encoder to the initial state. The output is discarded: call
462+
``.encode('', final=True)`` to reset the encoder and to get the output.
462463

463464

464465
.. method:: IncrementalEncoder.getstate()

Lib/test/test_multibytecodec.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ class TestStateful(unittest.TestCase):
260260
text = '\u4E16\u4E16'
261261
encoding = 'iso-2022-jp'
262262
expected = b'\x1b$B@$@$'
263-
expected_reset = b'\x1b$B@$@$\x1b(B'
263+
reset = b'\x1b(B'
264+
expected_reset = expected + reset
264265

265266
def test_encode(self):
266267
self.assertEqual(self.text.encode(self.encoding), self.expected_reset)
@@ -271,6 +272,8 @@ def test_incrementalencoder(self):
271272
encoder.encode(char)
272273
for char in self.text)
273274
self.assertEqual(output, self.expected)
275+
self.assertEqual(encoder.encode('', final=True), self.reset)
276+
self.assertEqual(encoder.encode('', final=True), b'')
274277

275278
def test_incrementalencoder_final(self):
276279
encoder = codecs.getincrementalencoder(self.encoding)()
@@ -279,12 +282,14 @@ def test_incrementalencoder_final(self):
279282
encoder.encode(char, index == last_index)
280283
for index, char in enumerate(self.text))
281284
self.assertEqual(output, self.expected_reset)
285+
self.assertEqual(encoder.encode('', final=True), b'')
282286

283287
class TestHZStateful(TestStateful):
284288
text = '\u804a\u804a'
285289
encoding = 'hz'
286290
expected = b'~{ADAD'
287-
expected_reset = b'~{ADAD~}'
291+
reset = b'~}'
292+
expected_reset = expected + reset
288293

289294
def test_main():
290295
support.run_unittest(__name__)

Misc/NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,12 @@ Core and Builtins
177177
Library
178178
-------
179179

180+
- Issue #12171: IncrementalEncoder.reset() of CJK codecs (multibytecodec) calls
181+
encreset() instead of decreset().
182+
180183
- Issue #12218: Removed wsgiref.egg-info.
181184

182-
- Issue #12196: Add pipe2() to the os module.
185+
- Issue #12196: Add pipe2() to the os module.
183186

184187
- Issue #985064: Make plistlib more resilient to faulty input plists.
185188
Patch by Mher Movsisyan.

Modules/cjkcodecs/multibytecodec.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,11 +901,17 @@ mbiencoder_encode(MultibyteIncrementalEncoderObject *self,
901901
static PyObject *
902902
mbiencoder_reset(MultibyteIncrementalEncoderObject *self)
903903
{
904-
if (self->codec->decreset != NULL &&
905-
self->codec->decreset(&self->state, self->codec->config) != 0)
906-
return NULL;
904+
/* Longest output: 4 bytes (b'\x0F\x1F(B') with ISO 2022 */
905+
unsigned char buffer[4], *outbuf;
906+
Py_ssize_t r;
907+
if (self->codec->encreset != NULL) {
908+
outbuf = buffer;
909+
r = self->codec->encreset(&self->state, self->codec->config,
910+
&outbuf, sizeof(buffer));
911+
if (r != 0)
912+
return NULL;
913+
}
907914
self->pendingsize = 0;
908-
909915
Py_RETURN_NONE;
910916
}
911917

0 commit comments

Comments
 (0)