Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * multibytecodec.c: Common Multibyte Codec Implementation |
| 3 | * |
| 4 | * Written by Hye-Shik Chang <perky@FreeBSD.org> |
Hye-Shik Chang | f5a149a | 2004-08-19 17:49:56 +0000 | [diff] [blame] | 5 | * $CJKCodecs: multibytecodec.c,v 1.13 2004/08/19 16:57:19 perky Exp $ |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "Python.h" |
| 9 | #include "multibytecodec.h" |
| 10 | |
| 11 | |
| 12 | typedef struct { |
| 13 | const Py_UNICODE *inbuf, *inbuf_top, *inbuf_end; |
| 14 | unsigned char *outbuf, *outbuf_end; |
| 15 | PyObject *excobj, *outobj; |
| 16 | } MultibyteEncodeBuffer; |
| 17 | |
| 18 | typedef struct { |
| 19 | const unsigned char *inbuf, *inbuf_top, *inbuf_end; |
| 20 | Py_UNICODE *outbuf, *outbuf_end; |
| 21 | PyObject *excobj, *outobj; |
| 22 | } MultibyteDecodeBuffer; |
| 23 | |
| 24 | PyDoc_STRVAR(MultibyteCodec_Encode__doc__, |
| 25 | "I.encode(unicode[, errors]) -> (string, length consumed)\n\ |
| 26 | \n\ |
| 27 | Return an encoded string version of `unicode'. errors may be given to\n\ |
| 28 | set a different error handling scheme. Default is 'strict' meaning that\n\ |
| 29 | encoding errors raise a UnicodeEncodeError. Other possible values are\n\ |
| 30 | 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name\n\ |
| 31 | registered with codecs.register_error that can handle UnicodeEncodeErrors."); |
| 32 | |
| 33 | PyDoc_STRVAR(MultibyteCodec_Decode__doc__, |
| 34 | "I.decode(string[, errors]) -> (unicodeobject, length consumed)\n\ |
| 35 | \n\ |
| 36 | Decodes `string' using I, an MultibyteCodec instance. errors may be given\n\ |
| 37 | to set a different error handling scheme. Default is 'strict' meaning\n\ |
| 38 | that encoding errors raise a UnicodeDecodeError. Other possible values\n\ |
| 39 | are 'ignore' and 'replace' as well as any other name registerd with\n\ |
| 40 | codecs.register_error that is able to handle UnicodeDecodeErrors."); |
| 41 | |
| 42 | PyDoc_STRVAR(MultibyteCodec_StreamReader__doc__, |
| 43 | "I.StreamReader(stream[, errors]) -> StreamReader instance"); |
| 44 | |
| 45 | PyDoc_STRVAR(MultibyteCodec_StreamWriter__doc__, |
| 46 | "I.StreamWriter(stream[, errors]) -> StreamWriter instance"); |
| 47 | |
| 48 | static char *codeckwarglist[] = {"input", "errors", NULL}; |
| 49 | static char *streamkwarglist[] = {"stream", "errors", NULL}; |
| 50 | |
| 51 | static PyObject *multibytecodec_encode(MultibyteCodec *, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 52 | MultibyteCodec_State *, const Py_UNICODE **, size_t, |
| 53 | PyObject *, int); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 54 | static PyObject *mbstreamreader_create(MultibyteCodec *, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 55 | PyObject *, const char *); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 56 | static PyObject *mbstreamwriter_create(MultibyteCodec *, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 57 | PyObject *, const char *); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 58 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 59 | #define MBENC_RESET MBENC_MAX<<1 /* reset after an encoding session */ |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 60 | |
| 61 | static PyObject * |
| 62 | make_tuple(PyObject *unicode, int len) |
| 63 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 64 | PyObject *v, *w; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 65 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 66 | if (unicode == NULL) |
| 67 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 68 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 69 | v = PyTuple_New(2); |
| 70 | if (v == NULL) { |
| 71 | Py_DECREF(unicode); |
| 72 | return NULL; |
| 73 | } |
| 74 | PyTuple_SET_ITEM(v, 0, unicode); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 75 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 76 | w = PyInt_FromLong(len); |
| 77 | if (w == NULL) { |
| 78 | Py_DECREF(v); |
| 79 | return NULL; |
| 80 | } |
| 81 | PyTuple_SET_ITEM(v, 1, w); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 82 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 83 | return v; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static PyObject * |
| 87 | get_errorcallback(const char *errors) |
| 88 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 89 | if (errors == NULL || strcmp(errors, "strict") == 0) |
| 90 | return ERROR_STRICT; |
| 91 | else if (strcmp(errors, "ignore") == 0) |
| 92 | return ERROR_IGNORE; |
| 93 | else if (strcmp(errors, "replace") == 0) |
| 94 | return ERROR_REPLACE; |
| 95 | else { |
| 96 | return PyCodec_LookupError(errors); |
| 97 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static int |
| 101 | expand_encodebuffer(MultibyteEncodeBuffer *buf, int esize) |
| 102 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 103 | int orgpos, orgsize; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 104 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 105 | orgpos = (int)((char*)buf->outbuf - PyString_AS_STRING(buf->outobj)); |
| 106 | orgsize = PyString_GET_SIZE(buf->outobj); |
| 107 | if (_PyString_Resize(&buf->outobj, orgsize + ( |
| 108 | esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize)) == -1) |
| 109 | return -1; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 110 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 111 | buf->outbuf = (unsigned char *)PyString_AS_STRING(buf->outobj) +orgpos; |
| 112 | buf->outbuf_end = (unsigned char *)PyString_AS_STRING(buf->outobj) |
| 113 | + PyString_GET_SIZE(buf->outobj); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 114 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 115 | return 0; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 116 | } |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 117 | #define REQUIRE_ENCODEBUFFER(buf, s) { \ |
| 118 | if ((s) < 1 || (buf)->outbuf + (s) > (buf)->outbuf_end) \ |
| 119 | if (expand_encodebuffer(buf, s) == -1) \ |
| 120 | goto errorexit; \ |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int |
| 124 | expand_decodebuffer(MultibyteDecodeBuffer *buf, int esize) |
| 125 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 126 | int orgpos, orgsize; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 127 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 128 | orgpos = (int)(buf->outbuf - PyUnicode_AS_UNICODE(buf->outobj)); |
| 129 | orgsize = PyUnicode_GET_SIZE(buf->outobj); |
| 130 | if (PyUnicode_Resize(&buf->outobj, orgsize + ( |
| 131 | esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize)) == -1) |
| 132 | return -1; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 133 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 134 | buf->outbuf = PyUnicode_AS_UNICODE(buf->outobj) + orgpos; |
| 135 | buf->outbuf_end = PyUnicode_AS_UNICODE(buf->outobj) |
| 136 | + PyUnicode_GET_SIZE(buf->outobj); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 137 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 138 | return 0; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 139 | } |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 140 | #define REQUIRE_DECODEBUFFER(buf, s) { \ |
| 141 | if ((s) < 1 || (buf)->outbuf + (s) > (buf)->outbuf_end) \ |
| 142 | if (expand_decodebuffer(buf, s) == -1) \ |
| 143 | goto errorexit; \ |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int |
| 147 | multibytecodec_encerror(MultibyteCodec *codec, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 148 | MultibyteCodec_State *state, |
| 149 | MultibyteEncodeBuffer *buf, |
| 150 | PyObject *errors, int e) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 151 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 152 | PyObject *retobj = NULL, *retstr = NULL, *argsobj, *tobj; |
| 153 | int retstrsize, newpos; |
| 154 | const char *reason; |
| 155 | size_t esize; |
| 156 | int start, end; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 157 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 158 | if (e > 0) { |
| 159 | reason = "illegal multibyte sequence"; |
| 160 | esize = e; |
| 161 | } |
| 162 | else { |
| 163 | switch (e) { |
| 164 | case MBERR_TOOSMALL: |
| 165 | REQUIRE_ENCODEBUFFER(buf, -1); |
| 166 | return 0; /* retry it */ |
| 167 | case MBERR_TOOFEW: |
| 168 | reason = "incomplete multibyte sequence"; |
| 169 | esize = (size_t)(buf->inbuf_end - buf->inbuf); |
| 170 | break; |
| 171 | case MBERR_INTERNAL: |
| 172 | PyErr_SetString(PyExc_RuntimeError, |
| 173 | "internal codec error"); |
| 174 | return -1; |
| 175 | default: |
| 176 | PyErr_SetString(PyExc_RuntimeError, |
| 177 | "unknown runtime error"); |
| 178 | return -1; |
| 179 | } |
| 180 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 181 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 182 | if (errors == ERROR_REPLACE) { |
| 183 | const Py_UNICODE replchar = '?', *inbuf = &replchar; |
| 184 | int r; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 185 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 186 | for (;;) { |
| 187 | size_t outleft; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 188 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 189 | outleft = (size_t)(buf->outbuf_end - buf->outbuf); |
| 190 | r = codec->encode(state, codec->config, &inbuf, 1, |
| 191 | &buf->outbuf, outleft, 0); |
| 192 | if (r == MBERR_TOOSMALL) { |
| 193 | REQUIRE_ENCODEBUFFER(buf, -1); |
| 194 | continue; |
| 195 | } |
| 196 | else |
| 197 | break; |
| 198 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 199 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 200 | if (r != 0) { |
| 201 | REQUIRE_ENCODEBUFFER(buf, 1); |
| 202 | *buf->outbuf++ = '?'; |
| 203 | } |
| 204 | } |
| 205 | if (errors == ERROR_IGNORE || errors == ERROR_REPLACE) { |
| 206 | buf->inbuf += esize; |
| 207 | return 0; |
| 208 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 209 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 210 | start = (int)(buf->inbuf - buf->inbuf_top); |
| 211 | end = start + esize; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 212 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 213 | /* use cached exception object if available */ |
| 214 | if (buf->excobj == NULL) { |
| 215 | buf->excobj = PyUnicodeEncodeError_Create(codec->encoding, |
| 216 | buf->inbuf_top, |
| 217 | (int)(buf->inbuf_end - buf->inbuf_top), |
| 218 | start, end, reason); |
| 219 | if (buf->excobj == NULL) |
| 220 | goto errorexit; |
| 221 | } |
| 222 | else |
| 223 | if (PyUnicodeEncodeError_SetStart(buf->excobj, start) != 0 || |
| 224 | PyUnicodeEncodeError_SetEnd(buf->excobj, end) != 0 || |
| 225 | PyUnicodeEncodeError_SetReason(buf->excobj, reason) != 0) |
| 226 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 227 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 228 | if (errors == ERROR_STRICT) { |
| 229 | PyCodec_StrictErrors(buf->excobj); |
| 230 | goto errorexit; |
| 231 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 232 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 233 | argsobj = PyTuple_New(1); |
| 234 | if (argsobj == NULL) |
| 235 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 236 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 237 | PyTuple_SET_ITEM(argsobj, 0, buf->excobj); |
| 238 | Py_INCREF(buf->excobj); |
| 239 | retobj = PyObject_CallObject(errors, argsobj); |
| 240 | Py_DECREF(argsobj); |
| 241 | if (retobj == NULL) |
| 242 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 243 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 244 | if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 || |
| 245 | !PyUnicode_Check((tobj = PyTuple_GET_ITEM(retobj, 0))) || |
| 246 | !PyInt_Check(PyTuple_GET_ITEM(retobj, 1))) { |
| 247 | PyErr_SetString(PyExc_ValueError, |
| 248 | "encoding error handler must return " |
| 249 | "(unicode, int) tuple"); |
| 250 | goto errorexit; |
| 251 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 252 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 253 | { |
| 254 | const Py_UNICODE *uraw = PyUnicode_AS_UNICODE(tobj); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 255 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 256 | retstr = multibytecodec_encode(codec, state, &uraw, |
| 257 | PyUnicode_GET_SIZE(tobj), ERROR_STRICT, |
| 258 | MBENC_FLUSH); |
| 259 | if (retstr == NULL) |
| 260 | goto errorexit; |
| 261 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 262 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 263 | retstrsize = PyString_GET_SIZE(retstr); |
| 264 | REQUIRE_ENCODEBUFFER(buf, retstrsize); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 265 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 266 | memcpy(buf->outbuf, PyString_AS_STRING(retstr), retstrsize); |
| 267 | buf->outbuf += retstrsize; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 268 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 269 | newpos = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(retobj, 1)); |
| 270 | if (newpos < 0) |
| 271 | newpos += (int)(buf->inbuf_end - buf->inbuf_top); |
| 272 | if (newpos < 0 || buf->inbuf_top + newpos > buf->inbuf_end) { |
| 273 | PyErr_Format(PyExc_IndexError, |
| 274 | "position %d from error handler out of bounds", |
| 275 | newpos); |
| 276 | goto errorexit; |
| 277 | } |
| 278 | buf->inbuf = buf->inbuf_top + newpos; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 279 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 280 | Py_DECREF(retobj); |
| 281 | Py_DECREF(retstr); |
| 282 | return 0; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 283 | |
| 284 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 285 | Py_XDECREF(retobj); |
| 286 | Py_XDECREF(retstr); |
| 287 | return -1; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int |
| 291 | multibytecodec_decerror(MultibyteCodec *codec, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 292 | MultibyteCodec_State *state, |
| 293 | MultibyteDecodeBuffer *buf, |
| 294 | PyObject *errors, int e) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 295 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 296 | PyObject *argsobj, *retobj = NULL, *retuni = NULL; |
| 297 | int retunisize, newpos; |
| 298 | const char *reason; |
| 299 | size_t esize; |
| 300 | int start, end; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 301 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 302 | if (e > 0) { |
| 303 | reason = "illegal multibyte sequence"; |
| 304 | esize = e; |
| 305 | } |
| 306 | else { |
| 307 | switch (e) { |
| 308 | case MBERR_TOOSMALL: |
| 309 | REQUIRE_DECODEBUFFER(buf, -1); |
| 310 | return 0; /* retry it */ |
| 311 | case MBERR_TOOFEW: |
| 312 | reason = "incomplete multibyte sequence"; |
| 313 | esize = (size_t)(buf->inbuf_end - buf->inbuf); |
| 314 | break; |
| 315 | case MBERR_INTERNAL: |
| 316 | PyErr_SetString(PyExc_RuntimeError, |
| 317 | "internal codec error"); |
| 318 | return -1; |
| 319 | default: |
| 320 | PyErr_SetString(PyExc_RuntimeError, |
| 321 | "unknown runtime error"); |
| 322 | return -1; |
| 323 | } |
| 324 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 325 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 326 | if (errors == ERROR_REPLACE) { |
| 327 | REQUIRE_DECODEBUFFER(buf, 1); |
| 328 | *buf->outbuf++ = Py_UNICODE_REPLACEMENT_CHARACTER; |
| 329 | } |
| 330 | if (errors == ERROR_IGNORE || errors == ERROR_REPLACE) { |
| 331 | buf->inbuf += esize; |
| 332 | return 0; |
| 333 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 334 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 335 | start = (int)(buf->inbuf - buf->inbuf_top); |
| 336 | end = start + esize; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 337 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 338 | /* use cached exception object if available */ |
| 339 | if (buf->excobj == NULL) { |
| 340 | buf->excobj = PyUnicodeDecodeError_Create(codec->encoding, |
Hye-Shik Chang | f5a149a | 2004-08-19 17:49:56 +0000 | [diff] [blame] | 341 | (const char *)buf->inbuf_top, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 342 | (int)(buf->inbuf_end - buf->inbuf_top), |
| 343 | start, end, reason); |
| 344 | if (buf->excobj == NULL) |
| 345 | goto errorexit; |
| 346 | } |
| 347 | else |
| 348 | if (PyUnicodeDecodeError_SetStart(buf->excobj, start) || |
| 349 | PyUnicodeDecodeError_SetEnd(buf->excobj, end) || |
| 350 | PyUnicodeDecodeError_SetReason(buf->excobj, reason)) |
| 351 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 352 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 353 | if (errors == ERROR_STRICT) { |
| 354 | PyCodec_StrictErrors(buf->excobj); |
| 355 | goto errorexit; |
| 356 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 357 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 358 | argsobj = PyTuple_New(1); |
| 359 | if (argsobj == NULL) |
| 360 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 361 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 362 | PyTuple_SET_ITEM(argsobj, 0, buf->excobj); |
| 363 | Py_INCREF(buf->excobj); |
| 364 | retobj = PyObject_CallObject(errors, argsobj); |
| 365 | Py_DECREF(argsobj); |
| 366 | if (retobj == NULL) |
| 367 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 368 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 369 | if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 || |
| 370 | !PyUnicode_Check((retuni = PyTuple_GET_ITEM(retobj, 0))) || |
| 371 | !PyInt_Check(PyTuple_GET_ITEM(retobj, 1))) { |
| 372 | PyErr_SetString(PyExc_ValueError, |
| 373 | "decoding error handler must return " |
| 374 | "(unicode, int) tuple"); |
| 375 | goto errorexit; |
| 376 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 377 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 378 | retunisize = PyUnicode_GET_SIZE(retuni); |
| 379 | if (retunisize > 0) { |
| 380 | REQUIRE_DECODEBUFFER(buf, retunisize); |
| 381 | memcpy((char *)buf->outbuf, PyUnicode_AS_DATA(retuni), |
| 382 | retunisize * Py_UNICODE_SIZE); |
| 383 | buf->outbuf += retunisize; |
| 384 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 385 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 386 | newpos = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(retobj, 1)); |
| 387 | if (newpos < 0) |
| 388 | newpos += (int)(buf->inbuf_end - buf->inbuf_top); |
| 389 | if (newpos < 0 || buf->inbuf_top + newpos > buf->inbuf_end) { |
| 390 | PyErr_Format(PyExc_IndexError, |
| 391 | "position %d from error handler out of bounds", |
| 392 | newpos); |
| 393 | goto errorexit; |
| 394 | } |
| 395 | buf->inbuf = buf->inbuf_top + newpos; |
| 396 | Py_DECREF(retobj); |
| 397 | return 0; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 398 | |
| 399 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 400 | Py_XDECREF(retobj); |
| 401 | return -1; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static PyObject * |
| 405 | multibytecodec_encode(MultibyteCodec *codec, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 406 | MultibyteCodec_State *state, |
| 407 | const Py_UNICODE **data, size_t datalen, |
| 408 | PyObject *errors, int flags) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 409 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 410 | MultibyteEncodeBuffer buf; |
| 411 | int finalsize, r = 0; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 412 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 413 | if (datalen == 0) |
| 414 | return PyString_FromString(""); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 415 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 416 | buf.excobj = NULL; |
| 417 | buf.inbuf = buf.inbuf_top = *data; |
| 418 | buf.inbuf_end = buf.inbuf_top + datalen; |
| 419 | buf.outobj = PyString_FromStringAndSize(NULL, datalen * 2 + 16); |
| 420 | if (buf.outobj == NULL) |
| 421 | goto errorexit; |
| 422 | buf.outbuf = (unsigned char *)PyString_AS_STRING(buf.outobj); |
| 423 | buf.outbuf_end = buf.outbuf + PyString_GET_SIZE(buf.outobj); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 424 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 425 | while (buf.inbuf < buf.inbuf_end) { |
| 426 | size_t inleft, outleft; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 427 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 428 | /* we don't reuse inleft and outleft here. |
| 429 | * error callbacks can relocate the cursor anywhere on buffer*/ |
| 430 | inleft = (size_t)(buf.inbuf_end - buf.inbuf); |
| 431 | outleft = (size_t)(buf.outbuf_end - buf.outbuf); |
| 432 | r = codec->encode(state, codec->config, &buf.inbuf, inleft, |
| 433 | &buf.outbuf, outleft, flags); |
| 434 | *data = buf.inbuf; |
| 435 | if ((r == 0) || (r == MBERR_TOOFEW && !(flags & MBENC_FLUSH))) |
| 436 | break; |
| 437 | else if (multibytecodec_encerror(codec, state, &buf, errors,r)) |
| 438 | goto errorexit; |
| 439 | else if (r == MBERR_TOOFEW) |
| 440 | break; |
| 441 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 442 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 443 | if (codec->encreset != NULL) |
| 444 | for (;;) { |
| 445 | size_t outleft; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 446 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 447 | outleft = (size_t)(buf.outbuf_end - buf.outbuf); |
| 448 | r = codec->encreset(state, codec->config, &buf.outbuf, |
| 449 | outleft); |
| 450 | if (r == 0) |
| 451 | break; |
| 452 | else if (multibytecodec_encerror(codec, state, |
| 453 | &buf, errors, r)) |
| 454 | goto errorexit; |
| 455 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 456 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 457 | finalsize = (int)((char*)buf.outbuf - PyString_AS_STRING(buf.outobj)); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 458 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 459 | if (finalsize != PyString_GET_SIZE(buf.outobj)) |
| 460 | if (_PyString_Resize(&buf.outobj, finalsize) == -1) |
| 461 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 462 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 463 | Py_XDECREF(buf.excobj); |
| 464 | return buf.outobj; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 465 | |
| 466 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 467 | Py_XDECREF(buf.excobj); |
| 468 | Py_XDECREF(buf.outobj); |
| 469 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static PyObject * |
| 473 | MultibyteCodec_Encode(MultibyteCodecObject *self, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 474 | PyObject *args, PyObject *kwargs) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 475 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 476 | MultibyteCodec_State state; |
| 477 | Py_UNICODE *data; |
| 478 | PyObject *errorcb, *r, *arg, *ucvt; |
| 479 | const char *errors = NULL; |
| 480 | int datalen; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 481 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 482 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|z:encode", |
| 483 | codeckwarglist, &arg, &errors)) |
| 484 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 485 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 486 | if (PyUnicode_Check(arg)) |
| 487 | ucvt = NULL; |
| 488 | else { |
| 489 | arg = ucvt = PyObject_Unicode(arg); |
| 490 | if (arg == NULL) |
| 491 | return NULL; |
| 492 | else if (!PyUnicode_Check(arg)) { |
| 493 | PyErr_SetString(PyExc_TypeError, |
| 494 | "couldn't convert the object to unicode."); |
| 495 | Py_DECREF(ucvt); |
| 496 | return NULL; |
| 497 | } |
| 498 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 499 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 500 | data = PyUnicode_AS_UNICODE(arg); |
| 501 | datalen = PyUnicode_GET_SIZE(arg); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 502 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 503 | errorcb = get_errorcallback(errors); |
| 504 | if (errorcb == NULL) { |
| 505 | Py_XDECREF(ucvt); |
| 506 | return NULL; |
| 507 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 508 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 509 | if (self->codec->encinit != NULL && |
| 510 | self->codec->encinit(&state, self->codec->config) != 0) |
| 511 | goto errorexit; |
| 512 | r = multibytecodec_encode(self->codec, &state, |
| 513 | (const Py_UNICODE **)&data, datalen, errorcb, |
| 514 | MBENC_FLUSH | MBENC_RESET); |
| 515 | if (r == NULL) |
| 516 | goto errorexit; |
| 517 | |
| 518 | if (errorcb > ERROR_MAX) { |
| 519 | Py_DECREF(errorcb); |
| 520 | } |
| 521 | Py_XDECREF(ucvt); |
| 522 | return make_tuple(r, datalen); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 523 | |
| 524 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 525 | if (errorcb > ERROR_MAX) { |
| 526 | Py_DECREF(errorcb); |
| 527 | } |
| 528 | Py_XDECREF(ucvt); |
| 529 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static PyObject * |
| 533 | MultibyteCodec_Decode(MultibyteCodecObject *self, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 534 | PyObject *args, PyObject *kwargs) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 535 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 536 | MultibyteCodec_State state; |
| 537 | MultibyteDecodeBuffer buf; |
| 538 | PyObject *errorcb; |
| 539 | const char *data, *errors = NULL; |
| 540 | int datalen, finalsize; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 541 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 542 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|z:decode", |
| 543 | codeckwarglist, &data, &datalen, &errors)) |
| 544 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 545 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 546 | errorcb = get_errorcallback(errors); |
| 547 | if (errorcb == NULL) |
| 548 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 549 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 550 | if (datalen == 0) { |
| 551 | if (errorcb > ERROR_MAX) { |
| 552 | Py_DECREF(errorcb); |
| 553 | } |
| 554 | return make_tuple(PyUnicode_FromUnicode(NULL, 0), 0); |
| 555 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 556 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 557 | buf.outobj = buf.excobj = NULL; |
| 558 | buf.inbuf = buf.inbuf_top = (unsigned char *)data; |
| 559 | buf.inbuf_end = buf.inbuf_top + datalen; |
| 560 | buf.outobj = PyUnicode_FromUnicode(NULL, datalen); |
| 561 | if (buf.outobj == NULL) |
| 562 | goto errorexit; |
| 563 | buf.outbuf = PyUnicode_AS_UNICODE(buf.outobj); |
| 564 | buf.outbuf_end = buf.outbuf + PyUnicode_GET_SIZE(buf.outobj); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 565 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 566 | if (self->codec->decinit != NULL && |
| 567 | self->codec->decinit(&state, self->codec->config) != 0) |
| 568 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 569 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 570 | while (buf.inbuf < buf.inbuf_end) { |
| 571 | size_t inleft, outleft; |
| 572 | int r; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 573 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 574 | inleft = (size_t)(buf.inbuf_end - buf.inbuf); |
| 575 | outleft = (size_t)(buf.outbuf_end - buf.outbuf); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 576 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 577 | r = self->codec->decode(&state, self->codec->config, |
| 578 | &buf.inbuf, inleft, &buf.outbuf, outleft); |
| 579 | if (r == 0) |
| 580 | break; |
| 581 | else if (multibytecodec_decerror(self->codec, &state, |
| 582 | &buf, errorcb, r)) |
| 583 | goto errorexit; |
| 584 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 585 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 586 | finalsize = (int)(buf.outbuf - PyUnicode_AS_UNICODE(buf.outobj)); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 587 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 588 | if (finalsize != PyUnicode_GET_SIZE(buf.outobj)) |
| 589 | if (PyUnicode_Resize(&buf.outobj, finalsize) == -1) |
| 590 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 591 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 592 | Py_XDECREF(buf.excobj); |
| 593 | if (errorcb > ERROR_MAX) { |
| 594 | Py_DECREF(errorcb); |
| 595 | } |
| 596 | return make_tuple(buf.outobj, datalen); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 597 | |
| 598 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 599 | if (errorcb > ERROR_MAX) { |
| 600 | Py_DECREF(errorcb); |
| 601 | } |
| 602 | Py_XDECREF(buf.excobj); |
| 603 | Py_XDECREF(buf.outobj); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 604 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 605 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static PyObject * |
| 609 | MultibyteCodec_StreamReader(MultibyteCodecObject *self, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 610 | PyObject *args, PyObject *kwargs) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 611 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 612 | PyObject *stream; |
| 613 | char *errors = NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 614 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 615 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|s:StreamReader", |
| 616 | streamkwarglist, &stream, &errors)) |
| 617 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 618 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 619 | return mbstreamreader_create(self->codec, stream, errors); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static PyObject * |
| 623 | MultibyteCodec_StreamWriter(MultibyteCodecObject *self, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 624 | PyObject *args, PyObject *kwargs) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 625 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 626 | PyObject *stream; |
| 627 | char *errors = NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 628 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 629 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|s:StreamWriter", |
| 630 | streamkwarglist, &stream, &errors)) |
| 631 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 632 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 633 | return mbstreamwriter_create(self->codec, stream, errors); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static struct PyMethodDef multibytecodec_methods[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 637 | {"encode", (PyCFunction)MultibyteCodec_Encode, |
| 638 | METH_VARARGS | METH_KEYWORDS, |
| 639 | MultibyteCodec_Encode__doc__}, |
| 640 | {"decode", (PyCFunction)MultibyteCodec_Decode, |
| 641 | METH_VARARGS | METH_KEYWORDS, |
| 642 | MultibyteCodec_Decode__doc__}, |
| 643 | {"StreamReader",(PyCFunction)MultibyteCodec_StreamReader, |
| 644 | METH_VARARGS | METH_KEYWORDS, |
| 645 | MultibyteCodec_StreamReader__doc__}, |
| 646 | {"StreamWriter",(PyCFunction)MultibyteCodec_StreamWriter, |
| 647 | METH_VARARGS | METH_KEYWORDS, |
| 648 | MultibyteCodec_StreamWriter__doc__}, |
| 649 | {NULL, NULL}, |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 650 | }; |
| 651 | |
| 652 | static void |
| 653 | multibytecodec_dealloc(MultibyteCodecObject *self) |
| 654 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 655 | PyObject_Del(self); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | |
| 659 | |
| 660 | static PyTypeObject MultibyteCodec_Type = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 661 | PyObject_HEAD_INIT(NULL) |
| 662 | 0, /* ob_size */ |
| 663 | "MultibyteCodec", /* tp_name */ |
| 664 | sizeof(MultibyteCodecObject), /* tp_basicsize */ |
| 665 | 0, /* tp_itemsize */ |
| 666 | /* methods */ |
| 667 | (destructor)multibytecodec_dealloc, /* tp_dealloc */ |
| 668 | 0, /* tp_print */ |
| 669 | 0, /* tp_getattr */ |
| 670 | 0, /* tp_setattr */ |
| 671 | 0, /* tp_compare */ |
| 672 | 0, /* tp_repr */ |
| 673 | 0, /* tp_as_number */ |
| 674 | 0, /* tp_as_sequence */ |
| 675 | 0, /* tp_as_mapping */ |
| 676 | 0, /* tp_hash */ |
| 677 | 0, /* tp_call */ |
| 678 | 0, /* tp_str */ |
| 679 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 680 | 0, /* tp_setattro */ |
| 681 | 0, /* tp_as_buffer */ |
| 682 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 683 | 0, /* tp_doc */ |
| 684 | 0, /* tp_traverse */ |
| 685 | 0, /* tp_clear */ |
| 686 | 0, /* tp_richcompare */ |
| 687 | 0, /* tp_weaklistoffset */ |
| 688 | 0, /* tp_iter */ |
| 689 | 0, /* tp_iterext */ |
| 690 | multibytecodec_methods, /* tp_methods */ |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | static PyObject * |
| 694 | mbstreamreader_iread(MultibyteStreamReaderObject *self, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 695 | const char *method, int sizehint) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 696 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 697 | MultibyteDecodeBuffer buf; |
| 698 | PyObject *cres; |
| 699 | int rsize, r, finalsize = 0; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 700 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 701 | if (sizehint == 0) |
| 702 | return PyUnicode_FromUnicode(NULL, 0); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 703 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 704 | buf.outobj = buf.excobj = NULL; |
| 705 | cres = NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 706 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 707 | for (;;) { |
| 708 | if (sizehint < 0) |
| 709 | cres = PyObject_CallMethod(self->stream, |
| 710 | (char *)method, NULL); |
| 711 | else |
| 712 | cres = PyObject_CallMethod(self->stream, |
| 713 | (char *)method, "i", sizehint); |
| 714 | if (cres == NULL) |
| 715 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 716 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 717 | if (!PyString_Check(cres)) { |
| 718 | PyErr_SetString(PyExc_TypeError, |
| 719 | "stream function returned a " |
| 720 | "non-string object"); |
| 721 | goto errorexit; |
| 722 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 723 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 724 | if (self->pendingsize > 0) { |
| 725 | PyObject *ctr; |
| 726 | char *ctrdata; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 727 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 728 | rsize = PyString_GET_SIZE(cres) + self->pendingsize; |
| 729 | ctr = PyString_FromStringAndSize(NULL, rsize); |
| 730 | if (ctr == NULL) |
| 731 | goto errorexit; |
| 732 | ctrdata = PyString_AS_STRING(ctr); |
| 733 | memcpy(ctrdata, self->pending, self->pendingsize); |
| 734 | memcpy(ctrdata + self->pendingsize, |
| 735 | PyString_AS_STRING(cres), |
| 736 | PyString_GET_SIZE(cres)); |
| 737 | Py_DECREF(cres); |
| 738 | cres = ctr; |
| 739 | self->pendingsize = 0; |
| 740 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 741 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 742 | rsize = PyString_GET_SIZE(cres); |
| 743 | buf.inbuf = buf.inbuf_top = |
| 744 | (unsigned char *)PyString_AS_STRING(cres); |
| 745 | buf.inbuf_end = buf.inbuf_top + rsize; |
| 746 | if (buf.outobj == NULL) { |
| 747 | buf.outobj = PyUnicode_FromUnicode(NULL, rsize); |
| 748 | if (buf.outobj == NULL) |
| 749 | goto errorexit; |
| 750 | buf.outbuf = PyUnicode_AS_UNICODE(buf.outobj); |
| 751 | buf.outbuf_end = buf.outbuf + |
| 752 | PyUnicode_GET_SIZE(buf.outobj); |
| 753 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 754 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 755 | r = 0; |
| 756 | if (rsize > 0) |
| 757 | while (buf.inbuf < buf.inbuf_end) { |
| 758 | size_t inleft, outleft; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 759 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 760 | inleft = (size_t)(buf.inbuf_end - buf.inbuf); |
| 761 | outleft = (size_t)(buf.outbuf_end -buf.outbuf); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 762 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 763 | r = self->codec->decode(&self->state, |
| 764 | self->codec->config, |
| 765 | &buf.inbuf, inleft, |
| 766 | &buf.outbuf, outleft); |
| 767 | if (r == 0 || r == MBERR_TOOFEW) |
| 768 | break; |
| 769 | else if (multibytecodec_decerror(self->codec, |
| 770 | &self->state, &buf, |
| 771 | self->errors, r)) |
| 772 | goto errorexit; |
| 773 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 774 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 775 | if (rsize == 0 || sizehint < 0) { /* end of file */ |
| 776 | if (buf.inbuf < buf.inbuf_end && |
| 777 | multibytecodec_decerror(self->codec, &self->state, |
| 778 | &buf, self->errors, MBERR_TOOFEW)) |
| 779 | goto errorexit; |
| 780 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 781 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 782 | if (buf.inbuf < buf.inbuf_end) { /* pending sequence exists */ |
| 783 | size_t npendings; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 784 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 785 | /* we can't assume that pendingsize is still 0 here. |
| 786 | * because this function can be called recursively |
| 787 | * from error callback */ |
| 788 | npendings = (size_t)(buf.inbuf_end - buf.inbuf); |
| 789 | if (npendings + self->pendingsize > MAXDECPENDING) { |
| 790 | PyErr_SetString(PyExc_RuntimeError, |
| 791 | "pending buffer overflow"); |
| 792 | goto errorexit; |
| 793 | } |
| 794 | memcpy(self->pending + self->pendingsize, buf.inbuf, |
| 795 | npendings); |
| 796 | self->pendingsize += npendings; |
| 797 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 798 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 799 | finalsize = (int)(buf.outbuf - |
| 800 | PyUnicode_AS_UNICODE(buf.outobj)); |
| 801 | Py_DECREF(cres); |
| 802 | cres = NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 803 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 804 | if (sizehint < 0 || finalsize != 0 || rsize == 0) |
| 805 | break; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 806 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 807 | sizehint = 1; /* read 1 more byte and retry */ |
| 808 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 809 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 810 | if (finalsize != PyUnicode_GET_SIZE(buf.outobj)) |
| 811 | if (PyUnicode_Resize(&buf.outobj, finalsize) == -1) |
| 812 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 813 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 814 | Py_XDECREF(cres); |
| 815 | Py_XDECREF(buf.excobj); |
| 816 | return buf.outobj; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 817 | |
| 818 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 819 | Py_XDECREF(cres); |
| 820 | Py_XDECREF(buf.excobj); |
| 821 | Py_XDECREF(buf.outobj); |
| 822 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static PyObject * |
| 826 | mbstreamreader_read(MultibyteStreamReaderObject *self, PyObject *args) |
| 827 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 828 | PyObject *sizeobj = NULL; |
| 829 | long size; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 830 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 831 | if (!PyArg_ParseTuple(args, "|O:read", &sizeobj)) |
| 832 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 833 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 834 | if (sizeobj == Py_None || sizeobj == NULL) |
| 835 | size = -1; |
| 836 | else if (PyInt_Check(sizeobj)) |
| 837 | size = PyInt_AsLong(sizeobj); |
| 838 | else { |
| 839 | PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer"); |
| 840 | return NULL; |
| 841 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 842 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 843 | return mbstreamreader_iread(self, "read", size); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static PyObject * |
| 847 | mbstreamreader_readline(MultibyteStreamReaderObject *self, PyObject *args) |
| 848 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 849 | PyObject *sizeobj = NULL; |
| 850 | long size; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 851 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 852 | if (!PyArg_ParseTuple(args, "|O:readline", &sizeobj)) |
| 853 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 854 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 855 | if (sizeobj == Py_None || sizeobj == NULL) |
| 856 | size = -1; |
| 857 | else if (PyInt_Check(sizeobj)) |
| 858 | size = PyInt_AsLong(sizeobj); |
| 859 | else { |
| 860 | PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer"); |
| 861 | return NULL; |
| 862 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 863 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 864 | return mbstreamreader_iread(self, "readline", size); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | static PyObject * |
| 868 | mbstreamreader_readlines(MultibyteStreamReaderObject *self, PyObject *args) |
| 869 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 870 | PyObject *sizehintobj = NULL, *r, *sr; |
| 871 | long sizehint; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 872 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 873 | if (!PyArg_ParseTuple(args, "|O:readlines", &sizehintobj)) |
| 874 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 875 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 876 | if (sizehintobj == Py_None || sizehintobj == NULL) |
| 877 | sizehint = -1; |
| 878 | else if (PyInt_Check(sizehintobj)) |
| 879 | sizehint = PyInt_AsLong(sizehintobj); |
| 880 | else { |
| 881 | PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer"); |
| 882 | return NULL; |
| 883 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 884 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 885 | r = mbstreamreader_iread(self, "read", sizehint); |
| 886 | if (r == NULL) |
| 887 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 888 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 889 | sr = PyUnicode_Splitlines(r, 1); |
| 890 | Py_DECREF(r); |
| 891 | return sr; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | static PyObject * |
| 895 | mbstreamreader_reset(MultibyteStreamReaderObject *self) |
| 896 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 897 | if (self->codec->decreset != NULL && |
| 898 | self->codec->decreset(&self->state, self->codec->config) != 0) |
| 899 | return NULL; |
| 900 | self->pendingsize = 0; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 901 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 902 | Py_INCREF(Py_None); |
| 903 | return Py_None; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static struct PyMethodDef mbstreamreader_methods[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 907 | {"read", (PyCFunction)mbstreamreader_read, |
| 908 | METH_VARARGS, NULL}, |
| 909 | {"readline", (PyCFunction)mbstreamreader_readline, |
| 910 | METH_VARARGS, NULL}, |
| 911 | {"readlines", (PyCFunction)mbstreamreader_readlines, |
| 912 | METH_VARARGS, NULL}, |
| 913 | {"reset", (PyCFunction)mbstreamreader_reset, |
| 914 | METH_NOARGS, NULL}, |
| 915 | {NULL, NULL}, |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 916 | }; |
| 917 | |
| 918 | static void |
| 919 | mbstreamreader_dealloc(MultibyteStreamReaderObject *self) |
| 920 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 921 | if (self->errors > ERROR_MAX) { |
| 922 | Py_DECREF(self->errors); |
| 923 | } |
| 924 | Py_DECREF(self->stream); |
| 925 | PyObject_Del(self); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | |
| 929 | |
| 930 | static PyTypeObject MultibyteStreamReader_Type = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 931 | PyObject_HEAD_INIT(NULL) |
| 932 | 0, /* ob_size */ |
| 933 | "MultibyteStreamReader", /* tp_name */ |
| 934 | sizeof(MultibyteStreamReaderObject), /* tp_basicsize */ |
| 935 | 0, /* tp_itemsize */ |
| 936 | /* methods */ |
| 937 | (destructor)mbstreamreader_dealloc, /* tp_dealloc */ |
| 938 | 0, /* tp_print */ |
| 939 | 0, /* tp_getattr */ |
| 940 | 0, /* tp_setattr */ |
| 941 | 0, /* tp_compare */ |
| 942 | 0, /* tp_repr */ |
| 943 | 0, /* tp_as_number */ |
| 944 | 0, /* tp_as_sequence */ |
| 945 | 0, /* tp_as_mapping */ |
| 946 | 0, /* tp_hash */ |
| 947 | 0, /* tp_call */ |
| 948 | 0, /* tp_str */ |
| 949 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 950 | 0, /* tp_setattro */ |
| 951 | 0, /* tp_as_buffer */ |
| 952 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 953 | 0, /* tp_doc */ |
| 954 | 0, /* tp_traverse */ |
| 955 | 0, /* tp_clear */ |
| 956 | 0, /* tp_richcompare */ |
| 957 | 0, /* tp_weaklistoffset */ |
| 958 | 0, /* tp_iter */ |
| 959 | 0, /* tp_iterext */ |
| 960 | mbstreamreader_methods, /* tp_methods */ |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 961 | }; |
| 962 | |
| 963 | static int |
| 964 | mbstreamwriter_iwrite(MultibyteStreamWriterObject *self, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 965 | PyObject *unistr) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 966 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 967 | PyObject *wr, *ucvt, *r = NULL; |
Hye-Shik Chang | f5a149a | 2004-08-19 17:49:56 +0000 | [diff] [blame] | 968 | Py_UNICODE *inbuf, *inbuf_end, *inbuf_tmp = NULL; |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 969 | int datalen; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 970 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 971 | if (PyUnicode_Check(unistr)) |
| 972 | ucvt = NULL; |
| 973 | else { |
| 974 | unistr = ucvt = PyObject_Unicode(unistr); |
| 975 | if (unistr == NULL) |
| 976 | return -1; |
| 977 | else if (!PyUnicode_Check(unistr)) { |
| 978 | PyErr_SetString(PyExc_TypeError, |
| 979 | "couldn't convert the object to unicode."); |
| 980 | Py_DECREF(ucvt); |
| 981 | return -1; |
| 982 | } |
| 983 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 984 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 985 | datalen = PyUnicode_GET_SIZE(unistr); |
| 986 | if (datalen == 0) { |
| 987 | Py_XDECREF(ucvt); |
| 988 | return 0; |
| 989 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 990 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 991 | if (self->pendingsize > 0) { |
| 992 | inbuf_tmp = PyMem_New(Py_UNICODE, datalen + self->pendingsize); |
| 993 | if (inbuf_tmp == NULL) |
| 994 | goto errorexit; |
| 995 | memcpy(inbuf_tmp, self->pending, |
| 996 | Py_UNICODE_SIZE * self->pendingsize); |
| 997 | memcpy(inbuf_tmp + self->pendingsize, |
| 998 | PyUnicode_AS_UNICODE(unistr), |
| 999 | Py_UNICODE_SIZE * datalen); |
| 1000 | datalen += self->pendingsize; |
| 1001 | self->pendingsize = 0; |
| 1002 | inbuf = inbuf_tmp; |
| 1003 | } |
| 1004 | else |
| 1005 | inbuf = (Py_UNICODE *)PyUnicode_AS_UNICODE(unistr); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1006 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1007 | inbuf_end = inbuf + datalen; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1008 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1009 | r = multibytecodec_encode(self->codec, &self->state, |
| 1010 | (const Py_UNICODE **)&inbuf, datalen, self->errors, 0); |
| 1011 | if (r == NULL) |
| 1012 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1013 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1014 | if (inbuf < inbuf_end) { |
| 1015 | self->pendingsize = (int)(inbuf_end - inbuf); |
| 1016 | if (self->pendingsize > MAXENCPENDING) { |
| 1017 | self->pendingsize = 0; |
| 1018 | PyErr_SetString(PyExc_RuntimeError, |
| 1019 | "pending buffer overflow"); |
| 1020 | goto errorexit; |
| 1021 | } |
| 1022 | memcpy(self->pending, inbuf, |
| 1023 | self->pendingsize * Py_UNICODE_SIZE); |
| 1024 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1025 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1026 | wr = PyObject_CallMethod(self->stream, "write", "O", r); |
| 1027 | if (wr == NULL) |
| 1028 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1029 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1030 | if (inbuf_tmp != NULL) |
| 1031 | PyMem_Del(inbuf_tmp); |
| 1032 | Py_DECREF(r); |
| 1033 | Py_DECREF(wr); |
| 1034 | Py_XDECREF(ucvt); |
| 1035 | return 0; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1036 | |
| 1037 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1038 | if (inbuf_tmp != NULL) |
| 1039 | PyMem_Del(inbuf_tmp); |
| 1040 | Py_XDECREF(r); |
| 1041 | Py_XDECREF(ucvt); |
| 1042 | return -1; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static PyObject * |
| 1046 | mbstreamwriter_write(MultibyteStreamWriterObject *self, PyObject *args) |
| 1047 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1048 | PyObject *strobj; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1049 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1050 | if (!PyArg_ParseTuple(args, "O:write", &strobj)) |
| 1051 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1052 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1053 | if (mbstreamwriter_iwrite(self, strobj)) |
| 1054 | return NULL; |
| 1055 | else { |
| 1056 | Py_INCREF(Py_None); |
| 1057 | return Py_None; |
| 1058 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | static PyObject * |
| 1062 | mbstreamwriter_writelines(MultibyteStreamWriterObject *self, PyObject *args) |
| 1063 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1064 | PyObject *lines, *strobj; |
| 1065 | int i, r; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1066 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1067 | if (!PyArg_ParseTuple(args, "O:writelines", &lines)) |
| 1068 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1069 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1070 | if (!PySequence_Check(lines)) { |
| 1071 | PyErr_SetString(PyExc_TypeError, |
| 1072 | "arg must be a sequence object"); |
| 1073 | return NULL; |
| 1074 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1075 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1076 | for (i = 0; i < PySequence_Length(lines); i++) { |
| 1077 | /* length can be changed even within this loop */ |
| 1078 | strobj = PySequence_GetItem(lines, i); |
| 1079 | if (strobj == NULL) |
| 1080 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1081 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1082 | r = mbstreamwriter_iwrite(self, strobj); |
| 1083 | Py_DECREF(strobj); |
| 1084 | if (r == -1) |
| 1085 | return NULL; |
| 1086 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1087 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1088 | Py_INCREF(Py_None); |
| 1089 | return Py_None; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | static PyObject * |
| 1093 | mbstreamwriter_reset(MultibyteStreamWriterObject *self) |
| 1094 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1095 | const Py_UNICODE *pending; |
| 1096 | PyObject *pwrt; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1097 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1098 | pending = self->pending; |
| 1099 | pwrt = multibytecodec_encode(self->codec, &self->state, |
| 1100 | &pending, self->pendingsize, self->errors, |
| 1101 | MBENC_FLUSH | MBENC_RESET); |
| 1102 | /* some pending buffer can be truncated when UnicodeEncodeError is |
| 1103 | * raised on 'strict' mode. but, 'reset' method is designed to |
| 1104 | * reset the pending buffer or states so failed string sequence |
| 1105 | * ought to be missed */ |
| 1106 | self->pendingsize = 0; |
| 1107 | if (pwrt == NULL) |
| 1108 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1109 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1110 | if (PyString_Size(pwrt) > 0) { |
| 1111 | PyObject *wr; |
| 1112 | wr = PyObject_CallMethod(self->stream, "write", "O", pwrt); |
| 1113 | if (wr == NULL) { |
| 1114 | Py_DECREF(pwrt); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | } |
| 1118 | Py_DECREF(pwrt); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1119 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1120 | Py_INCREF(Py_None); |
| 1121 | return Py_None; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | static void |
| 1125 | mbstreamwriter_dealloc(MultibyteStreamWriterObject *self) |
| 1126 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1127 | if (self->errors > ERROR_MAX) { |
| 1128 | Py_DECREF(self->errors); |
| 1129 | } |
| 1130 | Py_DECREF(self->stream); |
| 1131 | PyObject_Del(self); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | static struct PyMethodDef mbstreamwriter_methods[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1135 | {"write", (PyCFunction)mbstreamwriter_write, |
| 1136 | METH_VARARGS, NULL}, |
| 1137 | {"writelines", (PyCFunction)mbstreamwriter_writelines, |
| 1138 | METH_VARARGS, NULL}, |
| 1139 | {"reset", (PyCFunction)mbstreamwriter_reset, |
| 1140 | METH_NOARGS, NULL}, |
| 1141 | {NULL, NULL}, |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1142 | }; |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | static PyTypeObject MultibyteStreamWriter_Type = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1147 | PyObject_HEAD_INIT(NULL) |
| 1148 | 0, /* ob_size */ |
| 1149 | "MultibyteStreamWriter", /* tp_name */ |
| 1150 | sizeof(MultibyteStreamWriterObject), /* tp_basicsize */ |
| 1151 | 0, /* tp_itemsize */ |
| 1152 | /* methods */ |
| 1153 | (destructor)mbstreamwriter_dealloc, /* tp_dealloc */ |
| 1154 | 0, /* tp_print */ |
| 1155 | 0, /* tp_getattr */ |
| 1156 | 0, /* tp_setattr */ |
| 1157 | 0, /* tp_compare */ |
| 1158 | 0, /* tp_repr */ |
| 1159 | 0, /* tp_as_number */ |
| 1160 | 0, /* tp_as_sequence */ |
| 1161 | 0, /* tp_as_mapping */ |
| 1162 | 0, /* tp_hash */ |
| 1163 | 0, /* tp_call */ |
| 1164 | 0, /* tp_str */ |
| 1165 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1166 | 0, /* tp_setattro */ |
| 1167 | 0, /* tp_as_buffer */ |
| 1168 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1169 | 0, /* tp_doc */ |
| 1170 | 0, /* tp_traverse */ |
| 1171 | 0, /* tp_clear */ |
| 1172 | 0, /* tp_richcompare */ |
| 1173 | 0, /* tp_weaklistoffset */ |
| 1174 | 0, /* tp_iter */ |
| 1175 | 0, /* tp_iterext */ |
| 1176 | mbstreamwriter_methods, /* tp_methods */ |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1177 | }; |
| 1178 | |
| 1179 | static PyObject * |
| 1180 | __create_codec(PyObject *ignore, PyObject *arg) |
| 1181 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1182 | MultibyteCodecObject *self; |
| 1183 | MultibyteCodec *codec; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1184 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1185 | if (!PyCObject_Check(arg)) { |
| 1186 | PyErr_SetString(PyExc_ValueError, "argument type invalid"); |
| 1187 | return NULL; |
| 1188 | } |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1189 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1190 | codec = PyCObject_AsVoidPtr(arg); |
| 1191 | if (codec->codecinit != NULL && codec->codecinit(codec->config) != 0) |
| 1192 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1193 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1194 | self = PyObject_New(MultibyteCodecObject, &MultibyteCodec_Type); |
| 1195 | if (self == NULL) |
| 1196 | return NULL; |
| 1197 | self->codec = codec; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1198 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1199 | return (PyObject *)self; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static PyObject * |
| 1203 | mbstreamreader_create(MultibyteCodec *codec, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1204 | PyObject *stream, const char *errors) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1205 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1206 | MultibyteStreamReaderObject *self; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1207 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1208 | self = PyObject_New(MultibyteStreamReaderObject, |
| 1209 | &MultibyteStreamReader_Type); |
| 1210 | if (self == NULL) |
| 1211 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1212 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1213 | self->codec = codec; |
| 1214 | self->stream = stream; |
| 1215 | Py_INCREF(stream); |
| 1216 | self->pendingsize = 0; |
| 1217 | self->errors = get_errorcallback(errors); |
| 1218 | if (self->errors == NULL) |
| 1219 | goto errorexit; |
| 1220 | if (self->codec->decinit != NULL && |
| 1221 | self->codec->decinit(&self->state, self->codec->config) != 0) |
| 1222 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1223 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1224 | return (PyObject *)self; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1225 | |
| 1226 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1227 | Py_XDECREF(self); |
| 1228 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | static PyObject * |
| 1232 | mbstreamwriter_create(MultibyteCodec *codec, |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1233 | PyObject *stream, const char *errors) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1234 | { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1235 | MultibyteStreamWriterObject *self; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1236 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1237 | self = PyObject_New(MultibyteStreamWriterObject, |
| 1238 | &MultibyteStreamWriter_Type); |
| 1239 | if (self == NULL) |
| 1240 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1241 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1242 | self->codec = codec; |
| 1243 | self->stream = stream; |
| 1244 | Py_INCREF(stream); |
| 1245 | self->pendingsize = 0; |
| 1246 | self->errors = get_errorcallback(errors); |
| 1247 | if (self->errors == NULL) |
| 1248 | goto errorexit; |
| 1249 | if (self->codec->encinit != NULL && |
| 1250 | self->codec->encinit(&self->state, self->codec->config) != 0) |
| 1251 | goto errorexit; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1252 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1253 | return (PyObject *)self; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1254 | |
| 1255 | errorexit: |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1256 | Py_XDECREF(self); |
| 1257 | return NULL; |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | static struct PyMethodDef __methods[] = { |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1261 | {"__create_codec", (PyCFunction)__create_codec, METH_O}, |
| 1262 | {NULL, NULL}, |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1263 | }; |
| 1264 | |
| 1265 | void |
| 1266 | init_multibytecodec(void) |
| 1267 | { |
Neal Norwitz | 058bde1 | 2005-09-21 06:44:25 +0000 | [diff] [blame^] | 1268 | MultibyteCodec_Type.ob_type = &PyType_Type; |
| 1269 | MultibyteStreamReader_Type.ob_type = &PyType_Type; |
| 1270 | MultibyteStreamWriter_Type.ob_type = &PyType_Type; |
| 1271 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1272 | Py_InitModule("_multibytecodec", __methods); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1273 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 1274 | if (PyErr_Occurred()) |
| 1275 | Py_FatalError("can't initialize the _multibytecodec module"); |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1276 | } |