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 * |
| 127 | utf_8_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_8_decode", |
| 135 | &data, &size, &errors)) |
| 136 | return NULL; |
| 137 | |
| 138 | return codec_tuple(PyUnicode_DecodeUTF8(data, size, errors), |
| 139 | size); |
| 140 | } |
| 141 | |
| 142 | static PyObject * |
| 143 | utf_16_decode(PyObject *self, |
| 144 | PyObject *args) |
| 145 | { |
| 146 | const char *data; |
| 147 | int size; |
| 148 | const char *errors = NULL; |
| 149 | int byteorder = 0; |
| 150 | |
| 151 | if (!PyArg_ParseTuple(args, "t#|z:utf_16_decode", |
| 152 | &data, &size, &errors)) |
| 153 | return NULL; |
| 154 | return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder), |
| 155 | size); |
| 156 | } |
| 157 | |
| 158 | static PyObject * |
| 159 | utf_16_le_decode(PyObject *self, |
| 160 | PyObject *args) |
| 161 | { |
| 162 | const char *data; |
| 163 | int size; |
| 164 | const char *errors = NULL; |
| 165 | int byteorder = -1; |
| 166 | |
| 167 | if (!PyArg_ParseTuple(args, "t#|z:utf_16_le_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_be_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_be_decode", |
| 184 | &data, &size, &errors)) |
| 185 | return NULL; |
| 186 | return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder), |
| 187 | size); |
| 188 | } |
| 189 | |
| 190 | /* This non-standard version also provides access to the byteorder |
| 191 | parameter of the builtin UTF-16 codec. |
| 192 | |
| 193 | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
| 194 | being the value in effect at the end of data. |
| 195 | |
| 196 | */ |
| 197 | |
| 198 | static PyObject * |
| 199 | utf_16_ex_decode(PyObject *self, |
| 200 | PyObject *args) |
| 201 | { |
| 202 | const char *data; |
| 203 | int size; |
| 204 | const char *errors = NULL; |
| 205 | int byteorder = 0; |
| 206 | PyObject *unicode, *tuple; |
| 207 | |
| 208 | if (!PyArg_ParseTuple(args, "t#|zi:utf_16_ex_decode", |
| 209 | &data, &size, &errors, &byteorder)) |
| 210 | return NULL; |
| 211 | |
| 212 | unicode = PyUnicode_DecodeUTF16(data, size, errors, &byteorder); |
| 213 | if (unicode == NULL) |
| 214 | return NULL; |
| 215 | tuple = Py_BuildValue("Oii", unicode, size, byteorder); |
| 216 | Py_DECREF(unicode); |
| 217 | return tuple; |
| 218 | } |
| 219 | |
| 220 | static PyObject * |
| 221 | unicode_escape_decode(PyObject *self, |
| 222 | PyObject *args) |
| 223 | { |
| 224 | const char *data; |
| 225 | int size; |
| 226 | const char *errors = NULL; |
| 227 | |
| 228 | if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode", |
| 229 | &data, &size, &errors)) |
| 230 | return NULL; |
| 231 | |
| 232 | return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors), |
| 233 | size); |
| 234 | } |
| 235 | |
| 236 | static PyObject * |
| 237 | raw_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:raw_unicode_escape_decode", |
| 245 | &data, &size, &errors)) |
| 246 | return NULL; |
| 247 | |
| 248 | return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors), |
| 249 | size); |
| 250 | } |
| 251 | |
| 252 | static PyObject * |
| 253 | latin_1_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:latin_1_decode", |
| 261 | &data, &size, &errors)) |
| 262 | return NULL; |
| 263 | |
| 264 | return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors), |
| 265 | size); |
| 266 | } |
| 267 | |
| 268 | static PyObject * |
| 269 | ascii_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:ascii_decode", |
| 277 | &data, &size, &errors)) |
| 278 | return NULL; |
| 279 | |
| 280 | return codec_tuple(PyUnicode_DecodeASCII(data, size, errors), |
| 281 | size); |
| 282 | } |
| 283 | |
| 284 | static PyObject * |
| 285 | charmap_decode(PyObject *self, |
| 286 | PyObject *args) |
| 287 | { |
| 288 | const char *data; |
| 289 | int size; |
| 290 | const char *errors = NULL; |
| 291 | PyObject *mapping = NULL; |
| 292 | |
| 293 | if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode", |
| 294 | &data, &size, &errors, &mapping)) |
| 295 | return NULL; |
| 296 | if (mapping == Py_None) |
| 297 | mapping = NULL; |
| 298 | |
| 299 | return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors), |
| 300 | size); |
| 301 | } |
| 302 | |
Fredrik Lundh | 3083163 | 2001-06-26 15:11:00 +0000 | [diff] [blame] | 303 | #if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 304 | |
| 305 | static PyObject * |
| 306 | mbcs_decode(PyObject *self, |
| 307 | PyObject *args) |
| 308 | { |
| 309 | const char *data; |
| 310 | int size; |
| 311 | const char *errors = NULL; |
| 312 | |
| 313 | if (!PyArg_ParseTuple(args, "t#|z:mbcs_decode", |
| 314 | &data, &size, &errors)) |
| 315 | return NULL; |
| 316 | |
| 317 | return codec_tuple(PyUnicode_DecodeMBCS(data, size, errors), |
| 318 | size); |
| 319 | } |
| 320 | |
| 321 | #endif /* MS_WIN32 */ |
| 322 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 323 | /* --- Encoder ------------------------------------------------------------ */ |
| 324 | |
| 325 | static PyObject * |
| 326 | readbuffer_encode(PyObject *self, |
| 327 | PyObject *args) |
| 328 | { |
| 329 | const char *data; |
| 330 | int size; |
| 331 | const char *errors = NULL; |
| 332 | |
| 333 | if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode", |
| 334 | &data, &size, &errors)) |
| 335 | return NULL; |
| 336 | |
| 337 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 338 | size); |
| 339 | } |
| 340 | |
| 341 | static PyObject * |
| 342 | charbuffer_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, "t#|z:charbuffer_encode", |
| 350 | &data, &size, &errors)) |
| 351 | return NULL; |
| 352 | |
| 353 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 354 | size); |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 358 | unicode_internal_encode(PyObject *self, |
| 359 | PyObject *args) |
| 360 | { |
| 361 | PyObject *obj; |
| 362 | const char *errors = NULL; |
| 363 | const char *data; |
| 364 | int size; |
| 365 | |
| 366 | if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode", |
| 367 | &obj, &errors)) |
| 368 | return NULL; |
| 369 | |
| 370 | if (PyUnicode_Check(obj)) { |
| 371 | data = PyUnicode_AS_DATA(obj); |
| 372 | size = PyUnicode_GET_DATA_SIZE(obj); |
| 373 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 374 | size); |
| 375 | } |
| 376 | else { |
| 377 | if (PyObject_AsReadBuffer(obj, (const void **)&data, &size)) |
| 378 | return NULL; |
| 379 | return codec_tuple(PyString_FromStringAndSize(data, size), |
| 380 | size); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static PyObject * |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 385 | utf_8_encode(PyObject *self, |
| 386 | PyObject *args) |
| 387 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 388 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 389 | const char *errors = NULL; |
| 390 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 391 | if (!PyArg_ParseTuple(args, "O|z:utf_8_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 392 | &str, &errors)) |
| 393 | return NULL; |
| 394 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 395 | str = PyUnicode_FromObject(str); |
| 396 | if (str == NULL) |
| 397 | return NULL; |
| 398 | v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str), |
| 399 | PyUnicode_GET_SIZE(str), |
| 400 | errors), |
| 401 | PyUnicode_GET_SIZE(str)); |
| 402 | Py_DECREF(str); |
| 403 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* This version provides access to the byteorder parameter of the |
| 407 | builtin UTF-16 codecs as optional third argument. It defaults to 0 |
| 408 | which means: use the native byte order and prepend the data with a |
| 409 | BOM mark. |
| 410 | |
| 411 | */ |
| 412 | |
| 413 | static PyObject * |
| 414 | utf_16_encode(PyObject *self, |
| 415 | PyObject *args) |
| 416 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 417 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 418 | const char *errors = NULL; |
| 419 | int byteorder = 0; |
| 420 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 421 | if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 422 | &str, &errors, &byteorder)) |
| 423 | return NULL; |
| 424 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 425 | str = PyUnicode_FromObject(str); |
| 426 | if (str == NULL) |
| 427 | return NULL; |
| 428 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
| 429 | PyUnicode_GET_SIZE(str), |
| 430 | errors, |
| 431 | byteorder), |
| 432 | PyUnicode_GET_SIZE(str)); |
| 433 | Py_DECREF(str); |
| 434 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static PyObject * |
| 438 | utf_16_le_encode(PyObject *self, |
| 439 | PyObject *args) |
| 440 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 441 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 442 | const char *errors = NULL; |
| 443 | |
Marc-André Lemburg | 4157dd5 | 2001-06-17 18:32:36 +0000 | [diff] [blame] | 444 | if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 445 | &str, &errors)) |
| 446 | return NULL; |
| 447 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 448 | str = PyUnicode_FromObject(str); |
| 449 | if (str == NULL) |
| 450 | return NULL; |
| 451 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 452 | PyUnicode_GET_SIZE(str), |
| 453 | errors, |
| 454 | -1), |
| 455 | PyUnicode_GET_SIZE(str)); |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 456 | Py_DECREF(str); |
| 457 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static PyObject * |
| 461 | utf_16_be_encode(PyObject *self, |
| 462 | PyObject *args) |
| 463 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 464 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 465 | const char *errors = NULL; |
| 466 | |
Marc-André Lemburg | 4157dd5 | 2001-06-17 18:32:36 +0000 | [diff] [blame] | 467 | if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 468 | &str, &errors)) |
| 469 | return NULL; |
| 470 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 471 | str = PyUnicode_FromObject(str); |
| 472 | if (str == NULL) |
| 473 | return NULL; |
| 474 | v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str), |
| 475 | PyUnicode_GET_SIZE(str), |
| 476 | errors, |
| 477 | +1), |
| 478 | PyUnicode_GET_SIZE(str)); |
| 479 | Py_DECREF(str); |
| 480 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static PyObject * |
| 484 | unicode_escape_encode(PyObject *self, |
| 485 | PyObject *args) |
| 486 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 487 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 488 | const char *errors = NULL; |
| 489 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 490 | if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 491 | &str, &errors)) |
| 492 | return NULL; |
| 493 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 494 | str = PyUnicode_FromObject(str); |
| 495 | if (str == NULL) |
| 496 | return NULL; |
| 497 | v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str), |
| 498 | PyUnicode_GET_SIZE(str)), |
| 499 | PyUnicode_GET_SIZE(str)); |
| 500 | Py_DECREF(str); |
| 501 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static PyObject * |
| 505 | raw_unicode_escape_encode(PyObject *self, |
| 506 | PyObject *args) |
| 507 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 508 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 509 | const char *errors = NULL; |
| 510 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 511 | if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 512 | &str, &errors)) |
| 513 | return NULL; |
| 514 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 515 | str = PyUnicode_FromObject(str); |
| 516 | if (str == NULL) |
| 517 | return NULL; |
| 518 | v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape( |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 519 | PyUnicode_AS_UNICODE(str), |
| 520 | PyUnicode_GET_SIZE(str)), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 521 | PyUnicode_GET_SIZE(str)); |
| 522 | Py_DECREF(str); |
| 523 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static PyObject * |
| 527 | latin_1_encode(PyObject *self, |
| 528 | PyObject *args) |
| 529 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 530 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 531 | const char *errors = NULL; |
| 532 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 533 | if (!PyArg_ParseTuple(args, "O|z:latin_1_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 534 | &str, &errors)) |
| 535 | return NULL; |
| 536 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 537 | str = PyUnicode_FromObject(str); |
| 538 | if (str == NULL) |
| 539 | return NULL; |
| 540 | v = codec_tuple(PyUnicode_EncodeLatin1( |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 541 | PyUnicode_AS_UNICODE(str), |
| 542 | PyUnicode_GET_SIZE(str), |
| 543 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 544 | PyUnicode_GET_SIZE(str)); |
| 545 | Py_DECREF(str); |
| 546 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static PyObject * |
| 550 | ascii_encode(PyObject *self, |
| 551 | PyObject *args) |
| 552 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 553 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 554 | const char *errors = NULL; |
| 555 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 556 | if (!PyArg_ParseTuple(args, "O|z:ascii_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 557 | &str, &errors)) |
| 558 | return NULL; |
| 559 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 560 | str = PyUnicode_FromObject(str); |
| 561 | if (str == NULL) |
| 562 | return NULL; |
| 563 | v = codec_tuple(PyUnicode_EncodeASCII( |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 564 | PyUnicode_AS_UNICODE(str), |
| 565 | PyUnicode_GET_SIZE(str), |
| 566 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 567 | PyUnicode_GET_SIZE(str)); |
| 568 | Py_DECREF(str); |
| 569 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static PyObject * |
| 573 | charmap_encode(PyObject *self, |
| 574 | PyObject *args) |
| 575 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 576 | PyObject *str, *v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 577 | const char *errors = NULL; |
| 578 | PyObject *mapping = NULL; |
| 579 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 580 | if (!PyArg_ParseTuple(args, "O|zO:charmap_encode", |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 581 | &str, &errors, &mapping)) |
| 582 | return NULL; |
| 583 | if (mapping == Py_None) |
| 584 | mapping = NULL; |
| 585 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 586 | str = PyUnicode_FromObject(str); |
| 587 | if (str == NULL) |
| 588 | return NULL; |
| 589 | v = codec_tuple(PyUnicode_EncodeCharmap( |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 590 | PyUnicode_AS_UNICODE(str), |
| 591 | PyUnicode_GET_SIZE(str), |
| 592 | mapping, |
| 593 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 594 | PyUnicode_GET_SIZE(str)); |
| 595 | Py_DECREF(str); |
| 596 | return v; |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Fredrik Lundh | 3083163 | 2001-06-26 15:11:00 +0000 | [diff] [blame] | 599 | #if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 600 | |
| 601 | static PyObject * |
| 602 | mbcs_encode(PyObject *self, |
| 603 | PyObject *args) |
| 604 | { |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 605 | PyObject *str, *v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 606 | const char *errors = NULL; |
| 607 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 608 | if (!PyArg_ParseTuple(args, "O|z:mbcs_encode", |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 609 | &str, &errors)) |
| 610 | return NULL; |
| 611 | |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 612 | str = PyUnicode_FromObject(str); |
| 613 | if (str == NULL) |
| 614 | return NULL; |
| 615 | v = codec_tuple(PyUnicode_EncodeMBCS( |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 616 | PyUnicode_AS_UNICODE(str), |
| 617 | PyUnicode_GET_SIZE(str), |
| 618 | errors), |
Marc-André Lemburg | 5f0e29e | 2000-07-05 11:24:13 +0000 | [diff] [blame] | 619 | PyUnicode_GET_SIZE(str)); |
| 620 | Py_DECREF(str); |
| 621 | return v; |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | #endif /* MS_WIN32 */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 625 | #endif /* Py_USING_UNICODE */ |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 626 | |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 627 | /* --- Module API --------------------------------------------------------- */ |
| 628 | |
| 629 | static PyMethodDef _codecs_functions[] = { |
| 630 | {"register", codecregister, 1}, |
| 631 | {"lookup", codeclookup, 1}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 632 | #ifdef Py_USING_UNICODE |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 633 | {"utf_8_encode", utf_8_encode, 1}, |
| 634 | {"utf_8_decode", utf_8_decode, 1}, |
| 635 | {"utf_16_encode", utf_16_encode, 1}, |
| 636 | {"utf_16_le_encode", utf_16_le_encode, 1}, |
| 637 | {"utf_16_be_encode", utf_16_be_encode, 1}, |
| 638 | {"utf_16_decode", utf_16_decode, 1}, |
| 639 | {"utf_16_le_decode", utf_16_le_decode, 1}, |
| 640 | {"utf_16_be_decode", utf_16_be_decode, 1}, |
| 641 | {"utf_16_ex_decode", utf_16_ex_decode, 1}, |
| 642 | {"unicode_escape_encode", unicode_escape_encode, 1}, |
| 643 | {"unicode_escape_decode", unicode_escape_decode, 1}, |
Marc-André Lemburg | b425f5e | 2000-09-21 21:09:45 +0000 | [diff] [blame] | 644 | {"unicode_internal_encode", unicode_internal_encode, 1}, |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 645 | {"unicode_internal_decode", unicode_internal_decode, 1}, |
| 646 | {"raw_unicode_escape_encode", raw_unicode_escape_encode, 1}, |
| 647 | {"raw_unicode_escape_decode", raw_unicode_escape_decode, 1}, |
| 648 | {"latin_1_encode", latin_1_encode, 1}, |
| 649 | {"latin_1_decode", latin_1_decode, 1}, |
| 650 | {"ascii_encode", ascii_encode, 1}, |
| 651 | {"ascii_decode", ascii_decode, 1}, |
| 652 | {"charmap_encode", charmap_encode, 1}, |
| 653 | {"charmap_decode", charmap_decode, 1}, |
| 654 | {"readbuffer_encode", readbuffer_encode, 1}, |
| 655 | {"charbuffer_encode", charbuffer_encode, 1}, |
Fredrik Lundh | 3083163 | 2001-06-26 15:11:00 +0000 | [diff] [blame] | 656 | #if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 657 | {"mbcs_encode", mbcs_encode, 1}, |
| 658 | {"mbcs_decode", mbcs_decode, 1}, |
| 659 | #endif |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame^] | 660 | #endif /* Py_USING_UNICODE */ |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 661 | {NULL, NULL} /* sentinel */ |
| 662 | }; |
| 663 | |
| 664 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 665 | init_codecs(void) |
Guido van Rossum | e2d67f9 | 2000-03-10 23:09:23 +0000 | [diff] [blame] | 666 | { |
| 667 | Py_InitModule("_codecs", _codecs_functions); |
| 668 | } |