~pythonregexp2.7/python/issue2636-11

« back to all changes in this revision

Viewing changes to Modules/_codecsmodule.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-21 17:53:26 UTC
  • mfrom: (39025.1.14 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080921175326-92vaej2hc3yuecxb
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
232
232
utf_7_decode(PyObject *self,
233
233
             PyObject *args)
234
234
{
235
 
    const char *data;
236
 
    Py_ssize_t size;
 
235
        Py_buffer pbuf;
237
236
    const char *errors = NULL;
238
237
    int final = 0;
239
238
    Py_ssize_t consumed;
240
239
    PyObject *decoded = NULL;
241
240
 
242
 
    if (!PyArg_ParseTuple(args, "t#|zi:utf_7_decode",
243
 
                          &data, &size, &errors, &final))
244
 
        return NULL;
245
 
    consumed = size;
 
241
    if (!PyArg_ParseTuple(args, "s*|zi:utf_7_decode",
 
242
                          &pbuf, &errors, &final))
 
243
        return NULL;
 
244
    consumed = pbuf.len;
246
245
 
247
 
    decoded = PyUnicode_DecodeUTF7Stateful(data, size, errors,
248
 
                                           final ? NULL : &consumed);
 
246
    decoded = PyUnicode_DecodeUTF7Stateful(pbuf.buf, pbuf.len, errors,
 
247
                                           final ? NULL : &consumed);
 
248
        PyBuffer_Release(&pbuf);
249
249
    if (decoded == NULL)
250
250
        return NULL;
251
251
    return codec_tuple(decoded, consumed);
255
255
utf_8_decode(PyObject *self,
256
256
            PyObject *args)
257
257
{
258
 
    const char *data;
259
 
    Py_ssize_t size;
 
258
        Py_buffer pbuf;
260
259
    const char *errors = NULL;
261
260
    int final = 0;
262
261
    Py_ssize_t consumed;
263
262
    PyObject *decoded = NULL;
264
263
 
265
 
    if (!PyArg_ParseTuple(args, "t#|zi:utf_8_decode",
266
 
                          &data, &size, &errors, &final))
 
264
    if (!PyArg_ParseTuple(args, "s*|zi:utf_8_decode",
 
265
                          &pbuf, &errors, &final))
267
266
        return NULL;
268
 
    if (size < 0) {
269
 
            PyErr_SetString(PyExc_ValueError, "negative argument");
270
 
            return 0;
271
 
    }
272
 
    consumed = size;
273
 
        
274
 
    decoded = PyUnicode_DecodeUTF8Stateful(data, size, errors,
 
267
    consumed = pbuf.len;
 
268
 
 
269
    decoded = PyUnicode_DecodeUTF8Stateful(pbuf.buf, pbuf.len, errors,
275
270
                                           final ? NULL : &consumed);
 
271
        PyBuffer_Release(&pbuf);
276
272
    if (decoded == NULL)
277
273
        return NULL;
278
274
    return codec_tuple(decoded, consumed);
282
278
utf_16_decode(PyObject *self,
283
279
            PyObject *args)
284
280
{
285
 
    const char *data;
286
 
    Py_ssize_t size;
 
281
        Py_buffer pbuf;
287
282
    const char *errors = NULL;
288
283
    int byteorder = 0;
289
284
    int final = 0;
290
285
    Py_ssize_t consumed;
291
286
    PyObject *decoded;
292
287
 
293
 
    if (!PyArg_ParseTuple(args, "t#|zi:utf_16_decode",
294
 
                          &data, &size, &errors, &final))
 
288
    if (!PyArg_ParseTuple(args, "s*|zi:utf_16_decode",
 
289
                          &pbuf, &errors, &final))
295
290
        return NULL;
296
 
    if (size < 0) {
297
 
            PyErr_SetString(PyExc_ValueError, "negative argument");
298
 
            return 0;
299
 
    }
300
 
    consumed = size; /* This is overwritten unless final is true. */
301
 
    decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
302
 
                                            final ? NULL : &consumed);
 
291
    consumed = pbuf.len; /* This is overwritten unless final is true. */
 
292
    decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
 
293
                                        &byteorder, final ? NULL : &consumed);
 
294
        PyBuffer_Release(&pbuf);
303
295
    if (decoded == NULL)
304
296
        return NULL;
305
297
    return codec_tuple(decoded, consumed);
309
301
utf_16_le_decode(PyObject *self,
310
302
                 PyObject *args)
311
303
{
312
 
    const char *data;
313
 
    Py_ssize_t size;
 
304
        Py_buffer pbuf;
314
305
    const char *errors = NULL;
315
306
    int byteorder = -1;
316
307
    int final = 0;
317
308
    Py_ssize_t consumed;
318
309
    PyObject *decoded = NULL;
319
310
 
320
 
    if (!PyArg_ParseTuple(args, "t#|zi:utf_16_le_decode",
321
 
                          &data, &size, &errors, &final))
 
311
    if (!PyArg_ParseTuple(args, "s*|zi:utf_16_le_decode",
 
312
                          &pbuf, &errors, &final))
322
313
        return NULL;
323
314
 
324
 
    if (size < 0) {
325
 
          PyErr_SetString(PyExc_ValueError, "negative argument");
326
 
          return 0;
327
 
    }
328
 
    consumed = size; /* This is overwritten unless final is true. */
329
 
    decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
 
315
    consumed = pbuf.len; /* This is overwritten unless final is true. */
 
316
    decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
330
317
        &byteorder, final ? NULL : &consumed);
 
318
        PyBuffer_Release(&pbuf);
331
319
    if (decoded == NULL)
332
320
        return NULL;
333
321
    return codec_tuple(decoded, consumed);
334
 
 
335
322
}
336
323
 
337
324
static PyObject *
338
325
utf_16_be_decode(PyObject *self,
339
326
                 PyObject *args)
340
327
{
341
 
    const char *data;
342
 
    Py_ssize_t size;
 
328
        Py_buffer pbuf;
343
329
    const char *errors = NULL;
344
330
    int byteorder = 1;
345
331
    int final = 0;
346
332
    Py_ssize_t consumed;
347
333
    PyObject *decoded = NULL;
348
334
 
349
 
    if (!PyArg_ParseTuple(args, "t#|zi:utf_16_be_decode",
350
 
                          &data, &size, &errors, &final))
 
335
    if (!PyArg_ParseTuple(args, "s*|zi:utf_16_be_decode",
 
336
                          &pbuf, &errors, &final))
351
337
        return NULL;
352
 
    if (size < 0) {
353
 
          PyErr_SetString(PyExc_ValueError, "negative argument");
354
 
          return 0;
355
 
    }
356
 
    consumed = size; /* This is overwritten unless final is true. */
357
 
    decoded = PyUnicode_DecodeUTF16Stateful(data, size, errors,
 
338
 
 
339
    consumed = pbuf.len; /* This is overwritten unless final is true. */
 
340
    decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
358
341
        &byteorder, final ? NULL : &consumed);
 
342
        PyBuffer_Release(&pbuf);
359
343
    if (decoded == NULL)
360
344
        return NULL;
361
345
    return codec_tuple(decoded, consumed);
373
357
utf_16_ex_decode(PyObject *self,
374
358
                 PyObject *args)
375
359
{
376
 
    const char *data;
377
 
    Py_ssize_t size;
 
360
        Py_buffer pbuf;
378
361
    const char *errors = NULL;
379
362
    int byteorder = 0;
380
363
    PyObject *unicode, *tuple;
381
364
    int final = 0;
382
365
    Py_ssize_t consumed;
383
366
 
384
 
    if (!PyArg_ParseTuple(args, "t#|zii:utf_16_ex_decode",
385
 
                          &data, &size, &errors, &byteorder, &final))
 
367
    if (!PyArg_ParseTuple(args, "s*|zii:utf_16_ex_decode",
 
368
                          &pbuf, &errors, &byteorder, &final))
386
369
        return NULL;
387
 
    if (size < 0) {
388
 
            PyErr_SetString(PyExc_ValueError, "negative argument");
389
 
            return 0;
390
 
    }
391
 
    consumed = size; /* This is overwritten unless final is true. */
392
 
    unicode = PyUnicode_DecodeUTF16Stateful(data, size, errors, &byteorder,
393
 
                                            final ? NULL : &consumed);
 
370
    consumed = pbuf.len; /* This is overwritten unless final is true. */
 
371
    unicode = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
 
372
                                        &byteorder, final ? NULL : &consumed);
 
373
        PyBuffer_Release(&pbuf);
394
374
    if (unicode == NULL)
395
375
        return NULL;
396
376
    tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
402
382
utf_32_decode(PyObject *self,
403
383
            PyObject *args)
404
384
{
405
 
    const char *data;
406
 
    Py_ssize_t size;
 
385
        Py_buffer pbuf;
407
386
    const char *errors = NULL;
408
387
    int byteorder = 0;
409
388
    int final = 0;
410
389
    Py_ssize_t consumed;
411
390
    PyObject *decoded;
412
391
 
413
 
    if (!PyArg_ParseTuple(args, "t#|zi:utf_32_decode",
414
 
                          &data, &size, &errors, &final))
 
392
    if (!PyArg_ParseTuple(args, "s*|zi:utf_32_decode",
 
393
                          &pbuf, &errors, &final))
415
394
        return NULL;
416
 
    if (size < 0) {
417
 
            PyErr_SetString(PyExc_ValueError, "negative argument");
418
 
            return 0;
419
 
    }
420
 
    consumed = size; /* This is overwritten unless final is true. */
421
 
    decoded = PyUnicode_DecodeUTF32Stateful(data, size, errors, &byteorder,
422
 
                                            final ? NULL : &consumed);
 
395
    consumed = pbuf.len; /* This is overwritten unless final is true. */
 
396
    decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
 
397
                                        &byteorder, final ? NULL : &consumed);
 
398
        PyBuffer_Release(&pbuf);
423
399
    if (decoded == NULL)
424
400
        return NULL;
425
401
    return codec_tuple(decoded, consumed);
429
405
utf_32_le_decode(PyObject *self,
430
406
                 PyObject *args)
431
407
{
432
 
    const char *data;
433
 
    Py_ssize_t size;
 
408
        Py_buffer pbuf;
434
409
    const char *errors = NULL;
435
410
    int byteorder = -1;
436
411
    int final = 0;
437
412
    Py_ssize_t consumed;
438
 
    PyObject *decoded = NULL;
 
413
    PyObject *decoded;
439
414
 
440
 
    if (!PyArg_ParseTuple(args, "t#|zi:utf_32_le_decode",
441
 
                          &data, &size, &errors, &final))
 
415
    if (!PyArg_ParseTuple(args, "s*|zi:utf_32_le_decode",
 
416
                          &pbuf, &errors, &final))
442
417
        return NULL;
443
 
 
444
 
    if (size < 0) {
445
 
          PyErr_SetString(PyExc_ValueError, "negative argument");
446
 
          return 0;
447
 
    }
448
 
    consumed = size; /* This is overwritten unless final is true. */
449
 
    decoded = PyUnicode_DecodeUTF32Stateful(data, size, errors,
450
 
        &byteorder, final ? NULL : &consumed);
 
418
    consumed = pbuf.len; /* This is overwritten unless final is true. */
 
419
    decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
 
420
                                        &byteorder, final ? NULL : &consumed);
 
421
        PyBuffer_Release(&pbuf);
451
422
    if (decoded == NULL)
452
423
        return NULL;
453
424
    return codec_tuple(decoded, consumed);
454
 
 
455
425
}
456
426
 
457
427
static PyObject *
458
428
utf_32_be_decode(PyObject *self,
459
429
                 PyObject *args)
460
430
{
461
 
    const char *data;
462
 
    Py_ssize_t size;
 
431
        Py_buffer pbuf;
463
432
    const char *errors = NULL;
464
433
    int byteorder = 1;
465
434
    int final = 0;
466
435
    Py_ssize_t consumed;
467
 
    PyObject *decoded = NULL;
 
436
    PyObject *decoded;
468
437
 
469
 
    if (!PyArg_ParseTuple(args, "t#|zi:utf_32_be_decode",
470
 
                          &data, &size, &errors, &final))
 
438
    if (!PyArg_ParseTuple(args, "s*|zi:utf_32_be_decode",
 
439
                          &pbuf, &errors, &final))
471
440
        return NULL;
472
 
    if (size < 0) {
473
 
          PyErr_SetString(PyExc_ValueError, "negative argument");
474
 
          return 0;
475
 
    }
476
 
    consumed = size; /* This is overwritten unless final is true. */
477
 
    decoded = PyUnicode_DecodeUTF32Stateful(data, size, errors,
478
 
        &byteorder, final ? NULL : &consumed);
 
441
    consumed = pbuf.len; /* This is overwritten unless final is true. */
 
442
    decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
 
443
                                        &byteorder, final ? NULL : &consumed);
 
444
        PyBuffer_Release(&pbuf);
479
445
    if (decoded == NULL)
480
446
        return NULL;
481
447
    return codec_tuple(decoded, consumed);
493
459
utf_32_ex_decode(PyObject *self,
494
460
                 PyObject *args)
495
461
{
496
 
    const char *data;
497
 
    Py_ssize_t size;
 
462
        Py_buffer pbuf;
498
463
    const char *errors = NULL;
499
464
    int byteorder = 0;
500
465
    PyObject *unicode, *tuple;
501
466
    int final = 0;
502
467
    Py_ssize_t consumed;
503
468
 
504
 
    if (!PyArg_ParseTuple(args, "t#|zii:utf_32_ex_decode",
505
 
                          &data, &size, &errors, &byteorder, &final))
 
469
    if (!PyArg_ParseTuple(args, "s*|zii:utf_32_ex_decode",
 
470
                          &pbuf, &errors, &byteorder, &final))
506
471
        return NULL;
507
 
    if (size < 0) {
508
 
            PyErr_SetString(PyExc_ValueError, "negative argument");
509
 
            return 0;
510
 
    }
511
 
    consumed = size; /* This is overwritten unless final is true. */
512
 
    unicode = PyUnicode_DecodeUTF32Stateful(data, size, errors, &byteorder,
513
 
                                            final ? NULL : &consumed);
 
472
    consumed = pbuf.len; /* This is overwritten unless final is true. */
 
473
    unicode = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
 
474
                                        &byteorder, final ? NULL : &consumed);
 
475
        PyBuffer_Release(&pbuf);
514
476
    if (unicode == NULL)
515
477
        return NULL;
516
478
    tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
522
484
unicode_escape_decode(PyObject *self,
523
485
                     PyObject *args)
524
486
{
525
 
    const char *data;
526
 
    Py_ssize_t size;
 
487
        Py_buffer pbuf;
527
488
    const char *errors = NULL;
 
489
        PyObject *unicode;
528
490
 
529
 
    if (!PyArg_ParseTuple(args, "t#|z:unicode_escape_decode",
530
 
                          &data, &size, &errors))
 
491
    if (!PyArg_ParseTuple(args, "s*|z:unicode_escape_decode",
 
492
                          &pbuf, &errors))
531
493
        return NULL;
532
494
 
533
 
    return codec_tuple(PyUnicode_DecodeUnicodeEscape(data, size, errors),
534
 
                       size);
 
495
        unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors);
 
496
        PyBuffer_Release(&pbuf);
 
497
        return codec_tuple(unicode, pbuf.len);
535
498
}
536
499
 
537
500
static PyObject *
538
501
raw_unicode_escape_decode(PyObject *self,
539
502
                        PyObject *args)
540
503
{
541
 
    const char *data;
542
 
    Py_ssize_t size;
 
504
        Py_buffer pbuf;
543
505
    const char *errors = NULL;
 
506
        PyObject *unicode;
544
507
 
545
 
    if (!PyArg_ParseTuple(args, "t#|z:raw_unicode_escape_decode",
546
 
                          &data, &size, &errors))
 
508
    if (!PyArg_ParseTuple(args, "s*|z:raw_unicode_escape_decode",
 
509
                          &pbuf, &errors))
547
510
        return NULL;
548
511
 
549
 
    return codec_tuple(PyUnicode_DecodeRawUnicodeEscape(data, size, errors),
550
 
                       size);
 
512
        unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors);
 
513
        PyBuffer_Release(&pbuf);
 
514
        return codec_tuple(unicode, pbuf.len);
551
515
}
552
516
 
