1 /* implements the string, long, and float formatters. that is,
2 string.__format__, etc. */
6 /* Before including this, you must include either:
7 stringlib/unicodedefs.h
10 Also, you should define the names:
15 to be whatever you want the public names of these functions to
16 be. These are the only non-static functions defined here.
19 /* Raises an exception about an unknown presentation type for this
23 unknown_presentation_type(STRINGLIB_CHAR presentation_type
,
24 const char* type_name
)
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)
32 PyErr_Format(PyExc_ValueError
,
33 "Unknown format code '%c' "
34 "for object of type '%.200s'",
35 (char)presentation_type
,
37 #if STRINGLIB_IS_UNICODE
39 PyErr_Format(PyExc_ValueError
,
40 "Unknown format code '\\x%x' "
41 "for object of type '%.200s'",
42 (unsigned int)presentation_type
,
48 invalid_comma_type(STRINGLIB_CHAR presentation_type
)
50 #if STRINGLIB_IS_UNICODE
51 /* See comment in unknown_presentation_type */
52 if (presentation_type
> 32 && presentation_type
< 128)
54 PyErr_Format(PyExc_ValueError
,
55 "Cannot specify ',' with '%c'.",
56 (char)presentation_type
);
57 #if STRINGLIB_IS_UNICODE
59 PyErr_Format(PyExc_ValueError
,
60 "Cannot specify ',' with '\\x%x'.",
61 (unsigned int)presentation_type
);
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.
73 get_integer(STRINGLIB_CHAR
**ptr
, STRINGLIB_CHAR
*end
,
76 Py_ssize_t accumulator
, digitval
, oldaccumulator
;
78 accumulator
= numdigits
= 0;
79 for (;;(*ptr
)++, numdigits
++) {
82 digitval
= STRINGLIB_TODECIMAL(**ptr
);
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.
91 oldaccumulator
= accumulator
;
93 if ((accumulator
+10)/10 != oldaccumulator
+1) {
94 PyErr_Format(PyExc_ValueError
,
95 "Too many decimal digits in format string");
98 accumulator
+= digitval
;
100 *result
= accumulator
;
104 /************************************************************************/
105 /*********** standard format specifier parsing **************************/
106 /************************************************************************/
108 /* returns true if this character is a specifier alignment token */
110 is_alignment_token(STRINGLIB_CHAR c
)
113 case '<': case '>': case '=': case '^':
120 /* returns true if this character is a sign element */
122 is_sign_element(STRINGLIB_CHAR c
)
125 case ' ': case '+': case '-':
134 STRINGLIB_CHAR fill_char
;
135 STRINGLIB_CHAR align
;
139 int thousands_separators
;
140 Py_ssize_t precision
;
142 } InternalFormatSpec
;
145 ptr points to the start of the format_spec, end points just past its end.
146 fills in format with the parsed information.
147 returns 1 on success, 0 on failure.
148 if failure, sets the exception
151 parse_internal_render_format_spec(STRINGLIB_CHAR
*format_spec
,
152 Py_ssize_t format_spec_len
,
153 InternalFormatSpec
*format
,
156 STRINGLIB_CHAR
*ptr
= format_spec
;
157 STRINGLIB_CHAR
*end
= format_spec
+ format_spec_len
;
159 /* end-ptr is used throughout this code to specify the length of
164 format
->fill_char
= '\0';
165 format
->align
= '\0';
166 format
->alternate
= 0;
169 format
->thousands_separators
= 0;
170 format
->precision
= -1;
171 format
->type
= default_type
;
173 /* If the second char is an alignment token,
174 then parse the fill char */
175 if (end
-ptr
>= 2 && is_alignment_token(ptr
[1])) {
176 format
->align
= ptr
[1];
177 format
->fill_char
= ptr
[0];
180 else if (end
-ptr
>= 1 && is_alignment_token(ptr
[0])) {
181 format
->align
= ptr
[0];
185 /* Parse the various sign options */
186 if (end
-ptr
>= 1 && is_sign_element(ptr
[0])) {
187 format
->sign
= ptr
[0];
191 /* If the next character is #, we're in alternate mode. This only
192 applies to integers. */
193 if (end
-ptr
>= 1 && ptr
[0] == '#') {
194 format
->alternate
= 1;
198 /* The special case for 0-padding (backwards compat) */
199 if (format
->fill_char
== '\0' && end
-ptr
>= 1 && ptr
[0] == '0') {
200 format
->fill_char
= '0';
201 if (format
->align
== '\0') {
207 consumed
= get_integer(&ptr
, end
, &format
->width
);
209 /* Overflow error. Exception already set. */
212 /* If consumed is 0, we didn't consume any characters for the
213 width. In that case, reset the width to -1, because
214 get_integer() will have set it to zero. -1 is how we record
215 that the width wasn't specified. */
219 /* Comma signifies add thousands separators */
220 if (end
-ptr
&& ptr
[0] == ',') {
221 format
->thousands_separators
= 1;
225 /* Parse field precision */
226 if (end
-ptr
&& ptr
[0] == '.') {
229 consumed
= get_integer(&ptr
, end
, &format
->precision
);
231 /* Overflow error. Exception already set. */
234 /* Not having a precision after a dot is an error. */
236 PyErr_Format(PyExc_ValueError
,
237 "Format specifier missing precision");
243 /* Finally, parse the type field. */
246 /* More than one char remain, invalid conversion spec. */
247 PyErr_Format(PyExc_ValueError
, "Invalid conversion specification");
252 format
->type
= ptr
[0];
256 /* Do as much validating as we can, just by looking at the format
257 specifier. Do not take into account what type of formatting
258 we're doing (int, float, string). */
260 if (format
->thousands_separators
) {
261 switch (format
->type
) {
271 /* These are allowed. See PEP 378.*/
274 invalid_comma_type(format
->type
);
282 /* Calculate the padding needed. */
284 calc_padding(Py_ssize_t nchars
, Py_ssize_t width
, STRINGLIB_CHAR align
,
285 Py_ssize_t
*n_lpadding
, Py_ssize_t
*n_rpadding
,
295 /* not specified, use all of the chars and no more */
299 /* figure out how much leading space we need, based on the
302 *n_lpadding
= *n_total
- nchars
;
303 else if (align
== '^')
304 *n_lpadding
= (*n_total
- nchars
) / 2;
308 *n_rpadding
= *n_total
- nchars
- *n_lpadding
;
311 /* Do the padding, and return a pointer to where the caller-supplied
313 static STRINGLIB_CHAR
*
314 fill_padding(STRINGLIB_CHAR
*p
, Py_ssize_t nchars
, STRINGLIB_CHAR fill_char
,
315 Py_ssize_t n_lpadding
, Py_ssize_t n_rpadding
)
319 STRINGLIB_FILL(p
, fill_char
, n_lpadding
);
323 STRINGLIB_FILL(p
+ nchars
+ n_lpadding
, fill_char
, n_rpadding
);
325 /* Pointer to the user content. */
326 return p
+ n_lpadding
;
329 #if defined FORMAT_FLOAT || defined FORMAT_LONG || defined FORMAT_COMPLEX
330 /************************************************************************/
331 /*********** common routines for numeric formatting *********************/
332 /************************************************************************/
334 /* Locale type codes. */
335 #define LT_CURRENT_LOCALE 0
336 #define LT_DEFAULT_LOCALE 1
337 #define LT_NO_LOCALE 2
339 /* Locale info needed for formatting integers and the part of floats
340 before and including the decimal. Note that locales only support
341 8-bit chars, not unicode. */
348 /* describes the layout for an integer, see the comment in
349 calc_number_widths() for details */
351 Py_ssize_t n_lpadding
;
353 Py_ssize_t n_spadding
;
354 Py_ssize_t n_rpadding
;
356 Py_ssize_t n_sign
; /* number of digits needed for sign (0/1) */
357 Py_ssize_t n_grouped_digits
; /* Space taken up by the digits, including
358 any grouping chars. */
359 Py_ssize_t n_decimal
; /* 0 if only an integer */
360 Py_ssize_t n_remainder
; /* Digits in decimal and/or exponent part,
361 excluding the decimal itself, if
364 /* These 2 are not the widths of fields, but are needed by
365 STRINGLIB_GROUPING. */
366 Py_ssize_t n_digits
; /* The number of digits before a decimal
368 Py_ssize_t n_min_width
; /* The min_width we used when we computed
369 the n_grouped_digits width. */
373 /* Given a number of the form:
375 where ptr points to the start and end points to the end, find where
376 the integer part ends. This could be a decimal, an exponent, both,
378 If a decimal point is present, set *has_decimal and increment
380 Results are undefined (but shouldn't crash) for improperly
384 parse_number(STRINGLIB_CHAR
*ptr
, Py_ssize_t len
,
385 Py_ssize_t
*n_remainder
, int *has_decimal
)
387 STRINGLIB_CHAR
*end
= ptr
+ len
;
388 STRINGLIB_CHAR
*remainder
;
390 while (ptr
<end
&& isdigit(*ptr
))
394 /* Does remainder start with a decimal point? */
395 *has_decimal
= ptr
<end
&& *remainder
== '.';
397 /* Skip the decimal point. */
401 *n_remainder
= end
- remainder
;
404 /* not all fields of format are used. for example, precision is
405 unused. should this take discrete params in order to be more clear
406 about what it does? or is passing a single format parameter easier
407 and more efficient enough to justify a little obfuscation? */
409 calc_number_widths(NumberFieldWidths
*spec
, Py_ssize_t n_prefix
,
410 STRINGLIB_CHAR sign_char
, STRINGLIB_CHAR
*number
,
411 Py_ssize_t n_number
, Py_ssize_t n_remainder
,
412 int has_decimal
, const LocaleInfo
*locale
,
413 const InternalFormatSpec
*format
)
415 Py_ssize_t n_non_digit_non_padding
;
416 Py_ssize_t n_padding
;
418 spec
->n_digits
= n_number
- n_remainder
- (has_decimal
?1:0);
419 spec
->n_lpadding
= 0;
420 spec
->n_prefix
= n_prefix
;
421 spec
->n_decimal
= has_decimal
? strlen(locale
->decimal_point
) : 0;
422 spec
->n_remainder
= n_remainder
;
423 spec
->n_spadding
= 0;
424 spec
->n_rpadding
= 0;
428 /* the output will look like:
430 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
433 sign is computed from format->sign and the actual
436 prefix is given (it's for the '0x' prefix)
438 digits is already known
440 the total width is either given, or computed from the
443 only one of lpadding, spadding, and rpadding can be non-zero,
444 and it's calculated from the width and other fields
447 /* compute the various parts we're going to write */
448 switch (format
->sign
) {
450 /* always put a + or - */
452 spec
->sign
= (sign_char
== '-' ? '-' : '+');
456 spec
->sign
= (sign_char
== '-' ? '-' : ' ');
459 /* Not specified, or the default (-) */
460 if (sign_char
== '-') {
466 /* The number of chars used for non-digits and non-padding. */
467 n_non_digit_non_padding
= spec
->n_sign
+ spec
->n_prefix
+ spec
->n_decimal
+
470 /* min_width can go negative, that's okay. format->width == -1 means
472 if (format
->fill_char
== '0')
473 spec
->n_min_width
= format
->width
- n_non_digit_non_padding
;
475 spec
->n_min_width
= 0;
477 if (spec
->n_digits
== 0)
478 /* This case only occurs when using 'c' formatting, we need
479 to special case it because the grouping code always wants
480 to have at least one character. */
481 spec
->n_grouped_digits
= 0;
483 spec
->n_grouped_digits
= STRINGLIB_GROUPING(NULL
, 0, NULL
,
487 locale
->thousands_sep
);
489 /* Given the desired width and the total of digit and non-digit
490 space we consume, see if we need any padding. format->width can
491 be negative (meaning no padding), but this code still works in
493 n_padding
= format
->width
-
494 (n_non_digit_non_padding
+ spec
->n_grouped_digits
);
496 /* Some padding is needed. Determine if it's left, space, or right. */
497 switch (format
->align
) {
499 spec
->n_rpadding
= n_padding
;
502 spec
->n_lpadding
= n_padding
/ 2;
503 spec
->n_rpadding
= n_padding
- spec
->n_lpadding
;
506 spec
->n_spadding
= n_padding
;
509 /* Handles '>', plus catch-all just in case. */
510 spec
->n_lpadding
= n_padding
;
514 return spec
->n_lpadding
+ spec
->n_sign
+ spec
->n_prefix
+
515 spec
->n_spadding
+ spec
->n_grouped_digits
+ spec
->n_decimal
+
516 spec
->n_remainder
+ spec
->n_rpadding
;
519 /* Fill in the digit parts of a numbers's string representation,
520 as determined in calc_number_widths().
521 No error checking, since we know the buffer is the correct size. */
523 fill_number(STRINGLIB_CHAR
*buf
, const NumberFieldWidths
*spec
,
524 STRINGLIB_CHAR
*digits
, Py_ssize_t n_digits
,
525 STRINGLIB_CHAR
*prefix
, STRINGLIB_CHAR fill_char
,
526 LocaleInfo
*locale
, int toupper
)
528 /* Used to keep track of digits, decimal, and remainder. */
529 STRINGLIB_CHAR
*p
= digits
;
535 if (spec
->n_lpadding
) {
536 STRINGLIB_FILL(buf
, fill_char
, spec
->n_lpadding
);
537 buf
+= spec
->n_lpadding
;
539 if (spec
->n_sign
== 1) {
542 if (spec
->n_prefix
) {
545 spec
->n_prefix
* sizeof(STRINGLIB_CHAR
));
548 for (t
= 0; t
< spec
->n_prefix
; ++t
)
549 buf
[t
] = STRINGLIB_TOUPPER(buf
[t
]);
551 buf
+= spec
->n_prefix
;
553 if (spec
->n_spadding
) {
554 STRINGLIB_FILL(buf
, fill_char
, spec
->n_spadding
);
555 buf
+= spec
->n_spadding
;
558 /* Only for type 'c' special case, it has no digits. */
559 if (spec
->n_digits
!= 0) {
560 /* Fill the digits with InsertThousandsGrouping. */
564 STRINGLIB_GROUPING(buf
, spec
->n_grouped_digits
, digits
,
565 spec
->n_digits
, spec
->n_min_width
,
566 locale
->grouping
, locale
->thousands_sep
);
568 assert(r
== spec
->n_grouped_digits
);
574 for (t
= 0; t
< spec
->n_grouped_digits
; ++t
)
575 buf
[t
] = STRINGLIB_TOUPPER(buf
[t
]);
577 buf
+= spec
->n_grouped_digits
;
579 if (spec
->n_decimal
) {
581 for (t
= 0; t
< spec
->n_decimal
; ++t
)
582 buf
[t
] = locale
->decimal_point
[t
];
583 buf
+= spec
->n_decimal
;
587 if (spec
->n_remainder
) {
588 memcpy(buf
, p
, spec
->n_remainder
* sizeof(STRINGLIB_CHAR
));
589 buf
+= spec
->n_remainder
;
590 p
+= spec
->n_remainder
;
593 if (spec
->n_rpadding
) {
594 STRINGLIB_FILL(buf
, fill_char
, spec
->n_rpadding
);
595 buf
+= spec
->n_rpadding
;
599 static char no_grouping
[1] = {CHAR_MAX
};
601 /* Find the decimal point character(s?), thousands_separator(s?), and
602 grouping description, either for the current locale if type is
603 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
604 none if LT_NO_LOCALE. */
606 get_locale_info(int type
, LocaleInfo
*locale_info
)
609 case LT_CURRENT_LOCALE
: {
610 struct lconv
*locale_data
= localeconv();
611 locale_info
->decimal_point
= locale_data
->decimal_point
;
612 locale_info
->thousands_sep
= locale_data
->thousands_sep
;
613 locale_info
->grouping
= locale_data
->grouping
;
616 case LT_DEFAULT_LOCALE
:
617 locale_info
->decimal_point
= ".";
618 locale_info
->thousands_sep
= ",";
619 locale_info
->grouping
= "\3"; /* Group every 3 characters,
620 trailing 0 means repeat
624 locale_info
->decimal_point
= ".";
625 locale_info
->thousands_sep
= "";
626 locale_info
->grouping
= no_grouping
;
633 #endif /* FORMAT_FLOAT || FORMAT_LONG || FORMAT_COMPLEX */
635 /************************************************************************/
636 /*********** string formatting ******************************************/
637 /************************************************************************/
640 format_string_internal(PyObject
*value
, const InternalFormatSpec
*format
)
646 Py_ssize_t len
= STRINGLIB_LEN(value
);
647 PyObject
*result
= NULL
;
649 /* sign is not allowed on strings */
650 if (format
->sign
!= '\0') {
651 PyErr_SetString(PyExc_ValueError
,
652 "Sign not allowed in string format specifier");
656 /* alternate is not allowed on strings */
657 if (format
->alternate
) {
658 PyErr_SetString(PyExc_ValueError
,
659 "Alternate form (#) not allowed in string format "
664 /* '=' alignment not allowed on strings */
665 if (format
->align
== '=') {
666 PyErr_SetString(PyExc_ValueError
,
667 "'=' alignment not allowed "
668 "in string format specifier");
672 /* if precision is specified, output no more that format.precision
674 if (format
->precision
>= 0 && len
>= format
->precision
) {
675 len
= format
->precision
;
678 calc_padding(len
, format
->width
, format
->align
, &lpad
, &rpad
, &total
);
680 /* allocate the resulting string */
681 result
= STRINGLIB_NEW(NULL
, total
);
685 /* Write into that space. First the padding. */
686 p
= fill_padding(STRINGLIB_STR(result
), len
,
687 format
->fill_char
=='\0'?' ':format
->fill_char
,
690 /* Then the source string. */
691 memcpy(p
, STRINGLIB_STR(value
), len
* sizeof(STRINGLIB_CHAR
));
698 /************************************************************************/
699 /*********** long formatting ********************************************/
700 /************************************************************************/
702 #if defined FORMAT_LONG || defined FORMAT_INT
704 (*IntOrLongToString
)(PyObject
*value
, int base
);
707 format_int_or_long_internal(PyObject
*value
, const InternalFormatSpec
*format
,
708 IntOrLongToString tostring
)
710 PyObject
*result
= NULL
;
711 PyObject
*tmp
= NULL
;
712 STRINGLIB_CHAR
*pnumeric_chars
;
713 STRINGLIB_CHAR numeric_char
;
714 STRINGLIB_CHAR sign_char
= '\0';
715 Py_ssize_t n_digits
; /* count of digits need from the computed
717 Py_ssize_t n_remainder
= 0; /* Used only for 'c' formatting, which
718 produces non-digits */
719 Py_ssize_t n_prefix
= 0; /* Count of prefix chars, (e.g., '0x') */
721 STRINGLIB_CHAR
*prefix
= NULL
;
722 NumberFieldWidths spec
;
725 /* Locale settings, either from the actual locale or
726 from a hard-code pseudo-locale */
729 /* no precision allowed on integers */
730 if (format
->precision
!= -1) {
731 PyErr_SetString(PyExc_ValueError
,
732 "Precision not allowed in integer format specifier");
736 /* special case for character formatting */
737 if (format
->type
== 'c') {
738 /* error to specify a sign */
739 if (format
->sign
!= '\0') {
740 PyErr_SetString(PyExc_ValueError
,
741 "Sign not allowed with integer"
742 " format specifier 'c'");
746 /* Error to specify a comma. */
747 if (format
->thousands_separators
) {
748 PyErr_SetString(PyExc_ValueError
,
749 "Thousands separators not allowed with integer"
750 " format specifier 'c'");
754 /* taken from unicodeobject.c formatchar() */
755 /* Integer input truncated to a character */
756 /* XXX: won't work for int */
757 x
= PyLong_AsLong(value
);
758 if (x
== -1 && PyErr_Occurred())
760 #ifdef Py_UNICODE_WIDE
761 if (x
< 0 || x
> 0x10ffff) {
762 PyErr_SetString(PyExc_OverflowError
,
763 "%c arg not in range(0x110000) "
764 "(wide Python build)");
768 if (x
< 0 || x
> 0xffff) {
769 PyErr_SetString(PyExc_OverflowError
,
770 "%c arg not in range(0x10000) "
771 "(narrow Python build)");
775 numeric_char
= (STRINGLIB_CHAR
)x
;
776 pnumeric_chars
= &numeric_char
;
779 /* As a sort-of hack, we tell calc_number_widths that we only
780 have "remainder" characters. calc_number_widths thinks
781 these are characters that don't get formatted, only copied
782 into the output string. We do this for 'c' formatting,
783 because the characters are likely to be non-digits. */
788 int leading_chars_to_skip
= 0; /* Number of characters added by
789 PyNumber_ToBase that we want to
792 /* Compute the base and how many characters will be added by
794 switch (format
->type
) {
797 leading_chars_to_skip
= 2; /* 0b */
801 leading_chars_to_skip
= 2; /* 0o */
806 leading_chars_to_skip
= 2; /* 0x */
808 default: /* shouldn't be needed, but stops a compiler warning */
815 /* The number of prefix chars is the same as the leading
817 if (format
->alternate
)
818 n_prefix
= leading_chars_to_skip
;
820 /* Do the hard part, converting to a string in a given base */
821 tmp
= tostring(value
, base
);
825 pnumeric_chars
= STRINGLIB_STR(tmp
);
826 n_digits
= STRINGLIB_LEN(tmp
);
828 prefix
= pnumeric_chars
;
830 /* Remember not to modify what pnumeric_chars points to. it
831 might be interned. Only modify it after we copy it into a
832 newly allocated output buffer. */
834 /* Is a sign character present in the output? If so, remember it
836 if (pnumeric_chars
[0] == '-') {
837 sign_char
= pnumeric_chars
[0];
839 ++leading_chars_to_skip
;
842 /* Skip over the leading chars (0x, 0b, etc.) */
843 n_digits
-= leading_chars_to_skip
;
844 pnumeric_chars
+= leading_chars_to_skip
;
847 /* Determine the grouping, separator, and decimal point, if any. */
848 get_locale_info(format
->type
== 'n' ? LT_CURRENT_LOCALE
:
849 (format
->thousands_separators
?
854 /* Calculate how much memory we'll need. */
855 n_total
= calc_number_widths(&spec
, n_prefix
, sign_char
, pnumeric_chars
,
856 n_digits
, n_remainder
, 0, &locale
, format
);
858 /* Allocate the memory. */
859 result
= STRINGLIB_NEW(NULL
, n_total
);
863 /* Populate the memory. */
864 fill_number(STRINGLIB_STR(result
), &spec
, pnumeric_chars
, n_digits
,
865 prefix
, format
->fill_char
== '\0' ? ' ' : format
->fill_char
,
866 &locale
, format
->type
== 'X');
872 #endif /* defined FORMAT_LONG || defined FORMAT_INT */
874 /************************************************************************/
875 /*********** float formatting *******************************************/
876 /************************************************************************/
879 #if STRINGLIB_IS_UNICODE
881 strtounicode(Py_UNICODE
*buffer
, const char *charbuffer
, Py_ssize_t len
)
884 for (i
= 0; i
< len
; ++i
)
885 buffer
[i
] = (Py_UNICODE
)charbuffer
[i
];
889 /* much of this is taken from unicodeobject.c */
891 format_float_internal(PyObject
*value
,
892 const InternalFormatSpec
*format
)
894 char *buf
= NULL
; /* buffer returned from PyOS_double_to_string */
896 Py_ssize_t n_remainder
;
900 Py_ssize_t precision
= format
->precision
;
901 Py_ssize_t default_precision
= 6;
902 STRINGLIB_CHAR type
= format
->type
;
905 NumberFieldWidths spec
;
907 PyObject
*result
= NULL
;
908 STRINGLIB_CHAR sign_char
= '\0';
909 int float_type
; /* Used to see if we have a nan, inf, or regular float. */
911 #if STRINGLIB_IS_UNICODE
912 Py_UNICODE
*unicode_tmp
= NULL
;
915 /* Locale settings, either from the actual locale or
916 from a hard-code pseudo-locale */
919 /* Alternate is not allowed on floats. */
920 if (format
->alternate
) {
921 PyErr_SetString(PyExc_ValueError
,
922 "Alternate form (#) not allowed in float format "
928 /* Omitted type specifier. This is like 'g' but with at least one
929 digit after the decimal point, and different default precision.*/
931 default_precision
= PyFloat_STR_PRECISION
;
932 flags
|= Py_DTSF_ADD_DOT_0
;
936 /* 'n' is the same as 'g', except for the locale used to
937 format the result. We take care of that later. */
940 #if PY_VERSION_HEX < 0x0301000
941 /* 'F' is the same as 'f', per the PEP */
942 /* This is no longer the case in 3.x */
947 val
= PyFloat_AsDouble(value
);
948 if (val
== -1.0 && PyErr_Occurred())
958 precision
= default_precision
;
960 #if PY_VERSION_HEX < 0x03010000
961 /* 3.1 no longer converts large 'f' to 'g'. */
962 if ((type
== 'f' || type
== 'F') && fabs(val
) >= 1e50
)
966 /* Cast "type", because if we're in unicode we need to pass a
967 8-bit char. This is safe, because we've restricted what "type"
969 buf
= PyOS_double_to_string(val
, (char)type
, precision
, flags
,
973 n_digits
= strlen(buf
);
976 /* We know that buf has a trailing zero (since we just called
977 strlen() on it), and we don't use that fact any more. So we
978 can just write over the trailing zero. */
983 /* Since there is no unicode version of PyOS_double_to_string,
984 just use the 8 bit version and then convert to unicode. */
985 #if STRINGLIB_IS_UNICODE
986 unicode_tmp
= (Py_UNICODE
*)PyMem_Malloc((n_digits
)*sizeof(Py_UNICODE
));
987 if (unicode_tmp
== NULL
) {
991 strtounicode(unicode_tmp
, buf
, n_digits
);
997 /* Is a sign character present in the output? If so, remember it
1005 /* Determine if we have any "remainder" (after the digits, might include
1006 decimal or exponent or both (or neither)) */
1007 parse_number(p
, n_digits
, &n_remainder
, &has_decimal
);
1009 /* Determine the grouping, separator, and decimal point, if any. */
1010 get_locale_info(format
->type
== 'n' ? LT_CURRENT_LOCALE
:
1011 (format
->thousands_separators
?
1016 /* Calculate how much memory we'll need. */
1017 n_total
= calc_number_widths(&spec
, 0, sign_char
, p
, n_digits
,
1018 n_remainder
, has_decimal
, &locale
, format
);
1020 /* Allocate the memory. */
1021 result
= STRINGLIB_NEW(NULL
, n_total
);
1025 /* Populate the memory. */
1026 fill_number(STRINGLIB_STR(result
), &spec
, p
, n_digits
, NULL
,
1027 format
->fill_char
== '\0' ? ' ' : format
->fill_char
, &locale
,
1032 #if STRINGLIB_IS_UNICODE
1033 PyMem_Free(unicode_tmp
);
1037 #endif /* FORMAT_FLOAT */
1039 /************************************************************************/
1040 /*********** complex formatting *****************************************/
1041 /************************************************************************/
1043 #ifdef FORMAT_COMPLEX
1046 format_complex_internal(PyObject
*value
,
1047 const InternalFormatSpec
*format
)
1051 char *re_buf
= NULL
; /* buffer returned from PyOS_double_to_string */
1052 char *im_buf
= NULL
; /* buffer returned from PyOS_double_to_string */
1054 InternalFormatSpec tmp_format
= *format
;
1055 Py_ssize_t n_re_digits
;
1056 Py_ssize_t n_im_digits
;
1057 Py_ssize_t n_re_remainder
;
1058 Py_ssize_t n_im_remainder
;
1059 Py_ssize_t n_re_total
;
1060 Py_ssize_t n_im_total
;
1063 Py_ssize_t precision
= format
->precision
;
1064 Py_ssize_t default_precision
= 6;
1065 STRINGLIB_CHAR type
= format
->type
;
1066 STRINGLIB_CHAR
*p_re
;
1067 STRINGLIB_CHAR
*p_im
;
1068 NumberFieldWidths re_spec
;
1069 NumberFieldWidths im_spec
;
1071 PyObject
*result
= NULL
;
1073 STRINGLIB_CHAR re_sign_char
= '\0';
1074 STRINGLIB_CHAR im_sign_char
= '\0';
1075 int re_float_type
; /* Used to see if we have a nan, inf, or regular float. */
1083 #if STRINGLIB_IS_UNICODE
1084 Py_UNICODE
*re_unicode_tmp
= NULL
;
1085 Py_UNICODE
*im_unicode_tmp
= NULL
;
1088 /* Locale settings, either from the actual locale or
1089 from a hard-code pseudo-locale */
1092 /* Alternate is not allowed on complex. */
1093 if (format
->alternate
) {
1094 PyErr_SetString(PyExc_ValueError
,
1095 "Alternate form (#) not allowed in complex format "
1100 /* Neither is zero pading. */
1101 if (format
->fill_char
== '0') {
1102 PyErr_SetString(PyExc_ValueError
,
1103 "Zero padding is not allowed in complex format "
1108 /* Neither is '=' alignment . */
1109 if (format
->align
== '=') {
1110 PyErr_SetString(PyExc_ValueError
,
1111 "'=' alignment flag is not allowed in complex format "
1116 re
= PyComplex_RealAsDouble(value
);
1117 if (re
== -1.0 && PyErr_Occurred())
1119 im
= PyComplex_ImagAsDouble(value
);
1120 if (im
== -1.0 && PyErr_Occurred())
1124 /* Omitted type specifier. Should be like str(self). */
1126 default_precision
= PyFloat_STR_PRECISION
;
1133 /* 'n' is the same as 'g', except for the locale used to
1134 format the result. We take care of that later. */
1137 #if PY_VERSION_HEX < 0x03010000
1138 /* This is no longer the case in 3.x */
1139 /* 'F' is the same as 'f', per the PEP */
1145 precision
= default_precision
;
1147 /* Cast "type", because if we're in unicode we need to pass a
1148 8-bit char. This is safe, because we've restricted what "type"
1150 re_buf
= PyOS_double_to_string(re
, (char)type
, precision
, flags
,
1154 im_buf
= PyOS_double_to_string(im
, (char)type
, precision
, flags
,
1159 n_re_digits
= strlen(re_buf
);
1160 n_im_digits
= strlen(im_buf
);
1162 /* Since there is no unicode version of PyOS_double_to_string,
1163 just use the 8 bit version and then convert to unicode. */
1164 #if STRINGLIB_IS_UNICODE
1165 re_unicode_tmp
= (Py_UNICODE
*)PyMem_Malloc((n_re_digits
)*sizeof(Py_UNICODE
));
1166 if (re_unicode_tmp
== NULL
) {
1170 strtounicode(re_unicode_tmp
, re_buf
, n_re_digits
);
1171 p_re
= re_unicode_tmp
;
1173 im_unicode_tmp
= (Py_UNICODE
*)PyMem_Malloc((n_im_digits
)*sizeof(Py_UNICODE
));
1174 if (im_unicode_tmp
== NULL
) {
1178 strtounicode(im_unicode_tmp
, im_buf
, n_im_digits
);
1179 p_im
= im_unicode_tmp
;
1185 /* Is a sign character present in the output? If so, remember it
1188 re_sign_char
= *p_re
;
1193 im_sign_char
= *p_im
;
1198 /* Determine if we have any "remainder" (after the digits, might include
1199 decimal or exponent or both (or neither)) */
1200 parse_number(p_re
, n_re_digits
, &n_re_remainder
, &re_has_decimal
);
1201 parse_number(p_im
, n_im_digits
, &n_im_remainder
, &im_has_decimal
);
1203 /* Determine the grouping, separator, and decimal point, if any. */
1204 get_locale_info(format
->type
== 'n' ? LT_CURRENT_LOCALE
:
1205 (format
->thousands_separators
?
1210 /* Turn off any padding. We'll do it later after we've composed
1211 the numbers without padding. */
1212 tmp_format
.fill_char
= '\0';
1213 tmp_format
.align
= '\0';
1214 tmp_format
.width
= -1;
1216 /* Calculate how much memory we'll need. */
1217 n_re_total
= calc_number_widths(&re_spec
, 0, re_sign_char
, p_re
,
1218 n_re_digits
, n_re_remainder
,
1219 re_has_decimal
, &locale
, &tmp_format
);
1221 /* Same formatting, but always include a sign. */
1222 tmp_format
.sign
= '+';
1223 n_im_total
= calc_number_widths(&im_spec
, 0, im_sign_char
, p_im
,
1224 n_im_digits
, n_im_remainder
,
1225 im_has_decimal
, &locale
, &tmp_format
);
1230 /* Add 1 for the 'j', and optionally 2 for parens. */
1231 calc_padding(n_re_total
+ n_im_total
+ 1 + add_parens
* 2,
1232 format
->width
, format
->align
, &lpad
, &rpad
, &total
);
1234 result
= STRINGLIB_NEW(NULL
, total
);
1238 /* Populate the memory. First, the padding. */
1239 p
= fill_padding(STRINGLIB_STR(result
),
1240 n_re_total
+ n_im_total
+ 1 + add_parens
* 2,
1241 format
->fill_char
=='\0' ? ' ' : format
->fill_char
,
1248 fill_number(p
, &re_spec
, p_re
, n_re_digits
, NULL
, 0, &locale
, 0);
1251 fill_number(p
, &im_spec
, p_im
, n_im_digits
, NULL
, 0, &locale
, 0);
1261 #if STRINGLIB_IS_UNICODE
1262 PyMem_Free(re_unicode_tmp
);
1263 PyMem_Free(im_unicode_tmp
);
1267 #endif /* FORMAT_COMPLEX */
1269 /************************************************************************/
1270 /*********** built in formatters ****************************************/
1271 /************************************************************************/
1273 FORMAT_STRING(PyObject
*obj
,
1274 STRINGLIB_CHAR
*format_spec
,
1275 Py_ssize_t format_spec_len
)
1277 InternalFormatSpec format
;
1278 PyObject
*result
= NULL
;
1280 /* check for the special case of zero length format spec, make
1281 it equivalent to str(obj) */
1282 if (format_spec_len
== 0) {
1283 result
= STRINGLIB_TOSTR(obj
);
1287 /* parse the format_spec */
1288 if (!parse_internal_render_format_spec(format_spec
, format_spec_len
,
1292 /* type conversion? */
1293 switch (format
.type
) {
1295 /* no type conversion needed, already a string. do the formatting */
1296 result
= format_string_internal(obj
, &format
);
1300 unknown_presentation_type(format
.type
, obj
->ob_type
->tp_name
);
1308 #if defined FORMAT_LONG || defined FORMAT_INT
1310 format_int_or_long(PyObject
* obj
,
1311 STRINGLIB_CHAR
*format_spec
,
1312 Py_ssize_t format_spec_len
,
1313 IntOrLongToString tostring
)
1315 PyObject
*result
= NULL
;
1316 PyObject
*tmp
= NULL
;
1317 InternalFormatSpec format
;
1319 /* check for the special case of zero length format spec, make
1320 it equivalent to str(obj) */
1321 if (format_spec_len
== 0) {
1322 result
= STRINGLIB_TOSTR(obj
);
1326 /* parse the format_spec */
1327 if (!parse_internal_render_format_spec(format_spec
,
1332 /* type conversion? */
1333 switch (format
.type
) {
1341 /* no type conversion needed, already an int (or long). do
1343 result
= format_int_or_long_internal(obj
, &format
, tostring
);
1353 /* convert to float */
1354 tmp
= PyNumber_Float(obj
);
1357 result
= format_float_internal(tmp
, &format
);
1362 unknown_presentation_type(format
.type
, obj
->ob_type
->tp_name
);
1370 #endif /* FORMAT_LONG || defined FORMAT_INT */
1373 /* Need to define long_format as a function that will convert a long
1374 to a string. In 3.0, _PyLong_Format has the correct signature. In
1375 2.x, we need to fudge a few parameters */
1376 #if PY_VERSION_HEX >= 0x03000000
1377 #define long_format _PyLong_Format
1380 long_format(PyObject
* value
, int base
)
1382 /* Convert to base, don't add trailing 'L', and use the new octal
1383 format. We already know this is a long object */
1384 assert(PyLong_Check(value
));
1385 /* convert to base, don't add 'L', and use the new octal format */
1386 return _PyLong_Format(value
, base
, 0, 1);
1391 FORMAT_LONG(PyObject
*obj
,
1392 STRINGLIB_CHAR
*format_spec
,
1393 Py_ssize_t format_spec_len
)
1395 return format_int_or_long(obj
, format_spec
, format_spec_len
,
1398 #endif /* FORMAT_LONG */
1401 /* this is only used for 2.x, not 3.0 */
1403 int_format(PyObject
* value
, int base
)
1405 /* Convert to base, and use the new octal format. We already
1406 know this is an int object */
1407 assert(PyInt_Check(value
));
1408 return _PyInt_Format((PyIntObject
*)value
, base
, 1);
1412 FORMAT_INT(PyObject
*obj
,
1413 STRINGLIB_CHAR
*format_spec
,
1414 Py_ssize_t format_spec_len
)
1416 return format_int_or_long(obj
, format_spec
, format_spec_len
,
1419 #endif /* FORMAT_INT */
1423 FORMAT_FLOAT(PyObject
*obj
,
1424 STRINGLIB_CHAR
*format_spec
,
1425 Py_ssize_t format_spec_len
)
1427 PyObject
*result
= NULL
;
1428 InternalFormatSpec format
;
1430 /* check for the special case of zero length format spec, make
1431 it equivalent to str(obj) */
1432 if (format_spec_len
== 0) {
1433 result
= STRINGLIB_TOSTR(obj
);
1437 /* parse the format_spec */
1438 if (!parse_internal_render_format_spec(format_spec
,
1443 /* type conversion? */
1444 switch (format
.type
) {
1445 case '\0': /* No format code: like 'g', but with at least one decimal. */
1454 /* no conversion, already a float. do the formatting */
1455 result
= format_float_internal(obj
, &format
);
1460 unknown_presentation_type(format
.type
, obj
->ob_type
->tp_name
);
1467 #endif /* FORMAT_FLOAT */
1469 #ifdef FORMAT_COMPLEX
1471 FORMAT_COMPLEX(PyObject
*obj
,
1472 STRINGLIB_CHAR
*format_spec
,
1473 Py_ssize_t format_spec_len
)
1475 PyObject
*result
= NULL
;
1476 InternalFormatSpec format
;
1478 /* check for the special case of zero length format spec, make
1479 it equivalent to str(obj) */
1480 if (format_spec_len
== 0) {
1481 result
= STRINGLIB_TOSTR(obj
);
1485 /* parse the format_spec */
1486 if (!parse_internal_render_format_spec(format_spec
,
1491 /* type conversion? */
1492 switch (format
.type
) {
1493 case '\0': /* No format code: like 'g', but with at least one decimal. */
1501 /* no conversion, already a complex. do the formatting */
1502 result
= format_complex_internal(obj
, &format
);
1507 unknown_presentation_type(format
.type
, obj
->ob_type
->tp_name
);
1514 #endif /* FORMAT_COMPLEX */