485
485
PyObject *result = NULL;
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)
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;
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
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;
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);
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,
524
if (format_spec_object == NULL)
527
result = PyObject_Format(fieldobj, format_spec_object);
493
529
if (result == NULL)