553
517
static PyObject *
554
518
latin_1_decode(PyObject *self,
555
519
               PyObject *args)
556
520
{
557
 
    const char *data;
558
 
    Py_ssize_t size;
 
521
        Py_buffer pbuf;
 
522
        PyObject *unicode;
559
523
    const char *errors = NULL;
560
524
 
561
 
    if (!PyArg_ParseTuple(args, "t#|z:latin_1_decode",
562
 
                          &data, &size, &errors))
 
525
    if (!PyArg_ParseTuple(args, "s*|z:latin_1_decode",
 
526
                          &pbuf, &errors))
563
527
        return NULL;
564
528
 
565
 
    return codec_tuple(PyUnicode_DecodeLatin1(data, size, errors),
566
 
                       size);
 
529
        unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors);
 
530
        PyBuffer_Release(&pbuf);
 
531
        return codec_tuple(unicode, pbuf.len);
567
532
}
568
533
 
569
534
static PyObject *
570
535
ascii_decode(PyObject *self,
571
536
             PyObject *args)
572
537
{
573
 
    const char *data;
574
 
    Py_ssize_t size;
 
538
        Py_buffer pbuf;
 
539
        PyObject *unicode;
575
540
    const char *errors = NULL;
576
541
 
577
 
    if (!PyArg_ParseTuple(args, "t#|z:ascii_decode",
578
 
                          &data, &size, &errors))
 
542
    if (!PyArg_ParseTuple(args, "s*|z:ascii_decode",
 
543
                          &pbuf, &errors))
579
544
        return NULL;
580
545
 
581
 
    return codec_tuple(PyUnicode_DecodeASCII(data, size, errors),
582
 
                       size);
 
546
        unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors);
 
