~pythonregexp2.7/python/issue2636-09-01+10

« back to all changes in this revision

Viewing changes to Objects/stringlib/string_format.h

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 21:39:45 UTC
  • mfrom: (39055.1.33 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922213945-23717m5eiqpamcyn
Merged in changes from the Single-Loop Engine branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
483
483
{
484
484
    int ok = 0;
485
485
    PyObject *result = NULL;
486
 
 
487
 
    /* we need to create an object out of the pointers we have */
488
 
    PyObject *format_spec_object = SubString_new_object_or_empty(format_spec);
489
 
    if (format_spec_object == NULL)
490
 
        goto done;
491
 
 
492
 
    result = PyObject_Format(fieldobj, format_spec_object);
 
486
    PyObject *format_spec_object = NULL;
 
487
    PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL;
 
488
    STRINGLIB_CHAR* format_spec_start = format_spec->ptr ?
 
489
            format_spec->ptr : NULL;
 
490
    Py_ssize_t format_spec_len = format_spec->ptr ?
 
491
            format_spec->end - format_spec->ptr : 0;
 
492
 
 
493
    /* If we know the type exactly, skip the lookup of __format__ and just
 
494
       call the formatter directly. */
 
495
#if STRINGLIB_IS_UNICODE
 
496
    if (PyUnicode_CheckExact(fieldobj))
 
497
        formatter = _PyUnicode_FormatAdvanced;
 
498
    /* Unfortunately, there's a problem with checking for int, long,
 
499
       and float here.  If we're being included as unicode, their
 
500
       formatters expect string format_spec args.  For now, just skip
 
501
       this optimization for unicode.  This could be fixed, but it's a
 
502
       hassle. */
 
503
#else
 
504
    if (PyString_CheckExact(fieldobj))
 
505
        formatter = _PyBytes_FormatAdvanced;
 
506
    else if (PyInt_CheckExact(fieldobj))
 
507
        formatter =_PyInt_FormatAdvanced;
 
508
    else if (PyLong_CheckExact(fieldobj))
 
509
        formatter =_PyLong_FormatAdvanced;
 
510
    else if (PyFloat_CheckExact(fieldobj))
 
511
        formatter = _PyFloat_FormatAdvanced;
 
512
#endif
 
513
 
 
514
    if (formatter) {
 
515
        /* we know exactly which formatter will be called when __format__ is
 
516
           looked up, so call it directly, instead. */
 
517
        result = formatter(fieldobj, format_spec_start, format_spec_len);
 
518
    }
 
519
    else {
 
520
        /* We need to create an object out of the pointers we have, because
 
521
           __format__ takes a string/unicode object for format_spec. */
 
522
        format_spec_object = STRINGLIB_NEW(format_spec_start,
 
523
                                           format_spec_len);
 
524
        if (format_spec_object == NULL)
 
525
            goto done;
 
526
 
 
527
        result = PyObject_Format(fieldobj, format_spec_object);
 
528
    }
493
529
    if (result == NULL)
494
530
        goto done;
495
531
 
512
548
    ok = output_data(output,
513
549
                     STRINGLIB_STR(result), STRINGLIB_LEN(result));
514
550
done:
515
 
    Py_DECREF(format_spec_object);
 
551
    Py_XDECREF(format_spec_object);
516
552
    Py_XDECREF(result);
517
553
    return ok;
518
554
}