Source

pypy-mq / multibytecodec

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# HG changeset patch
# Parent 54515dec1d201fb0a5a3926d970830aad10a92df
add missing _multibytecodec stateful classes

diff -r 54515dec1d20 pypy/module/_multibytecodec/__init__.py
--- a/pypy/module/_multibytecodec/__init__.py	Fri Jul 29 14:17:43 2011 -0500
+++ b/pypy/module/_multibytecodec/__init__.py	Fri Jul 29 16:28:46 2011 -0700
@@ -7,13 +7,22 @@
         # for compatibility this name is obscured, and should be called
         # via the _codecs_*.py modules written in lib_pypy.
         '__getcodec': 'interp_multibytecodec.getcodec',
+
+        'MultibyteIncrementalEncoder':
+            'interp_multibytecodec.MultibyteIncrementalEncoder',
+        'MultibyteIncrementalDecoder':
+            'interp_multibytecodec.MultibyteIncrementalDecoder',
+#        'MultibyteStreamReader':
+#            'interp_multibytecodec.MultibyteStreamReader',
+#        'MultibyteStreamWriter':
+#            'interp_multibytecodec.MultibyteStreamWriter',
     }
 
     appleveldefs = {
-        'MultibyteIncrementalEncoder':
-            'app_multibytecodec.MultibyteIncrementalEncoder',
-        'MultibyteIncrementalDecoder':
-            'app_multibytecodec.MultibyteIncrementalDecoder',
+#        'MultibyteIncrementalEncoder':
+#            'app_multibytecodec.MultibyteIncrementalEncoder',
+#        'MultibyteIncrementalDecoder':
+#            'app_multibytecodec.MultibyteIncrementalDecoder',
         'MultibyteStreamReader':
             'app_multibytecodec.MultibyteStreamReader',
         'MultibyteStreamWriter':
diff -r 54515dec1d20 pypy/module/_multibytecodec/c_codecs.py
--- a/pypy/module/_multibytecodec/c_codecs.py	Fri Jul 29 14:17:43 2011 -0500
+++ b/pypy/module/_multibytecodec/c_codecs.py	Fri Jul 29 16:28:46 2011 -0700
@@ -4,6 +4,8 @@
 from pypy.tool.autopath import pypydir
 
 UNICODE_REPLACEMENT_CHARACTER = u'\uFFFD'
+MBENC_FLUSH = 0x0001 # encode all characters encodable
+MBENC_RESET = MBENC_FLUSH << 1 # reset after an encoding session
 
 
 class EncodeDecodeError(Exception):
@@ -57,6 +59,8 @@
         "pypy_cjk_dec_inbuf_remaining", "pypy_cjk_dec_inbuf_consumed",
         "pypy_cjk_dec_replace_on_error",
 
+        "pypy_cjk_dec_init_state", "pypy_cjk_dec_free_state",
+
         "pypy_cjk_enc_init", "pypy_cjk_enc_free", "pypy_cjk_enc_chunk",
         "pypy_cjk_enc_reset", "pypy_cjk_enc_outbuf", "pypy_cjk_enc_outlen",
         "pypy_cjk_enc_inbuf_remaining", "pypy_cjk_enc_inbuf_consumed",
@@ -72,6 +76,9 @@
 MULTIBYTECODEC_P = rffi.COpaquePtr('struct MultibyteCodec_s',
                                    compilation_info=eci)
 
+MULTIBYTECODECSTATE_P = rffi.COpaquePtr(typedef='MultibyteCodec_State',
+                                        compilation_info=eci)
+
 def llexternal(*args, **kwds):
     kwds.setdefault('compilation_info', eci)
     kwds.setdefault('sandboxsafe', True)
@@ -111,6 +118,11 @@
                                            [DECODEBUF_P, rffi.CWCHARP,
                                             rffi.SSIZE_T, rffi.SSIZE_T],
                                            rffi.SSIZE_T)