547
        PyBuffer_Release(&pbuf);
 
548
        return codec_tuple(unicode, pbuf.len);
583
549
}
584
550
 
585
551
static PyObject *
586
552
charmap_decode(PyObject *self,
587
553
               PyObject *args)
588
554
{
589
 
    const char *data;
590
 
    Py_ssize_t size;
 
555
        Py_buffer pbuf;
 
556
        PyObject *unicode;
591
557
    const char *errors = NULL;
592
558
    PyObject *mapping = NULL;
593
559
 
594
 
    if (!PyArg_ParseTuple(args, "t#|zO:charmap_decode",
595
 
                          &data, &size, &errors, &mapping))
 
560
    if (!PyArg_ParseTuple(args, "s*|zO:charmap_decode",
 
561
                          &pbuf, &errors, &mapping))
596
562
        return NULL;
597
563
    if (mapping == Py_None)
598
564
        mapping = NULL;
599
565
 
600
 
    return codec_tuple(PyUnicode_DecodeCharmap(data, size, mapping, errors),
601
 
                       size);
 
566
        unicode = PyUnicode_DecodeCharmap(pbuf.buf, pbuf.len, mapping, errors);
 
567
        PyBuffer_Release(&pbuf);
 
568
        return codec_tuple(unicode, pbuf.len);
602
569
}
603
570
 
