_codecsmodule.c 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* ------------------------------------------------------------------------

   _codecs -- Provides access to the codec registry and the builtin
              codecs.

   This module should never be imported directly. The standard library
   module "codecs" wraps this builtin module for use within Python.

   The codec registry is accessible via:

     register(search_function) -> None

     lookup(encoding) -> (encoder, decoder, stream_reader, stream_writer)

   The builtin Unicode codecs use the following interface:

     <encoding>_encode(Unicode_object[,errors='strict']) -> 
     	(string object, bytes consumed)

     <encoding>_decode(char_buffer_obj[,errors='strict']) -> 
        (Unicode object, bytes consumed)

23 24 25 26
   <encoding>_encode() interfaces also accept non-Unicode object as
   input. The objects are then converted to Unicode using
   PyUnicode_FromObject() prior to applying the conversion.

27
   These <encoding>s are available: utf_8, unicode_escape,
28 29 30
   raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit),
   mbcs (on win32).

31 32 33

Written by Marc-Andre Lemburg (mal@lemburg.com).

34
Copyright (c) Corporation for National Research Initiatives.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

   ------------------------------------------------------------------------ */

#include "Python.h"

/* --- Registry ----------------------------------------------------------- */

static
PyObject *codecregister(PyObject *self, PyObject *args)
{
    PyObject *search_function;

    if (!PyArg_ParseTuple(args, "O:register", &search_function))
        goto onError;

    if (PyCodec_Register(search_function))
	goto onError;
    
    Py_INCREF(Py_None);
    return Py_None;

 onError:
    return NULL;
}

static
PyObject *codeclookup(PyObject *self, PyObject *args)
{
    char *encoding;

    if (!PyArg_ParseTuple(args, "s:lookup", &encoding))
        goto onError;

    return _PyCodec_Lookup(encoding);

 onError:
    return NULL;
}

/* --- Helpers ------------------------------------------------------------ */

static
PyObject *codec_tuple(PyObject *unicode,
		      int len)
{
    PyObject *v,*w;
    
    if (unicode == NULL)
	return NULL;
    v = PyTuple_New(2);
    if (v == NULL) {
	Py_DECREF(unicode);
	return NULL;
    }
    PyTuple_SET_ITEM(v,0,unicode);
    w = PyInt_FromLong(len);
    if (w == NULL) {
	Py_DECREF(v);
	return NULL;
    }
    PyTuple_SET_ITEM(v,1,w);
    return v;
}

/* --- Decoder ------------------------------------------------------------ */

static PyObject *
unicode_internal_decode(PyObject *self,
			PyObject *args)
{
105 106
    PyObject *obj;
    const char *errors = NULL;
107 108 109
    const char *data;
    int size;
    
110 111 112 113 114 115 116 117 118 119 120 121 122
    if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
			  &obj, &errors))
	return NULL;

    if (PyUnicode_Check(obj))
	return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
    else {
	if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
	    return NULL;
	return codec_tuple(PyUnicode_FromUnicode((Py_UNICODE *)data,
						 size / sizeof(Py_UNICODE)),
			   size);
    }
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
}

static PyObject *
utf_8_decode(PyObject *self,
	    PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    
    if (!PyArg_ParseTuple(args, "t#|z:utf_8_decode",
			  &data, &size, &errors))
	return NULL;

    return codec_tuple(PyUnicode_DecodeUTF8(data, size, errors),
		       size);
}

static PyObject *
utf_16_decode(PyObject *self,
	    PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    int byteorder = 0;
    
    if (!PyArg_ParseTuple(args, "t#|z:utf_16_decode",
			  &data, &size, &errors))
	return NULL;
    return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder),
		       size);
}

static PyObject *
utf_16_le_decode(PyObject *self,
		 PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    int byteorder = -1;
    
    if (!PyArg_ParseTuple(args, "t#|z:utf_16_le_decode",
			  &data, &size, &errors))
	return NULL;
    return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder),
		       size);
}