+pypy_cjk_dec_init_state = llexternal('pypy_cjk_dec_init_state',
+                               [MULTIBYTECODEC_P],
+                               MULTIBYTECODECSTATE_P)
+pypy_cjk_dec_free_state = llexternal('pypy_cjk_dec_free_state',
+                                     [MULTIBYTECODECSTATE_P], lltype.Void)
 
 def decode(codec, stringdata, errors="strict", errorcb=None, namecb=None):
     inleft = len(stringdata)
@@ -179,7 +191,7 @@
                                ENCODEBUF_P)
 pypy_cjk_enc_free = llexternal('pypy_cjk_enc_free', [ENCODEBUF_P],
                                lltype.Void)
-pypy_cjk_enc_chunk = llexternal('pypy_cjk_enc_chunk', [ENCODEBUF_P],
+pypy_cjk_enc_chunk = llexternal('pypy_cjk_enc_chunk', [ENCODEBUF_P, rffi.INT],
                                 rffi.SSIZE_T)
 pypy_cjk_enc_reset = llexternal('pypy_cjk_enc_reset', [ENCODEBUF_P],
                                 rffi.SSIZE_T)
@@ -196,7 +208,8 @@
                                             rffi.SSIZE_T, rffi.SSIZE_T],
                                            rffi.SSIZE_T)
 
-def encode(codec, unicodedata, errors="strict", errorcb=None, namecb=None):
+def encode(codec, unicodedata, errors="strict", errorcb=None, namecb=None,
+           flags=0):
     inleft = len(unicodedata)
     inbuf = rffi.get_nonmoving_unicodebuffer(unicodedata)
     try:
@@ -205,8 +218,9 @@
             raise MemoryError
         try:
             while True:
-                r = pypy_cjk_enc_chunk(encodebuf)
-                if r == 0:
+                # XXX: maybe use rffi.CConstant instead
+                r = pypy_cjk_enc_chunk(encodebuf, rffi.cast(rffi.INT, flags))
+                if r == 0 or (r == MBERR_TOOFEW and not flags & MBENC_FLUSH):
                     break
                 multibytecodec_encerror(encodebuf, r, errors,
                                         codec, errorcb, namecb, unicodedata)
@@ -249,7 +263,9 @@
         replace = ""
     elif errors == "replace":
         try:
-            replace = encode(codec, u"?")
+            replace = encode(codec, u"?", MBENC_FLUSH | MBENC_RESET)
+            # 0 in code but resizes the buffer if it fails..
+            #replace = encode(codec, u"?", 0)
         except EncodeDecodeError:
             replace = "?"
     else:
diff -r 54515dec1d20 pypy/module/_multibytecodec/interp_multibytecodec.py
--- a/pypy/module/_multibytecodec/interp_multibytecodec.py	Fri Jul 29 14:17:43 2011 -0500
+++ b/pypy/module/_multibytecodec/interp_multibytecodec.py	Fri Jul 29 16:28:46 2011 -0700
@@ -1,9 +1,12 @@
+import sys
+
 from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.gateway import interp2app, unwrap_spec
-from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.typedef import GetSetProperty, TypeDef
 from pypy.interpreter.error import OperationError
 from pypy.module._multibytecodec import c_codecs
 from pypy.module._codecs.interp_codecs import CodecState
+from pypy.objspace.std.unicodetype import unicode_from_object
 
 
 class MultibyteCodec(Wrappable):
@@ -42,9 +45,11 @@
             errors = 'strict'
         state = space.fromcache(CodecState)
         #