604
571
#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
607
574
mbcs_decode(PyObject *self,
608
575
            PyObject *args)
609
576
{
610
 
    const char *data;
611
 
    Py_ssize_t size, consumed;
 
577
        Py_buffer pbuf;
612
578
    const char *errors = NULL;
613
579
    int final = 0;
614
 
    PyObject *decoded;
615
 
 
616
 
    if (!PyArg_ParseTuple(args, "t#|zi:mbcs_decode",
617
 
                          &data, &size, &errors, &final))
618
 
        return NULL;
619
 
 
620
 
    decoded = PyUnicode_DecodeMBCSStateful(
621
 
        data, size, errors, final ? NULL : &consumed);
622
 
    if (!decoded)
623
 
        return NULL;
624
 
    return codec_tuple(decoded, final ? size : consumed);
 
580
    Py_ssize_t consumed;
 
581
    PyObject *decoded = NULL;
 
582
 
 
583
    if (!PyArg_ParseTuple(args, "s*|zi:mbcs_decode",
 
584
                          &pbuf, &errors, &final))
 
585
        return NULL;
 
586
    consumed = pbuf.len;
 
587
 
 
588
    decoded = PyUnicode_DecodeMBCSStateful(pbuf.buf, pbuf.len, errors,
 
589
                                           final ? NULL : &consumed);
 
590
        PyBuffer_Release(&pbuf);
 
591
    if (decoded == NULL)
 
592
        return NULL;
 
593
    return codec_tuple(decoded, consumed);
625
594
}
626
595
 
627
596
#endif /* MS_WINDOWS */