static PyObject *
utf_16_be_decode(PyObject *self,
		 PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    int byteorder = 1;
    
    if (!PyArg_ParseTuple(args, "t#|z:utf_16_be_decode",
			  &data, &size, &errors))
	return NULL;
    return codec_tuple(PyUnicode_DecodeUTF16(data, size, errors, &byteorder),
		       size);
}

/* This non-standard version also provides access to the byteorder
   parameter of the builtin UTF-16 codec.

   It returns a tuple (unicode, bytesread, byteorder) with byteorder
   being the value in effect at the end of data.

*/

static PyObject *
utf_16_ex_decode(PyObject *self,
		 PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    int byteorder = 0;
    PyObject *unicode, *tuple;
    
    if (!PyArg_ParseTuple(args, "t#|zi:utf_16_ex_decode",
			  &data, &size, &errors, &byteorder))
	return NULL;

    unicode = PyUnicode_DecodeUTF16(data, size, errors, &byteorder);
    if (unicode == NULL)
	return NULL;
    tuple = Py_BuildValue("Oii", unicode, size, byteorder);
    Py_DECREF(unicode);
    return tuple;
}

static PyObject *
unicode_escape_decode(PyObject *self,
		     PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    
    if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode",
			  &data, &size, &errors))
	return NULL;

    return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors),
		       size);
}

static PyObject *
raw_unicode_escape_decode(PyObject *self,
			PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    
    if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode",
			  &data, &size, &errors))
	return NULL;

    return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors),
		       size);
}

static PyObject *
latin_1_decode(PyObject *self,
	       PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    
    if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode",
			  &data, &size, &errors))
	return NULL;

    return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors),
		       size);
}

static PyObject *
ascii_decode(PyObject *self,
	     PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    
    if (!PyArg_ParseTuple(args, "t#|z:ascii_decode",
			  &data, &size, &errors))
	return NULL;

    return codec_tuple(PyUnicode_DecodeASCII(data, size, errors),
		       size);
}

static PyObject *
charmap_decode(PyObject *self,
	       PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    PyObject *mapping = NULL;
    
    if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode",
			  &data, &size, &errors, &mapping))
	return NULL;
    if (mapping == Py_None)
	mapping = NULL;

    return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors),
		       size);
}

Guido van Rossum's avatar
Guido van Rossum committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
#ifdef MS_WIN32

static PyObject *
mbcs_decode(PyObject *self,
	    PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;
    
    if (!PyArg_ParseTuple(args, "t#|z:mbcs_decode",
			  &data, &size, &errors))
	return NULL;

    return codec_tuple(PyUnicode_DecodeMBCS(data, size, errors),
		       size);
}

#endif /* MS_WIN32 */

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
/* --- Encoder ------------------------------------------------------------ */

static PyObject *
readbuffer_encode(PyObject *self,
		  PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;

    if (!PyArg_ParseTuple(args, "s#|z:readbuffer_encode",
			  &data, &size, &errors))
	return NULL;

    return codec_tuple(PyString_FromStringAndSize(data, size),
		       size);
}

static PyObject *
charbuffer_encode(PyObject *self,
		  PyObject *args)
{
    const char *data;
    int size;
    const char *errors = NULL;

    if (!PyArg_ParseTuple(args, "t#|z:charbuffer_encode",
			  &data, &size, &errors))
	return NULL;

    return codec_tuple(PyString_FromStringAndSize(data, size),
		       size);
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
static PyObject *
unicode_internal_encode(PyObject *self,
			PyObject *args)
{
    PyObject *obj;
    const char *errors = NULL;
    const char *data;
    int size;
    
    if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
			  &obj, &errors))
	return NULL;

    if (PyUnicode_Check(obj)) {
	data = PyUnicode_AS_DATA(obj);
	size = PyUnicode_GET_DATA_SIZE(obj);
	return codec_tuple(PyString_FromStringAndSize(data, size),
			   size);
    }
    else {
	if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
	    return NULL;
	return codec_tuple(PyString_FromStringAndSize(data, size),
			   size);
    }
}