+        flags = c_codecs.MBENC_FLUSH | c_codecs.MBENC_RESET
         try:
             output = c_codecs.encode(self.codec, input, errors,
-                                     state.encode_error_handler, self.name)
+                                     state.encode_error_handler, self.name,
+                                     flags)
         except c_codecs.EncodeDecodeError, e:
             raise OperationError(
                 space.w_UnicodeEncodeError,
@@ -70,6 +75,249 @@
 MultibyteCodec.typedef.acceptable_as_base_class = False
 
 
+"""
+class MultibyteStatefulCodec(Wrappable):
+
+    def __init__(self, space, codec, errors):
+        self.codec, self.errors = codec, errors
+
+class MultibyteStatefulDecoder(Wrappable):
+
+    def __init__(self, )
+    """
+
+
+class MultibyteIncrementalEncoder(Wrappable):
+
+    def __init__(self, space, codec, errors):
+        self.codec = codec
+        self.errors = errors
+        self.pending = None
+
+        # should have codec, state, errors (general StatefulCodec --
+        # should I have a class hierarchy?).
+        # should also have pending[MAXENCPENDING (2)] and pendingsize (StatefulEncoder)
+
+        self.pendingsize = 0
+        self.pending = None
+
+    # XXX: i think this and above should probably be str,
+    # >>> 'hi'.encode('utf-8', None)
+    # Traceback (most recent call last):
+    #   File "<stdin>", line 1, in <module>
+    # TypeError: encode() argument 2 must be string, not None
+    @unwrap_spec(errors="str_or_None")
+    def mbiencoder_new(space, w_subtype, errors='strict'):
+        w_codec = space.getattr(w_subtype, space.wrap('codec'))
+        if not isinstance(w_codec, MultibyteCodec):
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("codec is unexpected type"))
+
+        w_obj = space.allocate_instance(MultibyteIncrementalEncoder, w_subtype)
+        w_obj.__init__(space, space.unwrap(w_codec), errors)
+        return w_obj
+
+    """
+    def mbiencoder_init(self, space):
+        pass
+        """
+
+    @unwrap_spec(final=int)
+    def encode(self, space, w_input, final=0):
+        return encoder_encode_stateful(space, self, w_input, final)
+
+    def reset(self):
+        #if (self->codec->decreset != NULL &&
+            #self->codec->decreset(&self->state, self->codec->config) != 0)
+            #return NULL;
+            #self->pendingsize = 0;
+        #try: from nose.tools import set_trace
+        #except ImportError: from pdb import set_trace
+        #set_trace()
+        # XXX: this calls decreset in the cpython code which is a bug
+        self.pendingsize = 0
+
+    def descr_errors(self, space):
+        return space.wrap(self.errors)
+
+    def descr_seterrors(self, space, w_errors):
+        self.errors = space.str_w(w_errors)
+
+
+MultibyteIncrementalEncoder.typedef = TypeDef(
+    'MultibyteIncrementalEncoder',
+    __module__ = '_multibytecodec',
+    __new__ = interp2app(MultibyteIncrementalEncoder.mbiencoder_new.im_func),
+#    __init__ = interp2app(MultibyteIncrementalEncoder.mbiencoder_init),
+    encode = interp2app(MultibyteIncrementalEncoder.encode),
+    reset = interp2app(MultibyteIncrementalEncoder.reset),
+    errors = GetSetProperty(MultibyteIncrementalEncoder.descr_errors,
+                            MultibyteIncrementalEncoder.descr_seterrors,
+                            cls=MultibyteIncrementalEncoder),
+    )
+
+def encoder_encode_stateful(space, w_ctx, w_input, final):
+    if not space.isinstance_w(w_input, space.w_unicode):
+        w_input = unicode_from_object(space, w_input)
+        if not space.isinstance_w(w_input, space.w_unicode):
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("couldn't convert the object to "
+                                            "unicode."))
+
+    input = space.unicode_w(w_input)
+    inputlen = len(input)
+    origpending = w_ctx.pendingsize
+    if origpending > 0:
+        if inputlen > (sys.maxint - origpending):
+            raise OperationError(space.w_MemoryError, space.w_None)
+        input = w_ctx.pending + input
+        inputlen += origpending
+        w_ctx.pendingsize = 0
+
+    state = space.fromcache(CodecState)
+    try:
+        output = c_codecs.encode(w_ctx.codec.codec, input, w_ctx.errors,
+                                 state.encode_error_handler, w_ctx.codec.name,
+                                 c_codecs.MBENC_FLUSH if final else 0)
+    except Exception, e:
+        if origpending > 0:
+            # XXX: recover the original pending buffer
+            # XXX: copy inbuf back to pending?
+            # this doesn't seem necessary, we didn't overwrite it earlier
+            pass
+        w_ctx.pendingsize = origpending
+        if not isinstance(e, c_codecs.EncodeDecodeError):
+            raise
+        raise OperationError(
+            space.w_UnicodeEncodeError,
+            space.newtuple([
+                space.wrap(w_ctx.codec.name),
+                space.wrap(input),
+                space.wrap(e.start),
+                space.wrap(e.end),
+                space.wrap(e.reason)]))
+
+    outputlen = len(output)
+    if outputlen < inputlen:
+        w_ctx.pendingsize = inputlen - outputlen
+        # XXX: MAXENCPENDING
+        if w_ctx.pendingsize > 2:
+            w_ctx.pendingsize = 0
+            raise OperationError(space.w_UnicodeError,
+                                 space.wrap("pending buffer overflow"))
+        w_ctx.pending = input[-w_ctx.pendingsize:]
+    return space.wrap(output)
+
+
+class MultibyteIncrementalDecoder(Wrappable):
+
+    def __init__(self, space, codec, errors):
+        self.errors = errors
+        self.codec = codec
+        self.state = c_codecs.pypy_cjk_dec_init_state(codec.codec)
+
+        # not exposed
+        self.pendingsize = 0
+        self.pending = None # XXX:
+
+    def __del__(self):
+        if self.state:
+            c_codecs.pypy_cjk_dec_free_state(self.state)
+
+    @unwrap_spec(errors="str_or_None")
+    def mbidecoder_new(space, w_subtype, errors='strict'):
+        codec = space.getattr(w_subtype, space.wrap('codec'))
+        if not isinstance(codec, MultibyteCodec):
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("codec is unexpected type"))
+
+        w_obj = space.allocate_instance(MultibyteIncrementalDecoder, w_subtype)
+        w_obj.__init__(space, codec, errors)
+        return w_obj
+
+    def mbidecoder_init(self, space):
+        pass
+
+    @unwrap_spec(final=int)
+    def decode(self, space, w_input, final=0):
+        input = space.str_w(w_input)
+        inputlen = len(input)
+        origpending = w_ctx.pendingsize
+        if origpending > 0:
+            if inputlen > (sys.maxint - origpending):
+                raise OperationError(space.w_MemoryError, space.w_None)
+            input = w_ctx.pending + input
+            inputlen += origpending
+            w_ctx.pendingsize = 0
+
+        codec = self.codec.codec
+        decodebuf = c_codecs.pypy_cjk_dec_init(codec, input, inputlen)
+
+        state = space.fromcache(CodecState)
+        try:
+            output = c_codecs.encode(w_ctx.codec.codec, input, w_ctx.errors,
+                                     state.encode_error_handler, w_ctx.codec.name,
+                                     c_codecs.MBENC_FLUSH if final else 0)
+        except Exception, e:
+            if origpending > 0:
+                # XXX: recover the original pending buffer
+                # XXX: copy inbuf back to pending?
+                # this doesn't seem necessary, we didn't overwrite it earlier
+                pass
+            w_ctx.pendingsize = origpending
+            if not isinstance(e, c_codecs.EncodeDecodeError):
+                raise
+            raise OperationError(
+                space.w_UnicodeEncodeError,
+                space.newtuple([
+                    space.wrap(w_ctx.codec.name),
+                    space.wrap(input),
+                    space.wrap(e.start),
+                    space.wrap(e.end),
+                    space.wrap(e.reason)]))
+
+        outputlen = len(output)
+        if outputlen < inputlen:
+            w_ctx.pendingsize = inputlen - outputlen
+            # XXX: MAXENCPENDING
+            if w_ctx.pendingsize > 2:
+                w_ctx.pendingsize = 0
+                raise OperationError(space.w_UnicodeError,
+                                     space.wrap("pending buffer overflow"))
+            w_ctx.pending = input[-w_ctx.pendingsize:]
+        return space.wrap(output)
+
+    def reset(self):
+        #if (self->codec->decreset != NULL &&
+            #self->codec->decreset(&self->state, self->codec->config) != 0)
+            #return NULL;
+            #self->pendingsize = 0;
+        #try: from nose.tools import set_trace
+        #except ImportError: from pdb import set_trace
+        #set_trace()
+        # XXX: this calls decreset in the cpython code which is a bug
+        self.pendingsize = 0
+
+    def descr_errors(self, space):
+        return space.wrap(self.errors)
+
+    def descr_seterrors(self, space, w_errors):
+        self.errors = space.str_w(w_errors)
+
+
+MultibyteIncrementalDecoder.typedef = TypeDef(
+    'MultibyteIncrementalDecoder',
+    __module__ = '_multibytecodec',
+    __new__ = interp2app(MultibyteIncrementalDecoder.mbidecoder_new.im_func),
+    __init__ = interp2app(MultibyteIncrementalDecoder.mbidecoder_init),
+    decode = interp2app(MultibyteIncrementalDecoder.decode),
+    reset = interp2app(MultibyteIncrementalDecoder.reset),
+    errors = GetSetProperty(MultibyteIncrementalDecoder.descr_errors,
+                            MultibyteIncrementalDecoder.descr_seterrors,
+                            cls=MultibyteIncrementalDecoder),
+    )
+
+
 @unwrap_spec(name=str)
 def getcodec(space, name):
     try:
