Skip to content

Commit cb2c9e7

Browse files
Pass codec iso. config to mbcodec API handlers
1 parent 214e568 commit cb2c9e7

File tree

6 files changed

+62
-59
lines changed

6 files changed

+62
-59
lines changed

Modules/cjkcodecs/_codecs_iso2022.c

+11-10
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
#define STATE_CLEARFLAG(f) do { ((state)->c[4]) &= ~(f); } while (0)
9191
#define STATE_CLEARFLAGS() do { ((state)->c[4]) = 0; } while (0)
9292

93-
#define ISO2022_CONFIG ((const struct iso2022_config *)config)
93+
#define ISO2022_CONFIG ((const struct iso2022_config *)(codec->config))
9494
#define CONFIG_ISSET(flag) (ISO2022_CONFIG->flags & (flag))
9595
#define CONFIG_DESIGNATIONS (ISO2022_CONFIG->designations)
9696

@@ -288,7 +288,7 @@ DECODER_RESET(iso2022)
288288
}
289289

290290
static Py_ssize_t
291-
iso2022processesc(const void *config, MultibyteCodec_State *state,
291+
iso2022processesc(const MultibyteCodec *codec, MultibyteCodec_State *state,
292292
const unsigned char **inbuf, Py_ssize_t *inleft)
293293
{
294294
unsigned char charset, designation;
@@ -388,7 +388,7 @@ iso2022processesc(const void *config, MultibyteCodec_State *state,
388388
}
389389

390390
static Py_ssize_t
391-
iso2022processg2(const void *config, MultibyteCodec_State *state,
391+
iso2022processg2(const MultibyteCodec *codec, MultibyteCodec_State *state,
392392
const unsigned char **inbuf, Py_ssize_t *inleft,
393393
_PyUnicodeWriter *writer)
394394
{
@@ -442,14 +442,14 @@ DECODER(iso2022)
442442
case ESC:
443443
REQUIRE_INBUF(2);
444444
if (IS_ISO2022ESC(INBYTE2)) {
445-
err = iso2022processesc(config, state,
445+
err = iso2022processesc(codec, state,
446446
inbuf, &inleft);
447447
if (err != 0)
448448
return err;
449449
}
450450
else if (CONFIG_ISSET(USE_G2) && INBYTE2 == 'N') {/* SS2 */
451451
REQUIRE_INBUF(3);
452-
err = iso2022processg2(config, state,
452+
err = iso2022processg2(codec, state,
453453
inbuf, &inleft, writer);
454454
if (err != 0)
455455
return err;
@@ -712,7 +712,7 @@ static Py_UCS4
712712
jisx0213_2000_1_decoder(const unsigned char *data)
713713
{
714714
Py_UCS4 u;
715-
EMULATE_JISX0213_2000_DECODE_PLANE1(u, data[0], data[1])
715+
EMULATE_JISX0213_2000_DECODE_PLANE1(config, u, data[0], data[1])
716716
else if (data[0] == 0x21 && data[1] == 0x40) /* F/W REVERSE SOLIDUS */
717717
return 0xff3c;
718718
else if (TRYMAP_DEC(jisx0208, u, data[0], data[1]))
@@ -732,7 +732,7 @@ static Py_UCS4
732732
jisx0213_2000_2_decoder(const unsigned char *data)
733733
{
734734
Py_UCS4 u;
735-
EMULATE_JISX0213_2000_DECODE_PLANE2_CHAR(u, data[0], data[1])
735+
EMULATE_JISX0213_2000_DECODE_PLANE2_CHAR(config, u, data[0], data[1])
736736
if (TRYMAP_DEC(jisx0213_2_bmp, u, data[0], data[1]))
737737
;
738738
else if (TRYMAP_DEC(jisx0213_2_emp, u, data[0], data[1]))
@@ -776,22 +776,23 @@ jisx0213_2004_2_decoder(const unsigned char *data)
776776
}
777777

778778
static DBCHAR
779-
jisx0213_encoder(const Py_UCS4 *data, Py_ssize_t *length, void *config)
779+
jisx0213_encoder(const Py_UCS4 *data, Py_ssize_t *length,
780+
const void *config)
780781
{
781782
DBCHAR coded;
782783

783784
switch (*length) {
784785
case 1: /* first character */
785786
if (*data >= 0x10000) {
786787
if ((*data) >> 16 == 0x20000 >> 16) {
787-
EMULATE_JISX0213_2000_ENCODE_EMP(coded, *data)
788+
EMULATE_JISX0213_2000_ENCODE_EMP(config, coded, *data)
788789
else if (TRYMAP_ENC(jisx0213_emp, coded, (*data) & 0xffff))
789790
return coded;
790791
}
791792
return MAP_UNMAPPABLE;
792793
}
793794

794-
EMULATE_JISX0213_2000_ENCODE_BMP(coded, *data)
795+
EMULATE_JISX0213_2000_ENCODE_BMP(config, coded, *data)
795796
else if (TRYMAP_ENC(jisx0213_bmp, coded, *data)) {
796797
if (coded == MULTIC)
797798
return MAP_MULTIPLE_AVAIL;

Modules/cjkcodecs/_codecs_jp.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ ENCODER(euc_jis_2004)
164164
insize = 1;
165165

166166
if (c <= 0xFFFF) {
167-
EMULATE_JISX0213_2000_ENCODE_BMP(code, c)
167+
EMULATE_JISX0213_2000_ENCODE_BMP(codec->config, code, c)
168168
else if (TRYMAP_ENC(jisx0213_bmp, code, c)) {
169169
if (code == MULTIC) {
170170
if (inlen - *inpos < 2) {
@@ -215,7 +215,7 @@ ENCODER(euc_jis_2004)
215215
return 1;
216216
}
217217
else if (c >> 16 == EMPBASE >> 16) {
218-
EMULATE_JISX0213_2000_ENCODE_EMP(code, c)
218+
EMULATE_JISX0213_2000_ENCODE_EMP(codec->config, code, c)
219219
else if (TRYMAP_ENC(jisx0213_emp, code, c & 0xffff))
220220
;
221221
else
@@ -271,7 +271,7 @@ DECODER(euc_jis_2004)
271271
c3 = INBYTE3 ^ 0x80;
272272

273273
/* JIS X 0213 Plane 2 or JIS X 0212 (see NOTES) */
274-
EMULATE_JISX0213_2000_DECODE_PLANE2(writer, c2, c3)
274+
EMULATE_JISX0213_2000_DECODE_PLANE2(codec->config, writer, c2, c3)
275275
else if (TRYMAP_DEC(jisx0213_2_bmp, decoded, c2, c3))
276276
OUTCHAR(decoded);
277277
else if (TRYMAP_DEC(jisx0213_2_emp, code, c2, c3)) {
@@ -293,7 +293,7 @@ DECODER(euc_jis_2004)
293293
c2 = INBYTE2 ^ 0x80;
294294

295295
/* JIS X 0213 Plane 1 */
296-
EMULATE_JISX0213_2000_DECODE_PLANE1(writer, c, c2)
296+
EMULATE_JISX0213_2000_DECODE_PLANE1(codec->config, writer, c, c2)
297297
else if (c == 0x21 && c2 == 0x40)
298298
OUTCHAR(0xff3c);
299299
else if (c == 0x22 && c2 == 0x32)
@@ -582,7 +582,7 @@ ENCODER(shift_jis_2004)
582582

583583
if (code == NOCHAR) {
584584
if (c <= 0xffff) {
585-
EMULATE_JISX0213_2000_ENCODE_BMP(code, c)
585+
EMULATE_JISX0213_2000_ENCODE_BMP(codec->config, code, c)
586586
else if (TRYMAP_ENC(jisx0213_bmp, code, c)) {
587587
if (code == MULTIC) {
588588
if (inlen - *inpos < 2) {
@@ -625,7 +625,7 @@ ENCODER(shift_jis_2004)
625625
return 1;
626626
}
627627
else if (c >> 16 == EMPBASE >> 16) {
628-
EMULATE_JISX0213_2000_ENCODE_EMP(code, c)
628+
EMULATE_JISX0213_2000_ENCODE_EMP(codec->config, code, c)
629629
else if (TRYMAP_ENC(jisx0213_emp, code, c&0xffff))
630630
;
631631
else
@@ -686,7 +686,7 @@ DECODER(shift_jis_2004)
686686

687687
if (c1 < 0x5e) { /* Plane 1 */
688688
c1 += 0x21;
689-
EMULATE_JISX0213_2000_DECODE_PLANE1(writer,
689+
EMULATE_JISX0213_2000_DECODE_PLANE1(codec->config, writer,
690690
c1, c2)
691691
else if (TRYMAP_DEC(jisx0208, decoded, c1, c2))
692692
OUTCHAR(decoded);
@@ -708,7 +708,7 @@ DECODER(shift_jis_2004)
708708
else
709709
c1 -= 0x3d;
710710

711-
EMULATE_JISX0213_2000_DECODE_PLANE2(writer,
711+
EMULATE_JISX0213_2000_DECODE_PLANE2(codec->config, writer,
712712
c1, c2)
713713
else if (TRYMAP_DEC(jisx0213_2_bmp, decoded, c1, c2))
714714
OUTCHAR(decoded);

Modules/cjkcodecs/cjkcodecs.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,33 @@ get_module_state(PyObject *mod)
7676
}
7777

7878
#define CODEC_INIT(encoding) \
79-
static int encoding##_codec_init(const void *config)
79+
static int encoding##_codec_init(const MultibyteCodec *codec)
8080

8181
#define ENCODER_INIT(encoding) \
8282
static int encoding##_encode_init( \
83-
MultibyteCodec_State *state, const void *config)
83+
MultibyteCodec_State *state, const MultibyteCodec *codec)
8484
#define ENCODER(encoding) \
8585
static Py_ssize_t encoding##_encode( \
86-
MultibyteCodec_State *state, const void *config, \
86+
MultibyteCodec_State *state, const MultibyteCodec *codec, \
8787
int kind, const void *data, \
8888
Py_ssize_t *inpos, Py_ssize_t inlen, \
8989
unsigned char **outbuf, Py_ssize_t outleft, int flags)
9090
#define ENCODER_RESET(encoding) \
9191
static Py_ssize_t encoding##_encode_reset( \
92-
MultibyteCodec_State *state, const void *config, \
92+
MultibyteCodec_State *state, const MultibyteCodec *codec, \
9393
unsigned char **outbuf, Py_ssize_t outleft)
9494

9595
#define DECODER_INIT(encoding) \
9696
static int encoding##_decode_init( \
97-
MultibyteCodec_State *state, const void *config)
97+
MultibyteCodec_State *state, const MultibyteCodec *codec)
9898
#define DECODER(encoding) \
9999
static Py_ssize_t encoding##_decode( \
100-
MultibyteCodec_State *state, const void *config, \
100+
MultibyteCodec_State *state, const MultibyteCodec *codec, \
101101
const unsigned char **inbuf, Py_ssize_t inleft, \
102102
_PyUnicodeWriter *writer)
103103
#define DECODER_RESET(encoding) \
104104
static Py_ssize_t encoding##_decode_reset( \
105-
MultibyteCodec_State *state, const void *config)
105+
MultibyteCodec_State *state, const MultibyteCodec *codec)
106106

107107
#define NEXT_IN(i) \
108108
do { \

Modules/cjkcodecs/emu_jisx0213_2000.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@
55
# define EMULATE_JISX0213_2000_ENCODE_INVALID 1
66
#endif
77

8-
#define EMULATE_JISX0213_2000_ENCODE_BMP(assi, c) \
9-
if (config == (void *)2000 && ( \
8+
#define EMULATE_JISX0213_2000_ENCODE_BMP(config, assi, c) \
9+
if ((config) == (void *)2000 && ( \
1010
(c) == 0x9B1C || (c) == 0x4FF1 || \
1111
(c) == 0x525D || (c) == 0x541E || \
1212
(c) == 0x5653 || (c) == 0x59F8 || \
1313
(c) == 0x5C5B || (c) == 0x5E77 || \
1414
(c) == 0x7626 || (c) == 0x7E6B)) { \
1515
return EMULATE_JISX0213_2000_ENCODE_INVALID; \
1616
} \
17-
else if (config == (void *)2000 && (c) == 0x9B1D) { \
17+
else if ((config) == (void *)2000 && (c) == 0x9B1D) { \
1818
(assi) = 0x8000 | 0x7d3b; \
1919
}
2020

21-
#define EMULATE_JISX0213_2000_ENCODE_EMP(assi, c) \
22-
if (config == (void *)2000 && (c) == 0x20B9F) { \
21+
#define EMULATE_JISX0213_2000_ENCODE_EMP(config, assi, c) \
22+
if ((config) == (void *)2000 && (c) == 0x20B9F) { \
2323
return EMULATE_JISX0213_2000_ENCODE_INVALID; \
2424
}
2525

2626
#ifndef EMULATE_JISX0213_2000_DECODE_INVALID
2727
# define EMULATE_JISX0213_2000_DECODE_INVALID 2
2828
#endif
2929

30-
#define EMULATE_JISX0213_2000_DECODE_PLANE1(assi, c1, c2) \
31-
if (config == (void *)2000 && \
30+
#define EMULATE_JISX0213_2000_DECODE_PLANE1(config, assi, c1, c2) \
31+
if ((config) == (void *)2000 && \
3232
(((c1) == 0x2E && (c2) == 0x21) || \
3333
((c1) == 0x2F && (c2) == 0x7E) || \
3434
((c1) == 0x4F && (c2) == 0x54) || \
@@ -42,13 +42,13 @@
4242
return EMULATE_JISX0213_2000_DECODE_INVALID; \
4343
}
4444

45-
#define EMULATE_JISX0213_2000_DECODE_PLANE2(writer, c1, c2) \
46-
if (config == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) { \
45+
#define EMULATE_JISX0213_2000_DECODE_PLANE2(config, writer, c1, c2) \
46+
if ((config) == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) { \
4747
OUTCHAR(0x9B1D); \
4848
}
4949

50-
#define EMULATE_JISX0213_2000_DECODE_PLANE2_CHAR(assi, c1, c2) \
51-
if (config == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) { \
50+
#define EMULATE_JISX0213_2000_DECODE_PLANE2_CHAR(config, assi, c1, c2) \
51+
if ((config) == (void *)2000 && (c1) == 0x7D && (c2) == 0x3B) { \
5252
(assi) = 0x9B1D; \
5353
}
5454

Modules/cjkcodecs/multibytecodec.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ multibytecodec_encerror(const MultibyteCodec *codec,
272272
for (;;) {
273273
Py_ssize_t outleft = (Py_ssize_t)(buf->outbuf_end - buf->outbuf);
274274

275-
r = codec->encode(state, codec->config,
275+
r = codec->encode(state, codec,
276276
kind, data, &inpos, 1,
277277
&buf->outbuf, outleft, 0);
278278
if (r == MBERR_TOOSMALL) {
@@ -521,7 +521,7 @@ multibytecodec_encode(const MultibyteCodec *codec,
521521
* error callbacks can relocate the cursor anywhere on buffer*/
522522
Py_ssize_t outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf);
523523

524-
r = codec->encode(state, codec->config,
524+
r = codec->encode(state, codec,
525525
kind, data,
526526
&buf.inpos, buf.inlen,
527527
&buf.outbuf, outleft, flags);
@@ -538,7 +538,7 @@ multibytecodec_encode(const MultibyteCodec *codec,
538538
Py_ssize_t outleft;
539539

540540
outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf);
541-
r = codec->encreset(state, codec->config, &buf.outbuf,
541+
r = codec->encreset(state, codec, &buf.outbuf,
542542
outleft);
543543
if (r == 0)
544544
break;
@@ -616,7 +616,7 @@ _multibytecodec_MultibyteCodec_encode_impl(MultibyteCodecObject *self,
616616
}
617617

618618
if (self->codec->encinit != NULL &&
619-
self->codec->encinit(&state, self->codec->config) != 0)
619+
self->codec->encinit(&state, self->codec) != 0)
620620
goto errorexit;
621621
r = multibytecodec_encode(self->codec, &state,
622622
input, NULL, errorcb,
@@ -680,15 +680,15 @@ _multibytecodec_MultibyteCodec_decode_impl(MultibyteCodecObject *self,
680680
buf.inbuf_end = buf.inbuf_top + datalen;
681681

682682
if (self->codec->decinit != NULL &&
683-
self->codec->decinit(&state, self->codec->config) != 0)
683+
self->codec->decinit(&state, self->codec) != 0)
684684
goto errorexit;
685685

686686
while (buf.inbuf < buf.inbuf_end) {
687687
Py_ssize_t inleft, r;
688688

689689
inleft = (Py_ssize_t)(buf.inbuf_end - buf.inbuf);
690690

691-
r = self->codec->decode(&state, self->codec->config,
691+
r = self->codec->decode(&state, self->codec,
692692
&buf.inbuf, inleft, &buf.writer);
693693
if (r == 0)
694694
break;
@@ -888,7 +888,7 @@ decoder_feed_buffer(MultibyteStatefulDecoderContext *ctx,
888888

889889
inleft = (Py_ssize_t)(buf->inbuf_end - buf->inbuf);
890890

891-
r = ctx->codec->decode(&ctx->state, ctx->codec->config,
891+
r = ctx->codec->decode(&ctx->state, ctx->codec,
892892
&buf->inbuf, inleft, &buf->writer);
893893
if (r == 0 || r == MBERR_TOOFEW)
894894
break;
@@ -1015,7 +1015,7 @@ _multibytecodec_MultibyteIncrementalEncoder_reset_impl(MultibyteIncrementalEncod
10151015
Py_ssize_t r;
10161016
if (self->codec->encreset != NULL) {
10171017
outbuf = buffer;
1018-
r = self->codec->encreset(&self->state, self->codec->config,
1018+
r = self->codec->encreset(&self->state, self->codec,
10191019
&outbuf, sizeof(buffer));
10201020
if (r != 0)
10211021
return NULL;
@@ -1063,7 +1063,7 @@ mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
10631063
if (self->errors == NULL)
10641064
goto errorexit;
10651065
if (self->codec->encinit != NULL &&
1066-
self->codec->encinit(&self->state, self->codec->config) != 0)
1066+
self->codec->encinit(&self->state, self->codec) != 0)
10671067
goto errorexit;
10681068

10691069
Py_DECREF(codec);
@@ -1292,7 +1292,7 @@ _multibytecodec_MultibyteIncrementalDecoder_reset_impl(MultibyteIncrementalDecod
12921292
/*[clinic end generated code: output=da423b1782c23ed1 input=3b63b3be85b2fb45]*/
12931293
{
12941294
if (self->codec->decreset != NULL &&
1295-
self->codec->decreset(&self->state, self->codec->config) != 0)
1295+
self->codec->decreset(&self->state, self->codec) != 0)
12961296
return NULL;
12971297
self->pendingsize = 0;
12981298

@@ -1338,7 +1338,7 @@ mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
13381338
if (self->errors == NULL)
13391339
goto errorexit;
13401340
if (self->codec->decinit != NULL &&
1341-
self->codec->decinit(&self->state, self->codec->config) != 0)
1341+
self->codec->decinit(&self->state, self->codec) != 0)
13421342
goto errorexit;
13431343

13441344
Py_DECREF(codec);
@@ -1600,7 +1600,7 @@ _multibytecodec_MultibyteStreamReader_reset_impl(MultibyteStreamReaderObject *se
16001600
/*[clinic end generated code: output=138490370a680abc input=5d4140db84b5e1e2]*/
16011601
{
16021602
if (self->codec->decreset != NULL &&
1603-
self->codec->decreset(&self->state, self->codec->config) != 0)
1603+
self->codec->decreset(&self->state, self->codec) != 0)
16041604
return NULL;
16051605
self->pendingsize = 0;
16061606

@@ -1654,7 +1654,7 @@ mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
16541654
if (self->errors == NULL)
16551655
goto errorexit;
16561656
if (self->codec->decinit != NULL &&
1657-
self->codec->decinit(&self->state, self->codec->config) != 0)
1657+
self->codec->decinit(&self->state, self->codec) != 0)
16581658
goto errorexit;
16591659

16601660
Py_DECREF(codec);
@@ -1877,7 +1877,7 @@ mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18771877
if (self->errors == NULL)
18781878
goto errorexit;
18791879
if (self->codec->encinit != NULL &&
1880-
self->codec->encinit(&self->state, self->codec->config) != 0)
1880+
self->codec->encinit(&self->state, self->codec) != 0)
18811881
goto errorexit;
18821882

18831883
Py_DECREF(codec);
@@ -1971,7 +1971,7 @@ _multibytecodec___create_codec(PyObject *module, PyObject *arg)
19711971

19721972
codec_capsule *data = PyCapsule_GetPointer(arg, CODEC_CAPSULE);
19731973
const MultibyteCodec *codec = data->codec;
1974-
if (codec->codecinit != NULL && codec->codecinit(codec->config) != 0)
1974+
if (codec->codecinit != NULL && codec->codecinit(codec) != 0)
19751975
return NULL;
19761976

19771977
module_state *state = get_module_state(module);

0 commit comments

Comments
 (0)