Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 1 | /* ------------------------------------------------------------------------ |
| 2 | |
| 3 | _codecs -- Provides access to the codec registry and the builtin |
| 4 | codecs. |
| 5 | |
| 6 | This module should never be imported directly. The standard library |
| 7 | module "codecs" wraps this builtin module for use within Python. |
| 8 | |
| 9 | The codec registry is accessible via: |
| 10 | |
| 11 | register(search_function) -> None |
| 12 | |
| 13 | lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer) |
| 14 | |
| 15 | The builtin Unicode codecs use the following interface: |
| 16 | |
| 17 | <encoding>_encode(Unicode_object[,errors='strict']) -> |
| 18 | (string object, bytes consumed) |
| 19 | |
| 20 | <encoding>_decode(char_buffer_obj[,errors='strict']) -> |
| 21 | (Unicode object, bytes consumed) |
| 22 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 23 | <encoding>_encode() interfaces also accept non-Unicode object as |
| 24 | input. The objects are then converted to Unicode using |
| 25 | PyUnicode_FromObject() prior to applying the conversion. |
| 26 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 27 | These <encoding>s are available: utf_8, unicode_escape, |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 28 | raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit), |
| 29 | mbcs (on win32). |
| 30 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 31 | |
| 32 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 33 | |
Guido van Rossum | 16b1ad9 | 2000-08-03 16:24:25 +0000 | [diff] [blame] | 34 | Copyright (c) Corporation for National Research Initiatives. |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 35 | |
| 36 | ------------------------------------------------------------------------ */ |
| 37 | |
| 38 | #include "Python.h" |
| 39 | |
| 40 | /* --- Registry ----------------------------------------------------------- */ |
| 41 | |
| 42 | static |
| 43 | PyObject *codecregister(PyObject *self, PyObject *args) |
| 44 | { |
| 45 | PyObject *search_function; |
| 46 | |
| 47 | if (!PyArg_ParseTuple(args, "O:register", &search_function)) |
| 48 | goto onError; |
| 49 | |
| 50 | if (PyCodec_Register(search_function)) |
| 51 | goto onError; |
| 52 | |
| 53 | Py_INCREF(Py_None); |
| 54 | return Py_None; |
| 55 | |
| 56 | onError: |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | static |
| 61 | PyObject *codeclookup(PyObject *self, PyObject *args) |
| 62 | { |
| 63 | char *encoding; |
| 64 | |
| 65 | if (!PyArg_ParseTuple(args, "s:lookup", &encoding)) |
| 66 | goto onError; |
| 67 | |
| 68 | return _PyCodec_Lookup(encoding); |
| 69 | |
| 70 | onError: |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 74 | #ifdef Py_USING_UNICODE |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 75 | /* --- Helpers ------------------------------------------------------------ */ |
| 76 | |
| 77 | static |
| 78 | PyObject *codec_tuple(PyObject *unicode, |
| 79 | int len) |
| 80 | { |
| 81 | PyObject *v,*w; |
| 82 | |
| 83 | if (unicode == NULL) |
| 84 | return NULL; |
| 85 | v = PyTuple_New(2); |
| 86 | if (v == NULL) { |
| 87 | Py_DECREF(unicode); |
| 88 | return NULL; |
| 89 | } |
| 90 | PyTuple_SET_ITEM(v,0,unicode); |
| 91 | w = PyInt_FromLong(len); |
| 92 | if (w == NULL) { |
| 93 | Py_DECREF(v); |
| 94 | return NULL; |
| 95 | } |
| 96 | PyTuple_SET_ITEM(v,1,w); |
| 97 | return v; |
| 98 | } |
| 99 | |
| 100 | /* --- Decoder ------------------------------------------------------------ */ |
| 101 | |
| 102 | static PyObject * |
| 103 | unicode_internal_decode(PyObject *self, |
| 104 | PyObject *args) |
| 105 | { |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 106 | PyObject *obj; |
| 107 | const char *errors = NULL; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 108 | const char *data; |
| 109 | int size; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 110 | |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 111 | if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode", |
| 112 | &obj, &errors)) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 113 | return NULL; |
| 114 | |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 115 | if (PyUnicode_Check(obj)) |
| 116 | return codec_tuple(obj, PyUnicode_GET_SIZE(obj)); |
| 117 | else { |
| 118 | if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) |
| 119 | return NULL; |
| 120 | return codec_tuple(PyUnicode_FromUnicode((Py_UNICODE *)data, |
| 121 | size / sizeof(Py_UNICODE)), |
| 122 | size); |
| 123 | } |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static PyObject * |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 127 | utf_7_decode(PyObject *self, |
| 128 | PyObject *args) |
| 129 | { |
| 130 | const char *data; |
| 131 | int size; |
| 132 | const char *errors = NULL; |
| 133 | |
| 134 | if (!PyArg_ParseTuple(args, "t#|z:utf_7_decode", |
| 135 | &data, &size, &errors)) |
| 136 | return NULL; |
| 137 | |
| 138 | return codec_tuple(PyUnicode_DecodeUTF7(data, size, errors), |
| 139 | size); |
| 140 | } |
| 141 | |
| 142 | static PyObject * |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 143 | utf_8_decode(PyObject *self, |
| 144 | PyObject *args) |
| 145 | { |
| 146 | const char *data; |
| 147 | int size; |
| 148 | const char *errors = NULL; |
| 149 | |
| 150 | if (!PyArg_ParseTuple(args, "t#|z:utf_8_decode", |
| 151 | &data, &size, &errors)) |
| 152 | return NULL; |
| 153 | |
| 154 | return codec_tuple(PyUnicode_DecodeUTF8(data, size, errors), |
| 155 | size); |
| 156 | } |
| 157 | |
| 158 | static PyObject * |
| 159 | utf_16_decode(PyObject *self, |
| 160 | PyObject *args) |
| 161 | { |
| 162 | const char *data; |
| 163 | int size; |
| 164 | const char *errors = NULL; |
| 165 | int byteorder = 0; |
| 166 | |
| 167 | if (!PyArg_ParseTuple(args, "t#|z:utf_16_decode", |
| 168 | &data, &size, &errors)) |
| 169 | return NULL; |
| 170 | return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder), |
| 171 | size); |
| 172 | } |
| 173 | |
| 174 | static PyObject * |
| 175 | utf_16_le_decode(PyObject *self, |
| 176 | PyObject *args) |
| 177 | { |
| 178 | const char *data; |
| 179 | int size; |
| 180 | const char *errors = NULL; |
| 181 | int byteorder = -1; |
| 182 | |
| 183 | if (!PyArg_ParseTuple(args, "t#|z:utf_16_le_decode", |
| 184 | &data, &size, &errors)) |
| 185 | return NULL; |
| 186 | return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder), |
| 187 | size); |
| 188 | } |
| 189 | |
| 190 | static PyObject * |
| 191 | utf_16_be_decode(PyObject *self, |
| 192 | PyObject *args) |
| 193 | { |
| 194 | const char *data; |
| 195 | int size; |
| 196 | const char *errors = NULL; |
| 197 | int byteorder = 1; |
| 198 | |
| 199 | if (!PyArg_ParseTuple(args, "t#|z:utf_16_be_decode", |
| 200 | &data, &size, &errors)) |
| 201 | return NULL; |
| 202 | return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder), |
| 203 | size); |
| 204 | } |
| 205 | |
| 206 | /* This non-standard version also provides access to the byteorder |
| 207 | parameter of the builtin UTF-16 codec. |
| 208 | |
| 209 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 210 | being the value in effect at the end of data. |
| 211 | |
| 212 | */ |
| 213 | |
| 214 | static PyObject * |
| 215 | utf_16_ex_decode(PyObject *self, |
| 216 | PyObject *args) |
| 217 | { |
| 218 | const char *data; |
| 219 | int size; |
| 220 | const char *errors = NULL; |
| 221 | int byteorder = 0; |
| 222 | PyObject *unicode, *tuple; |
| 223 | |
| 224 | if (!PyArg_ParseTuple(args, "t#|zi:utf_16_ex_decode", |
| 225 | &data, &size, &errors, &byteorder)) |
| 226 | return NULL; |
| 227 | |
| 228 | unicode = PyUnicode_DecodeUTF16(data, size, errors, &byteorder); |
| 229 | if (unicode == NULL) |
| 230 | return NULL; |
| 231 | tuple = Py_BuildValue("Oii", unicode, size, byteorder); |
| 232 | Py_DECREF(unicode); |
| 233 | return tuple; |
| 234 | } |
| 235 | |
| 236 | static PyObject * |
| 237 | unicode_escape_decode(PyObject *self, |
| 238 | PyObject *args) |
| 239 | { |
| 240 | const char *data; |
| 241 | int size; |
| 242 | const char *errors = NULL; |
| 243 | |
| 244 | if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode", |
| 245 | &data, &size, &errors)) |
| 246 | return NULL; |
| 247 | |
| 248 | return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors), |
| 249 | size); |
| 250 | } |
| 251 | |
| 252 | static PyObject * |
| 253 | raw_unicode_escape_decode(PyObject *self, |
| 254 | PyObject *args) |
| 255 | { |
| 256 | const char *data; |
| 257 | int size; |
| 258 | const char *errors = NULL; |
| 259 | |
| 260 | if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode", |
| 261 | &data, &size, &errors)) |
| 262 | return NULL; |
| 263 | |
| 264 | return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors), |
| 265 | size); |
| 266 | } |
| 267 | |
| 268 | static PyObject * |
| 269 | latin_1_decode(PyObject *self, |
| 270 | PyObject *args) |
| 271 | { |
| 272 | const char *data; |
| 273 | int size; |
| 274 | const char *errors = NULL; |
| 275 | |
| 276 | if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode", |
| 277 | &data, &size, &errors)) |
| 278 | return NULL; |
| 279 | |
| 280 | return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors), |
| 281 | size); |
| 282 | } |
| 283 | |
| 284 | static PyObject * |
| 285 | ascii_decode(PyObject *self, |
| 286 | PyObject *args) |
| 287 | { |
| 288 | const char *data; |
| 289 | int size; |
| 290 | const char *errors = NULL; |
| 291 | |
| 292 | if (!PyArg_ParseTuple(args, "t#|z:ascii_decode", |
| 293 | &data, &size, &errors)) |
| 294 | return NULL; |
| 295 | |
| 296 | return codec_tuple(PyUnicode_DecodeASCII(data, size, errors), |
| 297 | size); |
| 298 | } |
| 299 | |
| 300 | static PyObject * |
| 301 | charmap_decode(PyObject *self, |
| 302 | PyObject *args) |
| 303 | { |
| 304 | const char *data; |
| 305 | int size; |
| 306 | const char *errors = NULL; |
| 307 | PyObject *mapping = NULL; |
| 308 | |
| 309 | if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode", |
| 310 | &data, &size, &errors, &mapping)) |
| 311 | return NULL; |
| 312 | if (mapping == Py_None) |
| 313 | mapping = NULL; |
| 314 | |
| 315 | return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors), |
| 316 | size); |
| 317 | } |
| 318 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 319 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 320 | |
| 321 | static PyObject * |
| 322 | mbcs_decode(PyObject *self, |
| 323 | PyObject *args) |
| 324 | { |
| 325 | const char *data; |
| 326 | int size; |
| 327 | const char *errors = NULL; |
| 328 | |
| 329 | if (!PyArg_ParseTuple(args, "t#|z:mbcs_decode", |
| 330 | &data, &size, &errors)) |
| 331 | return NULL; |
| 332 | |
| 333 | return codec_tuple(PyUnicode_DecodeMBCS(data, size, errors), |
| 334 | size); |
| 335 | } |
| 336 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 337 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 339 | /* --- Encoder ------------------------------------------------------------ */ |
| 340 | |
| 341 | static PyObject * |
| 342 | readbuffer_encode(PyObject *self, |
| 343 | PyObject *args) |
| 344 | { |
| 345 | const char *data; |
| 346 | int size; |
| 347 | const char *errors = NULL; |
| 348 | |
| 349 | if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode", |
| 350 | &data, &size, &errors)) |
| 351 | return NULL; |
| 352 | |
| 353 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 354 | size); |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
| 358 | charbuffer_encode(PyObject *self, |
| 359 | PyObject *args) |
| 360 | { |
| 361 | const char *data; |
| 362 | int size; |
| 363 | const char *errors = NULL; |
| 364 | |
| 365 | if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode", |
| 366 | &data, &size, &errors)) |
| 367 | return NULL; |
| 368 | |
| 369 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 370 | size); |
| 371 | } |
| 372 | |
| 373 | static PyObject * |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 374 | unicode_internal_encode(PyObject *self, |
| 375 | PyObject *args) |
| 376 | { |
| 377 | PyObject *obj; |
| 378 | const char *errors = NULL; |
| 379 | const char *data; |
| 380 | int size; |
| 381 | |
| 382 | if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode", |
| 383 | &obj, &errors)) |
| 384 | return NULL; |
| 385 | |
| 386 | if (PyUnicode_Check(obj)) { |
| 387 | data = PyUnicode_AS_DATA(obj); |
| 388 | size = PyUnicode_GET_DATA_SIZE(obj); |
| 389 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 390 | size); |
| 391 | } |
| 392 | else { |
| 393 | if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) |
| 394 | return NULL; |
| 395 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 396 | size); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static PyObject * |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 401 | utf_7_encode(PyObject *self, |
| 402 | PyObject *args) |
| 403 | { |
| 404 | PyObject *str, *v; |
| 405 | const char *errors = NULL; |
| 406 | |
| 407 | if (!PyArg_ParseTuple(args, "O|z:utf_7_encode", |
| 408 | &str, &errors)) |
| 409 | return NULL; |
| 410 | |
| 411 | str = PyUnicode_FromObject(str); |
| 412 | if (str == NULL) |
| 413 | return NULL; |
| 414 | v = codec_tuple(PyUnicode_EncodeUTF7(PyUnicode_AS_UNICODE(str), |
| 415 | PyUnicode_GET_SIZE(str), |
| 416 | 0, |
| 417 | 0, |
| 418 | errors), |
| 419 | PyUnicode_GET_SIZE(str)); |
| 420 | Py_DECREF(str); |
| 421 | return v; |
| 422 | } |
| 423 | |
| 424 | static PyObject * |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 425 | utf_8_encode(PyObject *self, |
| 426 | PyObject *args) |
| 427 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 428 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 429 | const char *errors = NULL; |
| 430 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 431 | if (!PyArg_ParseTuple(args, "O|z:utf_8_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 432 | &str, &errors)) |
| 433 | return NULL; |
| 434 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 435 | str = PyUnicode_FromObject(str); |
| 436 | if (str == NULL) |
| 437 | return NULL; |
| 438 | v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str), |
| 439 | PyUnicode_GET_SIZE(str), |
| 440 | errors), |
| 441 | PyUnicode_GET_SIZE(str)); |
| 442 | Py_DECREF(str); |
| 443 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* This version provides access to the byteorder parameter of the |
| 447 | builtin UTF-16 codecs as optional third argument. It defaults to 0 |
| 448 | which means: use the native byte order and prepend the data with a |
| 449 | BOM mark. |
| 450 | |
| 451 | */ |
| 452 | |
| 453 | static PyObject * |
| 454 | utf_16_encode(PyObject *self, |
| 455 | PyObject *args) |
| 456 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 457 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 458 | const char *errors = NULL; |
| 459 | int byteorder = 0; |
| 460 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 461 | if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 462 | &str, &errors, &byteorder)) |
| 463 | return NULL; |
| 464 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 465 | str = PyUnicode_FromObject(str); |
| 466 | if (str == NULL) |
| 467 | return NULL; |
| 468 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
| 469 | PyUnicode_GET_SIZE(str), |
| 470 | errors, |
| 471 | byteorder), |
| 472 | PyUnicode_GET_SIZE(str)); |
| 473 | Py_DECREF(str); |
| 474 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static PyObject * |
| 478 | utf_16_le_encode(PyObject *self, |
| 479 | PyObject *args) |
| 480 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 481 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 482 | const char *errors = NULL; |
| 483 | |
Marc-André Lemburg | 4157dd5 | 2001-06-17 18:32:36 +0000 | [diff] [blame] | 484 | if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 485 | &str, &errors)) |
| 486 | return NULL; |
| 487 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 488 | str = PyUnicode_FromObject(str); |
| 489 | if (str == NULL) |
| 490 | return NULL; |
| 491 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 492 | PyUnicode_GET_SIZE(str), |
| 493 | errors, |
| 494 | -1), |
| 495 | PyUnicode_GET_SIZE(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 496 | Py_DECREF(str); |
| 497 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static PyObject * |
| 501 | utf_16_be_encode(PyObject *self, |
| 502 | PyObject *args) |
| 503 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 504 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 505 | const char *errors = NULL; |
| 506 | |
Marc-André Lemburg | 4157dd5 | 2001-06-17 18:32:36 +0000 | [diff] [blame] | 507 | if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 508 | &str, &errors)) |
| 509 | return NULL; |
| 510 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 511 | str = PyUnicode_FromObject(str); |
| 512 | if (str == NULL) |
| 513 | return NULL; |
| 514 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
| 515 | PyUnicode_GET_SIZE(str), |
| 516 | errors, |
| 517 | +1), |
| 518 | PyUnicode_GET_SIZE(str)); |
| 519 | Py_DECREF(str); |
| 520 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static PyObject * |
| 524 | unicode_escape_encode(PyObject *self, |
| 525 | PyObject *args) |
| 526 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 527 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 528 | const char *errors = NULL; |
| 529 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 530 | if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 531 | &str, &errors)) |
| 532 | return NULL; |
| 533 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 534 | str = PyUnicode_FromObject(str); |
| 535 | if (str == NULL) |
| 536 | return NULL; |
| 537 | v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str), |
| 538 | PyUnicode_GET_SIZE(str)), |
| 539 | PyUnicode_GET_SIZE(str)); |
| 540 | Py_DECREF(str); |
| 541 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | static PyObject * |
| 545 | raw_unicode_escape_encode(PyObject *self, |
| 546 | PyObject *args) |
| 547 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 548 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 549 | const char *errors = NULL; |
| 550 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 551 | if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 552 | &str, &errors)) |
| 553 | return NULL; |
| 554 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 555 | str = PyUnicode_FromObject(str); |
| 556 | if (str == NULL) |
| 557 | return NULL; |
| 558 | v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape( |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 559 | PyUnicode_AS_UNICODE(str), |
| 560 | PyUnicode_GET_SIZE(str)), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 561 | PyUnicode_GET_SIZE(str)); |
| 562 | Py_DECREF(str); |
| 563 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static PyObject * |
| 567 | latin_1_encode(PyObject *self, |
| 568 | PyObject *args) |
| 569 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 570 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 571 | const char *errors = NULL; |
| 572 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 573 | if (!PyArg_ParseTuple(args, "O|z:latin_1_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 574 | &str, &errors)) |
| 575 | return NULL; |
| 576 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 577 | str = PyUnicode_FromObject(str); |
| 578 | if (str == NULL) |
| 579 | return NULL; |
| 580 | v = codec_tuple(PyUnicode_EncodeLatin1( |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 581 | PyUnicode_AS_UNICODE(str), |
| 582 | PyUnicode_GET_SIZE(str), |
| 583 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 584 | PyUnicode_GET_SIZE(str)); |
| 585 | Py_DECREF(str); |
| 586 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static PyObject * |
| 590 | ascii_encode(PyObject *self, |
| 591 | PyObject *args) |
| 592 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 593 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 594 | const char *errors = NULL; |
| 595 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 596 | if (!PyArg_ParseTuple(args, "O|z:ascii_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 597 | &str, &errors)) |
| 598 | return NULL; |
| 599 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 600 | str = PyUnicode_FromObject(str); |
| 601 | if (str == NULL) |
| 602 | return NULL; |
| 603 | v = codec_tuple(PyUnicode_EncodeASCII( |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 604 | PyUnicode_AS_UNICODE(str), |
| 605 | PyUnicode_GET_SIZE(str), |
| 606 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 607 | PyUnicode_GET_SIZE(str)); |
| 608 | Py_DECREF(str); |
| 609 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static PyObject * |
| 613 | charmap_encode(PyObject *self, |
| 614 | PyObject *args) |
| 615 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 616 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 617 | const char *errors = NULL; |
| 618 | PyObject *mapping = NULL; |
| 619 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 620 | if (!PyArg_ParseTuple(args, "O|zO:charmap_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 621 | &str, &errors, &mapping)) |
| 622 | return NULL; |
| 623 | if (mapping == Py_None) |
| 624 | mapping = NULL; |
| 625 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 626 | str = PyUnicode_FromObject(str); |
| 627 | if (str == NULL) |
| 628 | return NULL; |
| 629 | v = codec_tuple(PyUnicode_EncodeCharmap( |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 630 | PyUnicode_AS_UNICODE(str), |
| 631 | PyUnicode_GET_SIZE(str), |
| 632 | mapping, |
| 633 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 634 | PyUnicode_GET_SIZE(str)); |
| 635 | Py_DECREF(str); |
| 636 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 639 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 640 | |
| 641 | static PyObject * |
| 642 | mbcs_encode(PyObject *self, |
| 643 | PyObject *args) |
| 644 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 645 | PyObject *str, *v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 646 | const char *errors = NULL; |
| 647 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 648 | if (!PyArg_ParseTuple(args, "O|z:mbcs_encode", |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 649 | &str, &errors)) |
| 650 | return NULL; |
| 651 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 652 | str = PyUnicode_FromObject(str); |
| 653 | if (str == NULL) |
| 654 | return NULL; |
| 655 | v = codec_tuple(PyUnicode_EncodeMBCS( |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 656 | PyUnicode_AS_UNICODE(str), |
| 657 | PyUnicode_GET_SIZE(str), |
| 658 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 659 | PyUnicode_GET_SIZE(str)); |
| 660 | Py_DECREF(str); |
| 661 | return v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 664 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 665 | #endif /* Py_USING_UNICODE */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 666 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 667 | /* --- Module API --------------------------------------------------------- */ |
| 668 | |
| 669 | static PyMethodDef _codecs_functions[] = { |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 670 | {"register", codecregister, METH_VARARGS}, |
| 671 | {"lookup", codeclookup, METH_VARARGS}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 672 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 673 | {"utf_8_encode", utf_8_encode, METH_VARARGS}, |
| 674 | {"utf_8_decode", utf_8_decode, METH_VARARGS}, |
| 675 | {"utf_7_encode", utf_7_encode, METH_VARARGS}, |
| 676 | {"utf_7_decode", utf_7_decode, METH_VARARGS}, |
| 677 | {"utf_16_encode", utf_16_encode, METH_VARARGS}, |
| 678 | {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS}, |
| 679 | {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS}, |
| 680 | {"utf_16_decode", utf_16_decode, METH_VARARGS}, |
| 681 | {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS}, |
| 682 | {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS}, |
| 683 | {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS}, |
| 684 | {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS}, |
| 685 | {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS}, |
| 686 | {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS}, |
| 687 | {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS}, |
| 688 | {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS}, |
| 689 | {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS}, |
| 690 | {"latin_1_encode", latin_1_encode, METH_VARARGS}, |
| 691 | {"latin_1_decode", latin_1_decode, METH_VARARGS}, |
| 692 | {"ascii_encode", ascii_encode, METH_VARARGS}, |
| 693 | {"ascii_decode", ascii_decode, METH_VARARGS}, |
| 694 | {"charmap_encode", charmap_encode, METH_VARARGS}, |
| 695 | {"charmap_decode", charmap_decode, METH_VARARGS}, |
| 696 | {"readbuffer_encode", readbuffer_encode, METH_VARARGS}, |
| 697 | {"charbuffer_encode", charbuffer_encode, METH_VARARGS}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 698 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 699 | {"mbcs_encode", mbcs_encode, METH_VARARGS}, |
| 700 | {"mbcs_decode", mbcs_decode, METH_VARARGS}, |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 701 | #endif |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 702 | #endif /* Py_USING_UNICODE */ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 703 | {NULL, NULL} /* sentinel */ |
| 704 | }; |
| 705 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 706 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 707 | init_codecs(void) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 708 | { |
| 709 | Py_InitModule("_codecs", _codecs_functions); |
| 710 | } |