diff -r 54515dec1d20 pypy/module/_multibytecodec/test/test_app_codecs.py
--- a/pypy/module/_multibytecodec/test/test_app_codecs.py	Fri Jul 29 14:17:43 2011 -0500
+++ b/pypy/module/_multibytecodec/test/test_app_codecs.py	Fri Jul 29 16:28:46 2011 -0700
@@ -106,3 +106,75 @@
         repl = u"\u2014"
         s = u"\uDDA1".encode("gbk", "test.multi_bad_handler")
         assert s == '\xA1\xAA'
+
+class AppTestIncrementalEncoder:
+    def setup_class(cls):
+        cls.space = gettestobjspace(usemodules=['_multibytecodec'])
+
+    def test_stateless(self):
+        import codecs
+        # This has no state
+        encoder = codecs.getincrementalencoder("cp949")()
+        s = encoder.encode(u'\ud30c\uc774\uc36c \ub9c8\uc744')
+        assert s == '\xc6\xc4\xc0\xcc\xbd\xe3 \xb8\xb6\xc0\xbb'
+        s = encoder.reset()
+        assert s is None
+        s = encoder.encode(u'\u2606\u223c\u2606', True)
+        assert s == '\xa1\xd9\xa1\xad\xa1\xd9'
+        s = encoder.reset()
+        assert s is None
+        s = encoder.encode(u'', True)
+        assert s == ''
+        s = encoder.encode(u'', False)
+        assert s == ''
+        s = encoder.reset()
+        assert s is None
+
+    def test_stateful(self):
+        import codecs
+        encoder = codecs.getincrementalencoder("jisx0213")()
+        s = encoder.encode(u'\u00e6\u0300')
+        assert s == '\xab\xc4'
+        s = encoder.encode(u'\u00e6')
+        assert s == ''
+        s = encoder.encode(u'\u0300')
+        assert s == '\xab\xc4'
+        s = encoder.encode(u'\u00e6', True)
+        assert s == '\xa9\xdc'
+        s = encoder.reset()
+        assert s is None
+        s = encoder.encode(u'\u0300')
+        assert s == '\xab\xdc'
+        s = encoder.encode(u'\u00e6')
+        assert s == ''
+        s = encoder.encode('', True)
+        assert s == '\xa9\xdc'
+        s = encoder.encode('', True)
+        assert s == ''
+
+    def test_stateful_keep_buffer(self):
+        import codecs
+        encoder = codecs.getincrementalencoder('jisx0213')()
+        s = encoder.encode(u'\u00e6')
+        assert s == ''
+        raises(UnicodeEncodeError, encoder.encode, u'\u0123')
+        s = encoder.encode(u'\u0300\u00e6')
+        assert s == '\xab\xc4'
+        raises(UnicodeEncodeError, encoder.encode, u'\u0123')
+        s = encoder.reset()
+        assert s is None
+        s = encoder.encode(u'\u0300')
+        assert s == '\xab\xdc'
+        s = encoder.encode(u'\u00e6')
+        assert s == ''
+        raises(UnicodeEncodeError, encoder.encode, u'\u0123')
+        s = encoder.encode(u'', True)
+        assert s == '\xa9\xdc'
+
+    def test_issue5640(self):
+        import codecs
+        encoder = codecs.getincrementalencoder('shift-jis')('backslashreplace')
+        s = encoder.encode(u'\xff')
+        assert s ==  b'\\xff'
+        s = encoder.encode(u'\n')
+        assert s == b'\n'
diff -r 54515dec1d20 pypy/translator/c/src/cjkcodecs/multibytecodec.c
--- a/pypy/translator/c/src/cjkcodecs/multibytecodec.c	Fri Jul 29 14:17:43 2011 -0500
+++ b/pypy/translator/c/src/cjkcodecs/multibytecodec.c	Fri Jul 29 16:28:46 2011 -0700
@@ -30,6 +30,50 @@
   return NULL;
 }
 