383 384 385 386
static PyObject *
utf_8_encode(PyObject *self,
	    PyObject *args)
{
387
    PyObject *str, *v;
388 389
    const char *errors = NULL;

390
    if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
391 392 393
			  &str, &errors))
	return NULL;

394 395 396 397 398 399 400 401 402
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
					 PyUnicode_GET_SIZE(str),
					 errors),
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
403 404 405 406 407 408 409 410 411 412 413 414 415
}

/* This version provides access to the byteorder parameter of the
   builtin UTF-16 codecs as optional third argument. It defaults to 0
   which means: use the native byte order and prepend the data with a
   BOM mark.  

*/

static PyObject *
utf_16_encode(PyObject *self,
	    PyObject *args)
{
416
    PyObject *str, *v;
417 418 419
    const char *errors = NULL;
    int byteorder = 0;

420
    if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
421 422 423
			  &str, &errors, &byteorder))
	return NULL;

424 425 426 427 428 429 430 431 432 433
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
					  PyUnicode_GET_SIZE(str),
					  errors,
					  byteorder),
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
434 435 436 437 438 439
}

static PyObject *
utf_16_le_encode(PyObject *self,
		 PyObject *args)
{
440
    PyObject *str, *v;
441 442
    const char *errors = NULL;

443
    if (!PyArg_ParseTuple(args, "O|zi:utf_16_le_encode",
444 445 446
			  &str, &errors))
	return NULL;

447 448 449 450
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
451 452 453 454
					     PyUnicode_GET_SIZE(str),
					     errors,
					     -1),
		       PyUnicode_GET_SIZE(str));
455 456
    Py_DECREF(str);
    return v;
457 458 459 460 461 462
}

static PyObject *
utf_16_be_encode(PyObject *self,
		 PyObject *args)
{
463
    PyObject *str, *v;
464 465
    const char *errors = NULL;

466
    if (!PyArg_ParseTuple(args, "O|zi:utf_16_be_encode",
467 468 469
			  &str, &errors))
	return NULL;

470 471 472 473 474 475 476 477 478 479
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
					  PyUnicode_GET_SIZE(str),
					  errors,
					  +1),
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
480 481 482 483 484 485
}

static PyObject *
unicode_escape_encode(PyObject *self,
		     PyObject *args)
{
486
    PyObject *str, *v;
487 488
    const char *errors = NULL;

489
    if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
490 491 492
			  &str, &errors))
	return NULL;

493 494 495 496 497 498 499 500
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str), 
						  PyUnicode_GET_SIZE(str)),
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
501 502 503 504 505 506
}

static PyObject *
raw_unicode_escape_encode(PyObject *self,
			PyObject *args)
{
507
    PyObject *str, *v;
508 509
    const char *errors = NULL;

510
    if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
511 512 513
			  &str, &errors))
	return NULL;

514 515 516 517
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
518 519
			       PyUnicode_AS_UNICODE(str), 
			       PyUnicode_GET_SIZE(str)),
520 521 522
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
523 524 525 526 527 528
}

static PyObject *
latin_1_encode(PyObject *self,
	       PyObject *args)
{
529
    PyObject *str, *v;
530 531
    const char *errors = NULL;

532
    if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
533 534 535
			  &str, &errors))
	return NULL;

536 537 538 539
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeLatin1(
540 541 542
			       PyUnicode_AS_UNICODE(str), 
			       PyUnicode_GET_SIZE(str),
			       errors),
543 544 545
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
546 547 548 549 550 551
}

static PyObject *
ascii_encode(PyObject *self,
	     PyObject *args)
{
552
    PyObject *str, *v;
553 554
    const char *errors = NULL;

555
    if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
556 557 558
			  &str, &errors))
	return NULL;

559 560 561 562
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeASCII(
563 564 565
			       PyUnicode_AS_UNICODE(str), 
			       PyUnicode_GET_SIZE(str),
			       errors),
