1 /* implements the string, long, and float formatters.  that is,
2    string.__format__, etc. */
3 
4 #include <locale.h>
5 
6 /* Before including this, you must include either:
7    stringlib/unicodedefs.h
8    stringlib/stringdefs.h
9 
10    Also, you should define the names:
11    FORMAT_STRING
12    FORMAT_LONG
13    FORMAT_FLOAT
14    FORMAT_COMPLEX
15    to be whatever you want the public names of these functions to
16    be.  These are the only non-static functions defined here.
17 */
18 
19 /* Raises an exception about an unknown presentation type for this
20  * type. */
21 
22 static void
unknown_presentation_type(STRINGLIB_CHAR presentation_type,const char * type_name)23 unknown_presentation_type(STRINGLIB_CHAR presentation_type,
24                           const char* type_name)
25 {
26 #if STRINGLIB_IS_UNICODE
27     /* If STRINGLIB_CHAR is Py_UNICODE, %c might be out-of-range,
28        hence the two cases. If it is char, gcc complains that the
29        condition below is always true, hence the ifdef. */
30     if (presentation_type > 32 && presentation_type < 128)
31 #endif
32         PyErr_Format(PyExc_ValueError,
33                      "Unknown format code '%c' "
34                      "for object of type '%.200s'",
35                      (char)presentation_type,
36                      type_name);
37 #if STRINGLIB_IS_UNICODE
38     else
39         PyErr_Format(PyExc_ValueError,
40                      "Unknown format code '\\x%x' "
41                      "for object of type '%.200s'",
42                      (unsigned int)presentation_type,
43                      type_name);
44 #endif
45 }
46 
47 static void
invalid_comma_type(STRINGLIB_CHAR presentation_type)48 invalid_comma_type(STRINGLIB_CHAR presentation_type)
49 {
50 #if STRINGLIB_IS_UNICODE
51     /* See comment in unknown_presentation_type */
52     if (presentation_type > 32 && presentation_type < 128)
53 #endif
54         PyErr_Format(PyExc_ValueError,
55                      "Cannot specify ',' with '%c'.",
56                      (char)presentation_type);
57 #if STRINGLIB_IS_UNICODE
58     else
59         PyErr_Format(PyExc_ValueError,
60                      "Cannot specify ',' with '\\x%x'.",
61                      (unsigned int)presentation_type);
62 #endif
63 }
64 
65 /*
66     get_integer consumes 0 or more decimal digit characters from an
67     input string, updates *result with the corresponding positive
68     integer, and returns the number of digits consumed.
69 
70     returns -1 on error.
71 */
72 static int
get_integer(STRINGLIB_CHAR ** ptr,STRINGLIB_CHAR * end,Py_ssize_t * result)73 get_integer(STRINGLIB_CHAR **ptr, STRINGLIB_CHAR *end,
74                   Py_ssize_t *result)
75 {
76     Py_ssize_t accumulator, digitval, oldaccumulator;
77     int numdigits;
78     accumulator = numdigits = 0;
79     for (;;(*ptr)++, numdigits++) {
80         if (*ptr >= end)
81             break;
82         digitval = STRINGLIB_TODECIMAL(**ptr);
83         if (digitval < 0)
84             break;
85         /*
86            This trick was copied from old Unicode format code.  It's cute,
87            but would really suck on an old machine with a slow divide
88            implementation.  Fortunately, in the normal case we do not
89            expect too many digits.
90         */
91         oldaccumulator = accumulator;
92         accumulator *= 10;
93         if ((accumulator+10)/10 != oldaccumulator+1) {
94             PyErr_Format(PyExc_ValueError,
95                          "Too many decimal digits in format string");
96             return -1;
97         }
98         accumulator += digitval;
99     }
100     *result = accumulator;
101     return numdigits;
102 }
103 
104 /************************************************************************/
105 /*********** standard format specifier parsing **************************/
106 /************************************************************************/
107 
108 /* returns true if this character is a specifier alignment token */
109 Py_LOCAL_INLINE(int)
is_alignment_token(STRINGLIB_CHAR c)110 is_alignment_token(STRINGLIB_CHAR c)
111 {
112     switch (c) {
113     case '<': case '>': case '=': case '^':
114         return 1;
115     default:
116         return 0;
117     }
118 }
119 
120 /* returns true if this character is a sign element */
121 Py_LOCAL_INLINE(int)
is_sign_element(STRINGLIB_CHAR c)122 is_sign_element(STRINGLIB_CHAR c)
123 {
124     switch (c) {
125     case ' ': case '+': case '-':
126         return 1;
127     default:
128         return 0;
129     }
130 }
131 
132 
133 typedef struct {
134     STRINGLIB_CHAR fill_char;
135     STRINGLIB_CHAR align;
136     int alternate;
137     STRINGLIB_CHAR sign;
138     Py_ssize_t width;
139     int thousands_separators;
140     Py_ssize_t precision;
141     STRINGLIB_CHAR type;
142 } InternalFormatSpec;
143 
144 
145 #if 0
146 /* Occassionally useful for debugging. Should normally be commented out. */
147 static void
148 DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
149 {
150     printf("internal format spec: fill_char %d\n", format->fill_char);
151     printf("internal format spec: align %d\n", format->align);
152     printf("internal format spec: alternate %d\n", format->alternate);
153     printf("internal format spec: sign %d\n", format->sign);
154     printf("internal format spec: width %zd\n", format->width);
155     printf("internal format spec: thousands_separators %d\n",
156            format->thousands_separators);
157     printf("internal format spec: precision %zd\n", format->precision);
158     printf("internal format spec: type %c\n", format->type);
159     printf("\n");
160 }
161 #endif
162 
163 
164 /*
165   ptr points to the start of the format_spec, end points just past its end.
166   fills in format with the parsed information.
167   returns 1 on success, 0 on failure.
168   if failure, sets the exception
169 */
170 static int
parse_internal_render_format_spec(STRINGLIB_CHAR * format_spec,Py_ssize_t format_spec_len,InternalFormatSpec * format,char default_type,char default_align)171 parse_internal_render_format_spec(STRINGLIB_CHAR *format_spec,
172                                   Py_ssize_t format_spec_len,
173                                   InternalFormatSpec *format,
174                                   char default_type,
175                                   char default_align)
176 {
177     STRINGLIB_CHAR *ptr = format_spec;
178     STRINGLIB_CHAR *end = format_spec + format_spec_len;
179 
180     /* end-ptr is used throughout this code to specify the length of
181        the input string */
182 
183     Py_ssize_t consumed;
184     int align_specified = 0;
185 
186     format->fill_char = '\0';
187     format->align = default_align;
188     format->alternate = 0;
189     format->sign = '\0';
190     format->width = -1;
191     format->thousands_separators = 0;
192     format->precision = -1;
193     format->type = default_type;
194 
195     /* If the second char is an alignment token,
196        then parse the fill char */
197     if (end-ptr >= 2 && is_alignment_token(ptr[1])) {
198         format->align = ptr[1];
199         format->fill_char = ptr[0];
200         align_specified = 1;
201         ptr += 2;
202     }
203     else if (end-ptr >= 1 && is_alignment_token(ptr[0])) {
204         format->align = ptr[0];
205         align_specified = 1;
206         ++ptr;
207     }
208 
209     /* Parse the various sign options */
210     if (end-ptr >= 1 && is_sign_element(ptr[0])) {
211         format->sign = ptr[0];
212         ++ptr;
213     }
214 
215     /* If the next character is #, we're in alternate mode.  This only
216        applies to integers. */
217     if (end-ptr >= 1 && ptr[0] == '#') {
218         format->alternate = 1;
219         ++ptr;
220     }
221 
222     /* The special case for 0-padding (backwards compat) */
223     if (format->fill_char == '\0' && end-ptr >= 1 && ptr[0] == '0') {
224         format->fill_char = '0';
225         if (!align_specified) {
226             format->align = '=';
227         }
228         ++ptr;
229     }
230 
231     consumed = get_integer(&ptr, end, &format->width);
232     if (consumed == -1)
233         /* Overflow error. Exception already set. */
234         return 0;
235 
236     /* If consumed is 0, we didn't consume any characters for the
237        width. In that case, reset the width to -1, because
238        get_integer() will have set it to zero. -1 is how we record
239        that the width wasn't specified. */
240     if (consumed == 0)
241         format->width = -1;
242 
243     /* Comma signifies add thousands separators */
244     if (end-ptr && ptr[0] == ',') {
245         format->thousands_separators = 1;
246         ++ptr;
247     }
248 
249     /* Parse field precision */
250     if (end-ptr && ptr[0] == '.') {
251         ++ptr;
252 
253         consumed = get_integer(&ptr, end, &format->precision);
254         if (consumed == -1)
255             /* Overflow error. Exception already set. */
256             return 0;
257 
258         /* Not having a precision after a dot is an error. */
259         if (consumed == 0) {
260             PyErr_Format(PyExc_ValueError,
261                          "Format specifier missing precision");
262             return 0;
263         }
264 
265     }
266 
267     /* Finally, parse the type field. */
268 
269     if (end-ptr > 1) {
270         /* More than one char remain, invalid conversion spec. */
271         PyErr_Format(PyExc_ValueError, "Invalid conversion specification");
272         return 0;
273     }
274 
275     if (end-ptr == 1) {
276         format->type = ptr[0];
277         ++ptr;
278     }
279 
280     /* Do as much validating as we can, just by looking at the format
281        specifier.  Do not take into account what type of formatting
282        we're doing (int, float, string). */
283 
284     if (format->thousands_separators) {
285         switch (format->type) {
286         case 'd':
287         case 'e':
288         case 'f':
289         case 'g':
290         case 'E':
291         case 'G':
292         case '%':
293         case 'F':
294         case '\0':
295             /* These are allowed. See PEP 378.*/
296             break;
297         default:
298             invalid_comma_type(format->type);
299             return 0;
300         }
301     }
302 
303     return 1;
304 }
305 
306 /* Calculate the padding needed. */
307 static void
calc_padding(Py_ssize_t nchars,Py_ssize_t width,STRINGLIB_CHAR align,Py_ssize_t * n_lpadding,Py_ssize_t * n_rpadding,Py_ssize_t * n_total)308 calc_padding(Py_ssize_t nchars, Py_ssize_t width, STRINGLIB_CHAR align,
309              Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
310              Py_ssize_t *n_total)
311 {
312     if (width >= 0) {
313         if (nchars > width)
314             *n_total = nchars;
315         else
316             *n_total = width;
317     }
318     else {
319         /* not specified, use all of the chars and no more */
320         *n_total = nchars;
321     }
322 
323     /* Figure out how much leading space we need, based on the
324        aligning */
325     if (align == '>')
326         *n_lpadding = *n_total - nchars;
327     else if (align == '^')
328         *n_lpadding = (*n_total - nchars) / 2;
329     else if (align == '<' || align == '=')
330         *n_lpadding = 0;
331     else {
332         /* We should never have an unspecified alignment. */
333         *n_lpadding = 0;
334         assert(0);
335     }
336 
337     *n_rpadding = *n_total - nchars - *n_lpadding;
338 }
339 
340 /* Do the padding, and return a pointer to where the caller-supplied
341    content goes. */
342 static STRINGLIB_CHAR *
fill_padding(STRINGLIB_CHAR * p,Py_ssize_t nchars,STRINGLIB_CHAR fill_char,Py_ssize_t n_lpadding,Py_ssize_t n_rpadding)343 fill_padding(STRINGLIB_CHAR *p, Py_ssize_t nchars, STRINGLIB_CHAR fill_char,
344              Py_ssize_t n_lpadding, Py_ssize_t n_rpadding)
345 {
346     /* Pad on left. */
347     if (n_lpadding)
348         STRINGLIB_FILL(p, fill_char, n_lpadding);
349 
350     /* Pad on right. */
351     if (n_rpadding)
352         STRINGLIB_FILL(p + nchars + n_lpadding, fill_char, n_rpadding);
353 
354     /* Pointer to the user content. */
355     return p + n_lpadding;
356 }
357 
358 #if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
359 /************************************************************************/
360 /*********** common routines for numeric formatting *********************/
361 /************************************************************************/
362 
363 /* Locale type codes. */
364 #define LT_CURRENT_LOCALE 0
365 #define LT_DEFAULT_LOCALE 1
366 #define LT_NO_LOCALE 2
367 
368 /* Locale info needed for formatting integers and the part of floats
369    before and including the decimal. Note that locales only support
370    8-bit chars, not unicode. */
371 typedef struct {
372     char *decimal_point;
373     char *thousands_sep;
374     char *grouping;
375 } LocaleInfo;
376 
377 /* describes the layout for an integer, see the comment in
378    calc_number_widths() for details */
379 typedef struct {
380     Py_ssize_t n_lpadding;
381     Py_ssize_t n_prefix;
382     Py_ssize_t n_spadding;
383     Py_ssize_t n_rpadding;
384     char sign;
385     Py_ssize_t n_sign;      /* number of digits needed for sign (0/1) */
386     Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
387                                     any grouping chars. */
388     Py_ssize_t n_decimal;   /* 0 if only an integer */
389     Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
390                                excluding the decimal itself, if
391                                present. */
392 
393     /* These 2 are not the widths of fields, but are needed by
394        STRINGLIB_GROUPING. */
395     Py_ssize_t n_digits;    /* The number of digits before a decimal
396                                or exponent. */
397     Py_ssize_t n_min_width; /* The min_width we used when we computed
398                                the n_grouped_digits width. */
399 } NumberFieldWidths;
400 
401 
402 /* Given a number of the form:
403    digits[remainder]
404    where ptr points to the start and end points to the end, find where
405     the integer part ends. This could be a decimal, an exponent, both,
406     or neither.
407    If a decimal point is present, set *has_decimal and increment
408     remainder beyond it.
409    Results are undefined (but shouldn't crash) for improperly
410     formatted strings.
411 */
412 static void
parse_number(STRINGLIB_CHAR * ptr,Py_ssize_t len,Py_ssize_t * n_remainder,int * has_decimal)413 parse_number(STRINGLIB_CHAR *ptr, Py_ssize_t len,
414              Py_ssize_t *n_remainder, int *has_decimal)
415 {
416     STRINGLIB_CHAR *end = ptr + len;
417     STRINGLIB_CHAR *remainder;
418 
419     while (ptr<end && isdigit(*ptr))
420         ++ptr;
421     remainder = ptr;
422 
423     /* Does remainder start with a decimal point? */
424     *has_decimal = ptr<end && *remainder == '.';
425 
426     /* Skip the decimal point. */
427     if (*has_decimal)
428         remainder++;
429 
430     *n_remainder = end - remainder;
431 }
432 
433 /* not all fields of format are used.  for example, precision is
434    unused.  should this take discrete params in order to be more clear
435    about what it does?  or is passing a single format parameter easier
436    and more efficient enough to justify a little obfuscation? */
437 static Py_ssize_t
calc_number_widths(NumberFieldWidths * spec,Py_ssize_t n_prefix,STRINGLIB_CHAR sign_char,STRINGLIB_CHAR * number,Py_ssize_t n_number,Py_ssize_t n_remainder,int has_decimal,const LocaleInfo * locale,const InternalFormatSpec * format)438 calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
439                    STRINGLIB_CHAR sign_char, STRINGLIB_CHAR *number,
440                    Py_ssize_t n_number, Py_ssize_t n_remainder,
441                    int has_decimal, const LocaleInfo *locale,
442                    const InternalFormatSpec *format)
443 {
444     Py_ssize_t n_non_digit_non_padding;
445     Py_ssize_t n_padding;
446 
447     spec->n_digits = n_number - n_remainder - (has_decimal?1:0);
448     spec->n_lpadding = 0;
449     spec->n_prefix = n_prefix;
450     spec->n_decimal = has_decimal ? strlen(locale->decimal_point) : 0;
451     spec->n_remainder = n_remainder;
452     spec->n_spadding = 0;
453     spec->n_rpadding = 0;
454     spec->sign = '\0';
455     spec->n_sign = 0;
456 
457     /* the output will look like:
458        |                                                                                         |
459        | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
460        |                                                                                         |
461 
462        sign is computed from format->sign and the actual
463        sign of the number
464 
465        prefix is given (it's for the '0x' prefix)
466 
467        digits is already known
468 
469        the total width is either given, or computed from the
470        actual digits
471 
472        only one of lpadding, spadding, and rpadding can be non-zero,
473        and it's calculated from the width and other fields
474     */
475 
476     /* compute the various parts we're going to write */
477     switch (format->sign) {
478     case '+':
479         /* always put a + or - */
480         spec->n_sign = 1;
481         spec->sign = (sign_char == '-' ? '-' : '+');
482         break;
483     case ' ':
484         spec->n_sign = 1;
485         spec->sign = (sign_char == '-' ? '-' : ' ');
486         break;
487     default:
488         /* Not specified, or the default (-) */
489         if (sign_char == '-') {
490             spec->n_sign = 1;
491             spec->sign = '-';
492         }
493     }
494 
495     /* The number of chars used for non-digits and non-padding. */
496     n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
497         spec->n_remainder;
498 
499     /* min_width can go negative, that's okay. format->width == -1 means
500        we don't care. */
501     if (format->fill_char == '0' && format->align == '=')
502         spec->n_min_width = format->width - n_non_digit_non_padding;
503     else
504         spec->n_min_width = 0;
505 
506     if (spec->n_digits == 0)
507         /* This case only occurs when using 'c' formatting, we need
508            to special case it because the grouping code always wants
509            to have at least one character. */
510         spec->n_grouped_digits = 0;
511     else
512         spec->n_grouped_digits = STRINGLIB_GROUPING(NULL, 0, NULL,
513                                                     spec->n_digits,
514                                                     spec->n_min_width,
515                                                     locale->grouping,
516                                                     locale->thousands_sep);
517 
518     /* Given the desired width and the total of digit and non-digit
519        space we consume, see if we need any padding. format->width can
520        be negative (meaning no padding), but this code still works in
521        that case. */
522     n_padding = format->width -
523                         (n_non_digit_non_padding + spec->n_grouped_digits);
524     if (n_padding > 0) {
525         /* Some padding is needed. Determine if it's left, space, or right. */
526         switch (format->align) {
527         case '<':
528             spec->n_rpadding = n_padding;
529             break;
530         case '^':
531             spec->n_lpadding = n_padding / 2;
532             spec->n_rpadding = n_padding - spec->n_lpadding;
533             break;
534         case '=':
535             spec->n_spadding = n_padding;
536             break;
537         case '>':
538             spec->n_lpadding = n_padding;
539             break;
540         default:
541             /* Shouldn't get here, but treat it as '>' */
542             spec->n_lpadding = n_padding;
543             assert(0);
544             break;
545         }
546     }
547     return spec->n_lpadding + spec->n_sign + spec->n_prefix +
548         spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
549         spec->n_remainder + spec->n_rpadding;
550 }
551 
552 /* Fill in the digit parts of a numbers's string representation,
553    as determined in calc_number_widths().
554    No error checking, since we know the buffer is the correct size. */
555 static void
fill_number(STRINGLIB_CHAR * buf,const NumberFieldWidths * spec,STRINGLIB_CHAR * digits,Py_ssize_t n_digits,STRINGLIB_CHAR * prefix,STRINGLIB_CHAR fill_char,LocaleInfo * locale,int toupper)556 fill_number(STRINGLIB_CHAR *buf, const NumberFieldWidths *spec,
557             STRINGLIB_CHAR *digits, Py_ssize_t n_digits,
558             STRINGLIB_CHAR *prefix, STRINGLIB_CHAR fill_char,
559             LocaleInfo *locale, int toupper)
560 {
561     /* Used to keep track of digits, decimal, and remainder. */
562     STRINGLIB_CHAR *p = digits;
563 
564 #ifndef NDEBUG
565     Py_ssize_t r;
566 #endif
567 
568     if (spec->n_lpadding) {
569         STRINGLIB_FILL(buf, fill_char, spec->n_lpadding);
570         buf += spec->n_lpadding;
571     }
572     if (spec->n_sign == 1) {
573         *buf++ = spec->sign;
574     }
575     if (spec->n_prefix) {
576         memmove(buf,
577                 prefix,
578                 spec->n_prefix * sizeof(STRINGLIB_CHAR));
579         if (toupper) {
580             Py_ssize_t t;
581             for (t = 0; t < spec->n_prefix; ++t)
582                 buf[t] = STRINGLIB_TOUPPER(buf[t]);
583         }
584         buf += spec->n_prefix;
585     }
586     if (spec->n_spadding) {
587         STRINGLIB_FILL(buf, fill_char, spec->n_spadding);
588         buf += spec->n_spadding;
589     }
590 
591     /* Only for type 'c' special case, it has no digits. */
592     if (spec->n_digits != 0) {
593         /* Fill the digits with InsertThousandsGrouping. */
594 #ifndef NDEBUG
595         r =
596 #endif
597             STRINGLIB_GROUPING(buf, spec->n_grouped_digits, digits,
598                                spec->n_digits, spec->n_min_width,
599                                locale->grouping, locale->thousands_sep);
600 #ifndef NDEBUG
601         assert(r == spec->n_grouped_digits);
602 #endif
603         p += spec->n_digits;
604     }
605     if (toupper) {
606         Py_ssize_t t;
607         for (t = 0; t < spec->n_grouped_digits; ++t)
608             buf[t] = STRINGLIB_TOUPPER(buf[t]);
609     }
610     buf += spec->n_grouped_digits;
611 
612     if (spec->n_decimal) {
613         Py_ssize_t t;
614         for (t = 0; t < spec->n_decimal; ++t)
615             buf[t] = locale->decimal_point[t];
616         buf += spec->n_decimal;
617         p += 1;
618     }
619 
620     if (spec->n_remainder) {
621         memcpy(buf, p, spec->n_remainder * sizeof(STRINGLIB_CHAR));
622         buf += spec->n_remainder;
623         p += spec->n_remainder;
624     }
625 
626     if (spec->n_rpadding) {
627         STRINGLIB_FILL(buf, fill_char, spec->n_rpadding);
628         buf += spec->n_rpadding;
629     }
630 }
631 
632 static char no_grouping[1] = {CHAR_MAX};
633 
634 /* Find the decimal point character(s?), thousands_separator(s?), and
635    grouping description, either for the current locale if type is
636    LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
637    none if LT_NO_LOCALE. */
638 static void
get_locale_info(int type,LocaleInfo * locale_info)639 get_locale_info(int type, LocaleInfo *locale_info)
640 {
641     switch (type) {
642     case LT_CURRENT_LOCALE: {
643         struct lconv *locale_data = localeconv();
644         locale_info->decimal_point = locale_data->decimal_point;
645         locale_info->thousands_sep = locale_data->thousands_sep;
646         locale_info->grouping = locale_data->grouping;
647         break;
648     }
649     case LT_DEFAULT_LOCALE:
650         locale_info->decimal_point = ".";
651         locale_info->thousands_sep = ",";
652         locale_info->grouping = "\3"; /* Group every 3 characters.  The
653                                          (implicit) trailing 0 means repeat
654                                          infinitely. */
655         break;
656     case LT_NO_LOCALE:
657         locale_info->decimal_point = ".";
658         locale_info->thousands_sep = "";
659         locale_info->grouping = no_grouping;
660         break;
661     default:
662         assert(0);
663     }
664 }
665 
666 #endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
667 
668 /************************************************************************/
669 /*********** string formatting ******************************************/
670 /************************************************************************/
671 
672 static PyObject *
format_string_internal(PyObject * value,const InternalFormatSpec * format)673 format_string_internal(PyObject *value, const InternalFormatSpec *format)
674 {
675     Py_ssize_t lpad;
676     Py_ssize_t rpad;
677     Py_ssize_t total;
678     STRINGLIB_CHAR *p;
679     Py_ssize_t len = STRINGLIB_LEN(value);
680     PyObject *result = NULL;
681 
682     /* sign is not allowed on strings */
683     if (format->sign != '\0') {
684         PyErr_SetString(PyExc_ValueError,
685                         "Sign not allowed in string format specifier");
686         goto done;
687     }
688 
689     /* alternate is not allowed on strings */
690     if (format->alternate) {
691         PyErr_SetString(PyExc_ValueError,
692                         "Alternate form (#) not allowed in string format "
693                         "specifier");
694         goto done;
695     }
696 
697     /* '=' alignment not allowed on strings */
698     if (format->align == '=') {
699         PyErr_SetString(PyExc_ValueError,
700                         "'=' alignment not allowed "
701                         "in string format specifier");
702         goto done;
703     }
704 
705     /* if precision is specified, output no more that format.precision
706        characters */
707     if (format->precision >= 0 && len >= format->precision) {
708         len = format->precision;
709     }
710 
711     calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
712 
713     /* allocate the resulting string */
714     result = STRINGLIB_NEW(NULL, total);
715     if (result == NULL)
716         goto done;
717 
718     /* Write into that space. First the padding. */
719     p = fill_padding(STRINGLIB_STR(result), len,
720                      format->fill_char=='\0'?' ':format->fill_char,
721                      lpad, rpad);
722 
723     /* Then the source string. */
724     memcpy(p, STRINGLIB_STR(value), len * sizeof(STRINGLIB_CHAR));
725 
726 done:
727     return result;
728 }
729 
730 
731 /************************************************************************/
732 /*********** long formatting ********************************************/
733 /************************************************************************/
734 
735 #if defined FORMAT_LONG || defined FORMAT_INT
736 typedef PyObject*
737 (*IntOrLongToString)(PyObject *value, int base);
738 
739 static PyObject *
format_int_or_long_internal(PyObject * value,const InternalFormatSpec * format,IntOrLongToString tostring)740 format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
741                             IntOrLongToString tostring)
742 {
743     PyObject *result = NULL;
744     PyObject *tmp = NULL;
745     STRINGLIB_CHAR *pnumeric_chars;
746     STRINGLIB_CHAR numeric_char;
747     STRINGLIB_CHAR sign_char = '\0';
748     Py_ssize_t n_digits;       /* count of digits need from the computed
749                                   string */
750     Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
751                                    produces non-digits */
752     Py_ssize_t n_prefix = 0;   /* Count of prefix chars, (e.g., '0x') */
753     Py_ssize_t n_total;
754     STRINGLIB_CHAR *prefix = NULL;
755     NumberFieldWidths spec;
756     long x;
757 
758     /* Locale settings, either from the actual locale or
759        from a hard-code pseudo-locale */
760     LocaleInfo locale;
761 
762     /* no precision allowed on integers */
763     if (format->precision != -1) {
764         PyErr_SetString(PyExc_ValueError,
765                         "Precision not allowed in integer format specifier");
766         goto done;
767     }
768 
769     /* special case for character formatting */
770     if (format->type == 'c') {
771         /* error to specify a sign */
772         if (format->sign != '\0') {
773             PyErr_SetString(PyExc_ValueError,
774                             "Sign not allowed with integer"
775                             " format specifier 'c'");
776             goto done;
777         }
778 
779         /* Error to specify a comma. */
780         if (format->thousands_separators) {
781             PyErr_SetString(PyExc_ValueError,
782                             "Thousands separators not allowed with integer"
783                             " format specifier 'c'");
784             goto done;
785         }
786 
787         /* taken from unicodeobject.c formatchar() */
788         /* Integer input truncated to a character */
789 /* XXX: won't work for int */
790         x = PyLong_AsLong(value);
791         if (x == -1 && PyErr_Occurred())
792             goto done;
793 #ifdef Py_UNICODE_WIDE
794         if (x < 0 || x > 0x10ffff) {
795             PyErr_SetString(PyExc_OverflowError,
796                             "%c arg not in range(0x110000) "
797                             "(wide Python build)");
798             goto done;
799         }
800 #else
801         if (x < 0 || x > 0xffff) {
802             PyErr_SetString(PyExc_OverflowError,
803                             "%c arg not in range(0x10000) "
804                             "(narrow Python build)");
805             goto done;
806         }
807 #endif
808         numeric_char = (STRINGLIB_CHAR)x;
809         pnumeric_chars = &numeric_char;
810         n_digits = 1;
811 
812         /* As a sort-of hack, we tell calc_number_widths that we only
813            have "remainder" characters. calc_number_widths thinks
814            these are characters that don't get formatted, only copied
815            into the output string. We do this for 'c' formatting,
816            because the characters are likely to be non-digits. */
817         n_remainder = 1;
818     }
819     else {
820         int base;
821         int leading_chars_to_skip = 0;  /* Number of characters added by
822                                            PyNumber_ToBase that we want to
823                                            skip over. */
824 
825         /* Compute the base and how many characters will be added by
826            PyNumber_ToBase */
827         switch (format->type) {
828         case 'b':
829             base = 2;
830             leading_chars_to_skip = 2; /* 0b */
831             break;
832         case 'o':
833             base = 8;
834             leading_chars_to_skip = 2; /* 0o */
835             break;
836         case 'x':
837         case 'X':
838             base = 16;
839             leading_chars_to_skip = 2; /* 0x */
840             break;
841         default:  /* shouldn't be needed, but stops a compiler warning */
842         case 'd':
843         case 'n':
844             base = 10;
845             break;
846         }
847 
848         /* The number of prefix chars is the same as the leading
849            chars to skip */
850         if (format->alternate)
851             n_prefix = leading_chars_to_skip;
852 
853         /* Do the hard part, converting to a string in a given base */
854         tmp = tostring(value, base);
855         if (tmp == NULL)
856             goto done;
857 
858         pnumeric_chars = STRINGLIB_STR(tmp);
859         n_digits = STRINGLIB_LEN(tmp);
860 
861         prefix = pnumeric_chars;
862 
863         /* Remember not to modify what pnumeric_chars points to.  it
864            might be interned.  Only modify it after we copy it into a
865            newly allocated output buffer. */
866 
867         /* Is a sign character present in the output?  If so, remember it
868            and skip it */
869         if (pnumeric_chars[0] == '-') {
870             sign_char = pnumeric_chars[0];
871             ++prefix;
872             ++leading_chars_to_skip;
873         }
874 
875         /* Skip over the leading chars (0x, 0b, etc.) */
876         n_digits -= leading_chars_to_skip;
877         pnumeric_chars += leading_chars_to_skip;
878     }
879 
880     /* Determine the grouping, separator, and decimal point, if any. */
881     get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
882                     (format->thousands_separators ?
883                      LT_DEFAULT_LOCALE :
884                      LT_NO_LOCALE),
885                     &locale);
886 
887     /* Calculate how much memory we'll need. */
888     n_total = calc_number_widths(&spec, n_prefix, sign_char, pnumeric_chars,
889                        n_digits, n_remainder, 0, &locale, format);
890 
891     /* Allocate the memory. */
892     result = STRINGLIB_NEW(NULL, n_total);
893     if (!result)
894         goto done;
895 
896     /* Populate the memory. */
897     fill_number(STRINGLIB_STR(result), &spec, pnumeric_chars, n_digits,
898                 prefix, format->fill_char == '\0' ? ' ' : format->fill_char,
899                 &locale, format->type == 'X');
900 
901 done:
902     Py_XDECREF(tmp);
903     return result;
904 }
905 #endif /* defined FORMAT_LONG || defined FORMAT_INT */
906 
907 /************************************************************************/
908 /*********** float formatting *******************************************/
909 /************************************************************************/
910 
911 #ifdef FORMAT_FLOAT
912 #if STRINGLIB_IS_UNICODE
913 static void
strtounicode(Py_UNICODE * buffer,const char * charbuffer,Py_ssize_t len)914 strtounicode(Py_UNICODE *buffer, const char *charbuffer, Py_ssize_t len)
915 {
916     Py_ssize_t i;
917     for (i = 0; i < len; ++i)
918         buffer[i] = (Py_UNICODE)charbuffer[i];
919 }
920 #endif
921 
922 /* much of this is taken from unicodeobject.c */
923 static PyObject *
format_float_internal(PyObject * value,const InternalFormatSpec * format)924 format_float_internal(PyObject *value,
925                       const InternalFormatSpec *format)
926 {
927     char *buf = NULL;       /* buffer returned from PyOS_double_to_string */
928     Py_ssize_t n_digits;
929     Py_ssize_t n_remainder;
930     Py_ssize_t n_total;
931     int has_decimal;
932     double val;
933     Py_ssize_t precision = format->precision;
934     Py_ssize_t default_precision = 6;
935     STRINGLIB_CHAR type = format->type;
936     int add_pct = 0;
937     STRINGLIB_CHAR *p;
938     NumberFieldWidths spec;
939     int flags = 0;
940     PyObject *result = NULL;
941     STRINGLIB_CHAR sign_char = '\0';
942     int float_type; /* Used to see if we have a nan, inf, or regular float. */
943 
944 #if STRINGLIB_IS_UNICODE
945     Py_UNICODE *unicode_tmp = NULL;
946 #endif
947 
948     /* Locale settings, either from the actual locale or
949        from a hard-code pseudo-locale */
950     LocaleInfo locale;
951 
952     /* Alternate is not allowed on floats. */
953     if (format->alternate) {
954         PyErr_SetString(PyExc_ValueError,
955                         "Alternate form (#) not allowed in float format "
956                         "specifier");
957         goto done;
958     }
959 
960     if (type == '\0') {
961         /* Omitted type specifier. This is like 'g' but with at least one
962            digit after the decimal point, and different default precision.*/
963         type = 'g';
964         default_precision = PyFloat_STR_PRECISION;
965         flags |= Py_DTSF_ADD_DOT_0;
966     }
967 
968     if (type == 'n')
969         /* 'n' is the same as 'g', except for the locale used to
970            format the result. We take care of that later. */
971         type = 'g';
972 
973     val = PyFloat_AsDouble(value);
974     if (val == -1.0 && PyErr_Occurred())
975         goto done;
976 
977     if (type == '%') {
978         type = 'f';
979         val *= 100;
980         add_pct = 1;
981     }
982 
983     if (precision < 0)
984         precision = default_precision;
985 
986     /* Cast "type", because if we're in unicode we need to pass a
987        8-bit char. This is safe, because we've restricted what "type"
988        can be. */
989     buf = PyOS_double_to_string(val, (char)type, precision, flags,
990                                 &float_type);
991     if (buf == NULL)
992         goto done;
993     n_digits = strlen(buf);
994 
995     if (add_pct) {
996         /* We know that buf has a trailing zero (since we just called
997            strlen() on it), and we don't use that fact any more. So we
998            can just write over the trailing zero. */
999         buf[n_digits] = '%';
1000         n_digits += 1;
1001     }
1002 
1003     /* Since there is no unicode version of PyOS_double_to_string,
1004        just use the 8 bit version and then convert to unicode. */
1005 #if STRINGLIB_IS_UNICODE
1006     unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_digits)*sizeof(Py_UNICODE));
1007     if (unicode_tmp == NULL) {
1008         PyErr_NoMemory();
1009         goto done;
1010     }
1011     strtounicode(unicode_tmp, buf, n_digits);
1012     p = unicode_tmp;
1013 #else
1014     p = buf;
1015 #endif
1016 
1017     /* Is a sign character present in the output?  If so, remember it
1018        and skip it */
1019     if (*p == '-') {
1020         sign_char = *p;
1021         ++p;
1022         --n_digits;
1023     }
1024 
1025     /* Determine if we have any "remainder" (after the digits, might include
1026        decimal or exponent or both (or neither)) */
1027     parse_number(p, n_digits, &n_remainder, &has_decimal);
1028 
1029     /* Determine the grouping, separator, and decimal point, if any. */
1030     get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1031                     (format->thousands_separators ?
1032                      LT_DEFAULT_LOCALE :
1033                      LT_NO_LOCALE),
1034                     &locale);
1035 
1036     /* Calculate how much memory we'll need. */
1037     n_total = calc_number_widths(&spec, 0, sign_char, p, n_digits,
1038                                  n_remainder, has_decimal, &locale, format);
1039 
1040     /* Allocate the memory. */
1041     result = STRINGLIB_NEW(NULL, n_total);
1042     if (result == NULL)
1043         goto done;
1044 
1045     /* Populate the memory. */
1046     fill_number(STRINGLIB_STR(result), &spec, p, n_digits, NULL,
1047                 format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
1048                 0);
1049 
1050 done:
1051     PyMem_Free(buf);
1052 #if STRINGLIB_IS_UNICODE
1053     PyMem_Free(unicode_tmp);
1054 #endif
1055     return result;
1056 }
1057 #endif /* FORMAT_FLOAT */
1058 
1059 /************************************************************************/
1060 /*********** complex formatting *****************************************/
1061 /************************************************************************/
1062 
1063 #ifdef FORMAT_COMPLEX
1064 
1065 static PyObject *
format_complex_internal(PyObject * value,const InternalFormatSpec * format)1066 format_complex_internal(PyObject *value,
1067                         const InternalFormatSpec *format)
1068 {
1069     double re;
1070     double im;
1071     char *re_buf = NULL;       /* buffer returned from PyOS_double_to_string */
1072     char *im_buf = NULL;       /* buffer returned from PyOS_double_to_string */
1073 
1074     InternalFormatSpec tmp_format = *format;
1075     Py_ssize_t n_re_digits;
1076     Py_ssize_t n_im_digits;
1077     Py_ssize_t n_re_remainder;
1078     Py_ssize_t n_im_remainder;
1079     Py_ssize_t n_re_total;
1080     Py_ssize_t n_im_total;
1081     int re_has_decimal;
1082     int im_has_decimal;
1083     Py_ssize_t precision = format->precision;
1084     Py_ssize_t default_precision = 6;
1085     STRINGLIB_CHAR type = format->type;
1086     STRINGLIB_CHAR *p_re;
1087     STRINGLIB_CHAR *p_im;
1088     NumberFieldWidths re_spec;
1089     NumberFieldWidths im_spec;
1090     int flags = 0;
1091     PyObject *result = NULL;
1092     STRINGLIB_CHAR *p;
1093     STRINGLIB_CHAR re_sign_char = '\0';
1094     STRINGLIB_CHAR im_sign_char = '\0';
1095     int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1096     int im_float_type;
1097     int add_parens = 0;
1098     int skip_re = 0;
1099     Py_ssize_t lpad;
1100     Py_ssize_t rpad;
1101     Py_ssize_t total;
1102 
1103 #if STRINGLIB_IS_UNICODE
1104     Py_UNICODE *re_unicode_tmp = NULL;
1105     Py_UNICODE *im_unicode_tmp = NULL;
1106 #endif
1107 
1108     /* Locale settings, either from the actual locale or
1109        from a hard-code pseudo-locale */
1110     LocaleInfo locale;
1111 
1112     /* Alternate is not allowed on complex. */
1113     if (format->alternate) {
1114         PyErr_SetString(PyExc_ValueError,
1115                         "Alternate form (#) not allowed in complex format "
1116                         "specifier");
1117         goto done;
1118     }
1119 
1120     /* Neither is zero pading. */
1121     if (format->fill_char == '0') {
1122         PyErr_SetString(PyExc_ValueError,
1123                         "Zero padding is not allowed in complex format "
1124                         "specifier");
1125         goto done;
1126     }
1127 
1128     /* Neither is '=' alignment . */
1129     if (format->align == '=') {
1130         PyErr_SetString(PyExc_ValueError,
1131                         "'=' alignment flag is not allowed in complex format "
1132                         "specifier");
1133         goto done;
1134     }
1135 
1136     re = PyComplex_RealAsDouble(value);
1137     if (re == -1.0 && PyErr_Occurred())
1138         goto done;
1139     im = PyComplex_ImagAsDouble(value);
1140     if (im == -1.0 && PyErr_Occurred())
1141         goto done;
1142 
1143     if (type == '\0') {
1144         /* Omitted type specifier. Should be like str(self). */
1145         type = 'g';
1146         default_precision = PyFloat_STR_PRECISION;
1147         if (re == 0.0 && copysign(1.0, re) == 1.0)
1148             skip_re = 1;
1149         else
1150             add_parens = 1;
1151     }
1152 
1153     if (type == 'n')
1154         /* 'n' is the same as 'g', except for the locale used to
1155            format the result. We take care of that later. */
1156         type = 'g';
1157 
1158     if (precision < 0)
1159         precision = default_precision;
1160 
1161     /* Cast "type", because if we're in unicode we need to pass a
1162        8-bit char. This is safe, because we've restricted what "type"
1163        can be. */
1164     re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1165                                    &re_float_type);
1166     if (re_buf == NULL)
1167         goto done;
1168     im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1169                                    &im_float_type);
1170     if (im_buf == NULL)
1171         goto done;
1172 
1173     n_re_digits = strlen(re_buf);
1174     n_im_digits = strlen(im_buf);
1175 
1176     /* Since there is no unicode version of PyOS_double_to_string,
1177        just use the 8 bit version and then convert to unicode. */
1178 #if STRINGLIB_IS_UNICODE
1179     re_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_re_digits)*sizeof(Py_UNICODE));
1180     if (re_unicode_tmp == NULL) {
1181         PyErr_NoMemory();
1182         goto done;
1183     }
1184     strtounicode(re_unicode_tmp, re_buf, n_re_digits);
1185     p_re = re_unicode_tmp;
1186 
1187     im_unicode_tmp = (Py_UNICODE*)PyMem_Malloc((n_im_digits)*sizeof(Py_UNICODE));
1188     if (im_unicode_tmp == NULL) {
1189         PyErr_NoMemory();
1190         goto done;
1191     }
1192     strtounicode(im_unicode_tmp, im_buf, n_im_digits);
1193     p_im = im_unicode_tmp;
1194 #else
1195     p_re = re_buf;
1196     p_im = im_buf;
1197 #endif
1198 
1199     /* Is a sign character present in the output?  If so, remember it
1200        and skip it */
1201     if (*p_re == '-') {
1202         re_sign_char = *p_re;
1203         ++p_re;
1204         --n_re_digits;
1205     }
1206     if (*p_im == '-') {
1207         im_sign_char = *p_im;
1208         ++p_im;
1209         --n_im_digits;
1210     }
1211 
1212     /* Determine if we have any "remainder" (after the digits, might include
1213        decimal or exponent or both (or neither)) */
1214     parse_number(p_re, n_re_digits, &n_re_remainder, &re_has_decimal);
1215     parse_number(p_im, n_im_digits, &n_im_remainder, &im_has_decimal);
1216 
1217     /* Determine the grouping, separator, and decimal point, if any. */
1218     get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1219                     (format->thousands_separators ?
1220                      LT_DEFAULT_LOCALE :
1221                      LT_NO_LOCALE),
1222                     &locale);
1223 
1224     /* Turn off any padding. We'll do it later after we've composed
1225        the numbers without padding. */
1226     tmp_format.fill_char = '\0';
1227     tmp_format.align = '<';
1228     tmp_format.width = -1;
1229 
1230     /* Calculate how much memory we'll need. */
1231     n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, p_re,
1232                                     n_re_digits, n_re_remainder,
1233                                     re_has_decimal, &locale, &tmp_format);
1234 
1235     /* Same formatting, but always include a sign, unless the real part is
1236      * going to be omitted, in which case we use whatever sign convention was
1237      * requested by the original format. */
1238     if (!skip_re)
1239         tmp_format.sign = '+';
1240     n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, p_im,
1241                                     n_im_digits, n_im_remainder,
1242                                     im_has_decimal, &locale, &tmp_format);
1243 
1244     if (skip_re)
1245         n_re_total = 0;
1246 
1247     /* Add 1 for the 'j', and optionally 2 for parens. */
1248     calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1249                  format->width, format->align, &lpad, &rpad, &total);
1250 
1251     result = STRINGLIB_NEW(NULL, total);
1252     if (result == NULL)
1253         goto done;
1254 
1255     /* Populate the memory. First, the padding. */
1256     p = fill_padding(STRINGLIB_STR(result),
1257                      n_re_total + n_im_total + 1 + add_parens * 2,
1258                      format->fill_char=='\0' ? ' ' : format->fill_char,
1259                      lpad, rpad);
1260 
1261     if (add_parens)
1262         *p++ = '(';
1263 
1264     if (!skip_re) {
1265         fill_number(p, &re_spec, p_re, n_re_digits, NULL, 0, &locale, 0);
1266         p += n_re_total;
1267     }
1268     fill_number(p, &im_spec, p_im, n_im_digits, NULL, 0, &locale, 0);
1269     p += n_im_total;
1270     *p++ = 'j';
1271 
1272     if (add_parens)
1273         *p++ = ')';
1274 
1275 done:
1276     PyMem_Free(re_buf);
1277     PyMem_Free(im_buf);
1278 #if STRINGLIB_IS_UNICODE
1279     PyMem_Free(re_unicode_tmp);
1280     PyMem_Free(im_unicode_tmp);
1281 #endif
1282     return result;
1283 }
1284 #endif /* FORMAT_COMPLEX */
1285 
1286 /************************************************************************/
1287 /*********** built in formatters ****************************************/
1288 /************************************************************************/
1289 PyObject *
FORMAT_STRING(PyObject * obj,STRINGLIB_CHAR * format_spec,Py_ssize_t format_spec_len)1290 FORMAT_STRING(PyObject *obj,
1291               STRINGLIB_CHAR *format_spec,
1292               Py_ssize_t format_spec_len)
1293 {
1294     InternalFormatSpec format;
1295     PyObject *result = NULL;
1296 
1297     /* check for the special case of zero length format spec, make
1298        it equivalent to str(obj) */
1299     if (format_spec_len == 0) {
1300         result = STRINGLIB_TOSTR(obj);
1301         goto done;
1302     }
1303 
1304     /* parse the format_spec */
1305     if (!parse_internal_render_format_spec(format_spec, format_spec_len,
1306                                            &format, 's', '<'))
1307         goto done;
1308 
1309     /* type conversion? */
1310     switch (format.type) {
1311     case 's':
1312         /* no type conversion needed, already a string.  do the formatting */
1313         result = format_string_internal(obj, &format);
1314         break;
1315     default:
1316         /* unknown */
1317         unknown_presentation_type(format.type, obj->ob_type->tp_name);
1318         goto done;
1319     }
1320 
1321 done:
1322     return result;
1323 }
1324 
1325 #if defined FORMAT_LONG || defined FORMAT_INT
1326 static PyObject*
format_int_or_long(PyObject * obj,STRINGLIB_CHAR * format_spec,Py_ssize_t format_spec_len,IntOrLongToString tostring)1327 format_int_or_long(PyObject* obj,
1328                    STRINGLIB_CHAR *format_spec,
1329                    Py_ssize_t format_spec_len,
1330                    IntOrLongToString tostring)
1331 {
1332     PyObject *result = NULL;
1333     PyObject *tmp = NULL;
1334     InternalFormatSpec format;
1335 
1336     /* check for the special case of zero length format spec, make
1337        it equivalent to str(obj) */
1338     if (format_spec_len == 0) {
1339         result = STRINGLIB_TOSTR(obj);
1340         goto done;
1341     }
1342 
1343     /* parse the format_spec */
1344     if (!parse_internal_render_format_spec(format_spec,
1345                                            format_spec_len,
1346                                            &format, 'd', '>'))
1347         goto done;
1348 
1349     /* type conversion? */
1350     switch (format.type) {
1351     case 'b':
1352     case 'c':
1353     case 'd':
1354     case 'o':
1355     case 'x':
1356     case 'X':
1357     case 'n':
1358         /* no type conversion needed, already an int (or long).  do
1359            the formatting */
1360             result = format_int_or_long_internal(obj, &format, tostring);
1361         break;
1362 
1363     case 'e':
1364     case 'E':
1365     case 'f':
1366     case 'F':
1367     case 'g':
1368     case 'G':
1369     case '%':
1370         /* convert to float */
1371         tmp = PyNumber_Float(obj);
1372         if (tmp == NULL)
1373             goto done;
1374         result = format_float_internal(tmp, &format);
1375         break;
1376 
1377     default:
1378         /* unknown */
1379         unknown_presentation_type(format.type, obj->ob_type->tp_name);
1380         goto done;
1381     }
1382 
1383 done:
1384     Py_XDECREF(tmp);
1385     return result;
1386 }
1387 #endif /* FORMAT_LONG || defined FORMAT_INT */
1388 
1389 #ifdef FORMAT_LONG
1390 /* Need to define long_format as a function that will convert a long
1391    to a string.  In 3.0, _PyLong_Format has the correct signature.  In
1392    2.x, we need to fudge a few parameters */
1393 #if PY_VERSION_HEX >= 0x03000000
1394 #define long_format _PyLong_Format
1395 #else
1396 static PyObject*
long_format(PyObject * value,int base)1397 long_format(PyObject* value, int base)
1398 {
1399     /* Convert to base, don't add trailing 'L', and use the new octal
1400        format. We already know this is a long object */
1401     assert(PyLong_Check(value));
1402     /* convert to base, don't add 'L', and use the new octal format */
1403     return _PyLong_Format(value, base, 0, 1);
1404 }
1405 #endif
1406 
1407 PyObject *
FORMAT_LONG(PyObject * obj,STRINGLIB_CHAR * format_spec,Py_ssize_t format_spec_len)1408 FORMAT_LONG(PyObject *obj,
1409             STRINGLIB_CHAR *format_spec,
1410             Py_ssize_t format_spec_len)
1411 {
1412     return format_int_or_long(obj, format_spec, format_spec_len,
1413                               long_format);
1414 }
1415 #endif /* FORMAT_LONG */
1416 
1417 #ifdef FORMAT_INT
1418 /* this is only used for 2.x, not 3.0 */
1419 static PyObject*
int_format(PyObject * value,int base)1420 int_format(PyObject* value, int base)
1421 {
1422     /* Convert to base, and use the new octal format. We already
1423        know this is an int object */
1424     assert(PyInt_Check(value));
1425     return _PyInt_Format((PyIntObject*)value, base, 1);
1426 }
1427 
1428 PyObject *
FORMAT_INT(PyObject * obj,STRINGLIB_CHAR * format_spec,Py_ssize_t format_spec_len)1429 FORMAT_INT(PyObject *obj,
1430            STRINGLIB_CHAR *format_spec,
1431            Py_ssize_t format_spec_len)
1432 {
1433     return format_int_or_long(obj, format_spec, format_spec_len,
1434                               int_format);
1435 }
1436 #endif /* FORMAT_INT */
1437 
1438 #ifdef FORMAT_FLOAT
1439 PyObject *
FORMAT_FLOAT(PyObject * obj,STRINGLIB_CHAR * format_spec,Py_ssize_t format_spec_len)1440 FORMAT_FLOAT(PyObject *obj,
1441              STRINGLIB_CHAR *format_spec,
1442              Py_ssize_t format_spec_len)
1443 {
1444     PyObject *result = NULL;
1445     InternalFormatSpec format;
1446 
1447     /* check for the special case of zero length format spec, make
1448        it equivalent to str(obj) */
1449     if (format_spec_len == 0) {
1450         result = STRINGLIB_TOSTR(obj);
1451         goto done;
1452     }
1453 
1454     /* parse the format_spec */
1455     if (!parse_internal_render_format_spec(format_spec,
1456                                            format_spec_len,
1457                                            &format, '\0', '>'))
1458         goto done;
1459 
1460     /* type conversion? */
1461     switch (format.type) {
1462     case '\0': /* No format code: like 'g', but with at least one decimal. */
1463     case 'e':
1464     case 'E':
1465     case 'f':
1466     case 'F':
1467     case 'g':
1468     case 'G':
1469     case 'n':
1470     case '%':
1471         /* no conversion, already a float.  do the formatting */
1472         result = format_float_internal(obj, &format);
1473         break;
1474 
1475     default:
1476         /* unknown */
1477         unknown_presentation_type(format.type, obj->ob_type->tp_name);
1478         goto done;
1479     }
1480 
1481 done:
1482     return result;
1483 }
1484 #endif /* FORMAT_FLOAT */
1485 
1486 #ifdef FORMAT_COMPLEX
1487 PyObject *
FORMAT_COMPLEX(PyObject * obj,STRINGLIB_CHAR * format_spec,Py_ssize_t format_spec_len)1488 FORMAT_COMPLEX(PyObject *obj,
1489                STRINGLIB_CHAR *format_spec,
1490                Py_ssize_t format_spec_len)
1491 {
1492     PyObject *result = NULL;
1493     InternalFormatSpec format;
1494 
1495     /* check for the special case of zero length format spec, make
1496        it equivalent to str(obj) */
1497     if (format_spec_len == 0) {
1498         result = STRINGLIB_TOSTR(obj);
1499         goto done;
1500     }
1501 
1502     /* parse the format_spec */
1503     if (!parse_internal_render_format_spec(format_spec,
1504                                            format_spec_len,
1505                                            &format, '\0', '>'))
1506         goto done;
1507 
1508     /* type conversion? */
1509     switch (format.type) {
1510     case '\0': /* No format code: like 'g', but with at least one decimal. */
1511     case 'e':
1512     case 'E':
1513     case 'f':
1514     case 'F':
1515     case 'g':
1516     case 'G':
1517     case 'n':
1518         /* no conversion, already a complex.  do the formatting */
1519         result = format_complex_internal(obj, &format);
1520         break;
1521 
1522     default:
1523         /* unknown */
1524         unknown_presentation_type(format.type, obj->ob_type->tp_name);
1525         goto done;
1526     }
1527 
1528 done:
1529     return result;
1530 }
1531 #endif /* FORMAT_COMPLEX */
1532