+MultibyteCodec_State *pypy_cjk_dec_init_state(const MultibyteCodec *codec)
+{
+  MultibyteCodec_State *state = malloc(sizeof(MultibyteCodec_State));
+  if (!state)
+    return NULL;
+  if (codec->decinit != NULL && codec->decinit(state, codec->config) != 0) {
+    free(state);
+    return NULL;
+  }
+  return state;
+}
+
+void pypy_cjk_dec_free_state(MultibyteCodec_State *state)
+{
+  free(state);
+}
+
+struct pypy_cjk_dec_s *pypy_cjk_prepare_buffer(const MultibyteCodec *codec,
+                                               char *inbuf, Py_ssize_t inlen)
+{
+  struct pypy_cjk_dec_s *d = malloc(sizeof(struct pypy_cjk_dec_s));
+  if (!d)
+    return NULL;
+  if (codec->decinit != NULL && codec->decinit(&d->state, codec->config) != 0)
+    goto errorexit;
+
+  d->codec = codec;
+  d->inbuf_start = inbuf;
+  d->inbuf = inbuf;
+  d->inbuf_end = inbuf + inlen;
+  d->outbuf_start = (inlen <= (PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) ?
+                     malloc(inlen * sizeof(Py_UNICODE)) :
+                     NULL);
+  if (!d->outbuf_start)
+    goto errorexit;
+  d->outbuf = d->outbuf_start;
+  d->outbuf_end = d->outbuf_start + inlen;
+  return d;
+
+ errorexit:
+  free(d);
+  return NULL;
+}
+
 void pypy_cjk_dec_free(struct pypy_cjk_dec_s *d)
 {
   free(d->outbuf_start);
@@ -169,9 +213,8 @@
 
 #define MBENC_RESET     MBENC_MAX<<1
 
-Py_ssize_t pypy_cjk_enc_chunk(struct pypy_cjk_enc_s *d)
+Py_ssize_t pypy_cjk_enc_chunk(struct pypy_cjk_enc_s *d, int flags)
 {
-  int flags = MBENC_FLUSH | MBENC_RESET;   /* XXX always, for now */
   while (1)
     {
       Py_ssize_t r;
diff -r 54515dec1d20 pypy/translator/c/src/cjkcodecs/multibytecodec.h
--- a/pypy/translator/c/src/cjkcodecs/multibytecodec.h	Fri Jul 29 14:17:43 2011 -0500
+++ b/pypy/translator/c/src/cjkcodecs/multibytecodec.h	Fri Jul 29 16:28:46 2011 -0700
@@ -115,7 +115,7 @@
 struct pypy_cjk_enc_s *pypy_cjk_enc_init(const MultibyteCodec *codec,
                                          Py_UNICODE *inbuf, Py_ssize_t inlen);
 void pypy_cjk_enc_free(struct pypy_cjk_enc_s *);
-Py_ssize_t pypy_cjk_enc_chunk(struct pypy_cjk_enc_s *);
+Py_ssize_t pypy_cjk_enc_chunk(struct pypy_cjk_enc_s *, int flags);
 Py_ssize_t pypy_cjk_enc_reset(struct pypy_cjk_enc_s *);
 char *pypy_cjk_enc_outbuf(struct pypy_cjk_enc_s *);
 Py_ssize_t pypy_cjk_enc_outlen(struct pypy_cjk_enc_s *);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.