| 1 | n/a | /* |
|---|
| 2 | n/a | * multibytecodec.c: Common Multibyte Codec Implementation |
|---|
| 3 | n/a | * |
|---|
| 4 | n/a | * Written by Hye-Shik Chang <perky@FreeBSD.org> |
|---|
| 5 | n/a | */ |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | #define PY_SSIZE_T_CLEAN |
|---|
| 8 | n/a | #include "Python.h" |
|---|
| 9 | n/a | #include "structmember.h" |
|---|
| 10 | n/a | #include "multibytecodec.h" |
|---|
| 11 | n/a | #include "clinic/multibytecodec.c.h" |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | /*[clinic input] |
|---|
| 14 | n/a | module _multibytecodec |
|---|
| 15 | n/a | class _multibytecodec.MultibyteCodec "MultibyteCodecObject *" "&MultibyteCodec_Type" |
|---|
| 16 | n/a | [clinic start generated code]*/ |
|---|
| 17 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=6ad689546cbb5450]*/ |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | typedef struct { |
|---|
| 20 | n/a | PyObject *inobj; |
|---|
| 21 | n/a | Py_ssize_t inpos, inlen; |
|---|
| 22 | n/a | unsigned char *outbuf, *outbuf_end; |
|---|
| 23 | n/a | PyObject *excobj, *outobj; |
|---|
| 24 | n/a | } MultibyteEncodeBuffer; |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | typedef struct { |
|---|
| 27 | n/a | const unsigned char *inbuf, *inbuf_top, *inbuf_end; |
|---|
| 28 | n/a | PyObject *excobj; |
|---|
| 29 | n/a | _PyUnicodeWriter writer; |
|---|
| 30 | n/a | } MultibyteDecodeBuffer; |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | static char *incnewkwarglist[] = {"errors", NULL}; |
|---|
| 33 | n/a | static char *streamkwarglist[] = {"stream", "errors", NULL}; |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | static PyObject *multibytecodec_encode(MultibyteCodec *, |
|---|
| 36 | n/a | MultibyteCodec_State *, PyObject *, Py_ssize_t *, |
|---|
| 37 | n/a | PyObject *, int); |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | #define MBENC_RESET MBENC_MAX<<1 /* reset after an encoding session */ |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | _Py_IDENTIFIER(write); |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | static PyObject * |
|---|
| 44 | n/a | make_tuple(PyObject *object, Py_ssize_t len) |
|---|
| 45 | n/a | { |
|---|
| 46 | n/a | PyObject *v, *w; |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | if (object == NULL) |
|---|
| 49 | n/a | return NULL; |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | v = PyTuple_New(2); |
|---|
| 52 | n/a | if (v == NULL) { |
|---|
| 53 | n/a | Py_DECREF(object); |
|---|
| 54 | n/a | return NULL; |
|---|
| 55 | n/a | } |
|---|
| 56 | n/a | PyTuple_SET_ITEM(v, 0, object); |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | w = PyLong_FromSsize_t(len); |
|---|
| 59 | n/a | if (w == NULL) { |
|---|
| 60 | n/a | Py_DECREF(v); |
|---|
| 61 | n/a | return NULL; |
|---|
| 62 | n/a | } |
|---|
| 63 | n/a | PyTuple_SET_ITEM(v, 1, w); |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | return v; |
|---|
| 66 | n/a | } |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | static PyObject * |
|---|
| 69 | n/a | internal_error_callback(const char *errors) |
|---|
| 70 | n/a | { |
|---|
| 71 | n/a | if (errors == NULL || strcmp(errors, "strict") == 0) |
|---|
| 72 | n/a | return ERROR_STRICT; |
|---|
| 73 | n/a | else if (strcmp(errors, "ignore") == 0) |
|---|
| 74 | n/a | return ERROR_IGNORE; |
|---|
| 75 | n/a | else if (strcmp(errors, "replace") == 0) |
|---|
| 76 | n/a | return ERROR_REPLACE; |
|---|
| 77 | n/a | else |
|---|
| 78 | n/a | return PyUnicode_FromString(errors); |
|---|
| 79 | n/a | } |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | static PyObject * |
|---|
| 82 | n/a | call_error_callback(PyObject *errors, PyObject *exc) |
|---|
| 83 | n/a | { |
|---|
| 84 | n/a | PyObject *args, *cb, *r; |
|---|
| 85 | n/a | const char *str; |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | assert(PyUnicode_Check(errors)); |
|---|
| 88 | n/a | str = PyUnicode_AsUTF8(errors); |
|---|
| 89 | n/a | if (str == NULL) |
|---|
| 90 | n/a | return NULL; |
|---|
| 91 | n/a | cb = PyCodec_LookupError(str); |
|---|
| 92 | n/a | if (cb == NULL) |
|---|
| 93 | n/a | return NULL; |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | args = PyTuple_New(1); |
|---|
| 96 | n/a | if (args == NULL) { |
|---|
| 97 | n/a | Py_DECREF(cb); |
|---|
| 98 | n/a | return NULL; |
|---|
| 99 | n/a | } |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | PyTuple_SET_ITEM(args, 0, exc); |
|---|
| 102 | n/a | Py_INCREF(exc); |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | r = PyObject_CallObject(cb, args); |
|---|
| 105 | n/a | Py_DECREF(args); |
|---|
| 106 | n/a | Py_DECREF(cb); |
|---|
| 107 | n/a | return r; |
|---|
| 108 | n/a | } |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | static PyObject * |
|---|
| 111 | n/a | codecctx_errors_get(MultibyteStatefulCodecContext *self) |
|---|
| 112 | n/a | { |
|---|
| 113 | n/a | const char *errors; |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | if (self->errors == ERROR_STRICT) |
|---|
| 116 | n/a | errors = "strict"; |
|---|
| 117 | n/a | else if (self->errors == ERROR_IGNORE) |
|---|
| 118 | n/a | errors = "ignore"; |
|---|
| 119 | n/a | else if (self->errors == ERROR_REPLACE) |
|---|
| 120 | n/a | errors = "replace"; |
|---|
| 121 | n/a | else { |
|---|
| 122 | n/a | Py_INCREF(self->errors); |
|---|
| 123 | n/a | return self->errors; |
|---|
| 124 | n/a | } |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | return PyUnicode_FromString(errors); |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | static int |
|---|
| 130 | n/a | codecctx_errors_set(MultibyteStatefulCodecContext *self, PyObject *value, |
|---|
| 131 | n/a | void *closure) |
|---|
| 132 | n/a | { |
|---|
| 133 | n/a | PyObject *cb; |
|---|
| 134 | n/a | const char *str; |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | if (!PyUnicode_Check(value)) { |
|---|
| 137 | n/a | PyErr_SetString(PyExc_TypeError, "errors must be a string"); |
|---|
| 138 | n/a | return -1; |
|---|
| 139 | n/a | } |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | str = PyUnicode_AsUTF8(value); |
|---|
| 142 | n/a | if (str == NULL) |
|---|
| 143 | n/a | return -1; |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | cb = internal_error_callback(str); |
|---|
| 146 | n/a | if (cb == NULL) |
|---|
| 147 | n/a | return -1; |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | ERROR_DECREF(self->errors); |
|---|
| 150 | n/a | self->errors = cb; |
|---|
| 151 | n/a | return 0; |
|---|
| 152 | n/a | } |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | /* This getset handlers list is used by all the stateful codec objects */ |
|---|
| 155 | n/a | static PyGetSetDef codecctx_getsets[] = { |
|---|
| 156 | n/a | {"errors", (getter)codecctx_errors_get, |
|---|
| 157 | n/a | (setter)codecctx_errors_set, |
|---|
| 158 | n/a | PyDoc_STR("how to treat errors")}, |
|---|
| 159 | n/a | {NULL,} |
|---|
| 160 | n/a | }; |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | static int |
|---|
| 163 | n/a | expand_encodebuffer(MultibyteEncodeBuffer *buf, Py_ssize_t esize) |
|---|
| 164 | n/a | { |
|---|
| 165 | n/a | Py_ssize_t orgpos, orgsize, incsize; |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | orgpos = (Py_ssize_t)((char *)buf->outbuf - |
|---|
| 168 | n/a | PyBytes_AS_STRING(buf->outobj)); |
|---|
| 169 | n/a | orgsize = PyBytes_GET_SIZE(buf->outobj); |
|---|
| 170 | n/a | incsize = (esize < (orgsize >> 1) ? (orgsize >> 1) | 1 : esize); |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | if (orgsize > PY_SSIZE_T_MAX - incsize) { |
|---|
| 173 | n/a | PyErr_NoMemory(); |
|---|
| 174 | n/a | return -1; |
|---|
| 175 | n/a | } |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | if (_PyBytes_Resize(&buf->outobj, orgsize + incsize) == -1) |
|---|
| 178 | n/a | return -1; |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | buf->outbuf = (unsigned char *)PyBytes_AS_STRING(buf->outobj) +orgpos; |
|---|
| 181 | n/a | buf->outbuf_end = (unsigned char *)PyBytes_AS_STRING(buf->outobj) |
|---|
| 182 | n/a | + PyBytes_GET_SIZE(buf->outobj); |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | return 0; |
|---|
| 185 | n/a | } |
|---|
| 186 | n/a | #define REQUIRE_ENCODEBUFFER(buf, s) do { \ |
|---|
| 187 | n/a | if ((s) < 0 || (s) > (buf)->outbuf_end - (buf)->outbuf) \ |
|---|
| 188 | n/a | if (expand_encodebuffer(buf, s) == -1) \ |
|---|
| 189 | n/a | goto errorexit; \ |
|---|
| 190 | n/a | } while(0) |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | /** |
|---|
| 194 | n/a | * MultibyteCodec object |
|---|
| 195 | n/a | */ |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | static int |
|---|
| 198 | n/a | multibytecodec_encerror(MultibyteCodec *codec, |
|---|
| 199 | n/a | MultibyteCodec_State *state, |
|---|
| 200 | n/a | MultibyteEncodeBuffer *buf, |
|---|
| 201 | n/a | PyObject *errors, Py_ssize_t e) |
|---|
| 202 | n/a | { |
|---|
| 203 | n/a | PyObject *retobj = NULL, *retstr = NULL, *tobj; |
|---|
| 204 | n/a | Py_ssize_t retstrsize, newpos; |
|---|
| 205 | n/a | Py_ssize_t esize, start, end; |
|---|
| 206 | n/a | const char *reason; |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | if (e > 0) { |
|---|
| 209 | n/a | reason = "illegal multibyte sequence"; |
|---|
| 210 | n/a | esize = e; |
|---|
| 211 | n/a | } |
|---|
| 212 | n/a | else { |
|---|
| 213 | n/a | switch (e) { |
|---|
| 214 | n/a | case MBERR_TOOSMALL: |
|---|
| 215 | n/a | REQUIRE_ENCODEBUFFER(buf, -1); |
|---|
| 216 | n/a | return 0; /* retry it */ |
|---|
| 217 | n/a | case MBERR_TOOFEW: |
|---|
| 218 | n/a | reason = "incomplete multibyte sequence"; |
|---|
| 219 | n/a | esize = (Py_ssize_t)buf->inpos; |
|---|
| 220 | n/a | break; |
|---|
| 221 | n/a | case MBERR_INTERNAL: |
|---|
| 222 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 223 | n/a | "internal codec error"); |
|---|
| 224 | n/a | return -1; |
|---|
| 225 | n/a | default: |
|---|
| 226 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 227 | n/a | "unknown runtime error"); |
|---|
| 228 | n/a | return -1; |
|---|
| 229 | n/a | } |
|---|
| 230 | n/a | } |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | if (errors == ERROR_REPLACE) { |
|---|
| 233 | n/a | PyObject *replchar; |
|---|
| 234 | n/a | Py_ssize_t r; |
|---|
| 235 | n/a | Py_ssize_t inpos; |
|---|
| 236 | n/a | int kind; |
|---|
| 237 | n/a | void *data; |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | replchar = PyUnicode_FromOrdinal('?'); |
|---|
| 240 | n/a | if (replchar == NULL) |
|---|
| 241 | n/a | goto errorexit; |
|---|
| 242 | n/a | kind = PyUnicode_KIND(replchar); |
|---|
| 243 | n/a | data = PyUnicode_DATA(replchar); |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | inpos = 0; |
|---|
| 246 | n/a | for (;;) { |
|---|
| 247 | n/a | Py_ssize_t outleft = (Py_ssize_t)(buf->outbuf_end - buf->outbuf); |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | r = codec->encode(state, codec->config, |
|---|
| 250 | n/a | kind, data, &inpos, 1, |
|---|
| 251 | n/a | &buf->outbuf, outleft, 0); |
|---|
| 252 | n/a | if (r == MBERR_TOOSMALL) { |
|---|
| 253 | n/a | REQUIRE_ENCODEBUFFER(buf, -1); |
|---|
| 254 | n/a | continue; |
|---|
| 255 | n/a | } |
|---|
| 256 | n/a | else |
|---|
| 257 | n/a | break; |
|---|
| 258 | n/a | } |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | Py_DECREF(replchar); |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | if (r != 0) { |
|---|
| 263 | n/a | REQUIRE_ENCODEBUFFER(buf, 1); |
|---|
| 264 | n/a | *buf->outbuf++ = '?'; |
|---|
| 265 | n/a | } |
|---|
| 266 | n/a | } |
|---|
| 267 | n/a | if (errors == ERROR_IGNORE || errors == ERROR_REPLACE) { |
|---|
| 268 | n/a | buf->inpos += esize; |
|---|
| 269 | n/a | return 0; |
|---|
| 270 | n/a | } |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | start = (Py_ssize_t)buf->inpos; |
|---|
| 273 | n/a | end = start + esize; |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | /* use cached exception object if available */ |
|---|
| 276 | n/a | if (buf->excobj == NULL) { |
|---|
| 277 | n/a | buf->excobj = PyObject_CallFunction(PyExc_UnicodeEncodeError, |
|---|
| 278 | n/a | "sOnns", |
|---|
| 279 | n/a | codec->encoding, buf->inobj, |
|---|
| 280 | n/a | start, end, reason); |
|---|
| 281 | n/a | if (buf->excobj == NULL) |
|---|
| 282 | n/a | goto errorexit; |
|---|
| 283 | n/a | } |
|---|
| 284 | n/a | else |
|---|
| 285 | n/a | if (PyUnicodeEncodeError_SetStart(buf->excobj, start) != 0 || |
|---|
| 286 | n/a | PyUnicodeEncodeError_SetEnd(buf->excobj, end) != 0 || |
|---|
| 287 | n/a | PyUnicodeEncodeError_SetReason(buf->excobj, reason) != 0) |
|---|
| 288 | n/a | goto errorexit; |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | if (errors == ERROR_STRICT) { |
|---|
| 291 | n/a | PyCodec_StrictErrors(buf->excobj); |
|---|
| 292 | n/a | goto errorexit; |
|---|
| 293 | n/a | } |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | retobj = call_error_callback(errors, buf->excobj); |
|---|
| 296 | n/a | if (retobj == NULL) |
|---|
| 297 | n/a | goto errorexit; |
|---|
| 298 | n/a | |
|---|
| 299 | n/a | if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 || |
|---|
| 300 | n/a | (!PyUnicode_Check((tobj = PyTuple_GET_ITEM(retobj, 0))) && !PyBytes_Check(tobj)) || |
|---|
| 301 | n/a | !PyLong_Check(PyTuple_GET_ITEM(retobj, 1))) { |
|---|
| 302 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 303 | n/a | "encoding error handler must return " |
|---|
| 304 | n/a | "(str, int) tuple"); |
|---|
| 305 | n/a | goto errorexit; |
|---|
| 306 | n/a | } |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | if (PyUnicode_Check(tobj)) { |
|---|
| 309 | n/a | Py_ssize_t inpos; |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | retstr = multibytecodec_encode(codec, state, tobj, |
|---|
| 312 | n/a | &inpos, ERROR_STRICT, |
|---|
| 313 | n/a | MBENC_FLUSH); |
|---|
| 314 | n/a | if (retstr == NULL) |
|---|
| 315 | n/a | goto errorexit; |
|---|
| 316 | n/a | } |
|---|
| 317 | n/a | else { |
|---|
| 318 | n/a | Py_INCREF(tobj); |
|---|
| 319 | n/a | retstr = tobj; |
|---|
| 320 | n/a | } |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | assert(PyBytes_Check(retstr)); |
|---|
| 323 | n/a | retstrsize = PyBytes_GET_SIZE(retstr); |
|---|
| 324 | n/a | if (retstrsize > 0) { |
|---|
| 325 | n/a | REQUIRE_ENCODEBUFFER(buf, retstrsize); |
|---|
| 326 | n/a | memcpy(buf->outbuf, PyBytes_AS_STRING(retstr), retstrsize); |
|---|
| 327 | n/a | buf->outbuf += retstrsize; |
|---|
| 328 | n/a | } |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | newpos = PyLong_AsSsize_t(PyTuple_GET_ITEM(retobj, 1)); |
|---|
| 331 | n/a | if (newpos < 0 && !PyErr_Occurred()) |
|---|
| 332 | n/a | newpos += (Py_ssize_t)buf->inlen; |
|---|
| 333 | n/a | if (newpos < 0 || newpos > buf->inlen) { |
|---|
| 334 | n/a | PyErr_Clear(); |
|---|
| 335 | n/a | PyErr_Format(PyExc_IndexError, |
|---|
| 336 | n/a | "position %zd from error handler out of bounds", |
|---|
| 337 | n/a | newpos); |
|---|
| 338 | n/a | goto errorexit; |
|---|
| 339 | n/a | } |
|---|
| 340 | n/a | buf->inpos = newpos; |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | Py_DECREF(retobj); |
|---|
| 343 | n/a | Py_DECREF(retstr); |
|---|
| 344 | n/a | return 0; |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | errorexit: |
|---|
| 347 | n/a | Py_XDECREF(retobj); |
|---|
| 348 | n/a | Py_XDECREF(retstr); |
|---|
| 349 | n/a | return -1; |
|---|
| 350 | n/a | } |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | static int |
|---|
| 353 | n/a | multibytecodec_decerror(MultibyteCodec *codec, |
|---|
| 354 | n/a | MultibyteCodec_State *state, |
|---|
| 355 | n/a | MultibyteDecodeBuffer *buf, |
|---|
| 356 | n/a | PyObject *errors, Py_ssize_t e) |
|---|
| 357 | n/a | { |
|---|
| 358 | n/a | PyObject *retobj = NULL, *retuni = NULL; |
|---|
| 359 | n/a | Py_ssize_t newpos; |
|---|
| 360 | n/a | const char *reason; |
|---|
| 361 | n/a | Py_ssize_t esize, start, end; |
|---|
| 362 | n/a | |
|---|
| 363 | n/a | if (e > 0) { |
|---|
| 364 | n/a | reason = "illegal multibyte sequence"; |
|---|
| 365 | n/a | esize = e; |
|---|
| 366 | n/a | } |
|---|
| 367 | n/a | else { |
|---|
| 368 | n/a | switch (e) { |
|---|
| 369 | n/a | case MBERR_TOOSMALL: |
|---|
| 370 | n/a | return 0; /* retry it */ |
|---|
| 371 | n/a | case MBERR_TOOFEW: |
|---|
| 372 | n/a | reason = "incomplete multibyte sequence"; |
|---|
| 373 | n/a | esize = (Py_ssize_t)(buf->inbuf_end - buf->inbuf); |
|---|
| 374 | n/a | break; |
|---|
| 375 | n/a | case MBERR_INTERNAL: |
|---|
| 376 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 377 | n/a | "internal codec error"); |
|---|
| 378 | n/a | return -1; |
|---|
| 379 | n/a | case MBERR_EXCEPTION: |
|---|
| 380 | n/a | return -1; |
|---|
| 381 | n/a | default: |
|---|
| 382 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 383 | n/a | "unknown runtime error"); |
|---|
| 384 | n/a | return -1; |
|---|
| 385 | n/a | } |
|---|
| 386 | n/a | } |
|---|
| 387 | n/a | |
|---|
| 388 | n/a | if (errors == ERROR_REPLACE) { |
|---|
| 389 | n/a | if (_PyUnicodeWriter_WriteChar(&buf->writer, |
|---|
| 390 | n/a | Py_UNICODE_REPLACEMENT_CHARACTER) < 0) |
|---|
| 391 | n/a | goto errorexit; |
|---|
| 392 | n/a | } |
|---|
| 393 | n/a | if (errors == ERROR_IGNORE || errors == ERROR_REPLACE) { |
|---|
| 394 | n/a | buf->inbuf += esize; |
|---|
| 395 | n/a | return 0; |
|---|
| 396 | n/a | } |
|---|
| 397 | n/a | |
|---|
| 398 | n/a | start = (Py_ssize_t)(buf->inbuf - buf->inbuf_top); |
|---|
| 399 | n/a | end = start + esize; |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | /* use cached exception object if available */ |
|---|
| 402 | n/a | if (buf->excobj == NULL) { |
|---|
| 403 | n/a | buf->excobj = PyUnicodeDecodeError_Create(codec->encoding, |
|---|
| 404 | n/a | (const char *)buf->inbuf_top, |
|---|
| 405 | n/a | (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top), |
|---|
| 406 | n/a | start, end, reason); |
|---|
| 407 | n/a | if (buf->excobj == NULL) |
|---|
| 408 | n/a | goto errorexit; |
|---|
| 409 | n/a | } |
|---|
| 410 | n/a | else |
|---|
| 411 | n/a | if (PyUnicodeDecodeError_SetStart(buf->excobj, start) || |
|---|
| 412 | n/a | PyUnicodeDecodeError_SetEnd(buf->excobj, end) || |
|---|
| 413 | n/a | PyUnicodeDecodeError_SetReason(buf->excobj, reason)) |
|---|
| 414 | n/a | goto errorexit; |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | if (errors == ERROR_STRICT) { |
|---|
| 417 | n/a | PyCodec_StrictErrors(buf->excobj); |
|---|
| 418 | n/a | goto errorexit; |
|---|
| 419 | n/a | } |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | retobj = call_error_callback(errors, buf->excobj); |
|---|
| 422 | n/a | if (retobj == NULL) |
|---|
| 423 | n/a | goto errorexit; |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 || |
|---|
| 426 | n/a | !PyUnicode_Check((retuni = PyTuple_GET_ITEM(retobj, 0))) || |
|---|
| 427 | n/a | !PyLong_Check(PyTuple_GET_ITEM(retobj, 1))) { |
|---|
| 428 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 429 | n/a | "decoding error handler must return " |
|---|
| 430 | n/a | "(str, int) tuple"); |
|---|
| 431 | n/a | goto errorexit; |
|---|
| 432 | n/a | } |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | if (_PyUnicodeWriter_WriteStr(&buf->writer, retuni) < 0) |
|---|
| 435 | n/a | goto errorexit; |
|---|
| 436 | n/a | |
|---|
| 437 | n/a | newpos = PyLong_AsSsize_t(PyTuple_GET_ITEM(retobj, 1)); |
|---|
| 438 | n/a | if (newpos < 0 && !PyErr_Occurred()) |
|---|
| 439 | n/a | newpos += (Py_ssize_t)(buf->inbuf_end - buf->inbuf_top); |
|---|
| 440 | n/a | if (newpos < 0 || buf->inbuf_top + newpos > buf->inbuf_end) { |
|---|
| 441 | n/a | PyErr_Clear(); |
|---|
| 442 | n/a | PyErr_Format(PyExc_IndexError, |
|---|
| 443 | n/a | "position %zd from error handler out of bounds", |
|---|
| 444 | n/a | newpos); |
|---|
| 445 | n/a | goto errorexit; |
|---|
| 446 | n/a | } |
|---|
| 447 | n/a | buf->inbuf = buf->inbuf_top + newpos; |
|---|
| 448 | n/a | Py_DECREF(retobj); |
|---|
| 449 | n/a | return 0; |
|---|
| 450 | n/a | |
|---|
| 451 | n/a | errorexit: |
|---|
| 452 | n/a | Py_XDECREF(retobj); |
|---|
| 453 | n/a | return -1; |
|---|
| 454 | n/a | } |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | static PyObject * |
|---|
| 457 | n/a | multibytecodec_encode(MultibyteCodec *codec, |
|---|
| 458 | n/a | MultibyteCodec_State *state, |
|---|
| 459 | n/a | PyObject *text, Py_ssize_t *inpos_t, |
|---|
| 460 | n/a | PyObject *errors, int flags) |
|---|
| 461 | n/a | { |
|---|
| 462 | n/a | MultibyteEncodeBuffer buf; |
|---|
| 463 | n/a | Py_ssize_t finalsize, r = 0; |
|---|
| 464 | n/a | Py_ssize_t datalen; |
|---|
| 465 | n/a | int kind; |
|---|
| 466 | n/a | void *data; |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | if (PyUnicode_READY(text) < 0) |
|---|
| 469 | n/a | return NULL; |
|---|
| 470 | n/a | datalen = PyUnicode_GET_LENGTH(text); |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | if (datalen == 0 && !(flags & MBENC_RESET)) |
|---|
| 473 | n/a | return PyBytes_FromStringAndSize(NULL, 0); |
|---|
| 474 | n/a | |
|---|
| 475 | n/a | buf.excobj = NULL; |
|---|
| 476 | n/a | buf.outobj = NULL; |
|---|
| 477 | n/a | buf.inobj = text; /* borrowed reference */ |
|---|
| 478 | n/a | buf.inpos = 0; |
|---|
| 479 | n/a | buf.inlen = datalen; |
|---|
| 480 | n/a | kind = PyUnicode_KIND(buf.inobj); |
|---|
| 481 | n/a | data = PyUnicode_DATA(buf.inobj); |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | if (datalen > (PY_SSIZE_T_MAX - 16) / 2) { |
|---|
| 484 | n/a | PyErr_NoMemory(); |
|---|
| 485 | n/a | goto errorexit; |
|---|
| 486 | n/a | } |
|---|
| 487 | n/a | |
|---|
| 488 | n/a | buf.outobj = PyBytes_FromStringAndSize(NULL, datalen * 2 + 16); |
|---|
| 489 | n/a | if (buf.outobj == NULL) |
|---|
| 490 | n/a | goto errorexit; |
|---|
| 491 | n/a | buf.outbuf = (unsigned char *)PyBytes_AS_STRING(buf.outobj); |
|---|
| 492 | n/a | buf.outbuf_end = buf.outbuf + PyBytes_GET_SIZE(buf.outobj); |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | while (buf.inpos < buf.inlen) { |
|---|
| 495 | n/a | /* we don't reuse inleft and outleft here. |
|---|
| 496 | n/a | * error callbacks can relocate the cursor anywhere on buffer*/ |
|---|
| 497 | n/a | Py_ssize_t outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf); |
|---|
| 498 | n/a | |
|---|
| 499 | n/a | r = codec->encode(state, codec->config, |
|---|
| 500 | n/a | kind, data, |
|---|
| 501 | n/a | &buf.inpos, buf.inlen, |
|---|
| 502 | n/a | &buf.outbuf, outleft, flags); |
|---|
| 503 | n/a | if ((r == 0) || (r == MBERR_TOOFEW && !(flags & MBENC_FLUSH))) |
|---|
| 504 | n/a | break; |
|---|
| 505 | n/a | else if (multibytecodec_encerror(codec, state, &buf, errors,r)) |
|---|
| 506 | n/a | goto errorexit; |
|---|
| 507 | n/a | else if (r == MBERR_TOOFEW) |
|---|
| 508 | n/a | break; |
|---|
| 509 | n/a | } |
|---|
| 510 | n/a | |
|---|
| 511 | n/a | if (codec->encreset != NULL && (flags & MBENC_RESET)) |
|---|
| 512 | n/a | for (;;) { |
|---|
| 513 | n/a | Py_ssize_t outleft; |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | outleft = (Py_ssize_t)(buf.outbuf_end - buf.outbuf); |
|---|
| 516 | n/a | r = codec->encreset(state, codec->config, &buf.outbuf, |
|---|
| 517 | n/a | outleft); |
|---|
| 518 | n/a | if (r == 0) |
|---|
| 519 | n/a | break; |
|---|
| 520 | n/a | else if (multibytecodec_encerror(codec, state, |
|---|
| 521 | n/a | &buf, errors, r)) |
|---|
| 522 | n/a | goto errorexit; |
|---|
| 523 | n/a | } |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | finalsize = (Py_ssize_t)((char *)buf.outbuf - |
|---|
| 526 | n/a | PyBytes_AS_STRING(buf.outobj)); |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | if (finalsize != PyBytes_GET_SIZE(buf.outobj)) |
|---|
| 529 | n/a | if (_PyBytes_Resize(&buf.outobj, finalsize) == -1) |
|---|
| 530 | n/a | goto errorexit; |
|---|
| 531 | n/a | |
|---|
| 532 | n/a | if (inpos_t) |
|---|
| 533 | n/a | *inpos_t = buf.inpos; |
|---|
| 534 | n/a | Py_XDECREF(buf.excobj); |
|---|
| 535 | n/a | return buf.outobj; |
|---|
| 536 | n/a | |
|---|
| 537 | n/a | errorexit: |
|---|
| 538 | n/a | Py_XDECREF(buf.excobj); |
|---|
| 539 | n/a | Py_XDECREF(buf.outobj); |
|---|
| 540 | n/a | return NULL; |
|---|
| 541 | n/a | } |
|---|
| 542 | n/a | |
|---|
| 543 | n/a | /*[clinic input] |
|---|
| 544 | n/a | _multibytecodec.MultibyteCodec.encode |
|---|
| 545 | n/a | |
|---|
| 546 | n/a | input: object |
|---|
| 547 | n/a | errors: str(accept={str, NoneType}) = NULL |
|---|
| 548 | n/a | |
|---|
| 549 | n/a | Return an encoded string version of `input'. |
|---|
| 550 | n/a | |
|---|
| 551 | n/a | 'errors' may be given to set a different error handling scheme. Default is |
|---|
| 552 | n/a | 'strict' meaning that encoding errors raise a UnicodeEncodeError. Other possible |
|---|
| 553 | n/a | values are 'ignore', 'replace' and 'xmlcharrefreplace' as well as any other name |
|---|
| 554 | n/a | registered with codecs.register_error that can handle UnicodeEncodeErrors. |
|---|
| 555 | n/a | [clinic start generated code]*/ |
|---|
| 556 | n/a | |
|---|
| 557 | n/a | static PyObject * |
|---|
| 558 | n/a | _multibytecodec_MultibyteCodec_encode_impl(MultibyteCodecObject *self, |
|---|
| 559 | n/a | PyObject *input, |
|---|
| 560 | n/a | const char *errors) |
|---|
| 561 | n/a | /*[clinic end generated code: output=7b26652045ba56a9 input=05f6ced3c8dd0582]*/ |
|---|
| 562 | n/a | { |
|---|
| 563 | n/a | MultibyteCodec_State state; |
|---|
| 564 | n/a | PyObject *errorcb, *r, *ucvt; |
|---|
| 565 | n/a | Py_ssize_t datalen; |
|---|
| 566 | n/a | |
|---|
| 567 | n/a | if (PyUnicode_Check(input)) |
|---|
| 568 | n/a | ucvt = NULL; |
|---|
| 569 | n/a | else { |
|---|
| 570 | n/a | input = ucvt = PyObject_Str(input); |
|---|
| 571 | n/a | if (input == NULL) |
|---|
| 572 | n/a | return NULL; |
|---|
| 573 | n/a | else if (!PyUnicode_Check(input)) { |
|---|
| 574 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 575 | n/a | "couldn't convert the object to unicode."); |
|---|
| 576 | n/a | Py_DECREF(ucvt); |
|---|
| 577 | n/a | return NULL; |
|---|
| 578 | n/a | } |
|---|
| 579 | n/a | } |
|---|
| 580 | n/a | |
|---|
| 581 | n/a | if (PyUnicode_READY(input) < 0) { |
|---|
| 582 | n/a | Py_XDECREF(ucvt); |
|---|
| 583 | n/a | return NULL; |
|---|
| 584 | n/a | } |
|---|
| 585 | n/a | datalen = PyUnicode_GET_LENGTH(input); |
|---|
| 586 | n/a | |
|---|
| 587 | n/a | errorcb = internal_error_callback(errors); |
|---|
| 588 | n/a | if (errorcb == NULL) { |
|---|
| 589 | n/a | Py_XDECREF(ucvt); |
|---|
| 590 | n/a | return NULL; |
|---|
| 591 | n/a | } |
|---|
| 592 | n/a | |
|---|
| 593 | n/a | if (self->codec->encinit != NULL && |
|---|
| 594 | n/a | self->codec->encinit(&state, self->codec->config) != 0) |
|---|
| 595 | n/a | goto errorexit; |
|---|
| 596 | n/a | r = multibytecodec_encode(self->codec, &state, |
|---|
| 597 | n/a | input, NULL, errorcb, |
|---|
| 598 | n/a | MBENC_FLUSH | MBENC_RESET); |
|---|
| 599 | n/a | if (r == NULL) |
|---|
| 600 | n/a | goto errorexit; |
|---|
| 601 | n/a | |
|---|
| 602 | n/a | ERROR_DECREF(errorcb); |
|---|
| 603 | n/a | Py_XDECREF(ucvt); |
|---|
| 604 | n/a | return make_tuple(r, datalen); |
|---|
| 605 | n/a | |
|---|
| 606 | n/a | errorexit: |
|---|
| 607 | n/a | ERROR_DECREF(errorcb); |
|---|
| 608 | n/a | Py_XDECREF(ucvt); |
|---|
| 609 | n/a | return NULL; |
|---|
| 610 | n/a | } |
|---|
| 611 | n/a | |
|---|
| 612 | n/a | /*[clinic input] |
|---|
| 613 | n/a | _multibytecodec.MultibyteCodec.decode |
|---|
| 614 | n/a | |
|---|
| 615 | n/a | input: Py_buffer |
|---|
| 616 | n/a | errors: str(accept={str, NoneType}) = NULL |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | Decodes 'input'. |
|---|
| 619 | n/a | |
|---|
| 620 | n/a | 'errors' may be given to set a different error handling scheme. Default is |
|---|
| 621 | n/a | 'strict' meaning that encoding errors raise a UnicodeDecodeError. Other possible |
|---|
| 622 | n/a | values are 'ignore' and 'replace' as well as any other name registered with |
|---|
| 623 | n/a | codecs.register_error that is able to handle UnicodeDecodeErrors." |
|---|
| 624 | n/a | [clinic start generated code]*/ |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | static PyObject * |
|---|
| 627 | n/a | _multibytecodec_MultibyteCodec_decode_impl(MultibyteCodecObject *self, |
|---|
| 628 | n/a | Py_buffer *input, |
|---|
| 629 | n/a | const char *errors) |
|---|
| 630 | n/a | /*[clinic end generated code: output=ff419f65bad6cc77 input=a7d45f87f75e5e02]*/ |
|---|
| 631 | n/a | { |
|---|
| 632 | n/a | MultibyteCodec_State state; |
|---|
| 633 | n/a | MultibyteDecodeBuffer buf; |
|---|
| 634 | n/a | PyObject *errorcb, *res; |
|---|
| 635 | n/a | const char *data; |
|---|
| 636 | n/a | Py_ssize_t datalen; |
|---|
| 637 | n/a | |
|---|
| 638 | n/a | data = input->buf; |
|---|
| 639 | n/a | datalen = input->len; |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | errorcb = internal_error_callback(errors); |
|---|
| 642 | n/a | if (errorcb == NULL) { |
|---|
| 643 | n/a | return NULL; |
|---|
| 644 | n/a | } |
|---|
| 645 | n/a | |
|---|
| 646 | n/a | if (datalen == 0) { |
|---|
| 647 | n/a | ERROR_DECREF(errorcb); |
|---|
| 648 | n/a | return make_tuple(PyUnicode_New(0, 0), 0); |
|---|
| 649 | n/a | } |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | _PyUnicodeWriter_Init(&buf.writer); |
|---|
| 652 | n/a | buf.writer.min_length = datalen; |
|---|
| 653 | n/a | buf.excobj = NULL; |
|---|
| 654 | n/a | buf.inbuf = buf.inbuf_top = (unsigned char *)data; |
|---|
| 655 | n/a | buf.inbuf_end = buf.inbuf_top + datalen; |
|---|
| 656 | n/a | |
|---|
| 657 | n/a | if (self->codec->decinit != NULL && |
|---|
| 658 | n/a | self->codec->decinit(&state, self->codec->config) != 0) |
|---|
| 659 | n/a | goto errorexit; |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | while (buf.inbuf < buf.inbuf_end) { |
|---|
| 662 | n/a | Py_ssize_t inleft, r; |
|---|
| 663 | n/a | |
|---|
| 664 | n/a | inleft = (Py_ssize_t)(buf.inbuf_end - buf.inbuf); |
|---|
| 665 | n/a | |
|---|
| 666 | n/a | r = self->codec->decode(&state, self->codec->config, |
|---|
| 667 | n/a | &buf.inbuf, inleft, &buf.writer); |
|---|
| 668 | n/a | if (r == 0) |
|---|
| 669 | n/a | break; |
|---|
| 670 | n/a | else if (multibytecodec_decerror(self->codec, &state, |
|---|
| 671 | n/a | &buf, errorcb, r)) |
|---|
| 672 | n/a | goto errorexit; |
|---|
| 673 | n/a | } |
|---|
| 674 | n/a | |
|---|
| 675 | n/a | res = _PyUnicodeWriter_Finish(&buf.writer); |
|---|
| 676 | n/a | if (res == NULL) |
|---|
| 677 | n/a | goto errorexit; |
|---|
| 678 | n/a | |
|---|
| 679 | n/a | Py_XDECREF(buf.excobj); |
|---|
| 680 | n/a | ERROR_DECREF(errorcb); |
|---|
| 681 | n/a | return make_tuple(res, datalen); |
|---|
| 682 | n/a | |
|---|
| 683 | n/a | errorexit: |
|---|
| 684 | n/a | ERROR_DECREF(errorcb); |
|---|
| 685 | n/a | Py_XDECREF(buf.excobj); |
|---|
| 686 | n/a | _PyUnicodeWriter_Dealloc(&buf.writer); |
|---|
| 687 | n/a | |
|---|
| 688 | n/a | return NULL; |
|---|
| 689 | n/a | } |
|---|
| 690 | n/a | |
|---|
| 691 | n/a | static struct PyMethodDef multibytecodec_methods[] = { |
|---|
| 692 | n/a | _MULTIBYTECODEC_MULTIBYTECODEC_ENCODE_METHODDEF |
|---|
| 693 | n/a | _MULTIBYTECODEC_MULTIBYTECODEC_DECODE_METHODDEF |
|---|
| 694 | n/a | {NULL, NULL}, |
|---|
| 695 | n/a | }; |
|---|
| 696 | n/a | |
|---|
| 697 | n/a | static void |
|---|
| 698 | n/a | multibytecodec_dealloc(MultibyteCodecObject *self) |
|---|
| 699 | n/a | { |
|---|
| 700 | n/a | PyObject_Del(self); |
|---|
| 701 | n/a | } |
|---|
| 702 | n/a | |
|---|
| 703 | n/a | static PyTypeObject MultibyteCodec_Type = { |
|---|
| 704 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 705 | n/a | "MultibyteCodec", /* tp_name */ |
|---|
| 706 | n/a | sizeof(MultibyteCodecObject), /* tp_basicsize */ |
|---|
| 707 | n/a | 0, /* tp_itemsize */ |
|---|
| 708 | n/a | /* methods */ |
|---|
| 709 | n/a | (destructor)multibytecodec_dealloc, /* tp_dealloc */ |
|---|
| 710 | n/a | 0, /* tp_print */ |
|---|
| 711 | n/a | 0, /* tp_getattr */ |
|---|
| 712 | n/a | 0, /* tp_setattr */ |
|---|
| 713 | n/a | 0, /* tp_reserved */ |
|---|
| 714 | n/a | 0, /* tp_repr */ |
|---|
| 715 | n/a | 0, /* tp_as_number */ |
|---|
| 716 | n/a | 0, /* tp_as_sequence */ |
|---|
| 717 | n/a | 0, /* tp_as_mapping */ |
|---|
| 718 | n/a | 0, /* tp_hash */ |
|---|
| 719 | n/a | 0, /* tp_call */ |
|---|
| 720 | n/a | 0, /* tp_str */ |
|---|
| 721 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 722 | n/a | 0, /* tp_setattro */ |
|---|
| 723 | n/a | 0, /* tp_as_buffer */ |
|---|
| 724 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|---|
| 725 | n/a | 0, /* tp_doc */ |
|---|
| 726 | n/a | 0, /* tp_traverse */ |
|---|
| 727 | n/a | 0, /* tp_clear */ |
|---|
| 728 | n/a | 0, /* tp_richcompare */ |
|---|
| 729 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 730 | n/a | 0, /* tp_iter */ |
|---|
| 731 | n/a | 0, /* tp_iterext */ |
|---|
| 732 | n/a | multibytecodec_methods, /* tp_methods */ |
|---|
| 733 | n/a | }; |
|---|
| 734 | n/a | |
|---|
| 735 | n/a | |
|---|
| 736 | n/a | /** |
|---|
| 737 | n/a | * Utility functions for stateful codec mechanism |
|---|
| 738 | n/a | */ |
|---|
| 739 | n/a | |
|---|
| 740 | n/a | #define STATEFUL_DCTX(o) ((MultibyteStatefulDecoderContext *)(o)) |
|---|
| 741 | n/a | #define STATEFUL_ECTX(o) ((MultibyteStatefulEncoderContext *)(o)) |
|---|
| 742 | n/a | |
|---|
| 743 | n/a | static PyObject * |
|---|
| 744 | n/a | encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx, |
|---|
| 745 | n/a | PyObject *unistr, int final) |
|---|
| 746 | n/a | { |
|---|
| 747 | n/a | PyObject *ucvt, *r = NULL; |
|---|
| 748 | n/a | PyObject *inbuf = NULL; |
|---|
| 749 | n/a | Py_ssize_t inpos, datalen; |
|---|
| 750 | n/a | PyObject *origpending = NULL; |
|---|
| 751 | n/a | |
|---|
| 752 | n/a | if (PyUnicode_Check(unistr)) |
|---|
| 753 | n/a | ucvt = NULL; |
|---|
| 754 | n/a | else { |
|---|
| 755 | n/a | unistr = ucvt = PyObject_Str(unistr); |
|---|
| 756 | n/a | if (unistr == NULL) |
|---|
| 757 | n/a | return NULL; |
|---|
| 758 | n/a | else if (!PyUnicode_Check(unistr)) { |
|---|
| 759 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 760 | n/a | "couldn't convert the object to str."); |
|---|
| 761 | n/a | Py_DECREF(ucvt); |
|---|
| 762 | n/a | return NULL; |
|---|
| 763 | n/a | } |
|---|
| 764 | n/a | } |
|---|
| 765 | n/a | |
|---|
| 766 | n/a | if (ctx->pending) { |
|---|
| 767 | n/a | PyObject *inbuf_tmp; |
|---|
| 768 | n/a | |
|---|
| 769 | n/a | Py_INCREF(ctx->pending); |
|---|
| 770 | n/a | origpending = ctx->pending; |
|---|
| 771 | n/a | |
|---|
| 772 | n/a | Py_INCREF(ctx->pending); |
|---|
| 773 | n/a | inbuf_tmp = ctx->pending; |
|---|
| 774 | n/a | PyUnicode_Append(&inbuf_tmp, unistr); |
|---|
| 775 | n/a | if (inbuf_tmp == NULL) |
|---|
| 776 | n/a | goto errorexit; |
|---|
| 777 | n/a | Py_CLEAR(ctx->pending); |
|---|
| 778 | n/a | inbuf = inbuf_tmp; |
|---|
| 779 | n/a | } |
|---|
| 780 | n/a | else { |
|---|
| 781 | n/a | origpending = NULL; |
|---|
| 782 | n/a | |
|---|
| 783 | n/a | Py_INCREF(unistr); |
|---|
| 784 | n/a | inbuf = unistr; |
|---|
| 785 | n/a | } |
|---|
| 786 | n/a | if (PyUnicode_READY(inbuf) < 0) |
|---|
| 787 | n/a | goto errorexit; |
|---|
| 788 | n/a | inpos = 0; |
|---|
| 789 | n/a | datalen = PyUnicode_GET_LENGTH(inbuf); |
|---|
| 790 | n/a | |
|---|
| 791 | n/a | r = multibytecodec_encode(ctx->codec, &ctx->state, |
|---|
| 792 | n/a | inbuf, &inpos, |
|---|
| 793 | n/a | ctx->errors, final ? MBENC_FLUSH | MBENC_RESET : 0); |
|---|
| 794 | n/a | if (r == NULL) { |
|---|
| 795 | n/a | /* recover the original pending buffer */ |
|---|
| 796 | n/a | Py_XSETREF(ctx->pending, origpending); |
|---|
| 797 | n/a | origpending = NULL; |
|---|
| 798 | n/a | goto errorexit; |
|---|
| 799 | n/a | } |
|---|
| 800 | n/a | Py_XDECREF(origpending); |
|---|
| 801 | n/a | |
|---|
| 802 | n/a | if (inpos < datalen) { |
|---|
| 803 | n/a | if (datalen - inpos > MAXENCPENDING) { |
|---|
| 804 | n/a | /* normal codecs can't reach here */ |
|---|
| 805 | n/a | PyErr_SetString(PyExc_UnicodeError, |
|---|
| 806 | n/a | "pending buffer overflow"); |
|---|
| 807 | n/a | goto errorexit; |
|---|
| 808 | n/a | } |
|---|
| 809 | n/a | ctx->pending = PyUnicode_Substring(inbuf, inpos, datalen); |
|---|
| 810 | n/a | if (ctx->pending == NULL) { |
|---|
| 811 | n/a | /* normal codecs can't reach here */ |
|---|
| 812 | n/a | goto errorexit; |
|---|
| 813 | n/a | } |
|---|
| 814 | n/a | } |
|---|
| 815 | n/a | |
|---|
| 816 | n/a | Py_DECREF(inbuf); |
|---|
| 817 | n/a | Py_XDECREF(ucvt); |
|---|
| 818 | n/a | return r; |
|---|
| 819 | n/a | |
|---|
| 820 | n/a | errorexit: |
|---|
| 821 | n/a | Py_XDECREF(r); |
|---|
| 822 | n/a | Py_XDECREF(ucvt); |
|---|
| 823 | n/a | Py_XDECREF(origpending); |
|---|
| 824 | n/a | Py_XDECREF(inbuf); |
|---|
| 825 | n/a | return NULL; |
|---|
| 826 | n/a | } |
|---|
| 827 | n/a | |
|---|
| 828 | n/a | static int |
|---|
| 829 | n/a | decoder_append_pending(MultibyteStatefulDecoderContext *ctx, |
|---|
| 830 | n/a | MultibyteDecodeBuffer *buf) |
|---|
| 831 | n/a | { |
|---|
| 832 | n/a | Py_ssize_t npendings; |
|---|
| 833 | n/a | |
|---|
| 834 | n/a | npendings = (Py_ssize_t)(buf->inbuf_end - buf->inbuf); |
|---|
| 835 | n/a | if (npendings + ctx->pendingsize > MAXDECPENDING || |
|---|
| 836 | n/a | npendings > PY_SSIZE_T_MAX - ctx->pendingsize) { |
|---|
| 837 | n/a | PyErr_SetString(PyExc_UnicodeError, "pending buffer overflow"); |
|---|
| 838 | n/a | return -1; |
|---|
| 839 | n/a | } |
|---|
| 840 | n/a | memcpy(ctx->pending + ctx->pendingsize, buf->inbuf, npendings); |
|---|
| 841 | n/a | ctx->pendingsize += npendings; |
|---|
| 842 | n/a | return 0; |
|---|
| 843 | n/a | } |
|---|
| 844 | n/a | |
|---|
| 845 | n/a | static int |
|---|
| 846 | n/a | decoder_prepare_buffer(MultibyteDecodeBuffer *buf, const char *data, |
|---|
| 847 | n/a | Py_ssize_t size) |
|---|
| 848 | n/a | { |
|---|
| 849 | n/a | buf->inbuf = buf->inbuf_top = (const unsigned char *)data; |
|---|
| 850 | n/a | buf->inbuf_end = buf->inbuf_top + size; |
|---|
| 851 | n/a | buf->writer.min_length += size; |
|---|
| 852 | n/a | return 0; |
|---|
| 853 | n/a | } |
|---|
| 854 | n/a | |
|---|
| 855 | n/a | static int |
|---|
| 856 | n/a | decoder_feed_buffer(MultibyteStatefulDecoderContext *ctx, |
|---|
| 857 | n/a | MultibyteDecodeBuffer *buf) |
|---|
| 858 | n/a | { |
|---|
| 859 | n/a | while (buf->inbuf < buf->inbuf_end) { |
|---|
| 860 | n/a | Py_ssize_t inleft; |
|---|
| 861 | n/a | Py_ssize_t r; |
|---|
| 862 | n/a | |
|---|
| 863 | n/a | inleft = (Py_ssize_t)(buf->inbuf_end - buf->inbuf); |
|---|
| 864 | n/a | |
|---|
| 865 | n/a | r = ctx->codec->decode(&ctx->state, ctx->codec->config, |
|---|
| 866 | n/a | &buf->inbuf, inleft, &buf->writer); |
|---|
| 867 | n/a | if (r == 0 || r == MBERR_TOOFEW) |
|---|
| 868 | n/a | break; |
|---|
| 869 | n/a | else if (multibytecodec_decerror(ctx->codec, &ctx->state, |
|---|
| 870 | n/a | buf, ctx->errors, r)) |
|---|
| 871 | n/a | return -1; |
|---|
| 872 | n/a | } |
|---|
| 873 | n/a | return 0; |
|---|
| 874 | n/a | } |
|---|
| 875 | n/a | |
|---|
| 876 | n/a | |
|---|
| 877 | n/a | /*[clinic input] |
|---|
| 878 | n/a | class _multibytecodec.MultibyteIncrementalEncoder "MultibyteIncrementalEncoderObject *" "&MultibyteIncrementalEncoder_Type" |
|---|
| 879 | n/a | [clinic start generated code]*/ |
|---|
| 880 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3be82909cd08924d]*/ |
|---|
| 881 | n/a | |
|---|
| 882 | n/a | /*[clinic input] |
|---|
| 883 | n/a | _multibytecodec.MultibyteIncrementalEncoder.encode |
|---|
| 884 | n/a | |
|---|
| 885 | n/a | input: object |
|---|
| 886 | n/a | final: int(c_default="0") = False |
|---|
| 887 | n/a | [clinic start generated code]*/ |
|---|
| 888 | n/a | |
|---|
| 889 | n/a | static PyObject * |
|---|
| 890 | n/a | _multibytecodec_MultibyteIncrementalEncoder_encode_impl(MultibyteIncrementalEncoderObject *self, |
|---|
| 891 | n/a | PyObject *input, |
|---|
| 892 | n/a | int final) |
|---|
| 893 | n/a | /*[clinic end generated code: output=123361b6c505e2c1 input=a345c688fa664f92]*/ |
|---|
| 894 | n/a | { |
|---|
| 895 | n/a | return encoder_encode_stateful(STATEFUL_ECTX(self), input, final); |
|---|
| 896 | n/a | } |
|---|
| 897 | n/a | |
|---|
| 898 | n/a | /*[clinic input] |
|---|
| 899 | n/a | _multibytecodec.MultibyteIncrementalEncoder.reset |
|---|
| 900 | n/a | [clinic start generated code]*/ |
|---|
| 901 | n/a | |
|---|
| 902 | n/a | static PyObject * |
|---|
| 903 | n/a | _multibytecodec_MultibyteIncrementalEncoder_reset_impl(MultibyteIncrementalEncoderObject *self) |
|---|
| 904 | n/a | /*[clinic end generated code: output=b4125d8f537a253f input=930f06760707b6ea]*/ |
|---|
| 905 | n/a | { |
|---|
| 906 | n/a | /* Longest output: 4 bytes (b'\x0F\x1F(B') with ISO 2022 */ |
|---|
| 907 | n/a | unsigned char buffer[4], *outbuf; |
|---|
| 908 | n/a | Py_ssize_t r; |
|---|
| 909 | n/a | if (self->codec->encreset != NULL) { |
|---|
| 910 | n/a | outbuf = buffer; |
|---|
| 911 | n/a | r = self->codec->encreset(&self->state, self->codec->config, |
|---|
| 912 | n/a | &outbuf, sizeof(buffer)); |
|---|
| 913 | n/a | if (r != 0) |
|---|
| 914 | n/a | return NULL; |
|---|
| 915 | n/a | } |
|---|
| 916 | n/a | Py_CLEAR(self->pending); |
|---|
| 917 | n/a | Py_RETURN_NONE; |
|---|
| 918 | n/a | } |
|---|
| 919 | n/a | |
|---|
| 920 | n/a | static struct PyMethodDef mbiencoder_methods[] = { |
|---|
| 921 | n/a | _MULTIBYTECODEC_MULTIBYTEINCREMENTALENCODER_ENCODE_METHODDEF |
|---|
| 922 | n/a | _MULTIBYTECODEC_MULTIBYTEINCREMENTALENCODER_RESET_METHODDEF |
|---|
| 923 | n/a | {NULL, NULL}, |
|---|
| 924 | n/a | }; |
|---|
| 925 | n/a | |
|---|
| 926 | n/a | static PyObject * |
|---|
| 927 | n/a | mbiencoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 928 | n/a | { |
|---|
| 929 | n/a | MultibyteIncrementalEncoderObject *self; |
|---|
| 930 | n/a | PyObject *codec = NULL; |
|---|
| 931 | n/a | char *errors = NULL; |
|---|
| 932 | n/a | |
|---|
| 933 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalEncoder", |
|---|
| 934 | n/a | incnewkwarglist, &errors)) |
|---|
| 935 | n/a | return NULL; |
|---|
| 936 | n/a | |
|---|
| 937 | n/a | self = (MultibyteIncrementalEncoderObject *)type->tp_alloc(type, 0); |
|---|
| 938 | n/a | if (self == NULL) |
|---|
| 939 | n/a | return NULL; |
|---|
| 940 | n/a | |
|---|
| 941 | n/a | codec = PyObject_GetAttrString((PyObject *)type, "codec"); |
|---|
| 942 | n/a | if (codec == NULL) |
|---|
| 943 | n/a | goto errorexit; |
|---|
| 944 | n/a | if (!MultibyteCodec_Check(codec)) { |
|---|
| 945 | n/a | PyErr_SetString(PyExc_TypeError, "codec is unexpected type"); |
|---|
| 946 | n/a | goto errorexit; |
|---|
| 947 | n/a | } |
|---|
| 948 | n/a | |
|---|
| 949 | n/a | self->codec = ((MultibyteCodecObject *)codec)->codec; |
|---|
| 950 | n/a | self->pending = NULL; |
|---|
| 951 | n/a | self->errors = internal_error_callback(errors); |
|---|
| 952 | n/a | if (self->errors == NULL) |
|---|
| 953 | n/a | goto errorexit; |
|---|
| 954 | n/a | if (self->codec->encinit != NULL && |
|---|
| 955 | n/a | self->codec->encinit(&self->state, self->codec->config) != 0) |
|---|
| 956 | n/a | goto errorexit; |
|---|
| 957 | n/a | |
|---|
| 958 | n/a | Py_DECREF(codec); |
|---|
| 959 | n/a | return (PyObject *)self; |
|---|
| 960 | n/a | |
|---|
| 961 | n/a | errorexit: |
|---|
| 962 | n/a | Py_XDECREF(self); |
|---|
| 963 | n/a | Py_XDECREF(codec); |
|---|
| 964 | n/a | return NULL; |
|---|
| 965 | n/a | } |
|---|
| 966 | n/a | |
|---|
| 967 | n/a | static int |
|---|
| 968 | n/a | mbiencoder_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 969 | n/a | { |
|---|
| 970 | n/a | return 0; |
|---|
| 971 | n/a | } |
|---|
| 972 | n/a | |
|---|
| 973 | n/a | static int |
|---|
| 974 | n/a | mbiencoder_traverse(MultibyteIncrementalEncoderObject *self, |
|---|
| 975 | n/a | visitproc visit, void *arg) |
|---|
| 976 | n/a | { |
|---|
| 977 | n/a | if (ERROR_ISCUSTOM(self->errors)) |
|---|
| 978 | n/a | Py_VISIT(self->errors); |
|---|
| 979 | n/a | return 0; |
|---|
| 980 | n/a | } |
|---|
| 981 | n/a | |
|---|
| 982 | n/a | static void |
|---|
| 983 | n/a | mbiencoder_dealloc(MultibyteIncrementalEncoderObject *self) |
|---|
| 984 | n/a | { |
|---|
| 985 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 986 | n/a | ERROR_DECREF(self->errors); |
|---|
| 987 | n/a | Py_TYPE(self)->tp_free(self); |
|---|
| 988 | n/a | } |
|---|
| 989 | n/a | |
|---|
| 990 | n/a | static PyTypeObject MultibyteIncrementalEncoder_Type = { |
|---|
| 991 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 992 | n/a | "MultibyteIncrementalEncoder", /* tp_name */ |
|---|
| 993 | n/a | sizeof(MultibyteIncrementalEncoderObject), /* tp_basicsize */ |
|---|
| 994 | n/a | 0, /* tp_itemsize */ |
|---|
| 995 | n/a | /* methods */ |
|---|
| 996 | n/a | (destructor)mbiencoder_dealloc, /* tp_dealloc */ |
|---|
| 997 | n/a | 0, /* tp_print */ |
|---|
| 998 | n/a | 0, /* tp_getattr */ |
|---|
| 999 | n/a | 0, /* tp_setattr */ |
|---|
| 1000 | n/a | 0, /* tp_reserved */ |
|---|
| 1001 | n/a | 0, /* tp_repr */ |
|---|
| 1002 | n/a | 0, /* tp_as_number */ |
|---|
| 1003 | n/a | 0, /* tp_as_sequence */ |
|---|
| 1004 | n/a | 0, /* tp_as_mapping */ |
|---|
| 1005 | n/a | 0, /* tp_hash */ |
|---|
| 1006 | n/a | 0, /* tp_call */ |
|---|
| 1007 | n/a | 0, /* tp_str */ |
|---|
| 1008 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 1009 | n/a | 0, /* tp_setattro */ |
|---|
| 1010 | n/a | 0, /* tp_as_buffer */ |
|---|
| 1011 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|---|
| 1012 | n/a | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 1013 | n/a | 0, /* tp_doc */ |
|---|
| 1014 | n/a | (traverseproc)mbiencoder_traverse, /* tp_traverse */ |
|---|
| 1015 | n/a | 0, /* tp_clear */ |
|---|
| 1016 | n/a | 0, /* tp_richcompare */ |
|---|
| 1017 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 1018 | n/a | 0, /* tp_iter */ |
|---|
| 1019 | n/a | 0, /* tp_iterext */ |
|---|
| 1020 | n/a | mbiencoder_methods, /* tp_methods */ |
|---|
| 1021 | n/a | 0, /* tp_members */ |
|---|
| 1022 | n/a | codecctx_getsets, /* tp_getset */ |
|---|
| 1023 | n/a | 0, /* tp_base */ |
|---|
| 1024 | n/a | 0, /* tp_dict */ |
|---|
| 1025 | n/a | 0, /* tp_descr_get */ |
|---|
| 1026 | n/a | 0, /* tp_descr_set */ |
|---|
| 1027 | n/a | 0, /* tp_dictoffset */ |
|---|
| 1028 | n/a | mbiencoder_init, /* tp_init */ |
|---|
| 1029 | n/a | 0, /* tp_alloc */ |
|---|
| 1030 | n/a | mbiencoder_new, /* tp_new */ |
|---|
| 1031 | n/a | }; |
|---|
| 1032 | n/a | |
|---|
| 1033 | n/a | |
|---|
| 1034 | n/a | /*[clinic input] |
|---|
| 1035 | n/a | class _multibytecodec.MultibyteIncrementalDecoder "MultibyteIncrementalDecoderObject *" "&MultibyteIncrementalDecoder_Type" |
|---|
| 1036 | n/a | [clinic start generated code]*/ |
|---|
| 1037 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f6003faaf2cea692]*/ |
|---|
| 1038 | n/a | |
|---|
| 1039 | n/a | /*[clinic input] |
|---|
| 1040 | n/a | _multibytecodec.MultibyteIncrementalDecoder.decode |
|---|
| 1041 | n/a | |
|---|
| 1042 | n/a | input: Py_buffer |
|---|
| 1043 | n/a | final: int(c_default="0") = False |
|---|
| 1044 | n/a | [clinic start generated code]*/ |
|---|
| 1045 | n/a | |
|---|
| 1046 | n/a | static PyObject * |
|---|
| 1047 | n/a | _multibytecodec_MultibyteIncrementalDecoder_decode_impl(MultibyteIncrementalDecoderObject *self, |
|---|
| 1048 | n/a | Py_buffer *input, |
|---|
| 1049 | n/a | int final) |
|---|
| 1050 | n/a | /*[clinic end generated code: output=b9b9090e8a9ce2ba input=576631c61906d39d]*/ |
|---|
| 1051 | n/a | { |
|---|
| 1052 | n/a | MultibyteDecodeBuffer buf; |
|---|
| 1053 | n/a | char *data, *wdata = NULL; |
|---|
| 1054 | n/a | Py_ssize_t wsize, size, origpending; |
|---|
| 1055 | n/a | PyObject *res; |
|---|
| 1056 | n/a | |
|---|
| 1057 | n/a | data = input->buf; |
|---|
| 1058 | n/a | size = input->len; |
|---|
| 1059 | n/a | |
|---|
| 1060 | n/a | _PyUnicodeWriter_Init(&buf.writer); |
|---|
| 1061 | n/a | buf.excobj = NULL; |
|---|
| 1062 | n/a | origpending = self->pendingsize; |
|---|
| 1063 | n/a | |
|---|
| 1064 | n/a | if (self->pendingsize == 0) { |
|---|
| 1065 | n/a | wsize = size; |
|---|
| 1066 | n/a | wdata = data; |
|---|
| 1067 | n/a | } |
|---|
| 1068 | n/a | else { |
|---|
| 1069 | n/a | if (size > PY_SSIZE_T_MAX - self->pendingsize) { |
|---|
| 1070 | n/a | PyErr_NoMemory(); |
|---|
| 1071 | n/a | goto errorexit; |
|---|
| 1072 | n/a | } |
|---|
| 1073 | n/a | wsize = size + self->pendingsize; |
|---|
| 1074 | n/a | wdata = PyMem_Malloc(wsize); |
|---|
| 1075 | n/a | if (wdata == NULL) { |
|---|
| 1076 | n/a | PyErr_NoMemory(); |
|---|
| 1077 | n/a | goto errorexit; |
|---|
| 1078 | n/a | } |
|---|
| 1079 | n/a | memcpy(wdata, self->pending, self->pendingsize); |
|---|
| 1080 | n/a | memcpy(wdata + self->pendingsize, data, size); |
|---|
| 1081 | n/a | self->pendingsize = 0; |
|---|
| 1082 | n/a | } |
|---|
| 1083 | n/a | |
|---|
| 1084 | n/a | if (decoder_prepare_buffer(&buf, wdata, wsize) != 0) |
|---|
| 1085 | n/a | goto errorexit; |
|---|
| 1086 | n/a | |
|---|
| 1087 | n/a | if (decoder_feed_buffer(STATEFUL_DCTX(self), &buf)) |
|---|
| 1088 | n/a | goto errorexit; |
|---|
| 1089 | n/a | |
|---|
| 1090 | n/a | if (final && buf.inbuf < buf.inbuf_end) { |
|---|
| 1091 | n/a | if (multibytecodec_decerror(self->codec, &self->state, |
|---|
| 1092 | n/a | &buf, self->errors, MBERR_TOOFEW)) { |
|---|
| 1093 | n/a | /* recover the original pending buffer */ |
|---|
| 1094 | n/a | memcpy(self->pending, wdata, origpending); |
|---|
| 1095 | n/a | self->pendingsize = origpending; |
|---|
| 1096 | n/a | goto errorexit; |
|---|
| 1097 | n/a | } |
|---|
| 1098 | n/a | } |
|---|
| 1099 | n/a | |
|---|
| 1100 | n/a | if (buf.inbuf < buf.inbuf_end) { /* pending sequence still exists */ |
|---|
| 1101 | n/a | if (decoder_append_pending(STATEFUL_DCTX(self), &buf) != 0) |
|---|
| 1102 | n/a | goto errorexit; |
|---|
| 1103 | n/a | } |
|---|
| 1104 | n/a | |
|---|
| 1105 | n/a | res = _PyUnicodeWriter_Finish(&buf.writer); |
|---|
| 1106 | n/a | if (res == NULL) |
|---|
| 1107 | n/a | goto errorexit; |
|---|
| 1108 | n/a | |
|---|
| 1109 | n/a | if (wdata != data) |
|---|
| 1110 | n/a | PyMem_Del(wdata); |
|---|
| 1111 | n/a | Py_XDECREF(buf.excobj); |
|---|
| 1112 | n/a | return res; |
|---|
| 1113 | n/a | |
|---|
| 1114 | n/a | errorexit: |
|---|
| 1115 | n/a | if (wdata != NULL && wdata != data) |
|---|
| 1116 | n/a | PyMem_Del(wdata); |
|---|
| 1117 | n/a | Py_XDECREF(buf.excobj); |
|---|
| 1118 | n/a | _PyUnicodeWriter_Dealloc(&buf.writer); |
|---|
| 1119 | n/a | return NULL; |
|---|
| 1120 | n/a | } |
|---|
| 1121 | n/a | |
|---|
| 1122 | n/a | /*[clinic input] |
|---|
| 1123 | n/a | _multibytecodec.MultibyteIncrementalDecoder.reset |
|---|
| 1124 | n/a | [clinic start generated code]*/ |
|---|
| 1125 | n/a | |
|---|
| 1126 | n/a | static PyObject * |
|---|
| 1127 | n/a | _multibytecodec_MultibyteIncrementalDecoder_reset_impl(MultibyteIncrementalDecoderObject *self) |
|---|
| 1128 | n/a | /*[clinic end generated code: output=da423b1782c23ed1 input=3b63b3be85b2fb45]*/ |
|---|
| 1129 | n/a | { |
|---|
| 1130 | n/a | if (self->codec->decreset != NULL && |
|---|
| 1131 | n/a | self->codec->decreset(&self->state, self->codec->config) != 0) |
|---|
| 1132 | n/a | return NULL; |
|---|
| 1133 | n/a | self->pendingsize = 0; |
|---|
| 1134 | n/a | |
|---|
| 1135 | n/a | Py_RETURN_NONE; |
|---|
| 1136 | n/a | } |
|---|
| 1137 | n/a | |
|---|
| 1138 | n/a | static struct PyMethodDef mbidecoder_methods[] = { |
|---|
| 1139 | n/a | _MULTIBYTECODEC_MULTIBYTEINCREMENTALDECODER_DECODE_METHODDEF |
|---|
| 1140 | n/a | _MULTIBYTECODEC_MULTIBYTEINCREMENTALDECODER_RESET_METHODDEF |
|---|
| 1141 | n/a | {NULL, NULL}, |
|---|
| 1142 | n/a | }; |
|---|
| 1143 | n/a | |
|---|
| 1144 | n/a | static PyObject * |
|---|
| 1145 | n/a | mbidecoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 1146 | n/a | { |
|---|
| 1147 | n/a | MultibyteIncrementalDecoderObject *self; |
|---|
| 1148 | n/a | PyObject *codec = NULL; |
|---|
| 1149 | n/a | char *errors = NULL; |
|---|
| 1150 | n/a | |
|---|
| 1151 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s:IncrementalDecoder", |
|---|
| 1152 | n/a | incnewkwarglist, &errors)) |
|---|
| 1153 | n/a | return NULL; |
|---|
| 1154 | n/a | |
|---|
| 1155 | n/a | self = (MultibyteIncrementalDecoderObject *)type->tp_alloc(type, 0); |
|---|
| 1156 | n/a | if (self == NULL) |
|---|
| 1157 | n/a | return NULL; |
|---|
| 1158 | n/a | |
|---|
| 1159 | n/a | codec = PyObject_GetAttrString((PyObject *)type, "codec"); |
|---|
| 1160 | n/a | if (codec == NULL) |
|---|
| 1161 | n/a | goto errorexit; |
|---|
| 1162 | n/a | if (!MultibyteCodec_Check(codec)) { |
|---|
| 1163 | n/a | PyErr_SetString(PyExc_TypeError, "codec is unexpected type"); |
|---|
| 1164 | n/a | goto errorexit; |
|---|
| 1165 | n/a | } |
|---|
| 1166 | n/a | |
|---|
| 1167 | n/a | self->codec = ((MultibyteCodecObject *)codec)->codec; |
|---|
| 1168 | n/a | self->pendingsize = 0; |
|---|
| 1169 | n/a | self->errors = internal_error_callback(errors); |
|---|
| 1170 | n/a | if (self->errors == NULL) |
|---|
| 1171 | n/a | goto errorexit; |
|---|
| 1172 | n/a | if (self->codec->decinit != NULL && |
|---|
| 1173 | n/a | self->codec->decinit(&self->state, self->codec->config) != 0) |
|---|
| 1174 | n/a | goto errorexit; |
|---|
| 1175 | n/a | |
|---|
| 1176 | n/a | Py_DECREF(codec); |
|---|
| 1177 | n/a | return (PyObject *)self; |
|---|
| 1178 | n/a | |
|---|
| 1179 | n/a | errorexit: |
|---|
| 1180 | n/a | Py_XDECREF(self); |
|---|
| 1181 | n/a | Py_XDECREF(codec); |
|---|
| 1182 | n/a | return NULL; |
|---|
| 1183 | n/a | } |
|---|
| 1184 | n/a | |
|---|
| 1185 | n/a | static int |
|---|
| 1186 | n/a | mbidecoder_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 1187 | n/a | { |
|---|
| 1188 | n/a | return 0; |
|---|
| 1189 | n/a | } |
|---|
| 1190 | n/a | |
|---|
| 1191 | n/a | static int |
|---|
| 1192 | n/a | mbidecoder_traverse(MultibyteIncrementalDecoderObject *self, |
|---|
| 1193 | n/a | visitproc visit, void *arg) |
|---|
| 1194 | n/a | { |
|---|
| 1195 | n/a | if (ERROR_ISCUSTOM(self->errors)) |
|---|
| 1196 | n/a | Py_VISIT(self->errors); |
|---|
| 1197 | n/a | return 0; |
|---|
| 1198 | n/a | } |
|---|
| 1199 | n/a | |
|---|
| 1200 | n/a | static void |
|---|
| 1201 | n/a | mbidecoder_dealloc(MultibyteIncrementalDecoderObject *self) |
|---|
| 1202 | n/a | { |
|---|
| 1203 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 1204 | n/a | ERROR_DECREF(self->errors); |
|---|
| 1205 | n/a | Py_TYPE(self)->tp_free(self); |
|---|
| 1206 | n/a | } |
|---|
| 1207 | n/a | |
|---|
| 1208 | n/a | static PyTypeObject MultibyteIncrementalDecoder_Type = { |
|---|
| 1209 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 1210 | n/a | "MultibyteIncrementalDecoder", /* tp_name */ |
|---|
| 1211 | n/a | sizeof(MultibyteIncrementalDecoderObject), /* tp_basicsize */ |
|---|
| 1212 | n/a | 0, /* tp_itemsize */ |
|---|
| 1213 | n/a | /* methods */ |
|---|
| 1214 | n/a | (destructor)mbidecoder_dealloc, /* tp_dealloc */ |
|---|
| 1215 | n/a | 0, /* tp_print */ |
|---|
| 1216 | n/a | 0, /* tp_getattr */ |
|---|
| 1217 | n/a | 0, /* tp_setattr */ |
|---|
| 1218 | n/a | 0, /* tp_reserved */ |
|---|
| 1219 | n/a | 0, /* tp_repr */ |
|---|
| 1220 | n/a | 0, /* tp_as_number */ |
|---|
| 1221 | n/a | 0, /* tp_as_sequence */ |
|---|
| 1222 | n/a | 0, /* tp_as_mapping */ |
|---|
| 1223 | n/a | 0, /* tp_hash */ |
|---|
| 1224 | n/a | 0, /* tp_call */ |
|---|
| 1225 | n/a | 0, /* tp_str */ |
|---|
| 1226 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 1227 | n/a | 0, /* tp_setattro */ |
|---|
| 1228 | n/a | 0, /* tp_as_buffer */ |
|---|
| 1229 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|---|
| 1230 | n/a | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 1231 | n/a | 0, /* tp_doc */ |
|---|
| 1232 | n/a | (traverseproc)mbidecoder_traverse, /* tp_traverse */ |
|---|
| 1233 | n/a | 0, /* tp_clear */ |
|---|
| 1234 | n/a | 0, /* tp_richcompare */ |
|---|
| 1235 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 1236 | n/a | 0, /* tp_iter */ |
|---|
| 1237 | n/a | 0, /* tp_iterext */ |
|---|
| 1238 | n/a | mbidecoder_methods, /* tp_methods */ |
|---|
| 1239 | n/a | 0, /* tp_members */ |
|---|
| 1240 | n/a | codecctx_getsets, /* tp_getset */ |
|---|
| 1241 | n/a | 0, /* tp_base */ |
|---|
| 1242 | n/a | 0, /* tp_dict */ |
|---|
| 1243 | n/a | 0, /* tp_descr_get */ |
|---|
| 1244 | n/a | 0, /* tp_descr_set */ |
|---|
| 1245 | n/a | 0, /* tp_dictoffset */ |
|---|
| 1246 | n/a | mbidecoder_init, /* tp_init */ |
|---|
| 1247 | n/a | 0, /* tp_alloc */ |
|---|
| 1248 | n/a | mbidecoder_new, /* tp_new */ |
|---|
| 1249 | n/a | }; |
|---|
| 1250 | n/a | |
|---|
| 1251 | n/a | |
|---|
| 1252 | n/a | /*[clinic input] |
|---|
| 1253 | n/a | class _multibytecodec.MultibyteStreamReader "MultibyteStreamReaderObject *" "MultibyteStreamReader_Type" |
|---|
| 1254 | n/a | [clinic start generated code]*/ |
|---|
| 1255 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d323634b74976f09]*/ |
|---|
| 1256 | n/a | |
|---|
| 1257 | n/a | static PyObject * |
|---|
| 1258 | n/a | mbstreamreader_iread(MultibyteStreamReaderObject *self, |
|---|
| 1259 | n/a | const char *method, Py_ssize_t sizehint) |
|---|
| 1260 | n/a | { |
|---|
| 1261 | n/a | MultibyteDecodeBuffer buf; |
|---|
| 1262 | n/a | PyObject *cres, *res; |
|---|
| 1263 | n/a | Py_ssize_t rsize; |
|---|
| 1264 | n/a | |
|---|
| 1265 | n/a | if (sizehint == 0) |
|---|
| 1266 | n/a | return PyUnicode_New(0, 0); |
|---|
| 1267 | n/a | |
|---|
| 1268 | n/a | _PyUnicodeWriter_Init(&buf.writer); |
|---|
| 1269 | n/a | buf.excobj = NULL; |
|---|
| 1270 | n/a | cres = NULL; |
|---|
| 1271 | n/a | |
|---|
| 1272 | n/a | for (;;) { |
|---|
| 1273 | n/a | int endoffile; |
|---|
| 1274 | n/a | |
|---|
| 1275 | n/a | if (sizehint < 0) |
|---|
| 1276 | n/a | cres = PyObject_CallMethod(self->stream, |
|---|
| 1277 | n/a | method, NULL); |
|---|
| 1278 | n/a | else |
|---|
| 1279 | n/a | cres = PyObject_CallMethod(self->stream, |
|---|
| 1280 | n/a | method, "i", sizehint); |
|---|
| 1281 | n/a | if (cres == NULL) |
|---|
| 1282 | n/a | goto errorexit; |
|---|
| 1283 | n/a | |
|---|
| 1284 | n/a | if (!PyBytes_Check(cres)) { |
|---|
| 1285 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1286 | n/a | "stream function returned a " |
|---|
| 1287 | n/a | "non-bytes object (%.100s)", |
|---|
| 1288 | n/a | cres->ob_type->tp_name); |
|---|
| 1289 | n/a | goto errorexit; |
|---|
| 1290 | n/a | } |
|---|
| 1291 | n/a | |
|---|
| 1292 | n/a | endoffile = (PyBytes_GET_SIZE(cres) == 0); |
|---|
| 1293 | n/a | |
|---|
| 1294 | n/a | if (self->pendingsize > 0) { |
|---|
| 1295 | n/a | PyObject *ctr; |
|---|
| 1296 | n/a | char *ctrdata; |
|---|
| 1297 | n/a | |
|---|
| 1298 | n/a | if (PyBytes_GET_SIZE(cres) > PY_SSIZE_T_MAX - self->pendingsize) { |
|---|
| 1299 | n/a | PyErr_NoMemory(); |
|---|
| 1300 | n/a | goto errorexit; |
|---|
| 1301 | n/a | } |
|---|
| 1302 | n/a | rsize = PyBytes_GET_SIZE(cres) + self->pendingsize; |
|---|
| 1303 | n/a | ctr = PyBytes_FromStringAndSize(NULL, rsize); |
|---|
| 1304 | n/a | if (ctr == NULL) |
|---|
| 1305 | n/a | goto errorexit; |
|---|
| 1306 | n/a | ctrdata = PyBytes_AS_STRING(ctr); |
|---|
| 1307 | n/a | memcpy(ctrdata, self->pending, self->pendingsize); |
|---|
| 1308 | n/a | memcpy(ctrdata + self->pendingsize, |
|---|
| 1309 | n/a | PyBytes_AS_STRING(cres), |
|---|
| 1310 | n/a | PyBytes_GET_SIZE(cres)); |
|---|
| 1311 | n/a | Py_DECREF(cres); |
|---|
| 1312 | n/a | cres = ctr; |
|---|
| 1313 | n/a | self->pendingsize = 0; |
|---|
| 1314 | n/a | } |
|---|
| 1315 | n/a | |
|---|
| 1316 | n/a | rsize = PyBytes_GET_SIZE(cres); |
|---|
| 1317 | n/a | if (decoder_prepare_buffer(&buf, PyBytes_AS_STRING(cres), |
|---|
| 1318 | n/a | rsize) != 0) |
|---|
| 1319 | n/a | goto errorexit; |
|---|
| 1320 | n/a | |
|---|
| 1321 | n/a | if (rsize > 0 && decoder_feed_buffer( |
|---|
| 1322 | n/a | (MultibyteStatefulDecoderContext *)self, &buf)) |
|---|
| 1323 | n/a | goto errorexit; |
|---|
| 1324 | n/a | |
|---|
| 1325 | n/a | if (endoffile || sizehint < 0) { |
|---|
| 1326 | n/a | if (buf.inbuf < buf.inbuf_end && |
|---|
| 1327 | n/a | multibytecodec_decerror(self->codec, &self->state, |
|---|
| 1328 | n/a | &buf, self->errors, MBERR_TOOFEW)) |
|---|
| 1329 | n/a | goto errorexit; |
|---|
| 1330 | n/a | } |
|---|
| 1331 | n/a | |
|---|
| 1332 | n/a | if (buf.inbuf < buf.inbuf_end) { /* pending sequence exists */ |
|---|
| 1333 | n/a | if (decoder_append_pending(STATEFUL_DCTX(self), |
|---|
| 1334 | n/a | &buf) != 0) |
|---|
| 1335 | n/a | goto errorexit; |
|---|
| 1336 | n/a | } |
|---|
| 1337 | n/a | |
|---|
| 1338 | n/a | Py_DECREF(cres); |
|---|
| 1339 | n/a | cres = NULL; |
|---|
| 1340 | n/a | |
|---|
| 1341 | n/a | if (sizehint < 0 || buf.writer.pos != 0 || rsize == 0) |
|---|
| 1342 | n/a | break; |
|---|
| 1343 | n/a | |
|---|
| 1344 | n/a | sizehint = 1; /* read 1 more byte and retry */ |
|---|
| 1345 | n/a | } |
|---|
| 1346 | n/a | |
|---|
| 1347 | n/a | res = _PyUnicodeWriter_Finish(&buf.writer); |
|---|
| 1348 | n/a | if (res == NULL) |
|---|
| 1349 | n/a | goto errorexit; |
|---|
| 1350 | n/a | |
|---|
| 1351 | n/a | Py_XDECREF(cres); |
|---|
| 1352 | n/a | Py_XDECREF(buf.excobj); |
|---|
| 1353 | n/a | return res; |
|---|
| 1354 | n/a | |
|---|
| 1355 | n/a | errorexit: |
|---|
| 1356 | n/a | Py_XDECREF(cres); |
|---|
| 1357 | n/a | Py_XDECREF(buf.excobj); |
|---|
| 1358 | n/a | _PyUnicodeWriter_Dealloc(&buf.writer); |
|---|
| 1359 | n/a | return NULL; |
|---|
| 1360 | n/a | } |
|---|
| 1361 | n/a | |
|---|
| 1362 | n/a | /*[clinic input] |
|---|
| 1363 | n/a | _multibytecodec.MultibyteStreamReader.read |
|---|
| 1364 | n/a | |
|---|
| 1365 | n/a | sizeobj: object = None |
|---|
| 1366 | n/a | / |
|---|
| 1367 | n/a | [clinic start generated code]*/ |
|---|
| 1368 | n/a | |
|---|
| 1369 | n/a | static PyObject * |
|---|
| 1370 | n/a | _multibytecodec_MultibyteStreamReader_read_impl(MultibyteStreamReaderObject *self, |
|---|
| 1371 | n/a | PyObject *sizeobj) |
|---|
| 1372 | n/a | /*[clinic end generated code: output=35621eb75355d5b8 input=015b0d3ff2fca485]*/ |
|---|
| 1373 | n/a | { |
|---|
| 1374 | n/a | Py_ssize_t size; |
|---|
| 1375 | n/a | |
|---|
| 1376 | n/a | if (sizeobj == Py_None) |
|---|
| 1377 | n/a | size = -1; |
|---|
| 1378 | n/a | else if (PyLong_Check(sizeobj)) |
|---|
| 1379 | n/a | size = PyLong_AsSsize_t(sizeobj); |
|---|
| 1380 | n/a | else { |
|---|
| 1381 | n/a | PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer"); |
|---|
| 1382 | n/a | return NULL; |
|---|
| 1383 | n/a | } |
|---|
| 1384 | n/a | |
|---|
| 1385 | n/a | if (size == -1 && PyErr_Occurred()) |
|---|
| 1386 | n/a | return NULL; |
|---|
| 1387 | n/a | |
|---|
| 1388 | n/a | return mbstreamreader_iread(self, "read", size); |
|---|
| 1389 | n/a | } |
|---|
| 1390 | n/a | |
|---|
| 1391 | n/a | /*[clinic input] |
|---|
| 1392 | n/a | _multibytecodec.MultibyteStreamReader.readline |
|---|
| 1393 | n/a | |
|---|
| 1394 | n/a | sizeobj: object = None |
|---|
| 1395 | n/a | / |
|---|
| 1396 | n/a | [clinic start generated code]*/ |
|---|
| 1397 | n/a | |
|---|
| 1398 | n/a | static PyObject * |
|---|
| 1399 | n/a | _multibytecodec_MultibyteStreamReader_readline_impl(MultibyteStreamReaderObject *self, |
|---|
| 1400 | n/a | PyObject *sizeobj) |
|---|
| 1401 | n/a | /*[clinic end generated code: output=4fbfaae1ed457a11 input=41ccc64f9bb0cec3]*/ |
|---|
| 1402 | n/a | { |
|---|
| 1403 | n/a | Py_ssize_t size; |
|---|
| 1404 | n/a | |
|---|
| 1405 | n/a | if (sizeobj == Py_None) |
|---|
| 1406 | n/a | size = -1; |
|---|
| 1407 | n/a | else if (PyLong_Check(sizeobj)) |
|---|
| 1408 | n/a | size = PyLong_AsSsize_t(sizeobj); |
|---|
| 1409 | n/a | else { |
|---|
| 1410 | n/a | PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer"); |
|---|
| 1411 | n/a | return NULL; |
|---|
| 1412 | n/a | } |
|---|
| 1413 | n/a | |
|---|
| 1414 | n/a | if (size == -1 && PyErr_Occurred()) |
|---|
| 1415 | n/a | return NULL; |
|---|
| 1416 | n/a | |
|---|
| 1417 | n/a | return mbstreamreader_iread(self, "readline", size); |
|---|
| 1418 | n/a | } |
|---|
| 1419 | n/a | |
|---|
| 1420 | n/a | /*[clinic input] |
|---|
| 1421 | n/a | _multibytecodec.MultibyteStreamReader.readlines |
|---|
| 1422 | n/a | |
|---|
| 1423 | n/a | sizehintobj: object = None |
|---|
| 1424 | n/a | / |
|---|
| 1425 | n/a | [clinic start generated code]*/ |
|---|
| 1426 | n/a | |
|---|
| 1427 | n/a | static PyObject * |
|---|
| 1428 | n/a | _multibytecodec_MultibyteStreamReader_readlines_impl(MultibyteStreamReaderObject *self, |
|---|
| 1429 | n/a | PyObject *sizehintobj) |
|---|
| 1430 | n/a | /*[clinic end generated code: output=e7c4310768ed2ad4 input=54932f5d4d88e880]*/ |
|---|
| 1431 | n/a | { |
|---|
| 1432 | n/a | PyObject *r, *sr; |
|---|
| 1433 | n/a | Py_ssize_t sizehint; |
|---|
| 1434 | n/a | |
|---|
| 1435 | n/a | if (sizehintobj == Py_None) |
|---|
| 1436 | n/a | sizehint = -1; |
|---|
| 1437 | n/a | else if (PyLong_Check(sizehintobj)) |
|---|
| 1438 | n/a | sizehint = PyLong_AsSsize_t(sizehintobj); |
|---|
| 1439 | n/a | else { |
|---|
| 1440 | n/a | PyErr_SetString(PyExc_TypeError, "arg 1 must be an integer"); |
|---|
| 1441 | n/a | return NULL; |
|---|
| 1442 | n/a | } |
|---|
| 1443 | n/a | |
|---|
| 1444 | n/a | if (sizehint == -1 && PyErr_Occurred()) |
|---|
| 1445 | n/a | return NULL; |
|---|
| 1446 | n/a | |
|---|
| 1447 | n/a | r = mbstreamreader_iread(self, "read", sizehint); |
|---|
| 1448 | n/a | if (r == NULL) |
|---|
| 1449 | n/a | return NULL; |
|---|
| 1450 | n/a | |
|---|
| 1451 | n/a | sr = PyUnicode_Splitlines(r, 1); |
|---|
| 1452 | n/a | Py_DECREF(r); |
|---|
| 1453 | n/a | return sr; |
|---|
| 1454 | n/a | } |
|---|
| 1455 | n/a | |
|---|
| 1456 | n/a | /*[clinic input] |
|---|
| 1457 | n/a | _multibytecodec.MultibyteStreamReader.reset |
|---|
| 1458 | n/a | [clinic start generated code]*/ |
|---|
| 1459 | n/a | |
|---|
| 1460 | n/a | static PyObject * |
|---|
| 1461 | n/a | _multibytecodec_MultibyteStreamReader_reset_impl(MultibyteStreamReaderObject *self) |
|---|
| 1462 | n/a | /*[clinic end generated code: output=138490370a680abc input=5d4140db84b5e1e2]*/ |
|---|
| 1463 | n/a | { |
|---|
| 1464 | n/a | if (self->codec->decreset != NULL && |
|---|
| 1465 | n/a | self->codec->decreset(&self->state, self->codec->config) != 0) |
|---|
| 1466 | n/a | return NULL; |
|---|
| 1467 | n/a | self->pendingsize = 0; |
|---|
| 1468 | n/a | |
|---|
| 1469 | n/a | Py_RETURN_NONE; |
|---|
| 1470 | n/a | } |
|---|
| 1471 | n/a | |
|---|
| 1472 | n/a | static struct PyMethodDef mbstreamreader_methods[] = { |
|---|
| 1473 | n/a | _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READ_METHODDEF |
|---|
| 1474 | n/a | _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READLINE_METHODDEF |
|---|
| 1475 | n/a | _MULTIBYTECODEC_MULTIBYTESTREAMREADER_READLINES_METHODDEF |
|---|
| 1476 | n/a | _MULTIBYTECODEC_MULTIBYTESTREAMREADER_RESET_METHODDEF |
|---|
| 1477 | n/a | {NULL, NULL}, |
|---|
| 1478 | n/a | }; |
|---|
| 1479 | n/a | |
|---|
| 1480 | n/a | static PyMemberDef mbstreamreader_members[] = { |
|---|
| 1481 | n/a | {"stream", T_OBJECT, |
|---|
| 1482 | n/a | offsetof(MultibyteStreamReaderObject, stream), |
|---|
| 1483 | n/a | READONLY, NULL}, |
|---|
| 1484 | n/a | {NULL,} |
|---|
| 1485 | n/a | }; |
|---|
| 1486 | n/a | |
|---|
| 1487 | n/a | static PyObject * |
|---|
| 1488 | n/a | mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 1489 | n/a | { |
|---|
| 1490 | n/a | MultibyteStreamReaderObject *self; |
|---|
| 1491 | n/a | PyObject *stream, *codec = NULL; |
|---|
| 1492 | n/a | char *errors = NULL; |
|---|
| 1493 | n/a | |
|---|
| 1494 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamReader", |
|---|
| 1495 | n/a | streamkwarglist, &stream, &errors)) |
|---|
| 1496 | n/a | return NULL; |
|---|
| 1497 | n/a | |
|---|
| 1498 | n/a | self = (MultibyteStreamReaderObject *)type->tp_alloc(type, 0); |
|---|
| 1499 | n/a | if (self == NULL) |
|---|
| 1500 | n/a | return NULL; |
|---|
| 1501 | n/a | |
|---|
| 1502 | n/a | codec = PyObject_GetAttrString((PyObject *)type, "codec"); |
|---|
| 1503 | n/a | if (codec == NULL) |
|---|
| 1504 | n/a | goto errorexit; |
|---|
| 1505 | n/a | if (!MultibyteCodec_Check(codec)) { |
|---|
| 1506 | n/a | PyErr_SetString(PyExc_TypeError, "codec is unexpected type"); |
|---|
| 1507 | n/a | goto errorexit; |
|---|
| 1508 | n/a | } |
|---|
| 1509 | n/a | |
|---|
| 1510 | n/a | self->codec = ((MultibyteCodecObject *)codec)->codec; |
|---|
| 1511 | n/a | self->stream = stream; |
|---|
| 1512 | n/a | Py_INCREF(stream); |
|---|
| 1513 | n/a | self->pendingsize = 0; |
|---|
| 1514 | n/a | self->errors = internal_error_callback(errors); |
|---|
| 1515 | n/a | if (self->errors == NULL) |
|---|
| 1516 | n/a | goto errorexit; |
|---|
| 1517 | n/a | if (self->codec->decinit != NULL && |
|---|
| 1518 | n/a | self->codec->decinit(&self->state, self->codec->config) != 0) |
|---|
| 1519 | n/a | goto errorexit; |
|---|
| 1520 | n/a | |
|---|
| 1521 | n/a | Py_DECREF(codec); |
|---|
| 1522 | n/a | return (PyObject *)self; |
|---|
| 1523 | n/a | |
|---|
| 1524 | n/a | errorexit: |
|---|
| 1525 | n/a | Py_XDECREF(self); |
|---|
| 1526 | n/a | Py_XDECREF(codec); |
|---|
| 1527 | n/a | return NULL; |
|---|
| 1528 | n/a | } |
|---|
| 1529 | n/a | |
|---|
| 1530 | n/a | static int |
|---|
| 1531 | n/a | mbstreamreader_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 1532 | n/a | { |
|---|
| 1533 | n/a | return 0; |
|---|
| 1534 | n/a | } |
|---|
| 1535 | n/a | |
|---|
| 1536 | n/a | static int |
|---|
| 1537 | n/a | mbstreamreader_traverse(MultibyteStreamReaderObject *self, |
|---|
| 1538 | n/a | visitproc visit, void *arg) |
|---|
| 1539 | n/a | { |
|---|
| 1540 | n/a | if (ERROR_ISCUSTOM(self->errors)) |
|---|
| 1541 | n/a | Py_VISIT(self->errors); |
|---|
| 1542 | n/a | Py_VISIT(self->stream); |
|---|
| 1543 | n/a | return 0; |
|---|
| 1544 | n/a | } |
|---|
| 1545 | n/a | |
|---|
| 1546 | n/a | static void |
|---|
| 1547 | n/a | mbstreamreader_dealloc(MultibyteStreamReaderObject *self) |
|---|
| 1548 | n/a | { |
|---|
| 1549 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 1550 | n/a | ERROR_DECREF(self->errors); |
|---|
| 1551 | n/a | Py_XDECREF(self->stream); |
|---|
| 1552 | n/a | Py_TYPE(self)->tp_free(self); |
|---|
| 1553 | n/a | } |
|---|
| 1554 | n/a | |
|---|
| 1555 | n/a | static PyTypeObject MultibyteStreamReader_Type = { |
|---|
| 1556 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 1557 | n/a | "MultibyteStreamReader", /* tp_name */ |
|---|
| 1558 | n/a | sizeof(MultibyteStreamReaderObject), /* tp_basicsize */ |
|---|
| 1559 | n/a | 0, /* tp_itemsize */ |
|---|
| 1560 | n/a | /* methods */ |
|---|
| 1561 | n/a | (destructor)mbstreamreader_dealloc, /* tp_dealloc */ |
|---|
| 1562 | n/a | 0, /* tp_print */ |
|---|
| 1563 | n/a | 0, /* tp_getattr */ |
|---|
| 1564 | n/a | 0, /* tp_setattr */ |
|---|
| 1565 | n/a | 0, /* tp_reserved */ |
|---|
| 1566 | n/a | 0, /* tp_repr */ |
|---|
| 1567 | n/a | 0, /* tp_as_number */ |
|---|
| 1568 | n/a | 0, /* tp_as_sequence */ |
|---|
| 1569 | n/a | 0, /* tp_as_mapping */ |
|---|
| 1570 | n/a | 0, /* tp_hash */ |
|---|
| 1571 | n/a | 0, /* tp_call */ |
|---|
| 1572 | n/a | 0, /* tp_str */ |
|---|
| 1573 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 1574 | n/a | 0, /* tp_setattro */ |
|---|
| 1575 | n/a | 0, /* tp_as_buffer */ |
|---|
| 1576 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|---|
| 1577 | n/a | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 1578 | n/a | 0, /* tp_doc */ |
|---|
| 1579 | n/a | (traverseproc)mbstreamreader_traverse, /* tp_traverse */ |
|---|
| 1580 | n/a | 0, /* tp_clear */ |
|---|
| 1581 | n/a | 0, /* tp_richcompare */ |
|---|
| 1582 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 1583 | n/a | 0, /* tp_iter */ |
|---|
| 1584 | n/a | 0, /* tp_iterext */ |
|---|
| 1585 | n/a | mbstreamreader_methods, /* tp_methods */ |
|---|
| 1586 | n/a | mbstreamreader_members, /* tp_members */ |
|---|
| 1587 | n/a | codecctx_getsets, /* tp_getset */ |
|---|
| 1588 | n/a | 0, /* tp_base */ |
|---|
| 1589 | n/a | 0, /* tp_dict */ |
|---|
| 1590 | n/a | 0, /* tp_descr_get */ |
|---|
| 1591 | n/a | 0, /* tp_descr_set */ |
|---|
| 1592 | n/a | 0, /* tp_dictoffset */ |
|---|
| 1593 | n/a | mbstreamreader_init, /* tp_init */ |
|---|
| 1594 | n/a | 0, /* tp_alloc */ |
|---|
| 1595 | n/a | mbstreamreader_new, /* tp_new */ |
|---|
| 1596 | n/a | }; |
|---|
| 1597 | n/a | |
|---|
| 1598 | n/a | |
|---|
| 1599 | n/a | /*[clinic input] |
|---|
| 1600 | n/a | class _multibytecodec.MultibyteStreamWriter "MultibyteStreamWriterObject *" "&MultibyteStreamWriter_Type" |
|---|
| 1601 | n/a | [clinic start generated code]*/ |
|---|
| 1602 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=cde22780a215d6ac]*/ |
|---|
| 1603 | n/a | |
|---|
| 1604 | n/a | static int |
|---|
| 1605 | n/a | mbstreamwriter_iwrite(MultibyteStreamWriterObject *self, |
|---|
| 1606 | n/a | PyObject *unistr) |
|---|
| 1607 | n/a | { |
|---|
| 1608 | n/a | PyObject *str, *wr; |
|---|
| 1609 | n/a | |
|---|
| 1610 | n/a | str = encoder_encode_stateful(STATEFUL_ECTX(self), unistr, 0); |
|---|
| 1611 | n/a | if (str == NULL) |
|---|
| 1612 | n/a | return -1; |
|---|
| 1613 | n/a | |
|---|
| 1614 | n/a | wr = _PyObject_CallMethodIdObjArgs(self->stream, &PyId_write, str, NULL); |
|---|
| 1615 | n/a | Py_DECREF(str); |
|---|
| 1616 | n/a | if (wr == NULL) |
|---|
| 1617 | n/a | return -1; |
|---|
| 1618 | n/a | |
|---|
| 1619 | n/a | Py_DECREF(wr); |
|---|
| 1620 | n/a | return 0; |
|---|
| 1621 | n/a | } |
|---|
| 1622 | n/a | |
|---|
| 1623 | n/a | /*[clinic input] |
|---|
| 1624 | n/a | _multibytecodec.MultibyteStreamWriter.write |
|---|
| 1625 | n/a | |
|---|
| 1626 | n/a | strobj: object |
|---|
| 1627 | n/a | / |
|---|
| 1628 | n/a | [clinic start generated code]*/ |
|---|
| 1629 | n/a | |
|---|
| 1630 | n/a | static PyObject * |
|---|
| 1631 | n/a | _multibytecodec_MultibyteStreamWriter_write(MultibyteStreamWriterObject *self, |
|---|
| 1632 | n/a | PyObject *strobj) |
|---|
| 1633 | n/a | /*[clinic end generated code: output=e13ae841c895251e input=551dc4c018c10a2b]*/ |
|---|
| 1634 | n/a | { |
|---|
| 1635 | n/a | if (mbstreamwriter_iwrite(self, strobj)) |
|---|
| 1636 | n/a | return NULL; |
|---|
| 1637 | n/a | else |
|---|
| 1638 | n/a | Py_RETURN_NONE; |
|---|
| 1639 | n/a | } |
|---|
| 1640 | n/a | |
|---|
| 1641 | n/a | /*[clinic input] |
|---|
| 1642 | n/a | _multibytecodec.MultibyteStreamWriter.writelines |
|---|
| 1643 | n/a | |
|---|
| 1644 | n/a | lines: object |
|---|
| 1645 | n/a | / |
|---|
| 1646 | n/a | [clinic start generated code]*/ |
|---|
| 1647 | n/a | |
|---|
| 1648 | n/a | static PyObject * |
|---|
| 1649 | n/a | _multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *self, |
|---|
| 1650 | n/a | PyObject *lines) |
|---|
| 1651 | n/a | /*[clinic end generated code: output=e5c4285ac8e7d522 input=57797fe7008d4e96]*/ |
|---|
| 1652 | n/a | { |
|---|
| 1653 | n/a | PyObject *strobj; |
|---|
| 1654 | n/a | int i, r; |
|---|
| 1655 | n/a | |
|---|
| 1656 | n/a | if (!PySequence_Check(lines)) { |
|---|
| 1657 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1658 | n/a | "arg must be a sequence object"); |
|---|
| 1659 | n/a | return NULL; |
|---|
| 1660 | n/a | } |
|---|
| 1661 | n/a | |
|---|
| 1662 | n/a | for (i = 0; i < PySequence_Length(lines); i++) { |
|---|
| 1663 | n/a | /* length can be changed even within this loop */ |
|---|
| 1664 | n/a | strobj = PySequence_GetItem(lines, i); |
|---|
| 1665 | n/a | if (strobj == NULL) |
|---|
| 1666 | n/a | return NULL; |
|---|
| 1667 | n/a | |
|---|
| 1668 | n/a | r = mbstreamwriter_iwrite(self, strobj); |
|---|
| 1669 | n/a | Py_DECREF(strobj); |
|---|
| 1670 | n/a | if (r == -1) |
|---|
| 1671 | n/a | return NULL; |
|---|
| 1672 | n/a | } |
|---|
| 1673 | n/a | |
|---|
| 1674 | n/a | Py_RETURN_NONE; |
|---|
| 1675 | n/a | } |
|---|
| 1676 | n/a | |
|---|
| 1677 | n/a | /*[clinic input] |
|---|
| 1678 | n/a | _multibytecodec.MultibyteStreamWriter.reset |
|---|
| 1679 | n/a | [clinic start generated code]*/ |
|---|
| 1680 | n/a | |
|---|
| 1681 | n/a | static PyObject * |
|---|
| 1682 | n/a | _multibytecodec_MultibyteStreamWriter_reset_impl(MultibyteStreamWriterObject *self) |
|---|
| 1683 | n/a | /*[clinic end generated code: output=8f54a4d9b03db5ff input=b56dbcbaf35cc10c]*/ |
|---|
| 1684 | n/a | { |
|---|
| 1685 | n/a | PyObject *pwrt; |
|---|
| 1686 | n/a | |
|---|
| 1687 | n/a | if (!self->pending) |
|---|
| 1688 | n/a | Py_RETURN_NONE; |
|---|
| 1689 | n/a | |
|---|
| 1690 | n/a | pwrt = multibytecodec_encode(self->codec, &self->state, |
|---|
| 1691 | n/a | self->pending, NULL, self->errors, |
|---|
| 1692 | n/a | MBENC_FLUSH | MBENC_RESET); |
|---|
| 1693 | n/a | /* some pending buffer can be truncated when UnicodeEncodeError is |
|---|
| 1694 | n/a | * raised on 'strict' mode. but, 'reset' method is designed to |
|---|
| 1695 | n/a | * reset the pending buffer or states so failed string sequence |
|---|
| 1696 | n/a | * ought to be missed */ |
|---|
| 1697 | n/a | Py_CLEAR(self->pending); |
|---|
| 1698 | n/a | if (pwrt == NULL) |
|---|
| 1699 | n/a | return NULL; |
|---|
| 1700 | n/a | |
|---|
| 1701 | n/a | assert(PyBytes_Check(pwrt)); |
|---|
| 1702 | n/a | if (PyBytes_Size(pwrt) > 0) { |
|---|
| 1703 | n/a | PyObject *wr; |
|---|
| 1704 | n/a | |
|---|
| 1705 | n/a | wr = _PyObject_CallMethodIdObjArgs(self->stream, &PyId_write, pwrt); |
|---|
| 1706 | n/a | if (wr == NULL) { |
|---|
| 1707 | n/a | Py_DECREF(pwrt); |
|---|
| 1708 | n/a | return NULL; |
|---|
| 1709 | n/a | } |
|---|
| 1710 | n/a | } |
|---|
| 1711 | n/a | Py_DECREF(pwrt); |
|---|
| 1712 | n/a | |
|---|
| 1713 | n/a | Py_RETURN_NONE; |
|---|
| 1714 | n/a | } |
|---|
| 1715 | n/a | |
|---|
| 1716 | n/a | static PyObject * |
|---|
| 1717 | n/a | mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 1718 | n/a | { |
|---|
| 1719 | n/a | MultibyteStreamWriterObject *self; |
|---|
| 1720 | n/a | PyObject *stream, *codec = NULL; |
|---|
| 1721 | n/a | char *errors = NULL; |
|---|
| 1722 | n/a | |
|---|
| 1723 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s:StreamWriter", |
|---|
| 1724 | n/a | streamkwarglist, &stream, &errors)) |
|---|
| 1725 | n/a | return NULL; |
|---|
| 1726 | n/a | |
|---|
| 1727 | n/a | self = (MultibyteStreamWriterObject *)type->tp_alloc(type, 0); |
|---|
| 1728 | n/a | if (self == NULL) |
|---|
| 1729 | n/a | return NULL; |
|---|
| 1730 | n/a | |
|---|
| 1731 | n/a | codec = PyObject_GetAttrString((PyObject *)type, "codec"); |
|---|
| 1732 | n/a | if (codec == NULL) |
|---|
| 1733 | n/a | goto errorexit; |
|---|
| 1734 | n/a | if (!MultibyteCodec_Check(codec)) { |
|---|
| 1735 | n/a | PyErr_SetString(PyExc_TypeError, "codec is unexpected type"); |
|---|
| 1736 | n/a | goto errorexit; |
|---|
| 1737 | n/a | } |
|---|
| 1738 | n/a | |
|---|
| 1739 | n/a | self->codec = ((MultibyteCodecObject *)codec)->codec; |
|---|
| 1740 | n/a | self->stream = stream; |
|---|
| 1741 | n/a | Py_INCREF(stream); |
|---|
| 1742 | n/a | self->pending = NULL; |
|---|
| 1743 | n/a | self->errors = internal_error_callback(errors); |
|---|
| 1744 | n/a | if (self->errors == NULL) |
|---|
| 1745 | n/a | goto errorexit; |
|---|
| 1746 | n/a | if (self->codec->encinit != NULL && |
|---|
| 1747 | n/a | self->codec->encinit(&self->state, self->codec->config) != 0) |
|---|
| 1748 | n/a | goto errorexit; |
|---|
| 1749 | n/a | |
|---|
| 1750 | n/a | Py_DECREF(codec); |
|---|
| 1751 | n/a | return (PyObject *)self; |
|---|
| 1752 | n/a | |
|---|
| 1753 | n/a | errorexit: |
|---|
| 1754 | n/a | Py_XDECREF(self); |
|---|
| 1755 | n/a | Py_XDECREF(codec); |
|---|
| 1756 | n/a | return NULL; |
|---|
| 1757 | n/a | } |
|---|
| 1758 | n/a | |
|---|
| 1759 | n/a | static int |
|---|
| 1760 | n/a | mbstreamwriter_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 1761 | n/a | { |
|---|
| 1762 | n/a | return 0; |
|---|
| 1763 | n/a | } |
|---|
| 1764 | n/a | |
|---|
| 1765 | n/a | static int |
|---|
| 1766 | n/a | mbstreamwriter_traverse(MultibyteStreamWriterObject *self, |
|---|
| 1767 | n/a | visitproc visit, void *arg) |
|---|
| 1768 | n/a | { |
|---|
| 1769 | n/a | if (ERROR_ISCUSTOM(self->errors)) |
|---|
| 1770 | n/a | Py_VISIT(self->errors); |
|---|
| 1771 | n/a | Py_VISIT(self->stream); |
|---|
| 1772 | n/a | return 0; |
|---|
| 1773 | n/a | } |
|---|
| 1774 | n/a | |
|---|
| 1775 | n/a | static void |
|---|
| 1776 | n/a | mbstreamwriter_dealloc(MultibyteStreamWriterObject *self) |
|---|
| 1777 | n/a | { |
|---|
| 1778 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 1779 | n/a | ERROR_DECREF(self->errors); |
|---|
| 1780 | n/a | Py_XDECREF(self->stream); |
|---|
| 1781 | n/a | Py_TYPE(self)->tp_free(self); |
|---|
| 1782 | n/a | } |
|---|
| 1783 | n/a | |
|---|
| 1784 | n/a | static struct PyMethodDef mbstreamwriter_methods[] = { |
|---|
| 1785 | n/a | _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITE_METHODDEF |
|---|
| 1786 | n/a | _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_WRITELINES_METHODDEF |
|---|
| 1787 | n/a | _MULTIBYTECODEC_MULTIBYTESTREAMWRITER_RESET_METHODDEF |
|---|
| 1788 | n/a | {NULL, NULL}, |
|---|
| 1789 | n/a | }; |
|---|
| 1790 | n/a | |
|---|
| 1791 | n/a | static PyMemberDef mbstreamwriter_members[] = { |
|---|
| 1792 | n/a | {"stream", T_OBJECT, |
|---|
| 1793 | n/a | offsetof(MultibyteStreamWriterObject, stream), |
|---|
| 1794 | n/a | READONLY, NULL}, |
|---|
| 1795 | n/a | {NULL,} |
|---|
| 1796 | n/a | }; |
|---|
| 1797 | n/a | |
|---|
| 1798 | n/a | static PyTypeObject MultibyteStreamWriter_Type = { |
|---|
| 1799 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 1800 | n/a | "MultibyteStreamWriter", /* tp_name */ |
|---|
| 1801 | n/a | sizeof(MultibyteStreamWriterObject), /* tp_basicsize */ |
|---|
| 1802 | n/a | 0, /* tp_itemsize */ |
|---|
| 1803 | n/a | /* methods */ |
|---|
| 1804 | n/a | (destructor)mbstreamwriter_dealloc, /* tp_dealloc */ |
|---|
| 1805 | n/a | 0, /* tp_print */ |
|---|
| 1806 | n/a | 0, /* tp_getattr */ |
|---|
| 1807 | n/a | 0, /* tp_setattr */ |
|---|
| 1808 | n/a | 0, /* tp_reserved */ |
|---|
| 1809 | n/a | 0, /* tp_repr */ |
|---|
| 1810 | n/a | 0, /* tp_as_number */ |
|---|
| 1811 | n/a | 0, /* tp_as_sequence */ |
|---|
| 1812 | n/a | 0, /* tp_as_mapping */ |
|---|
| 1813 | n/a | 0, /* tp_hash */ |
|---|
| 1814 | n/a | 0, /* tp_call */ |
|---|
| 1815 | n/a | 0, /* tp_str */ |
|---|
| 1816 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 1817 | n/a | 0, /* tp_setattro */ |
|---|
| 1818 | n/a | 0, /* tp_as_buffer */ |
|---|
| 1819 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|---|
| 1820 | n/a | | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 1821 | n/a | 0, /* tp_doc */ |
|---|
| 1822 | n/a | (traverseproc)mbstreamwriter_traverse, /* tp_traverse */ |
|---|
| 1823 | n/a | 0, /* tp_clear */ |
|---|
| 1824 | n/a | 0, /* tp_richcompare */ |
|---|
| 1825 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 1826 | n/a | 0, /* tp_iter */ |
|---|
| 1827 | n/a | 0, /* tp_iterext */ |
|---|
| 1828 | n/a | mbstreamwriter_methods, /* tp_methods */ |
|---|
| 1829 | n/a | mbstreamwriter_members, /* tp_members */ |
|---|
| 1830 | n/a | codecctx_getsets, /* tp_getset */ |
|---|
| 1831 | n/a | 0, /* tp_base */ |
|---|
| 1832 | n/a | 0, /* tp_dict */ |
|---|
| 1833 | n/a | 0, /* tp_descr_get */ |
|---|
| 1834 | n/a | 0, /* tp_descr_set */ |
|---|
| 1835 | n/a | 0, /* tp_dictoffset */ |
|---|
| 1836 | n/a | mbstreamwriter_init, /* tp_init */ |
|---|
| 1837 | n/a | 0, /* tp_alloc */ |
|---|
| 1838 | n/a | mbstreamwriter_new, /* tp_new */ |
|---|
| 1839 | n/a | }; |
|---|
| 1840 | n/a | |
|---|
| 1841 | n/a | |
|---|
| 1842 | n/a | /*[clinic input] |
|---|
| 1843 | n/a | _multibytecodec.__create_codec |
|---|
| 1844 | n/a | |
|---|
| 1845 | n/a | arg: object |
|---|
| 1846 | n/a | / |
|---|
| 1847 | n/a | [clinic start generated code]*/ |
|---|
| 1848 | n/a | |
|---|
| 1849 | n/a | static PyObject * |
|---|
| 1850 | n/a | _multibytecodec___create_codec(PyObject *module, PyObject *arg) |
|---|
| 1851 | n/a | /*[clinic end generated code: output=cfa3dce8260e809d input=6840b2a6b183fcfa]*/ |
|---|
| 1852 | n/a | { |
|---|
| 1853 | n/a | MultibyteCodecObject *self; |
|---|
| 1854 | n/a | MultibyteCodec *codec; |
|---|
| 1855 | n/a | |
|---|
| 1856 | n/a | if (!PyCapsule_IsValid(arg, PyMultibyteCodec_CAPSULE_NAME)) { |
|---|
| 1857 | n/a | PyErr_SetString(PyExc_ValueError, "argument type invalid"); |
|---|
| 1858 | n/a | return NULL; |
|---|
| 1859 | n/a | } |
|---|
| 1860 | n/a | |
|---|
| 1861 | n/a | codec = PyCapsule_GetPointer(arg, PyMultibyteCodec_CAPSULE_NAME); |
|---|
| 1862 | n/a | if (codec->codecinit != NULL && codec->codecinit(codec->config) != 0) |
|---|
| 1863 | n/a | return NULL; |
|---|
| 1864 | n/a | |
|---|
| 1865 | n/a | self = PyObject_New(MultibyteCodecObject, &MultibyteCodec_Type); |
|---|
| 1866 | n/a | if (self == NULL) |
|---|
| 1867 | n/a | return NULL; |
|---|
| 1868 | n/a | self->codec = codec; |
|---|
| 1869 | n/a | |
|---|
| 1870 | n/a | return (PyObject *)self; |
|---|
| 1871 | n/a | } |
|---|
| 1872 | n/a | |
|---|
| 1873 | n/a | static struct PyMethodDef __methods[] = { |
|---|
| 1874 | n/a | _MULTIBYTECODEC___CREATE_CODEC_METHODDEF |
|---|
| 1875 | n/a | {NULL, NULL}, |
|---|
| 1876 | n/a | }; |
|---|
| 1877 | n/a | |
|---|
| 1878 | n/a | |
|---|
| 1879 | n/a | static struct PyModuleDef _multibytecodecmodule = { |
|---|
| 1880 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 1881 | n/a | "_multibytecodec", |
|---|
| 1882 | n/a | NULL, |
|---|
| 1883 | n/a | -1, |
|---|
| 1884 | n/a | __methods, |
|---|
| 1885 | n/a | NULL, |
|---|
| 1886 | n/a | NULL, |
|---|
| 1887 | n/a | NULL, |
|---|
| 1888 | n/a | NULL |
|---|
| 1889 | n/a | }; |
|---|
| 1890 | n/a | |
|---|
| 1891 | n/a | PyMODINIT_FUNC |
|---|
| 1892 | n/a | PyInit__multibytecodec(void) |
|---|
| 1893 | n/a | { |
|---|
| 1894 | n/a | int i; |
|---|
| 1895 | n/a | PyObject *m; |
|---|
| 1896 | n/a | PyTypeObject *typelist[] = { |
|---|
| 1897 | n/a | &MultibyteIncrementalEncoder_Type, |
|---|
| 1898 | n/a | &MultibyteIncrementalDecoder_Type, |
|---|
| 1899 | n/a | &MultibyteStreamReader_Type, |
|---|
| 1900 | n/a | &MultibyteStreamWriter_Type, |
|---|
| 1901 | n/a | NULL |
|---|
| 1902 | n/a | }; |
|---|
| 1903 | n/a | |
|---|
| 1904 | n/a | if (PyType_Ready(&MultibyteCodec_Type) < 0) |
|---|
| 1905 | n/a | return NULL; |
|---|
| 1906 | n/a | |
|---|
| 1907 | n/a | m = PyModule_Create(&_multibytecodecmodule); |
|---|
| 1908 | n/a | if (m == NULL) |
|---|
| 1909 | n/a | return NULL; |
|---|
| 1910 | n/a | |
|---|
| 1911 | n/a | for (i = 0; typelist[i] != NULL; i++) { |
|---|
| 1912 | n/a | if (PyType_Ready(typelist[i]) < 0) |
|---|
| 1913 | n/a | return NULL; |
|---|
| 1914 | n/a | Py_INCREF(typelist[i]); |
|---|
| 1915 | n/a | PyModule_AddObject(m, typelist[i]->tp_name, |
|---|
| 1916 | n/a | (PyObject *)typelist[i]); |
|---|
| 1917 | n/a | } |
|---|
| 1918 | n/a | |
|---|
| 1919 | n/a | if (PyErr_Occurred()) { |
|---|
| 1920 | n/a | Py_FatalError("can't initialize the _multibytecodec module"); |
|---|
| 1921 | n/a | Py_DECREF(m); |
|---|
| 1922 | n/a | m = NULL; |
|---|
| 1923 | n/a | } |
|---|
| 1924 | n/a | return m; |
|---|
| 1925 | n/a | } |
|---|