566 567 568
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
569 570 571 572 573 574
}

static PyObject *
charmap_encode(PyObject *self,
	     PyObject *args)
{
575
    PyObject *str, *v;
576 577 578
    const char *errors = NULL;
    PyObject *mapping = NULL;

579
    if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
580 581 582 583 584
			  &str, &errors, &mapping))
	return NULL;
    if (mapping == Py_None)
	mapping = NULL;

585 586 587 588
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeCharmap(
589 590 591 592
			       PyUnicode_AS_UNICODE(str), 
			       PyUnicode_GET_SIZE(str),
			       mapping, 
			       errors),
593 594 595
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
596 597
}

Guido van Rossum's avatar
Guido van Rossum committed
598 599 600 601 602 603
#ifdef MS_WIN32

static PyObject *
mbcs_encode(PyObject *self,
	    PyObject *args)
{
604
    PyObject *str, *v;
Guido van Rossum's avatar
Guido van Rossum committed
605 606
    const char *errors = NULL;

607
    if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
Guido van Rossum's avatar
Guido van Rossum committed
608 609 610
			  &str, &errors))
	return NULL;

611 612 613 614
    str = PyUnicode_FromObject(str);
    if (str == NULL)
	return NULL;
    v = codec_tuple(PyUnicode_EncodeMBCS(
Guido van Rossum's avatar
Guido van Rossum committed
615 616 617
			       PyUnicode_AS_UNICODE(str), 
			       PyUnicode_GET_SIZE(str),
			       errors),
618 619 620
		    PyUnicode_GET_SIZE(str));
    Py_DECREF(str);
    return v;
Guido van Rossum's avatar
Guido van Rossum committed
621 622 623 624
}

#endif /* MS_WIN32 */

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
/* --- Module API --------------------------------------------------------- */

static PyMethodDef _codecs_functions[] = {
    {"register",		codecregister,			1},
    {"lookup",			codeclookup, 			1},
    {"utf_8_encode",		utf_8_encode,			1},
    {"utf_8_decode",		utf_8_decode,			1},
    {"utf_16_encode",		utf_16_encode,			1},
    {"utf_16_le_encode",	utf_16_le_encode,		1},
    {"utf_16_be_encode",	utf_16_be_encode,		1},
    {"utf_16_decode",		utf_16_decode,			1},
    {"utf_16_le_decode",	utf_16_le_decode,		1},
    {"utf_16_be_decode",	utf_16_be_decode,		1},
    {"utf_16_ex_decode",	utf_16_ex_decode,		1},
    {"unicode_escape_encode",	unicode_escape_encode,		1},
    {"unicode_escape_decode",	unicode_escape_decode,		1},
641
    {"unicode_internal_encode",	unicode_internal_encode,	1},
642 643 644 645 646 647 648 649 650 651 652
    {"unicode_internal_decode",	unicode_internal_decode,	1},
    {"raw_unicode_escape_encode", raw_unicode_escape_encode,	1},
    {"raw_unicode_escape_decode", raw_unicode_escape_decode,	1},
    {"latin_1_encode", 		latin_1_encode,			1},
    {"latin_1_decode", 		latin_1_decode,			1},
    {"ascii_encode", 		ascii_encode,			1},
    {"ascii_decode", 		ascii_decode,			1},
    {"charmap_encode", 		charmap_encode,			1},
    {"charmap_decode", 		charmap_decode,			1},
    {"readbuffer_encode",	readbuffer_encode,		1},
    {"charbuffer_encode",	charbuffer_encode,		1},
Guido van Rossum's avatar
Guido van Rossum committed
653 654 655 656
#ifdef MS_WIN32
    {"mbcs_encode", 		mbcs_encode,			1},
    {"mbcs_decode", 		mbcs_decode,			1},
#endif
657 658 659 660
    {NULL, NULL}		/* sentinel */
};

DL_EXPORT(void)
661
init_codecs(void)
662 663 664
{
    Py_InitModule("_codecs", _codecs_functions);
}