blob: b67f3482faf8dac7d3ea8986a586ea1cc88d47cd [file] [log] [blame]
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +00001/*
2 * cjkcodecs.h: common header for cjkcodecs
3 *
4 * Written by Hye-Shik Chang <perky@FreeBSD.org>
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +00005 */
6
7#ifndef _CJKCODECS_H_
8#define _CJKCODECS_H_
9
Hye-Shik Chang4b96c132006-03-04 16:08:19 +000010#define PY_SSIZE_T_CLEAN
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000011#include "Python.h"
12#include "multibytecodec.h"
13
14
Serhiy Storchakad3faf432015-01-18 11:28:37 +020015/* a unicode "undefined" code point */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016#define UNIINV 0xFFFE
Hye-Shik Chang331649a2005-10-06 15:51:59 +000017
Serhiy Storchakad3faf432015-01-18 11:28:37 +020018/* internal-use DBCS code points which aren't used by any charsets */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019#define NOCHAR 0xFFFF
20#define MULTIC 0xFFFE
21#define DBCINV 0xFFFD
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000022
23/* shorter macros to save source size of mapping tables */
24#define U UNIINV
25#define N NOCHAR
26#define M MULTIC
27#define D DBCINV
28
29struct dbcs_index {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 const ucs2_t *map;
31 unsigned char bottom, top;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000032};
33typedef struct dbcs_index decode_map;
34
35struct widedbcs_index {
Victor Stinnera0dd0212013-04-11 22:09:04 +020036 const Py_UCS4 *map;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 unsigned char bottom, top;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000038};
39typedef struct widedbcs_index widedecode_map;
40
41struct unim_index {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 const DBCHAR *map;
43 unsigned char bottom, top;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000044};
45typedef struct unim_index encode_map;
46
47struct unim_index_bytebased {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 const unsigned char *map;
49 unsigned char bottom, top;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000050};
51
52struct dbcs_map {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 const char *charset;
54 const struct unim_index *encmap;
55 const struct dbcs_index *decmap;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000056};
57
58struct pair_encodemap {
Victor Stinnera0dd0212013-04-11 22:09:04 +020059 Py_UCS4 uniseq;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 DBCHAR code;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000061};
62
Hye-Shik Chang64a9e382004-07-18 15:02:45 +000063static const MultibyteCodec *codec_list;
64static const struct dbcs_map *mapping_list;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066#define CODEC_INIT(encoding) \
67 static int encoding##_codec_init(const void *config)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069#define ENCODER_INIT(encoding) \
70 static int encoding##_encode_init( \
71 MultibyteCodec_State *state, const void *config)
72#define ENCODER(encoding) \
73 static Py_ssize_t encoding##_encode( \
74 MultibyteCodec_State *state, const void *config, \
Victor Stinnerd9491262013-04-14 02:06:32 +020075 int kind, void *data, \
76 Py_ssize_t *inpos, Py_ssize_t inlen, \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 unsigned char **outbuf, Py_ssize_t outleft, int flags)
78#define ENCODER_RESET(encoding) \
79 static Py_ssize_t encoding##_encode_reset( \
80 MultibyteCodec_State *state, const void *config, \
81 unsigned char **outbuf, Py_ssize_t outleft)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083#define DECODER_INIT(encoding) \
84 static int encoding##_decode_init( \
85 MultibyteCodec_State *state, const void *config)
86#define DECODER(encoding) \
87 static Py_ssize_t encoding##_decode( \
88 MultibyteCodec_State *state, const void *config, \
89 const unsigned char **inbuf, Py_ssize_t inleft, \
Victor Stinnera0dd0212013-04-11 22:09:04 +020090 _PyUnicodeWriter *writer)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091#define DECODER_RESET(encoding) \
92 static Py_ssize_t encoding##_decode_reset( \
93 MultibyteCodec_State *state, const void *config)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +000094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095#define NEXT_IN(i) \
Victor Stinnera0dd0212013-04-11 22:09:04 +020096 do { \
97 (*inbuf) += (i); \
98 (inleft) -= (i); \
99 } while (0)
Victor Stinnerd9491262013-04-14 02:06:32 +0200100#define NEXT_INCHAR(i) \
101 do { \
102 (*inpos) += (i); \
103 } while (0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104#define NEXT_OUT(o) \
Victor Stinnerd9491262013-04-14 02:06:32 +0200105 do { \
106 (*outbuf) += (o); \
107 (outleft) -= (o); \
108 } while (0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109#define NEXT(i, o) \
Victor Stinnerd9491262013-04-14 02:06:32 +0200110 do { \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100111 NEXT_INCHAR(i); \
112 NEXT_OUT(o); \
Victor Stinnerd9491262013-04-14 02:06:32 +0200113 } while (0)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115#define REQUIRE_INBUF(n) \
Victor Stinner28c63f72013-10-29 00:59:44 +0100116 do { \
117 if (inleft < (n)) \
118 return MBERR_TOOFEW; \
119 } while (0)
120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121#define REQUIRE_OUTBUF(n) \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100122 do { \
123 if (outleft < (n)) \
124 return MBERR_TOOSMALL; \
125 } while (0)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000126
Victor Stinnerd9491262013-04-14 02:06:32 +0200127#define INBYTE1 ((*inbuf)[0])
128#define INBYTE2 ((*inbuf)[1])
129#define INBYTE3 ((*inbuf)[2])
130#define INBYTE4 ((*inbuf)[3])
131
Victor Stinner14c9fea2013-10-29 00:19:27 +0100132#define INCHAR1 (PyUnicode_READ(kind, data, *inpos))
133#define INCHAR2 (PyUnicode_READ(kind, data, *inpos + 1))
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000134
Victor Stinnera0dd0212013-04-11 22:09:04 +0200135#define OUTCHAR(c) \
136 do { \
137 if (_PyUnicodeWriter_WriteChar(writer, (c)) < 0) \
Victor Stinnerd1f99422013-07-16 21:41:43 +0200138 return MBERR_EXCEPTION; \
Victor Stinnera0dd0212013-04-11 22:09:04 +0200139 } while (0)
140
141#define OUTCHAR2(c1, c2) \
142 do { \
143 Py_UCS4 _c1 = (c1); \
144 Py_UCS4 _c2 = (c2); \
145 if (_PyUnicodeWriter_Prepare(writer, 2, Py_MAX(_c1, c2)) < 0) \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100146 return MBERR_EXCEPTION; \
Victor Stinnera0dd0212013-04-11 22:09:04 +0200147 PyUnicode_WRITE(writer->kind, writer->data, writer->pos, _c1); \
148 PyUnicode_WRITE(writer->kind, writer->data, writer->pos + 1, _c2); \
149 writer->pos += 2; \
150 } while (0)
151
Alexey Izbyshev5f459792019-03-29 10:48:47 +0300152#define OUTBYTEI(c, i) \
153 do { \
154 assert((unsigned char)(c) == (c)); \
155 ((*outbuf)[i]) = (c); \
156 } while (0)
157
158#define OUTBYTE1(c) OUTBYTEI(c, 0)
159#define OUTBYTE2(c) OUTBYTEI(c, 1)
160#define OUTBYTE3(c) OUTBYTEI(c, 2)
161#define OUTBYTE4(c) OUTBYTEI(c, 3)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000162
Victor Stinnerd9491262013-04-14 02:06:32 +0200163#define WRITEBYTE1(c1) \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100164 do { \
165 REQUIRE_OUTBUF(1); \
Alexey Izbyshev5f459792019-03-29 10:48:47 +0300166 OUTBYTE1(c1); \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100167 } while (0)
Victor Stinnerd9491262013-04-14 02:06:32 +0200168#define WRITEBYTE2(c1, c2) \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100169 do { \
170 REQUIRE_OUTBUF(2); \
Alexey Izbyshev5f459792019-03-29 10:48:47 +0300171 OUTBYTE1(c1); \
172 OUTBYTE2(c2); \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100173 } while (0)
Victor Stinnerd9491262013-04-14 02:06:32 +0200174#define WRITEBYTE3(c1, c2, c3) \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100175 do { \
176 REQUIRE_OUTBUF(3); \
Alexey Izbyshev5f459792019-03-29 10:48:47 +0300177 OUTBYTE1(c1); \
178 OUTBYTE2(c2); \
179 OUTBYTE3(c3); \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100180 } while (0)
Victor Stinnerd9491262013-04-14 02:06:32 +0200181#define WRITEBYTE4(c1, c2, c3, c4) \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100182 do { \
183 REQUIRE_OUTBUF(4); \
Alexey Izbyshev5f459792019-03-29 10:48:47 +0300184 OUTBYTE1(c1); \
185 OUTBYTE2(c2); \
186 OUTBYTE3(c3); \
187 OUTBYTE4(c4); \
Victor Stinner14c9fea2013-10-29 00:19:27 +0100188 } while (0)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190#define _TRYMAP_ENC(m, assi, val) \
191 ((m)->map != NULL && (val) >= (m)->bottom && \
192 (val)<= (m)->top && ((assi) = (m)->map[(val) - \
193 (m)->bottom]) != NOCHAR)
Victor Stinnerbd97ac32013-10-28 23:54:13 +0100194#define TRYMAP_ENC(charset, assi, uni) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 _TRYMAP_ENC(&charset##_encmap[(uni) >> 8], assi, (uni) & 0xff)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000196
Victor Stinner11bdf912013-10-28 23:18:39 +0100197#define _TRYMAP_DEC(m, assi, val) \
Victor Stinnera0dd0212013-04-11 22:09:04 +0200198 ((m)->map != NULL && \
199 (val) >= (m)->bottom && \
200 (val)<= (m)->top && \
201 ((assi) = (m)->map[(val) - (m)->bottom]) != UNIINV)
Victor Stinner11bdf912013-10-28 23:18:39 +0100202#define TRYMAP_DEC(charset, assi, c1, c2) \
203 _TRYMAP_DEC(&charset##_decmap[c1], assi, c2)
Victor Stinnera0dd0212013-04-11 22:09:04 +0200204
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000205#define BEGIN_MAPPINGS_LIST static const struct dbcs_map _mapping_list[] = {
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000206#define MAPPING_ENCONLY(enc) {#enc, (void*)enc##_encmap, NULL},
207#define MAPPING_DECONLY(enc) {#enc, NULL, (void*)enc##_decmap},
208#define MAPPING_ENCDEC(enc) {#enc, (void*)enc##_encmap, (void*)enc##_decmap},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209#define END_MAPPINGS_LIST \
210 {"", NULL, NULL} }; \
211 static const struct dbcs_map *mapping_list = \
212 (const struct dbcs_map *)_mapping_list;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000213
Hye-Shik Chang64a9e382004-07-18 15:02:45 +0000214#define BEGIN_CODECS_LIST static const MultibyteCodec _codec_list[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215#define _STATEFUL_METHODS(enc) \
216 enc##_encode, \
217 enc##_encode_init, \
218 enc##_encode_reset, \
219 enc##_decode, \
220 enc##_decode_init, \
221 enc##_decode_reset,
222#define _STATELESS_METHODS(enc) \
223 enc##_encode, NULL, NULL, \
224 enc##_decode, NULL, NULL,
225#define CODEC_STATEFUL(enc) { \
226 #enc, NULL, NULL, \
227 _STATEFUL_METHODS(enc) \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000228},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229#define CODEC_STATELESS(enc) { \
230 #enc, NULL, NULL, \
231 _STATELESS_METHODS(enc) \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000232},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233#define CODEC_STATELESS_WINIT(enc) { \
234 #enc, NULL, \
235 enc##_codec_init, \
236 _STATELESS_METHODS(enc) \
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000237},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238#define END_CODECS_LIST \
239 {"", NULL,} }; \
240 static const MultibyteCodec *codec_list = \
241 (const MultibyteCodec *)_codec_list;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000242
Benjamin Petersonb173f782009-05-05 22:31:58 +0000243
244
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000245static PyObject *
246getmultibytecodec(void)
247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 static PyObject *cofunc = NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (cofunc == NULL) {
251 PyObject *mod = PyImport_ImportModuleNoBlock("_multibytecodec");
252 if (mod == NULL)
253 return NULL;
254 cofunc = PyObject_GetAttrString(mod, "__create_codec");
255 Py_DECREF(mod);
256 }
257 return cofunc;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000258}
259
260static PyObject *
261getcodec(PyObject *self, PyObject *encoding)
262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PyObject *codecobj, *r, *cofunc;
264 const MultibyteCodec *codec;
265 const char *enc;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (!PyUnicode_Check(encoding)) {
268 PyErr_SetString(PyExc_TypeError,
269 "encoding name must be a string.");
270 return NULL;
271 }
Serhiy Storchaka06515832016-11-20 09:13:07 +0200272 enc = PyUnicode_AsUTF8(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (enc == NULL)
274 return NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 cofunc = getmultibytecodec();
277 if (cofunc == NULL)
278 return NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 for (codec = codec_list; codec->encoding[0]; codec++)
281 if (strcmp(codec->encoding, enc) == 0)
282 break;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 if (codec->encoding[0] == '\0') {
285 PyErr_SetString(PyExc_LookupError,
286 "no such codec is supported.");
287 return NULL;
288 }
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 codecobj = PyCapsule_New((void *)codec, PyMultibyteCodec_CAPSULE_NAME, NULL);
291 if (codecobj == NULL)
292 return NULL;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 r = PyObject_CallFunctionObjArgs(cofunc, codecobj, NULL);
295 Py_DECREF(codecobj);
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 return r;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000298}
299
300static struct PyMethodDef __methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 {"getcodec", (PyCFunction)getcodec, METH_O, ""},
302 {NULL, NULL},
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000303};
304
305static int
306register_maps(PyObject *module)
307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 const struct dbcs_map *h;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 for (h = mapping_list; h->charset[0] != '\0'; h++) {
311 char mhname[256] = "__map_";
312 int r;
313 strcpy(mhname + sizeof("__map_") - 1, h->charset);
314 r = PyModule_AddObject(module, mhname,
315 PyCapsule_New((void *)h, PyMultibyteCodec_CAPSULE_NAME, NULL));
316 if (r == -1)
317 return -1;
318 }
319 return 0;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000320}
321
322#ifdef USING_BINARY_PAIR_SEARCH
323static DBCHAR
324find_pairencmap(ucs2_t body, ucs2_t modifier,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 const struct pair_encodemap *haystack, int haystacksize)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 int pos, min, max;
Victor Stinnera0dd0212013-04-11 22:09:04 +0200328 Py_UCS4 value = body << 16 | modifier;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 min = 0;
331 max = haystacksize;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000332
Benjamin Peterson5a093c12016-05-23 22:47:50 -0700333 for (pos = haystacksize >> 1; min != max; pos = (min + max) >> 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if (value < haystack[pos].uniseq) {
Benjamin Peterson5a093c12016-05-23 22:47:50 -0700335 if (max != pos) {
336 max = pos;
337 continue;
338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 }
340 else if (value > haystack[pos].uniseq) {
Benjamin Peterson5a093c12016-05-23 22:47:50 -0700341 if (min != pos) {
342 min = pos;
343 continue;
344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 }
Benjamin Peterson5a093c12016-05-23 22:47:50 -0700346 break;
347 }
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000348
Benjamin Peterson5a093c12016-05-23 22:47:50 -0700349 if (value == haystack[pos].uniseq) {
350 return haystack[pos].code;
351 }
352 return DBCINV;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000353}
354#endif
355
356#ifdef USING_IMPORTED_MAPS
357#define IMPORT_MAP(locale, charset, encmap, decmap) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 importmap("_codecs_" #locale, "__map_" #charset, \
359 (const void**)encmap, (const void**)decmap)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000360
361static int
362importmap(const char *modname, const char *symbol,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 const void **encmap, const void **decmap)
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 PyObject *o, *mod;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000366
Serhiy Storchakac6792272013-10-19 21:03:34 +0300367 mod = PyImport_ImportModule(modname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 if (mod == NULL)
369 return -1;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000370
Serhiy Storchaka20b39b22014-09-28 11:27:24 +0300371 o = PyObject_GetAttrString(mod, symbol);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (o == NULL)
373 goto errorexit;
374 else if (!PyCapsule_IsValid(o, PyMultibyteCodec_CAPSULE_NAME)) {
375 PyErr_SetString(PyExc_ValueError,
376 "map data must be a Capsule.");
377 goto errorexit;
378 }
379 else {
380 struct dbcs_map *map;
381 map = PyCapsule_GetPointer(o, PyMultibyteCodec_CAPSULE_NAME);
382 if (encmap != NULL)
383 *encmap = map->encmap;
384 if (decmap != NULL)
385 *decmap = map->decmap;
386 Py_DECREF(o);
387 }
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 Py_DECREF(mod);
390 return 0;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000391
392errorexit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_DECREF(mod);
394 return -1;
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000395}
396#endif
397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398#define I_AM_A_MODULE_FOR(loc) \
399 static struct PyModuleDef __module = { \
400 PyModuleDef_HEAD_INIT, \
401 "_codecs_"#loc, \
402 NULL, \
403 0, \
404 __methods, \
405 NULL, \
406 NULL, \
407 NULL, \
408 NULL \
409 }; \
Victor Stinnerf024d262015-03-17 17:48:27 +0100410 PyMODINIT_FUNC \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 PyInit__codecs_##loc(void) \
412 { \
413 PyObject *m = PyModule_Create(&__module); \
414 if (m != NULL) \
415 (void)register_maps(m); \
416 return m; \
417 }
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000418
419#endif