blob: 617d58b2072d1008ec82e7ebe690922cab378176 [file] [log] [blame]
Eric Smith8c663262007-08-25 02:26:07 +00001/* implements the unicode (as opposed to string) version of the
2 built-in formatters for string, int, float. that is, the versions
3 of int.__float__, etc., that take and return unicode objects */
4
5#include "Python.h"
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02006#include <locale.h>
7
8/* Raises an exception about an unknown presentation type for this
9 * type. */
10
11static void
12unknown_presentation_type(Py_UCS4 presentation_type,
13 const char* type_name)
14{
15 /* %c might be out-of-range, hence the two cases. */
16 if (presentation_type > 32 && presentation_type < 128)
17 PyErr_Format(PyExc_ValueError,
18 "Unknown format code '%c' "
19 "for object of type '%.200s'",
20 (char)presentation_type,
21 type_name);
22 else
23 PyErr_Format(PyExc_ValueError,
24 "Unknown format code '\\x%x' "
25 "for object of type '%.200s'",
26 (unsigned int)presentation_type,
27 type_name);
28}
29
30static void
31invalid_comma_type(Py_UCS4 presentation_type)
32{
33 if (presentation_type > 32 && presentation_type < 128)
34 PyErr_Format(PyExc_ValueError,
35 "Cannot specify ',' with '%c'.",
36 (char)presentation_type);
37 else
38 PyErr_Format(PyExc_ValueError,
39 "Cannot specify ',' with '\\x%x'.",
40 (unsigned int)presentation_type);
41}
42
43/*
44 get_integer consumes 0 or more decimal digit characters from an
45 input string, updates *result with the corresponding positive
46 integer, and returns the number of digits consumed.
47
48 returns -1 on error.
49*/
50static int
51get_integer(PyObject *str, Py_ssize_t *pos, Py_ssize_t end,
52 Py_ssize_t *result)
53{
Mark Dickinson47862d42011-12-01 15:27:04 +000054 Py_ssize_t accumulator, digitval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020055 int numdigits;
56 accumulator = numdigits = 0;
57 for (;;(*pos)++, numdigits++) {
58 if (*pos >= end)
59 break;
60 digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ_CHAR(str, *pos));
61 if (digitval < 0)
62 break;
63 /*
Mark Dickinson47862d42011-12-01 15:27:04 +000064 Detect possible overflow before it happens:
65
66 accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
67 accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020068 */
Mark Dickinson47862d42011-12-01 15:27:04 +000069 if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020070 PyErr_Format(PyExc_ValueError,
71 "Too many decimal digits in format string");
72 return -1;
73 }
Mark Dickinson47862d42011-12-01 15:27:04 +000074 accumulator = accumulator * 10 + digitval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020075 }
76 *result = accumulator;
77 return numdigits;
78}
79
80/************************************************************************/
81/*********** standard format specifier parsing **************************/
82/************************************************************************/
83
84/* returns true if this character is a specifier alignment token */
85Py_LOCAL_INLINE(int)
86is_alignment_token(Py_UCS4 c)
87{
88 switch (c) {
89 case '<': case '>': case '=': case '^':
90 return 1;
91 default:
92 return 0;
93 }
94}
95
96/* returns true if this character is a sign element */
97Py_LOCAL_INLINE(int)
98is_sign_element(Py_UCS4 c)
99{
100 switch (c) {
101 case ' ': case '+': case '-':
102 return 1;
103 default:
104 return 0;
105 }
106}
Eric Smith8c663262007-08-25 02:26:07 +0000107
Eric Smith4a7d76d2008-05-30 18:10:19 +0000108
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200109typedef struct {
110 Py_UCS4 fill_char;
111 Py_UCS4 align;
112 int alternate;
113 Py_UCS4 sign;
114 Py_ssize_t width;
115 int thousands_separators;
116 Py_ssize_t precision;
117 Py_UCS4 type;
118} InternalFormatSpec;
Eric Smith4a7d76d2008-05-30 18:10:19 +0000119
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200120#if 0
Martin Panter0be894b2016-09-07 12:03:06 +0000121/* Occasionally useful for debugging. Should normally be commented out. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200122static void
123DEBUG_PRINT_FORMAT_SPEC(InternalFormatSpec *format)
124{
125 printf("internal format spec: fill_char %d\n", format->fill_char);
126 printf("internal format spec: align %d\n", format->align);
127 printf("internal format spec: alternate %d\n", format->alternate);
128 printf("internal format spec: sign %d\n", format->sign);
129 printf("internal format spec: width %zd\n", format->width);
130 printf("internal format spec: thousands_separators %d\n",
131 format->thousands_separators);
132 printf("internal format spec: precision %zd\n", format->precision);
133 printf("internal format spec: type %c\n", format->type);
134 printf("\n");
135}
136#endif
137
138
139/*
140 ptr points to the start of the format_spec, end points just past its end.
141 fills in format with the parsed information.
142 returns 1 on success, 0 on failure.
143 if failure, sets the exception
144*/
145static int
146parse_internal_render_format_spec(PyObject *format_spec,
147 Py_ssize_t start, Py_ssize_t end,
148 InternalFormatSpec *format,
149 char default_type,
150 char default_align)
151{
152 Py_ssize_t pos = start;
153 /* end-pos is used throughout this code to specify the length of
154 the input string */
155#define READ_spec(index) PyUnicode_READ_CHAR(format_spec, index)
156
157 Py_ssize_t consumed;
158 int align_specified = 0;
Eric V. Smith2ea97122014-04-14 11:55:10 -0400159 int fill_char_specified = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200160
Eric V. Smith2ea97122014-04-14 11:55:10 -0400161 format->fill_char = ' ';
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200162 format->align = default_align;
163 format->alternate = 0;
164 format->sign = '\0';
165 format->width = -1;
166 format->thousands_separators = 0;
167 format->precision = -1;
168 format->type = default_type;
169
170 /* If the second char is an alignment token,
171 then parse the fill char */
172 if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) {
173 format->align = READ_spec(pos+1);
174 format->fill_char = READ_spec(pos);
Eric V. Smith2ea97122014-04-14 11:55:10 -0400175 fill_char_specified = 1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200176 align_specified = 1;
177 pos += 2;
178 }
179 else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) {
180 format->align = READ_spec(pos);
181 align_specified = 1;
182 ++pos;
183 }
184
185 /* Parse the various sign options */
186 if (end-pos >= 1 && is_sign_element(READ_spec(pos))) {
187 format->sign = READ_spec(pos);
188 ++pos;
189 }
190
191 /* If the next character is #, we're in alternate mode. This only
192 applies to integers. */
193 if (end-pos >= 1 && READ_spec(pos) == '#') {
194 format->alternate = 1;
195 ++pos;
196 }
197
198 /* The special case for 0-padding (backwards compat) */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400199 if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200200 format->fill_char = '0';
201 if (!align_specified) {
202 format->align = '=';
203 }
204 ++pos;
205 }
206
207 consumed = get_integer(format_spec, &pos, end, &format->width);
208 if (consumed == -1)
209 /* Overflow error. Exception already set. */
210 return 0;
211
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. */
216 if (consumed == 0)
217 format->width = -1;
218
219 /* Comma signifies add thousands separators */
220 if (end-pos && READ_spec(pos) == ',') {
221 format->thousands_separators = 1;
222 ++pos;
223 }
224
225 /* Parse field precision */
226 if (end-pos && READ_spec(pos) == '.') {
227 ++pos;
228
229 consumed = get_integer(format_spec, &pos, end, &format->precision);
230 if (consumed == -1)
231 /* Overflow error. Exception already set. */
232 return 0;
233
234 /* Not having a precision after a dot is an error. */
235 if (consumed == 0) {
236 PyErr_Format(PyExc_ValueError,
237 "Format specifier missing precision");
238 return 0;
239 }
240
241 }
242
243 /* Finally, parse the type field. */
244
245 if (end-pos > 1) {
Eric V. Smithd25cfe62012-01-19 20:04:28 -0500246 /* More than one char remain, invalid format specifier. */
247 PyErr_Format(PyExc_ValueError, "Invalid format specifier");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200248 return 0;
249 }
250
251 if (end-pos == 1) {
252 format->type = READ_spec(pos);
253 ++pos;
254 }
255
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). */
259
260 if (format->thousands_separators) {
261 switch (format->type) {
262 case 'd':
263 case 'e':
264 case 'f':
265 case 'g':
266 case 'E':
267 case 'G':
268 case '%':
269 case 'F':
270 case '\0':
271 /* These are allowed. See PEP 378.*/
272 break;
273 default:
274 invalid_comma_type(format->type);
275 return 0;
276 }
277 }
278
Victor Stinnera4ac6002012-01-21 15:50:49 +0100279 assert (format->align <= 127);
280 assert (format->sign <= 127);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200281 return 1;
282}
283
284/* Calculate the padding needed. */
285static void
286calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align,
287 Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
288 Py_ssize_t *n_total)
289{
290 if (width >= 0) {
291 if (nchars > width)
292 *n_total = nchars;
293 else
294 *n_total = width;
295 }
296 else {
297 /* not specified, use all of the chars and no more */
298 *n_total = nchars;
299 }
300
301 /* Figure out how much leading space we need, based on the
302 aligning */
303 if (align == '>')
304 *n_lpadding = *n_total - nchars;
305 else if (align == '^')
306 *n_lpadding = (*n_total - nchars) / 2;
307 else if (align == '<' || align == '=')
308 *n_lpadding = 0;
309 else {
310 /* We should never have an unspecified alignment. */
311 *n_lpadding = 0;
312 assert(0);
313 }
314
315 *n_rpadding = *n_total - nchars - *n_lpadding;
316}
317
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200318/* Do the padding, and return a pointer to where the caller-supplied
319 content goes. */
Victor Stinner9ce59bb2013-05-17 00:04:56 +0200320static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200321fill_padding(_PyUnicodeWriter *writer,
322 Py_ssize_t nchars,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200323 Py_UCS4 fill_char, Py_ssize_t n_lpadding,
324 Py_ssize_t n_rpadding)
325{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200326 Py_ssize_t pos;
327
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200328 /* Pad on left. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200329 if (n_lpadding) {
330 pos = writer->pos;
331 _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char);
332 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200333
334 /* Pad on right. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200335 if (n_rpadding) {
336 pos = writer->pos + nchars + n_lpadding;
337 _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char);
338 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200339
340 /* Pointer to the user content. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200341 writer->pos += n_lpadding;
342 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200343}
344
345/************************************************************************/
346/*********** common routines for numeric formatting *********************/
347/************************************************************************/
348
349/* Locale type codes. */
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700350enum LocaleType {
351 LT_CURRENT_LOCALE,
352 LT_DEFAULT_LOCALE,
353 LT_NO_LOCALE
354};
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200355
356/* Locale info needed for formatting integers and the part of floats
357 before and including the decimal. Note that locales only support
358 8-bit chars, not unicode. */
359typedef struct {
Victor Stinner41a863c2012-02-24 00:37:51 +0100360 PyObject *decimal_point;
361 PyObject *thousands_sep;
362 const char *grouping;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200363} LocaleInfo;
364
Victor Stinner41a863c2012-02-24 00:37:51 +0100365#define STATIC_LOCALE_INFO_INIT {0, 0, 0}
366
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200367/* describes the layout for an integer, see the comment in
368 calc_number_widths() for details */
369typedef struct {
370 Py_ssize_t n_lpadding;
371 Py_ssize_t n_prefix;
372 Py_ssize_t n_spadding;
373 Py_ssize_t n_rpadding;
374 char sign;
375 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
376 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
377 any grouping chars. */
378 Py_ssize_t n_decimal; /* 0 if only an integer */
379 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
380 excluding the decimal itself, if
381 present. */
382
383 /* These 2 are not the widths of fields, but are needed by
384 STRINGLIB_GROUPING. */
385 Py_ssize_t n_digits; /* The number of digits before a decimal
386 or exponent. */
387 Py_ssize_t n_min_width; /* The min_width we used when we computed
388 the n_grouped_digits width. */
389} NumberFieldWidths;
390
391
392/* Given a number of the form:
393 digits[remainder]
394 where ptr points to the start and end points to the end, find where
395 the integer part ends. This could be a decimal, an exponent, both,
396 or neither.
397 If a decimal point is present, set *has_decimal and increment
398 remainder beyond it.
399 Results are undefined (but shouldn't crash) for improperly
400 formatted strings.
401*/
402static void
403parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end,
404 Py_ssize_t *n_remainder, int *has_decimal)
405{
406 Py_ssize_t remainder;
407
Antoine Pitrouc73c5612013-02-09 23:14:42 +0100408 while (pos<end && Py_ISDIGIT(PyUnicode_READ_CHAR(s, pos)))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200409 ++pos;
410 remainder = pos;
411
412 /* Does remainder start with a decimal point? */
413 *has_decimal = pos<end && PyUnicode_READ_CHAR(s, remainder) == '.';
414
415 /* Skip the decimal point. */
416 if (*has_decimal)
417 remainder++;
418
419 *n_remainder = end - remainder;
420}
421
422/* not all fields of format are used. for example, precision is
423 unused. should this take discrete params in order to be more clear
424 about what it does? or is passing a single format parameter easier
425 and more efficient enough to justify a little obfuscation? */
426static Py_ssize_t
427calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
428 Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start,
429 Py_ssize_t n_end, Py_ssize_t n_remainder,
430 int has_decimal, const LocaleInfo *locale,
Victor Stinner41a863c2012-02-24 00:37:51 +0100431 const InternalFormatSpec *format, Py_UCS4 *maxchar)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200432{
433 Py_ssize_t n_non_digit_non_padding;
434 Py_ssize_t n_padding;
435
436 spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0);
437 spec->n_lpadding = 0;
438 spec->n_prefix = n_prefix;
Victor Stinner41a863c2012-02-24 00:37:51 +0100439 spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200440 spec->n_remainder = n_remainder;
441 spec->n_spadding = 0;
442 spec->n_rpadding = 0;
443 spec->sign = '\0';
444 spec->n_sign = 0;
445
446 /* the output will look like:
447 | |
448 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
449 | |
450
451 sign is computed from format->sign and the actual
452 sign of the number
453
454 prefix is given (it's for the '0x' prefix)
455
456 digits is already known
457
458 the total width is either given, or computed from the
459 actual digits
460
461 only one of lpadding, spadding, and rpadding can be non-zero,
462 and it's calculated from the width and other fields
463 */
464
465 /* compute the various parts we're going to write */
466 switch (format->sign) {
467 case '+':
468 /* always put a + or - */
469 spec->n_sign = 1;
470 spec->sign = (sign_char == '-' ? '-' : '+');
471 break;
472 case ' ':
473 spec->n_sign = 1;
474 spec->sign = (sign_char == '-' ? '-' : ' ');
475 break;
476 default:
477 /* Not specified, or the default (-) */
478 if (sign_char == '-') {
479 spec->n_sign = 1;
480 spec->sign = '-';
481 }
482 }
483
484 /* The number of chars used for non-digits and non-padding. */
485 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
486 spec->n_remainder;
487
488 /* min_width can go negative, that's okay. format->width == -1 means
489 we don't care. */
490 if (format->fill_char == '0' && format->align == '=')
491 spec->n_min_width = format->width - n_non_digit_non_padding;
492 else
493 spec->n_min_width = 0;
494
495 if (spec->n_digits == 0)
496 /* This case only occurs when using 'c' formatting, we need
497 to special case it because the grouping code always wants
498 to have at least one character. */
499 spec->n_grouped_digits = 0;
Victor Stinner41a863c2012-02-24 00:37:51 +0100500 else {
501 Py_UCS4 grouping_maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200502 spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
Victor Stinner41a863c2012-02-24 00:37:51 +0100503 NULL, 0,
504 0, NULL,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200505 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100506 locale->grouping, locale->thousands_sep, &grouping_maxchar);
507 *maxchar = Py_MAX(*maxchar, grouping_maxchar);
508 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200509
510 /* Given the desired width and the total of digit and non-digit
511 space we consume, see if we need any padding. format->width can
512 be negative (meaning no padding), but this code still works in
513 that case. */
514 n_padding = format->width -
515 (n_non_digit_non_padding + spec->n_grouped_digits);
516 if (n_padding > 0) {
517 /* Some padding is needed. Determine if it's left, space, or right. */
518 switch (format->align) {
519 case '<':
520 spec->n_rpadding = n_padding;
521 break;
522 case '^':
523 spec->n_lpadding = n_padding / 2;
524 spec->n_rpadding = n_padding - spec->n_lpadding;
525 break;
526 case '=':
527 spec->n_spadding = n_padding;
528 break;
529 case '>':
530 spec->n_lpadding = n_padding;
531 break;
532 default:
533 /* Shouldn't get here, but treat it as '>' */
534 spec->n_lpadding = n_padding;
535 assert(0);
536 break;
537 }
538 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100539
540 if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding)
541 *maxchar = Py_MAX(*maxchar, format->fill_char);
542
Victor Stinner90f50d42012-02-24 01:44:47 +0100543 if (spec->n_decimal)
544 *maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point));
545
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200546 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
547 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
548 spec->n_remainder + spec->n_rpadding;
549}
550
551/* Fill in the digit parts of a numbers's string representation,
552 as determined in calc_number_widths().
Victor Stinnerafbaa202011-09-28 21:50:16 +0200553 Return -1 on error, or 0 on success. */
554static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200555fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200556 PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end,
Victor Stinnerafbaa202011-09-28 21:50:16 +0200557 PyObject *prefix, Py_ssize_t p_start,
558 Py_UCS4 fill_char,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200559 LocaleInfo *locale, int toupper)
560{
561 /* Used to keep track of digits, decimal, and remainder. */
562 Py_ssize_t d_pos = d_start;
Victor Stinner22c103b2013-05-07 23:50:03 +0200563 const unsigned int kind = writer->kind;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200564 const void *data = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200565 Py_ssize_t r;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200566
567 if (spec->n_lpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200568 _PyUnicode_FastFill(writer->buffer,
569 writer->pos, spec->n_lpadding, fill_char);
570 writer->pos += spec->n_lpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200571 }
572 if (spec->n_sign == 1) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200573 PyUnicode_WRITE(kind, data, writer->pos, spec->sign);
574 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200575 }
576 if (spec->n_prefix) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200577 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
578 prefix, p_start,
579 spec->n_prefix);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200580 if (toupper) {
581 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500582 for (t = 0; t < spec->n_prefix; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200583 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100584 c = Py_TOUPPER(c);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100585 assert (c <= 127);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200586 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500587 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200588 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200589 writer->pos += spec->n_prefix;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200590 }
591 if (spec->n_spadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200592 _PyUnicode_FastFill(writer->buffer,
593 writer->pos, spec->n_spadding, fill_char);
594 writer->pos += spec->n_spadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200595 }
596
597 /* Only for type 'c' special case, it has no digits. */
598 if (spec->n_digits != 0) {
599 /* Fill the digits with InsertThousandsGrouping. */
Victor Stinnerdba2dee2011-09-28 21:50:42 +0200600 char *pdigits;
601 if (PyUnicode_READY(digits))
602 return -1;
603 pdigits = PyUnicode_DATA(digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604 if (PyUnicode_KIND(digits) < kind) {
605 pdigits = _PyUnicode_AsKind(digits, kind);
Victor Stinnerafbaa202011-09-28 21:50:16 +0200606 if (pdigits == NULL)
607 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200608 }
Victor Stinner90f50d42012-02-24 01:44:47 +0100609 r = _PyUnicode_InsertThousandsGrouping(
Victor Stinnerd3f08822012-05-29 12:57:52 +0200610 writer->buffer, writer->pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200611 spec->n_grouped_digits,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200612 pdigits + kind * d_pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200613 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100614 locale->grouping, locale->thousands_sep, NULL);
Victor Stinner90f50d42012-02-24 01:44:47 +0100615 if (r == -1)
616 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 assert(r == spec->n_grouped_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200618 if (PyUnicode_KIND(digits) < kind)
619 PyMem_Free(pdigits);
620 d_pos += spec->n_digits;
621 }
622 if (toupper) {
623 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500624 for (t = 0; t < spec->n_grouped_digits; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200625 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100626 c = Py_TOUPPER(c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500627 if (c > 127) {
628 PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
629 return -1;
630 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200631 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500632 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200634 writer->pos += spec->n_grouped_digits;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200635
636 if (spec->n_decimal) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200637 _PyUnicode_FastCopyCharacters(
638 writer->buffer, writer->pos,
639 locale->decimal_point, 0, spec->n_decimal);
640 writer->pos += spec->n_decimal;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200641 d_pos += 1;
642 }
643
644 if (spec->n_remainder) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200645 _PyUnicode_FastCopyCharacters(
646 writer->buffer, writer->pos,
647 digits, d_pos, spec->n_remainder);
648 writer->pos += spec->n_remainder;
Brett Cannon8a250fa2012-06-25 16:13:44 -0400649 /* d_pos += spec->n_remainder; */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200650 }
651
652 if (spec->n_rpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200653 _PyUnicode_FastFill(writer->buffer,
654 writer->pos, spec->n_rpadding,
655 fill_char);
656 writer->pos += spec->n_rpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657 }
Victor Stinnerafbaa202011-09-28 21:50:16 +0200658 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200659}
660
661static char no_grouping[1] = {CHAR_MAX};
662
663/* Find the decimal point character(s?), thousands_separator(s?), and
664 grouping description, either for the current locale if type is
665 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
666 none if LT_NO_LOCALE. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100667static int
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700668get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200669{
670 switch (type) {
671 case LT_CURRENT_LOCALE: {
672 struct lconv *locale_data = localeconv();
Victor Stinner41a863c2012-02-24 00:37:51 +0100673 locale_info->decimal_point = PyUnicode_DecodeLocale(
674 locale_data->decimal_point,
675 NULL);
676 if (locale_info->decimal_point == NULL)
677 return -1;
678 locale_info->thousands_sep = PyUnicode_DecodeLocale(
679 locale_data->thousands_sep,
680 NULL);
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700681 if (locale_info->thousands_sep == NULL)
Victor Stinner41a863c2012-02-24 00:37:51 +0100682 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200683 locale_info->grouping = locale_data->grouping;
684 break;
685 }
686 case LT_DEFAULT_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100687 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
688 locale_info->thousands_sep = PyUnicode_FromOrdinal(',');
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700689 if (!locale_info->decimal_point || !locale_info->thousands_sep)
Victor Stinner41a863c2012-02-24 00:37:51 +0100690 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200691 locale_info->grouping = "\3"; /* Group every 3 characters. The
692 (implicit) trailing 0 means repeat
693 infinitely. */
694 break;
695 case LT_NO_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100696 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
697 locale_info->thousands_sep = PyUnicode_New(0, 0);
Benjamin Peterson59e5e0d2016-09-13 22:43:45 -0700698 if (!locale_info->decimal_point || !locale_info->thousands_sep)
Victor Stinner41a863c2012-02-24 00:37:51 +0100699 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200700 locale_info->grouping = no_grouping;
701 break;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200702 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100703 return 0;
704}
705
706static void
707free_locale_info(LocaleInfo *locale_info)
708{
709 Py_XDECREF(locale_info->decimal_point);
710 Py_XDECREF(locale_info->thousands_sep);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200711}
712
713/************************************************************************/
714/*********** string formatting ******************************************/
715/************************************************************************/
716
Victor Stinnerd3f08822012-05-29 12:57:52 +0200717static int
718format_string_internal(PyObject *value, const InternalFormatSpec *format,
719 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200720{
721 Py_ssize_t lpad;
722 Py_ssize_t rpad;
723 Py_ssize_t total;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200724 Py_ssize_t len;
725 int result = -1;
Victor Stinnerece58de2012-04-23 23:36:38 +0200726 Py_UCS4 maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200727
Victor Stinnerd3f08822012-05-29 12:57:52 +0200728 assert(PyUnicode_IS_READY(value));
729 len = PyUnicode_GET_LENGTH(value);
730
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200731 /* sign is not allowed on strings */
732 if (format->sign != '\0') {
733 PyErr_SetString(PyExc_ValueError,
734 "Sign not allowed in string format specifier");
735 goto done;
736 }
737
738 /* alternate is not allowed on strings */
739 if (format->alternate) {
740 PyErr_SetString(PyExc_ValueError,
741 "Alternate form (#) not allowed in string format "
742 "specifier");
743 goto done;
744 }
745
746 /* '=' alignment not allowed on strings */
747 if (format->align == '=') {
748 PyErr_SetString(PyExc_ValueError,
749 "'=' alignment not allowed "
750 "in string format specifier");
751 goto done;
752 }
753
Victor Stinner621ef3d2012-10-02 00:33:47 +0200754 if ((format->width == -1 || format->width <= len)
755 && (format->precision == -1 || format->precision >= len)) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200756 /* Fast path */
757 return _PyUnicodeWriter_WriteStr(writer, value);
758 }
759
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200760 /* if precision is specified, output no more that format.precision
761 characters */
762 if (format->precision >= 0 && len >= format->precision) {
763 len = format->precision;
764 }
765
766 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
767
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200768 maxchar = writer->maxchar;
Victor Stinnera4ac6002012-01-21 15:50:49 +0100769 if (lpad != 0 || rpad != 0)
770 maxchar = Py_MAX(maxchar, format->fill_char);
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200771 if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
772 Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
773 maxchar = Py_MAX(maxchar, valmaxchar);
774 }
Victor Stinnera4ac6002012-01-21 15:50:49 +0100775
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200776 /* allocate the resulting string */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200777 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200778 goto done;
779
780 /* Write into that space. First the padding. */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400781 result = fill_padding(writer, len, format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200782 if (result == -1)
783 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200784
785 /* Then the source string. */
Victor Stinnerc9d369f2012-06-16 02:22:37 +0200786 if (len) {
787 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
788 value, 0, len);
789 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200790 writer->pos += (len + rpad);
791 result = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200792
793done:
794 return result;
795}
796
797
798/************************************************************************/
799/*********** long formatting ********************************************/
800/************************************************************************/
801
Victor Stinnerd3f08822012-05-29 12:57:52 +0200802static int
803format_long_internal(PyObject *value, const InternalFormatSpec *format,
804 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200805{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200806 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +0100807 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200808 PyObject *tmp = NULL;
809 Py_ssize_t inumeric_chars;
810 Py_UCS4 sign_char = '\0';
811 Py_ssize_t n_digits; /* count of digits need from the computed
812 string */
813 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
814 produces non-digits */
815 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
816 Py_ssize_t n_total;
Victor Stinnered277852012-02-01 00:22:23 +0100817 Py_ssize_t prefix = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200818 NumberFieldWidths spec;
819 long x;
820
821 /* Locale settings, either from the actual locale or
822 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +0100823 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200824
825 /* no precision allowed on integers */
826 if (format->precision != -1) {
827 PyErr_SetString(PyExc_ValueError,
828 "Precision not allowed in integer format specifier");
829 goto done;
830 }
831
832 /* special case for character formatting */
833 if (format->type == 'c') {
834 /* error to specify a sign */
835 if (format->sign != '\0') {
836 PyErr_SetString(PyExc_ValueError,
837 "Sign not allowed with integer"
838 " format specifier 'c'");
839 goto done;
840 }
Eric V. Smitha12572f2014-04-15 22:37:55 -0400841 /* error to request alternate format */
842 if (format->alternate) {
843 PyErr_SetString(PyExc_ValueError,
844 "Alternate form (#) not allowed with integer"
845 " format specifier 'c'");
846 goto done;
847 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200848
849 /* taken from unicodeobject.c formatchar() */
850 /* Integer input truncated to a character */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200851 x = PyLong_AsLong(value);
852 if (x == -1 && PyErr_Occurred())
853 goto done;
854 if (x < 0 || x > 0x10ffff) {
855 PyErr_SetString(PyExc_OverflowError,
Victor Stinnera4ac6002012-01-21 15:50:49 +0100856 "%c arg not in range(0x110000)");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200857 goto done;
858 }
859 tmp = PyUnicode_FromOrdinal(x);
860 inumeric_chars = 0;
861 n_digits = 1;
Amaury Forgeot d'Arc6d766fc2012-01-23 23:20:43 +0100862 maxchar = Py_MAX(maxchar, (Py_UCS4)x);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200863
864 /* As a sort-of hack, we tell calc_number_widths that we only
865 have "remainder" characters. calc_number_widths thinks
866 these are characters that don't get formatted, only copied
867 into the output string. We do this for 'c' formatting,
868 because the characters are likely to be non-digits. */
869 n_remainder = 1;
870 }
871 else {
872 int base;
873 int leading_chars_to_skip = 0; /* Number of characters added by
874 PyNumber_ToBase that we want to
875 skip over. */
876
877 /* Compute the base and how many characters will be added by
878 PyNumber_ToBase */
879 switch (format->type) {
880 case 'b':
881 base = 2;
882 leading_chars_to_skip = 2; /* 0b */
883 break;
884 case 'o':
885 base = 8;
886 leading_chars_to_skip = 2; /* 0o */
887 break;
888 case 'x':
889 case 'X':
890 base = 16;
891 leading_chars_to_skip = 2; /* 0x */
892 break;
893 default: /* shouldn't be needed, but stops a compiler warning */
894 case 'd':
895 case 'n':
896 base = 10;
897 break;
898 }
899
Victor Stinnerd3f08822012-05-29 12:57:52 +0200900 if (format->sign != '+' && format->sign != ' '
901 && format->width == -1
902 && format->type != 'X' && format->type != 'n'
903 && !format->thousands_separators
904 && PyLong_CheckExact(value))
905 {
906 /* Fast path */
907 return _PyLong_FormatWriter(writer, value, base, format->alternate);
908 }
909
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200910 /* The number of prefix chars is the same as the leading
911 chars to skip */
912 if (format->alternate)
913 n_prefix = leading_chars_to_skip;
914
915 /* Do the hard part, converting to a string in a given base */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200916 tmp = _PyLong_Format(value, base);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200917 if (tmp == NULL || PyUnicode_READY(tmp) == -1)
918 goto done;
919
920 inumeric_chars = 0;
921 n_digits = PyUnicode_GET_LENGTH(tmp);
922
923 prefix = inumeric_chars;
924
925 /* Is a sign character present in the output? If so, remember it
926 and skip it */
927 if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
928 sign_char = '-';
929 ++prefix;
930 ++leading_chars_to_skip;
931 }
932
933 /* Skip over the leading chars (0x, 0b, etc.) */
934 n_digits -= leading_chars_to_skip;
935 inumeric_chars += leading_chars_to_skip;
936 }
937
938 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100939 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
940 (format->thousands_separators ?
941 LT_DEFAULT_LOCALE :
942 LT_NO_LOCALE),
943 &locale) == -1)
944 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200945
946 /* Calculate how much memory we'll need. */
947 n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars,
Victor Stinner41a863c2012-02-24 00:37:51 +0100948 inumeric_chars + n_digits, n_remainder, 0,
949 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100950
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200951 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200952 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953 goto done;
954
955 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200956 result = fill_number(writer, &spec,
957 tmp, inumeric_chars, inumeric_chars + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -0400958 tmp, prefix, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +0200959 &locale, format->type == 'X');
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200960
961done:
962 Py_XDECREF(tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +0100963 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200964 return result;
965}
966
967/************************************************************************/
968/*********** float formatting *******************************************/
969/************************************************************************/
970
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200971/* much of this is taken from unicodeobject.c */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200972static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200973format_float_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +0200974 const InternalFormatSpec *format,
975 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200976{
977 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
978 Py_ssize_t n_digits;
979 Py_ssize_t n_remainder;
980 Py_ssize_t n_total;
981 int has_decimal;
982 double val;
Victor Stinner76d38502013-06-24 23:34:15 +0200983 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200984 Py_UCS4 type = format->type;
985 int add_pct = 0;
986 Py_ssize_t index;
987 NumberFieldWidths spec;
988 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200989 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +0100990 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200991 Py_UCS4 sign_char = '\0';
992 int float_type; /* Used to see if we have a nan, inf, or regular float. */
993 PyObject *unicode_tmp = NULL;
994
995 /* Locale settings, either from the actual locale or
996 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +0100997 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200998
Victor Stinner2f084ec2013-06-23 14:54:30 +0200999 if (format->precision > INT_MAX) {
1000 PyErr_SetString(PyExc_ValueError, "precision too big");
1001 goto done;
1002 }
1003 precision = (int)format->precision;
1004
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001005 if (format->alternate)
1006 flags |= Py_DTSF_ALT;
1007
1008 if (type == '\0') {
1009 /* Omitted type specifier. Behaves in the same way as repr(x)
1010 and str(x) if no precision is given, else like 'g', but with
1011 at least one digit after the decimal point. */
1012 flags |= Py_DTSF_ADD_DOT_0;
1013 type = 'r';
1014 default_precision = 0;
1015 }
1016
1017 if (type == 'n')
1018 /* 'n' is the same as 'g', except for the locale used to
1019 format the result. We take care of that later. */
1020 type = 'g';
1021
1022 val = PyFloat_AsDouble(value);
1023 if (val == -1.0 && PyErr_Occurred())
1024 goto done;
1025
1026 if (type == '%') {
1027 type = 'f';
1028 val *= 100;
1029 add_pct = 1;
1030 }
1031
1032 if (precision < 0)
1033 precision = default_precision;
1034 else if (type == 'r')
1035 type = 'g';
1036
Martin Panter4c359642016-05-08 13:53:41 +00001037 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001038 8-bit char. This is safe, because we've restricted what "type"
1039 can be. */
1040 buf = PyOS_double_to_string(val, (char)type, precision, flags,
1041 &float_type);
1042 if (buf == NULL)
1043 goto done;
1044 n_digits = strlen(buf);
1045
1046 if (add_pct) {
1047 /* We know that buf has a trailing zero (since we just called
1048 strlen() on it), and we don't use that fact any more. So we
1049 can just write over the trailing zero. */
1050 buf[n_digits] = '%';
1051 n_digits += 1;
1052 }
1053
Victor Stinnerd3f08822012-05-29 12:57:52 +02001054 if (format->sign != '+' && format->sign != ' '
1055 && format->width == -1
1056 && format->type != 'n'
1057 && !format->thousands_separators)
1058 {
1059 /* Fast path */
Victor Stinner4a587072013-11-19 12:54:53 +01001060 result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits);
1061 PyMem_Free(buf);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001062 return result;
1063 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001064
Victor Stinner4a587072013-11-19 12:54:53 +01001065 /* Since there is no unicode version of PyOS_double_to_string,
1066 just use the 8 bit version and then convert to unicode. */
1067 unicode_tmp = _PyUnicode_FromASCII(buf, n_digits);
1068 PyMem_Free(buf);
1069 if (unicode_tmp == NULL)
1070 goto done;
1071
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001072 /* Is a sign character present in the output? If so, remember it
1073 and skip it */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001074 index = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001075 if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') {
1076 sign_char = '-';
1077 ++index;
1078 --n_digits;
1079 }
1080
1081 /* Determine if we have any "remainder" (after the digits, might include
1082 decimal or exponent or both (or neither)) */
1083 parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal);
1084
1085 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001086 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1087 (format->thousands_separators ?
1088 LT_DEFAULT_LOCALE :
1089 LT_NO_LOCALE),
1090 &locale) == -1)
1091 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001092
1093 /* Calculate how much memory we'll need. */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001094 n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001095 index + n_digits, n_remainder, has_decimal,
Victor Stinner41a863c2012-02-24 00:37:51 +01001096 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +01001097
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001098 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001099 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001100 goto done;
1101
1102 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001103 result = fill_number(writer, &spec,
1104 unicode_tmp, index, index + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001105 NULL, 0, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001106 &locale, 0);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001107
1108done:
Stefan Krahd9c1bf72012-09-06 13:02:46 +02001109 Py_XDECREF(unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001110 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001111 return result;
1112}
1113
1114/************************************************************************/
1115/*********** complex formatting *****************************************/
1116/************************************************************************/
1117
Victor Stinnerd3f08822012-05-29 12:57:52 +02001118static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001119format_complex_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001120 const InternalFormatSpec *format,
1121 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001122{
1123 double re;
1124 double im;
1125 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1126 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1127
1128 InternalFormatSpec tmp_format = *format;
1129 Py_ssize_t n_re_digits;
1130 Py_ssize_t n_im_digits;
1131 Py_ssize_t n_re_remainder;
1132 Py_ssize_t n_im_remainder;
1133 Py_ssize_t n_re_total;
1134 Py_ssize_t n_im_total;
1135 int re_has_decimal;
1136 int im_has_decimal;
Victor Stinner76d38502013-06-24 23:34:15 +02001137 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001138 Py_UCS4 type = format->type;
1139 Py_ssize_t i_re;
1140 Py_ssize_t i_im;
1141 NumberFieldWidths re_spec;
1142 NumberFieldWidths im_spec;
1143 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001144 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001145 Py_UCS4 maxchar = 127;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001146 enum PyUnicode_Kind rkind;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001147 void *rdata;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001148 Py_UCS4 re_sign_char = '\0';
1149 Py_UCS4 im_sign_char = '\0';
1150 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1151 int im_float_type;
1152 int add_parens = 0;
1153 int skip_re = 0;
1154 Py_ssize_t lpad;
1155 Py_ssize_t rpad;
1156 Py_ssize_t total;
1157 PyObject *re_unicode_tmp = NULL;
1158 PyObject *im_unicode_tmp = NULL;
1159
1160 /* Locale settings, either from the actual locale or
1161 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001162 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001163
Victor Stinner2f084ec2013-06-23 14:54:30 +02001164 if (format->precision > INT_MAX) {
1165 PyErr_SetString(PyExc_ValueError, "precision too big");
1166 goto done;
1167 }
1168 precision = (int)format->precision;
1169
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001170 /* Zero padding is not allowed. */
1171 if (format->fill_char == '0') {
1172 PyErr_SetString(PyExc_ValueError,
1173 "Zero padding is not allowed in complex format "
1174 "specifier");
1175 goto done;
1176 }
1177
1178 /* Neither is '=' alignment . */
1179 if (format->align == '=') {
1180 PyErr_SetString(PyExc_ValueError,
1181 "'=' alignment flag is not allowed in complex format "
1182 "specifier");
1183 goto done;
1184 }
1185
1186 re = PyComplex_RealAsDouble(value);
1187 if (re == -1.0 && PyErr_Occurred())
1188 goto done;
1189 im = PyComplex_ImagAsDouble(value);
1190 if (im == -1.0 && PyErr_Occurred())
1191 goto done;
1192
1193 if (format->alternate)
1194 flags |= Py_DTSF_ALT;
1195
1196 if (type == '\0') {
1197 /* Omitted type specifier. Should be like str(self). */
1198 type = 'r';
1199 default_precision = 0;
1200 if (re == 0.0 && copysign(1.0, re) == 1.0)
1201 skip_re = 1;
1202 else
1203 add_parens = 1;
1204 }
1205
1206 if (type == 'n')
1207 /* 'n' is the same as 'g', except for the locale used to
1208 format the result. We take care of that later. */
1209 type = 'g';
1210
1211 if (precision < 0)
1212 precision = default_precision;
1213 else if (type == 'r')
1214 type = 'g';
1215
Martin Panter4c359642016-05-08 13:53:41 +00001216 /* Cast "type", because if we're in unicode we need to pass an
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001217 8-bit char. This is safe, because we've restricted what "type"
1218 can be. */
1219 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1220 &re_float_type);
1221 if (re_buf == NULL)
1222 goto done;
1223 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1224 &im_float_type);
1225 if (im_buf == NULL)
1226 goto done;
1227
1228 n_re_digits = strlen(re_buf);
1229 n_im_digits = strlen(im_buf);
1230
1231 /* Since there is no unicode version of PyOS_double_to_string,
1232 just use the 8 bit version and then convert to unicode. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001233 re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001234 if (re_unicode_tmp == NULL)
1235 goto done;
1236 i_re = 0;
1237
Victor Stinnerd3f08822012-05-29 12:57:52 +02001238 im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001239 if (im_unicode_tmp == NULL)
1240 goto done;
1241 i_im = 0;
1242
1243 /* Is a sign character present in the output? If so, remember it
1244 and skip it */
1245 if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') {
1246 re_sign_char = '-';
1247 ++i_re;
1248 --n_re_digits;
1249 }
1250 if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') {
1251 im_sign_char = '-';
1252 ++i_im;
1253 --n_im_digits;
1254 }
1255
1256 /* Determine if we have any "remainder" (after the digits, might include
1257 decimal or exponent or both (or neither)) */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001258 parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001259 &n_re_remainder, &re_has_decimal);
Victor Stinnerafbaa202011-09-28 21:50:16 +02001260 parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001261 &n_im_remainder, &im_has_decimal);
1262
1263 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001264 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1265 (format->thousands_separators ?
1266 LT_DEFAULT_LOCALE :
1267 LT_NO_LOCALE),
1268 &locale) == -1)
1269 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001270
1271 /* Turn off any padding. We'll do it later after we've composed
1272 the numbers without padding. */
1273 tmp_format.fill_char = '\0';
1274 tmp_format.align = '<';
1275 tmp_format.width = -1;
1276
1277 /* Calculate how much memory we'll need. */
1278 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp,
1279 i_re, i_re + n_re_digits, n_re_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001280 re_has_decimal, &locale, &tmp_format,
1281 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001282
1283 /* Same formatting, but always include a sign, unless the real part is
1284 * going to be omitted, in which case we use whatever sign convention was
1285 * requested by the original format. */
1286 if (!skip_re)
1287 tmp_format.sign = '+';
1288 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp,
1289 i_im, i_im + n_im_digits, n_im_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001290 im_has_decimal, &locale, &tmp_format,
1291 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001292
1293 if (skip_re)
1294 n_re_total = 0;
1295
1296 /* Add 1 for the 'j', and optionally 2 for parens. */
1297 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1298 format->width, format->align, &lpad, &rpad, &total);
1299
Victor Stinner41a863c2012-02-24 00:37:51 +01001300 if (lpad || rpad)
Victor Stinnera4ac6002012-01-21 15:50:49 +01001301 maxchar = Py_MAX(maxchar, format->fill_char);
1302
Victor Stinnerd3f08822012-05-29 12:57:52 +02001303 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001304 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001305 rkind = writer->kind;
1306 rdata = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001307
1308 /* Populate the memory. First, the padding. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001309 result = fill_padding(writer,
1310 n_re_total + n_im_total + 1 + add_parens * 2,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001311 format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001312 if (result == -1)
1313 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001314
Victor Stinnerd3f08822012-05-29 12:57:52 +02001315 if (add_parens) {
1316 PyUnicode_WRITE(rkind, rdata, writer->pos, '(');
1317 writer->pos++;
1318 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001319
1320 if (!skip_re) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001321 result = fill_number(writer, &re_spec,
1322 re_unicode_tmp, i_re, i_re + n_re_digits,
1323 NULL, 0,
1324 0,
1325 &locale, 0);
1326 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001327 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001328 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001329 result = fill_number(writer, &im_spec,
1330 im_unicode_tmp, i_im, i_im + n_im_digits,
1331 NULL, 0,
1332 0,
1333 &locale, 0);
1334 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001335 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001336 PyUnicode_WRITE(rkind, rdata, writer->pos, 'j');
1337 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001338
Victor Stinnerd3f08822012-05-29 12:57:52 +02001339 if (add_parens) {
1340 PyUnicode_WRITE(rkind, rdata, writer->pos, ')');
1341 writer->pos++;
1342 }
1343
1344 writer->pos += rpad;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001345
1346done:
1347 PyMem_Free(re_buf);
1348 PyMem_Free(im_buf);
1349 Py_XDECREF(re_unicode_tmp);
1350 Py_XDECREF(im_unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001351 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001352 return result;
1353}
1354
1355/************************************************************************/
1356/*********** built in formatters ****************************************/
1357/************************************************************************/
doko@ubuntu.com39378f72012-06-21 12:12:20 +02001358static int
Victor Stinnerd3f08822012-05-29 12:57:52 +02001359format_obj(PyObject *obj, _PyUnicodeWriter *writer)
1360{
1361 PyObject *str;
1362 int err;
1363
1364 str = PyObject_Str(obj);
1365 if (str == NULL)
1366 return -1;
1367 err = _PyUnicodeWriter_WriteStr(writer, str);
1368 Py_DECREF(str);
1369 return err;
1370}
1371
1372int
1373_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1374 PyObject *obj,
1375 PyObject *format_spec,
1376 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001377{
1378 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001379
1380 assert(PyUnicode_Check(obj));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001381
1382 /* check for the special case of zero length format spec, make
1383 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001384 if (start == end) {
1385 if (PyUnicode_CheckExact(obj))
1386 return _PyUnicodeWriter_WriteStr(writer, obj);
1387 else
1388 return format_obj(obj, writer);
1389 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001390
1391 /* parse the format_spec */
1392 if (!parse_internal_render_format_spec(format_spec, start, end,
1393 &format, 's', '<'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001394 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001395
1396 /* type conversion? */
1397 switch (format.type) {
1398 case 's':
1399 /* no type conversion needed, already a string. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001400 return format_string_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001401 default:
1402 /* unknown */
1403 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001404 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001405 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001406}
1407
Victor Stinnerd3f08822012-05-29 12:57:52 +02001408int
1409_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1410 PyObject *obj,
1411 PyObject *format_spec,
1412 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001413{
Victor Stinnerd3f08822012-05-29 12:57:52 +02001414 PyObject *tmp = NULL, *str = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001415 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001416 int result = -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001417
1418 /* check for the special case of zero length format spec, make
1419 it equivalent to str(obj) */
1420 if (start == end) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001421 if (PyLong_CheckExact(obj))
1422 return _PyLong_FormatWriter(writer, obj, 10, 0);
1423 else
1424 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001425 }
1426
1427 /* parse the format_spec */
1428 if (!parse_internal_render_format_spec(format_spec, start, end,
1429 &format, 'd', '>'))
1430 goto done;
1431
1432 /* type conversion? */
1433 switch (format.type) {
1434 case 'b':
1435 case 'c':
1436 case 'd':
1437 case 'o':
1438 case 'x':
1439 case 'X':
1440 case 'n':
Serhiy Storchaka95949422013-08-27 19:40:23 +03001441 /* no type conversion needed, already an int. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001442 result = format_long_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001443 break;
1444
1445 case 'e':
1446 case 'E':
1447 case 'f':
1448 case 'F':
1449 case 'g':
1450 case 'G':
1451 case '%':
1452 /* convert to float */
1453 tmp = PyNumber_Float(obj);
1454 if (tmp == NULL)
1455 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001456 result = format_float_internal(tmp, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001457 break;
1458
1459 default:
1460 /* unknown */
1461 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1462 goto done;
1463 }
1464
1465done:
1466 Py_XDECREF(tmp);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001467 Py_XDECREF(str);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001468 return result;
1469}
1470
Victor Stinnerd3f08822012-05-29 12:57:52 +02001471int
1472_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1473 PyObject *obj,
1474 PyObject *format_spec,
1475 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001476{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001477 InternalFormatSpec format;
1478
1479 /* check for the special case of zero length format spec, make
1480 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001481 if (start == end)
1482 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001483
1484 /* parse the format_spec */
1485 if (!parse_internal_render_format_spec(format_spec, start, end,
1486 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001487 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001488
1489 /* type conversion? */
1490 switch (format.type) {
1491 case '\0': /* No format code: like 'g', but with at least one decimal. */
1492 case 'e':
1493 case 'E':
1494 case 'f':
1495 case 'F':
1496 case 'g':
1497 case 'G':
1498 case 'n':
1499 case '%':
1500 /* no conversion, already a float. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001501 return format_float_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001502
1503 default:
1504 /* unknown */
1505 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001506 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001507 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001508}
1509
Victor Stinnerd3f08822012-05-29 12:57:52 +02001510int
1511_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1512 PyObject *obj,
1513 PyObject *format_spec,
1514 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001515{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001516 InternalFormatSpec format;
1517
1518 /* check for the special case of zero length format spec, make
1519 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001520 if (start == end)
1521 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001522
1523 /* parse the format_spec */
1524 if (!parse_internal_render_format_spec(format_spec, start, end,
1525 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001526 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001527
1528 /* type conversion? */
1529 switch (format.type) {
1530 case '\0': /* No format code: like 'g', but with at least one decimal. */
1531 case 'e':
1532 case 'E':
1533 case 'f':
1534 case 'F':
1535 case 'g':
1536 case 'G':
1537 case 'n':
1538 /* no conversion, already a complex. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001539 return format_complex_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001540
1541 default:
1542 /* unknown */
1543 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001544 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001545 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001546}