blob: a428fbec809cbec898e4b4d5b8e10f031a399f7b [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
121/* Occassionally useful for debugging. Should normally be commented out. */
122static 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. */
350#define LT_CURRENT_LOCALE 0
351#define LT_DEFAULT_LOCALE 1
352#define LT_NO_LOCALE 2
353
354/* Locale info needed for formatting integers and the part of floats
355 before and including the decimal. Note that locales only support
356 8-bit chars, not unicode. */
357typedef struct {
Victor Stinner41a863c2012-02-24 00:37:51 +0100358 PyObject *decimal_point;
359 PyObject *thousands_sep;
360 const char *grouping;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200361} LocaleInfo;
362
Victor Stinner41a863c2012-02-24 00:37:51 +0100363#define STATIC_LOCALE_INFO_INIT {0, 0, 0}
364
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200365/* describes the layout for an integer, see the comment in
366 calc_number_widths() for details */
367typedef struct {
368 Py_ssize_t n_lpadding;
369 Py_ssize_t n_prefix;
370 Py_ssize_t n_spadding;
371 Py_ssize_t n_rpadding;
372 char sign;
373 Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */
374 Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including
375 any grouping chars. */
376 Py_ssize_t n_decimal; /* 0 if only an integer */
377 Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part,
378 excluding the decimal itself, if
379 present. */
380
381 /* These 2 are not the widths of fields, but are needed by
382 STRINGLIB_GROUPING. */
383 Py_ssize_t n_digits; /* The number of digits before a decimal
384 or exponent. */
385 Py_ssize_t n_min_width; /* The min_width we used when we computed
386 the n_grouped_digits width. */
387} NumberFieldWidths;
388
389
390/* Given a number of the form:
391 digits[remainder]
392 where ptr points to the start and end points to the end, find where
393 the integer part ends. This could be a decimal, an exponent, both,
394 or neither.
395 If a decimal point is present, set *has_decimal and increment
396 remainder beyond it.
397 Results are undefined (but shouldn't crash) for improperly
398 formatted strings.
399*/
400static void
401parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end,
402 Py_ssize_t *n_remainder, int *has_decimal)
403{
404 Py_ssize_t remainder;
405
Antoine Pitrouc73c5612013-02-09 23:14:42 +0100406 while (pos<end && Py_ISDIGIT(PyUnicode_READ_CHAR(s, pos)))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200407 ++pos;
408 remainder = pos;
409
410 /* Does remainder start with a decimal point? */
411 *has_decimal = pos<end && PyUnicode_READ_CHAR(s, remainder) == '.';
412
413 /* Skip the decimal point. */
414 if (*has_decimal)
415 remainder++;
416
417 *n_remainder = end - remainder;
418}
419
420/* not all fields of format are used. for example, precision is
421 unused. should this take discrete params in order to be more clear
422 about what it does? or is passing a single format parameter easier
423 and more efficient enough to justify a little obfuscation? */
424static Py_ssize_t
425calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
426 Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start,
427 Py_ssize_t n_end, Py_ssize_t n_remainder,
428 int has_decimal, const LocaleInfo *locale,
Victor Stinner41a863c2012-02-24 00:37:51 +0100429 const InternalFormatSpec *format, Py_UCS4 *maxchar)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200430{
431 Py_ssize_t n_non_digit_non_padding;
432 Py_ssize_t n_padding;
433
434 spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0);
435 spec->n_lpadding = 0;
436 spec->n_prefix = n_prefix;
Victor Stinner41a863c2012-02-24 00:37:51 +0100437 spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200438 spec->n_remainder = n_remainder;
439 spec->n_spadding = 0;
440 spec->n_rpadding = 0;
441 spec->sign = '\0';
442 spec->n_sign = 0;
443
444 /* the output will look like:
445 | |
446 | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> |
447 | |
448
449 sign is computed from format->sign and the actual
450 sign of the number
451
452 prefix is given (it's for the '0x' prefix)
453
454 digits is already known
455
456 the total width is either given, or computed from the
457 actual digits
458
459 only one of lpadding, spadding, and rpadding can be non-zero,
460 and it's calculated from the width and other fields
461 */
462
463 /* compute the various parts we're going to write */
464 switch (format->sign) {
465 case '+':
466 /* always put a + or - */
467 spec->n_sign = 1;
468 spec->sign = (sign_char == '-' ? '-' : '+');
469 break;
470 case ' ':
471 spec->n_sign = 1;
472 spec->sign = (sign_char == '-' ? '-' : ' ');
473 break;
474 default:
475 /* Not specified, or the default (-) */
476 if (sign_char == '-') {
477 spec->n_sign = 1;
478 spec->sign = '-';
479 }
480 }
481
482 /* The number of chars used for non-digits and non-padding. */
483 n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
484 spec->n_remainder;
485
486 /* min_width can go negative, that's okay. format->width == -1 means
487 we don't care. */
488 if (format->fill_char == '0' && format->align == '=')
489 spec->n_min_width = format->width - n_non_digit_non_padding;
490 else
491 spec->n_min_width = 0;
492
493 if (spec->n_digits == 0)
494 /* This case only occurs when using 'c' formatting, we need
495 to special case it because the grouping code always wants
496 to have at least one character. */
497 spec->n_grouped_digits = 0;
Victor Stinner41a863c2012-02-24 00:37:51 +0100498 else {
499 Py_UCS4 grouping_maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200500 spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
Victor Stinner41a863c2012-02-24 00:37:51 +0100501 NULL, 0,
502 0, NULL,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200503 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100504 locale->grouping, locale->thousands_sep, &grouping_maxchar);
505 *maxchar = Py_MAX(*maxchar, grouping_maxchar);
506 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200507
508 /* Given the desired width and the total of digit and non-digit
509 space we consume, see if we need any padding. format->width can
510 be negative (meaning no padding), but this code still works in
511 that case. */
512 n_padding = format->width -
513 (n_non_digit_non_padding + spec->n_grouped_digits);
514 if (n_padding > 0) {
515 /* Some padding is needed. Determine if it's left, space, or right. */
516 switch (format->align) {
517 case '<':
518 spec->n_rpadding = n_padding;
519 break;
520 case '^':
521 spec->n_lpadding = n_padding / 2;
522 spec->n_rpadding = n_padding - spec->n_lpadding;
523 break;
524 case '=':
525 spec->n_spadding = n_padding;
526 break;
527 case '>':
528 spec->n_lpadding = n_padding;
529 break;
530 default:
531 /* Shouldn't get here, but treat it as '>' */
532 spec->n_lpadding = n_padding;
533 assert(0);
534 break;
535 }
536 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100537
538 if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding)
539 *maxchar = Py_MAX(*maxchar, format->fill_char);
540
Victor Stinner90f50d42012-02-24 01:44:47 +0100541 if (spec->n_decimal)
542 *maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point));
543
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200544 return spec->n_lpadding + spec->n_sign + spec->n_prefix +
545 spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
546 spec->n_remainder + spec->n_rpadding;
547}
548
549/* Fill in the digit parts of a numbers's string representation,
550 as determined in calc_number_widths().
Victor Stinnerafbaa202011-09-28 21:50:16 +0200551 Return -1 on error, or 0 on success. */
552static int
Victor Stinnerd3f08822012-05-29 12:57:52 +0200553fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200554 PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end,
Victor Stinnerafbaa202011-09-28 21:50:16 +0200555 PyObject *prefix, Py_ssize_t p_start,
556 Py_UCS4 fill_char,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200557 LocaleInfo *locale, int toupper)
558{
559 /* Used to keep track of digits, decimal, and remainder. */
560 Py_ssize_t d_pos = d_start;
Victor Stinner22c103b2013-05-07 23:50:03 +0200561 const unsigned int kind = writer->kind;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200562 const void *data = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200563 Py_ssize_t r;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200564
565 if (spec->n_lpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200566 _PyUnicode_FastFill(writer->buffer,
567 writer->pos, spec->n_lpadding, fill_char);
568 writer->pos += spec->n_lpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200569 }
570 if (spec->n_sign == 1) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200571 PyUnicode_WRITE(kind, data, writer->pos, spec->sign);
572 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200573 }
574 if (spec->n_prefix) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200575 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
576 prefix, p_start,
577 spec->n_prefix);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200578 if (toupper) {
579 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500580 for (t = 0; t < spec->n_prefix; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200581 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100582 c = Py_TOUPPER(c);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100583 assert (c <= 127);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200584 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500585 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200586 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200587 writer->pos += spec->n_prefix;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200588 }
589 if (spec->n_spadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200590 _PyUnicode_FastFill(writer->buffer,
591 writer->pos, spec->n_spadding, fill_char);
592 writer->pos += spec->n_spadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200593 }
594
595 /* Only for type 'c' special case, it has no digits. */
596 if (spec->n_digits != 0) {
597 /* Fill the digits with InsertThousandsGrouping. */
Victor Stinnerdba2dee2011-09-28 21:50:42 +0200598 char *pdigits;
599 if (PyUnicode_READY(digits))
600 return -1;
601 pdigits = PyUnicode_DATA(digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200602 if (PyUnicode_KIND(digits) < kind) {
603 pdigits = _PyUnicode_AsKind(digits, kind);
Victor Stinnerafbaa202011-09-28 21:50:16 +0200604 if (pdigits == NULL)
605 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200606 }
Victor Stinner90f50d42012-02-24 01:44:47 +0100607 r = _PyUnicode_InsertThousandsGrouping(
Victor Stinnerd3f08822012-05-29 12:57:52 +0200608 writer->buffer, writer->pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200609 spec->n_grouped_digits,
Martin v. Löwisc47adb02011-10-07 20:55:35 +0200610 pdigits + kind * d_pos,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200611 spec->n_digits, spec->n_min_width,
Victor Stinner41a863c2012-02-24 00:37:51 +0100612 locale->grouping, locale->thousands_sep, NULL);
Victor Stinner90f50d42012-02-24 01:44:47 +0100613 if (r == -1)
614 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200615 assert(r == spec->n_grouped_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200616 if (PyUnicode_KIND(digits) < kind)
617 PyMem_Free(pdigits);
618 d_pos += spec->n_digits;
619 }
620 if (toupper) {
621 Py_ssize_t t;
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500622 for (t = 0; t < spec->n_grouped_digits; t++) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200623 Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
Victor Stinnered277852012-02-01 00:22:23 +0100624 c = Py_TOUPPER(c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500625 if (c > 127) {
626 PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
627 return -1;
628 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200629 PyUnicode_WRITE(kind, data, writer->pos + t, c);
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500630 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200631 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200632 writer->pos += spec->n_grouped_digits;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633
634 if (spec->n_decimal) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200635 _PyUnicode_FastCopyCharacters(
636 writer->buffer, writer->pos,
637 locale->decimal_point, 0, spec->n_decimal);
638 writer->pos += spec->n_decimal;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200639 d_pos += 1;
640 }
641
642 if (spec->n_remainder) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200643 _PyUnicode_FastCopyCharacters(
644 writer->buffer, writer->pos,
645 digits, d_pos, spec->n_remainder);
646 writer->pos += spec->n_remainder;
Brett Cannon8a250fa2012-06-25 16:13:44 -0400647 /* d_pos += spec->n_remainder; */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200648 }
649
650 if (spec->n_rpadding) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200651 _PyUnicode_FastFill(writer->buffer,
652 writer->pos, spec->n_rpadding,
653 fill_char);
654 writer->pos += spec->n_rpadding;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200655 }
Victor Stinnerafbaa202011-09-28 21:50:16 +0200656 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657}
658
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200659static const char no_grouping[1] = {CHAR_MAX};
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200660
661/* Find the decimal point character(s?), thousands_separator(s?), and
662 grouping description, either for the current locale if type is
663 LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or
664 none if LT_NO_LOCALE. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100665static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200666get_locale_info(int type, LocaleInfo *locale_info)
667{
668 switch (type) {
669 case LT_CURRENT_LOCALE: {
670 struct lconv *locale_data = localeconv();
Victor Stinner41a863c2012-02-24 00:37:51 +0100671 locale_info->decimal_point = PyUnicode_DecodeLocale(
672 locale_data->decimal_point,
673 NULL);
674 if (locale_info->decimal_point == NULL)
675 return -1;
676 locale_info->thousands_sep = PyUnicode_DecodeLocale(
677 locale_data->thousands_sep,
678 NULL);
679 if (locale_info->thousands_sep == NULL) {
680 Py_DECREF(locale_info->decimal_point);
681 return -1;
682 }
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(',');
689 if (!locale_info->decimal_point || !locale_info->thousands_sep) {
690 Py_XDECREF(locale_info->decimal_point);
691 Py_XDECREF(locale_info->thousands_sep);
692 return -1;
693 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200694 locale_info->grouping = "\3"; /* Group every 3 characters. The
695 (implicit) trailing 0 means repeat
696 infinitely. */
697 break;
698 case LT_NO_LOCALE:
Victor Stinner41a863c2012-02-24 00:37:51 +0100699 locale_info->decimal_point = PyUnicode_FromOrdinal('.');
700 locale_info->thousands_sep = PyUnicode_New(0, 0);
701 if (!locale_info->decimal_point || !locale_info->thousands_sep) {
702 Py_XDECREF(locale_info->decimal_point);
703 Py_XDECREF(locale_info->thousands_sep);
704 return -1;
705 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200706 locale_info->grouping = no_grouping;
707 break;
708 default:
709 assert(0);
710 }
Victor Stinner41a863c2012-02-24 00:37:51 +0100711 return 0;
712}
713
714static void
715free_locale_info(LocaleInfo *locale_info)
716{
717 Py_XDECREF(locale_info->decimal_point);
718 Py_XDECREF(locale_info->thousands_sep);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200719}
720
721/************************************************************************/
722/*********** string formatting ******************************************/
723/************************************************************************/
724
Victor Stinnerd3f08822012-05-29 12:57:52 +0200725static int
726format_string_internal(PyObject *value, const InternalFormatSpec *format,
727 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200728{
729 Py_ssize_t lpad;
730 Py_ssize_t rpad;
731 Py_ssize_t total;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200732 Py_ssize_t len;
733 int result = -1;
Victor Stinnerece58de2012-04-23 23:36:38 +0200734 Py_UCS4 maxchar;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200735
Victor Stinnerd3f08822012-05-29 12:57:52 +0200736 assert(PyUnicode_IS_READY(value));
737 len = PyUnicode_GET_LENGTH(value);
738
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200739 /* sign is not allowed on strings */
740 if (format->sign != '\0') {
741 PyErr_SetString(PyExc_ValueError,
742 "Sign not allowed in string format specifier");
743 goto done;
744 }
745
746 /* alternate is not allowed on strings */
747 if (format->alternate) {
748 PyErr_SetString(PyExc_ValueError,
749 "Alternate form (#) not allowed in string format "
750 "specifier");
751 goto done;
752 }
753
754 /* '=' alignment not allowed on strings */
755 if (format->align == '=') {
756 PyErr_SetString(PyExc_ValueError,
757 "'=' alignment not allowed "
758 "in string format specifier");
759 goto done;
760 }
761
Victor Stinner621ef3d2012-10-02 00:33:47 +0200762 if ((format->width == -1 || format->width <= len)
763 && (format->precision == -1 || format->precision >= len)) {
Victor Stinnerd3f08822012-05-29 12:57:52 +0200764 /* Fast path */
765 return _PyUnicodeWriter_WriteStr(writer, value);
766 }
767
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200768 /* if precision is specified, output no more that format.precision
769 characters */
770 if (format->precision >= 0 && len >= format->precision) {
771 len = format->precision;
772 }
773
774 calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
775
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200776 maxchar = writer->maxchar;
Victor Stinnera4ac6002012-01-21 15:50:49 +0100777 if (lpad != 0 || rpad != 0)
778 maxchar = Py_MAX(maxchar, format->fill_char);
Victor Stinnereb4b5ac2013-04-03 02:02:33 +0200779 if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
780 Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
781 maxchar = Py_MAX(maxchar, valmaxchar);
782 }
Victor Stinnera4ac6002012-01-21 15:50:49 +0100783
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200784 /* allocate the resulting string */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200785 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200786 goto done;
787
788 /* Write into that space. First the padding. */
Eric V. Smith2ea97122014-04-14 11:55:10 -0400789 result = fill_padding(writer, len, format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +0200790 if (result == -1)
791 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200792
793 /* Then the source string. */
Victor Stinnerc9d369f2012-06-16 02:22:37 +0200794 if (len) {
795 _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
796 value, 0, len);
797 }
Victor Stinnerd3f08822012-05-29 12:57:52 +0200798 writer->pos += (len + rpad);
799 result = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200800
801done:
802 return result;
803}
804
805
806/************************************************************************/
807/*********** long formatting ********************************************/
808/************************************************************************/
809
Victor Stinnerd3f08822012-05-29 12:57:52 +0200810static int
811format_long_internal(PyObject *value, const InternalFormatSpec *format,
812 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200813{
Victor Stinnerd3f08822012-05-29 12:57:52 +0200814 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +0100815 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200816 PyObject *tmp = NULL;
817 Py_ssize_t inumeric_chars;
818 Py_UCS4 sign_char = '\0';
819 Py_ssize_t n_digits; /* count of digits need from the computed
820 string */
821 Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which
822 produces non-digits */
823 Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
824 Py_ssize_t n_total;
Victor Stinnered277852012-02-01 00:22:23 +0100825 Py_ssize_t prefix = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200826 NumberFieldWidths spec;
827 long x;
828
829 /* Locale settings, either from the actual locale or
830 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +0100831 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200832
833 /* no precision allowed on integers */
834 if (format->precision != -1) {
835 PyErr_SetString(PyExc_ValueError,
836 "Precision not allowed in integer format specifier");
837 goto done;
838 }
839
840 /* special case for character formatting */
841 if (format->type == 'c') {
842 /* error to specify a sign */
843 if (format->sign != '\0') {
844 PyErr_SetString(PyExc_ValueError,
845 "Sign not allowed with integer"
846 " format specifier 'c'");
847 goto done;
848 }
Eric V. Smitha12572f2014-04-15 22:37:55 -0400849 /* error to request alternate format */
850 if (format->alternate) {
851 PyErr_SetString(PyExc_ValueError,
852 "Alternate form (#) not allowed with integer"
853 " format specifier 'c'");
854 goto done;
855 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200856
857 /* taken from unicodeobject.c formatchar() */
858 /* Integer input truncated to a character */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200859 x = PyLong_AsLong(value);
860 if (x == -1 && PyErr_Occurred())
861 goto done;
862 if (x < 0 || x > 0x10ffff) {
863 PyErr_SetString(PyExc_OverflowError,
Victor Stinnera4ac6002012-01-21 15:50:49 +0100864 "%c arg not in range(0x110000)");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200865 goto done;
866 }
867 tmp = PyUnicode_FromOrdinal(x);
868 inumeric_chars = 0;
869 n_digits = 1;
Amaury Forgeot d'Arc6d766fc2012-01-23 23:20:43 +0100870 maxchar = Py_MAX(maxchar, (Py_UCS4)x);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200871
872 /* As a sort-of hack, we tell calc_number_widths that we only
873 have "remainder" characters. calc_number_widths thinks
874 these are characters that don't get formatted, only copied
875 into the output string. We do this for 'c' formatting,
876 because the characters are likely to be non-digits. */
877 n_remainder = 1;
878 }
879 else {
880 int base;
881 int leading_chars_to_skip = 0; /* Number of characters added by
882 PyNumber_ToBase that we want to
883 skip over. */
884
885 /* Compute the base and how many characters will be added by
886 PyNumber_ToBase */
887 switch (format->type) {
888 case 'b':
889 base = 2;
890 leading_chars_to_skip = 2; /* 0b */
891 break;
892 case 'o':
893 base = 8;
894 leading_chars_to_skip = 2; /* 0o */
895 break;
896 case 'x':
897 case 'X':
898 base = 16;
899 leading_chars_to_skip = 2; /* 0x */
900 break;
901 default: /* shouldn't be needed, but stops a compiler warning */
902 case 'd':
903 case 'n':
904 base = 10;
905 break;
906 }
907
Victor Stinnerd3f08822012-05-29 12:57:52 +0200908 if (format->sign != '+' && format->sign != ' '
909 && format->width == -1
910 && format->type != 'X' && format->type != 'n'
911 && !format->thousands_separators
912 && PyLong_CheckExact(value))
913 {
914 /* Fast path */
915 return _PyLong_FormatWriter(writer, value, base, format->alternate);
916 }
917
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200918 /* The number of prefix chars is the same as the leading
919 chars to skip */
920 if (format->alternate)
921 n_prefix = leading_chars_to_skip;
922
923 /* Do the hard part, converting to a string in a given base */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200924 tmp = _PyLong_Format(value, base);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200925 if (tmp == NULL || PyUnicode_READY(tmp) == -1)
926 goto done;
927
928 inumeric_chars = 0;
929 n_digits = PyUnicode_GET_LENGTH(tmp);
930
931 prefix = inumeric_chars;
932
933 /* Is a sign character present in the output? If so, remember it
934 and skip it */
935 if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
936 sign_char = '-';
937 ++prefix;
938 ++leading_chars_to_skip;
939 }
940
941 /* Skip over the leading chars (0x, 0b, etc.) */
942 n_digits -= leading_chars_to_skip;
943 inumeric_chars += leading_chars_to_skip;
944 }
945
946 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +0100947 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
948 (format->thousands_separators ?
949 LT_DEFAULT_LOCALE :
950 LT_NO_LOCALE),
951 &locale) == -1)
952 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953
954 /* Calculate how much memory we'll need. */
955 n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars,
Victor Stinner41a863c2012-02-24 00:37:51 +0100956 inumeric_chars + n_digits, n_remainder, 0,
957 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +0100958
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200959 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200960 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200961 goto done;
962
963 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200964 result = fill_number(writer, &spec,
965 tmp, inumeric_chars, inumeric_chars + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -0400966 tmp, prefix, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +0200967 &locale, format->type == 'X');
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968
969done:
970 Py_XDECREF(tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +0100971 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200972 return result;
973}
974
975/************************************************************************/
976/*********** float formatting *******************************************/
977/************************************************************************/
978
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200979/* much of this is taken from unicodeobject.c */
Victor Stinnerd3f08822012-05-29 12:57:52 +0200980static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200981format_float_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +0200982 const InternalFormatSpec *format,
983 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200984{
985 char *buf = NULL; /* buffer returned from PyOS_double_to_string */
986 Py_ssize_t n_digits;
987 Py_ssize_t n_remainder;
988 Py_ssize_t n_total;
989 int has_decimal;
990 double val;
Victor Stinner76d38502013-06-24 23:34:15 +0200991 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200992 Py_UCS4 type = format->type;
993 int add_pct = 0;
994 Py_ssize_t index;
995 NumberFieldWidths spec;
996 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +0200997 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +0100998 Py_UCS4 maxchar = 127;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200999 Py_UCS4 sign_char = '\0';
1000 int float_type; /* Used to see if we have a nan, inf, or regular float. */
1001 PyObject *unicode_tmp = NULL;
1002
1003 /* Locale settings, either from the actual locale or
1004 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001005 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001006
Victor Stinner2f084ec2013-06-23 14:54:30 +02001007 if (format->precision > INT_MAX) {
1008 PyErr_SetString(PyExc_ValueError, "precision too big");
1009 goto done;
1010 }
1011 precision = (int)format->precision;
1012
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001013 if (format->alternate)
1014 flags |= Py_DTSF_ALT;
1015
1016 if (type == '\0') {
1017 /* Omitted type specifier. Behaves in the same way as repr(x)
1018 and str(x) if no precision is given, else like 'g', but with
1019 at least one digit after the decimal point. */
1020 flags |= Py_DTSF_ADD_DOT_0;
1021 type = 'r';
1022 default_precision = 0;
1023 }
1024
1025 if (type == 'n')
1026 /* 'n' is the same as 'g', except for the locale used to
1027 format the result. We take care of that later. */
1028 type = 'g';
1029
1030 val = PyFloat_AsDouble(value);
1031 if (val == -1.0 && PyErr_Occurred())
1032 goto done;
1033
1034 if (type == '%') {
1035 type = 'f';
1036 val *= 100;
1037 add_pct = 1;
1038 }
1039
1040 if (precision < 0)
1041 precision = default_precision;
1042 else if (type == 'r')
1043 type = 'g';
1044
1045 /* Cast "type", because if we're in unicode we need to pass a
1046 8-bit char. This is safe, because we've restricted what "type"
1047 can be. */
1048 buf = PyOS_double_to_string(val, (char)type, precision, flags,
1049 &float_type);
1050 if (buf == NULL)
1051 goto done;
1052 n_digits = strlen(buf);
1053
1054 if (add_pct) {
1055 /* We know that buf has a trailing zero (since we just called
1056 strlen() on it), and we don't use that fact any more. So we
1057 can just write over the trailing zero. */
1058 buf[n_digits] = '%';
1059 n_digits += 1;
1060 }
1061
Victor Stinnerd3f08822012-05-29 12:57:52 +02001062 if (format->sign != '+' && format->sign != ' '
1063 && format->width == -1
1064 && format->type != 'n'
1065 && !format->thousands_separators)
1066 {
1067 /* Fast path */
Victor Stinner4a587072013-11-19 12:54:53 +01001068 result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits);
1069 PyMem_Free(buf);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001070 return result;
1071 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001072
Victor Stinner4a587072013-11-19 12:54:53 +01001073 /* Since there is no unicode version of PyOS_double_to_string,
1074 just use the 8 bit version and then convert to unicode. */
1075 unicode_tmp = _PyUnicode_FromASCII(buf, n_digits);
1076 PyMem_Free(buf);
1077 if (unicode_tmp == NULL)
1078 goto done;
1079
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001080 /* Is a sign character present in the output? If so, remember it
1081 and skip it */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001082 index = 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001083 if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') {
1084 sign_char = '-';
1085 ++index;
1086 --n_digits;
1087 }
1088
1089 /* Determine if we have any "remainder" (after the digits, might include
1090 decimal or exponent or both (or neither)) */
1091 parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal);
1092
1093 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001094 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1095 (format->thousands_separators ?
1096 LT_DEFAULT_LOCALE :
1097 LT_NO_LOCALE),
1098 &locale) == -1)
1099 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001100
1101 /* Calculate how much memory we'll need. */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001102 n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001103 index + n_digits, n_remainder, has_decimal,
Victor Stinner41a863c2012-02-24 00:37:51 +01001104 &locale, format, &maxchar);
Victor Stinnera4ac6002012-01-21 15:50:49 +01001105
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001106 /* Allocate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001107 if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001108 goto done;
1109
1110 /* Populate the memory. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001111 result = fill_number(writer, &spec,
1112 unicode_tmp, index, index + n_digits,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001113 NULL, 0, format->fill_char,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001114 &locale, 0);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001115
1116done:
Stefan Krahd9c1bf72012-09-06 13:02:46 +02001117 Py_XDECREF(unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001118 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001119 return result;
1120}
1121
1122/************************************************************************/
1123/*********** complex formatting *****************************************/
1124/************************************************************************/
1125
Victor Stinnerd3f08822012-05-29 12:57:52 +02001126static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001127format_complex_internal(PyObject *value,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001128 const InternalFormatSpec *format,
1129 _PyUnicodeWriter *writer)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001130{
1131 double re;
1132 double im;
1133 char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */
1134 char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */
1135
1136 InternalFormatSpec tmp_format = *format;
1137 Py_ssize_t n_re_digits;
1138 Py_ssize_t n_im_digits;
1139 Py_ssize_t n_re_remainder;
1140 Py_ssize_t n_im_remainder;
1141 Py_ssize_t n_re_total;
1142 Py_ssize_t n_im_total;
1143 int re_has_decimal;
1144 int im_has_decimal;
Victor Stinner76d38502013-06-24 23:34:15 +02001145 int precision, default_precision = 6;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001146 Py_UCS4 type = format->type;
1147 Py_ssize_t i_re;
1148 Py_ssize_t i_im;
1149 NumberFieldWidths re_spec;
1150 NumberFieldWidths im_spec;
1151 int flags = 0;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001152 int result = -1;
Amaury Forgeot d'Arccd27df32012-01-23 22:42:19 +01001153 Py_UCS4 maxchar = 127;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001154 enum PyUnicode_Kind rkind;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001155 void *rdata;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001156 Py_UCS4 re_sign_char = '\0';
1157 Py_UCS4 im_sign_char = '\0';
1158 int re_float_type; /* Used to see if we have a nan, inf, or regular float. */
1159 int im_float_type;
1160 int add_parens = 0;
1161 int skip_re = 0;
1162 Py_ssize_t lpad;
1163 Py_ssize_t rpad;
1164 Py_ssize_t total;
1165 PyObject *re_unicode_tmp = NULL;
1166 PyObject *im_unicode_tmp = NULL;
1167
1168 /* Locale settings, either from the actual locale or
1169 from a hard-code pseudo-locale */
Victor Stinner41a863c2012-02-24 00:37:51 +01001170 LocaleInfo locale = STATIC_LOCALE_INFO_INIT;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001171
Victor Stinner2f084ec2013-06-23 14:54:30 +02001172 if (format->precision > INT_MAX) {
1173 PyErr_SetString(PyExc_ValueError, "precision too big");
1174 goto done;
1175 }
1176 precision = (int)format->precision;
1177
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001178 /* Zero padding is not allowed. */
1179 if (format->fill_char == '0') {
1180 PyErr_SetString(PyExc_ValueError,
1181 "Zero padding is not allowed in complex format "
1182 "specifier");
1183 goto done;
1184 }
1185
1186 /* Neither is '=' alignment . */
1187 if (format->align == '=') {
1188 PyErr_SetString(PyExc_ValueError,
1189 "'=' alignment flag is not allowed in complex format "
1190 "specifier");
1191 goto done;
1192 }
1193
1194 re = PyComplex_RealAsDouble(value);
1195 if (re == -1.0 && PyErr_Occurred())
1196 goto done;
1197 im = PyComplex_ImagAsDouble(value);
1198 if (im == -1.0 && PyErr_Occurred())
1199 goto done;
1200
1201 if (format->alternate)
1202 flags |= Py_DTSF_ALT;
1203
1204 if (type == '\0') {
1205 /* Omitted type specifier. Should be like str(self). */
1206 type = 'r';
1207 default_precision = 0;
1208 if (re == 0.0 && copysign(1.0, re) == 1.0)
1209 skip_re = 1;
1210 else
1211 add_parens = 1;
1212 }
1213
1214 if (type == 'n')
1215 /* 'n' is the same as 'g', except for the locale used to
1216 format the result. We take care of that later. */
1217 type = 'g';
1218
1219 if (precision < 0)
1220 precision = default_precision;
1221 else if (type == 'r')
1222 type = 'g';
1223
1224 /* Cast "type", because if we're in unicode we need to pass a
1225 8-bit char. This is safe, because we've restricted what "type"
1226 can be. */
1227 re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
1228 &re_float_type);
1229 if (re_buf == NULL)
1230 goto done;
1231 im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
1232 &im_float_type);
1233 if (im_buf == NULL)
1234 goto done;
1235
1236 n_re_digits = strlen(re_buf);
1237 n_im_digits = strlen(im_buf);
1238
1239 /* Since there is no unicode version of PyOS_double_to_string,
1240 just use the 8 bit version and then convert to unicode. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001241 re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001242 if (re_unicode_tmp == NULL)
1243 goto done;
1244 i_re = 0;
1245
Victor Stinnerd3f08822012-05-29 12:57:52 +02001246 im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001247 if (im_unicode_tmp == NULL)
1248 goto done;
1249 i_im = 0;
1250
1251 /* Is a sign character present in the output? If so, remember it
1252 and skip it */
1253 if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') {
1254 re_sign_char = '-';
1255 ++i_re;
1256 --n_re_digits;
1257 }
1258 if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') {
1259 im_sign_char = '-';
1260 ++i_im;
1261 --n_im_digits;
1262 }
1263
1264 /* Determine if we have any "remainder" (after the digits, might include
1265 decimal or exponent or both (or neither)) */
Victor Stinnerafbaa202011-09-28 21:50:16 +02001266 parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001267 &n_re_remainder, &re_has_decimal);
Victor Stinnerafbaa202011-09-28 21:50:16 +02001268 parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001269 &n_im_remainder, &im_has_decimal);
1270
1271 /* Determine the grouping, separator, and decimal point, if any. */
Victor Stinner41a863c2012-02-24 00:37:51 +01001272 if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
1273 (format->thousands_separators ?
1274 LT_DEFAULT_LOCALE :
1275 LT_NO_LOCALE),
1276 &locale) == -1)
1277 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001278
1279 /* Turn off any padding. We'll do it later after we've composed
1280 the numbers without padding. */
1281 tmp_format.fill_char = '\0';
1282 tmp_format.align = '<';
1283 tmp_format.width = -1;
1284
1285 /* Calculate how much memory we'll need. */
1286 n_re_total = calc_number_widths(&re_spec, 0, re_sign_char, re_unicode_tmp,
1287 i_re, i_re + n_re_digits, n_re_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001288 re_has_decimal, &locale, &tmp_format,
1289 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001290
1291 /* Same formatting, but always include a sign, unless the real part is
1292 * going to be omitted, in which case we use whatever sign convention was
1293 * requested by the original format. */
1294 if (!skip_re)
1295 tmp_format.sign = '+';
1296 n_im_total = calc_number_widths(&im_spec, 0, im_sign_char, im_unicode_tmp,
1297 i_im, i_im + n_im_digits, n_im_remainder,
Victor Stinner41a863c2012-02-24 00:37:51 +01001298 im_has_decimal, &locale, &tmp_format,
1299 &maxchar);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001300
1301 if (skip_re)
1302 n_re_total = 0;
1303
1304 /* Add 1 for the 'j', and optionally 2 for parens. */
1305 calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
1306 format->width, format->align, &lpad, &rpad, &total);
1307
Victor Stinner41a863c2012-02-24 00:37:51 +01001308 if (lpad || rpad)
Victor Stinnera4ac6002012-01-21 15:50:49 +01001309 maxchar = Py_MAX(maxchar, format->fill_char);
1310
Victor Stinnerd3f08822012-05-29 12:57:52 +02001311 if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001312 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001313 rkind = writer->kind;
1314 rdata = writer->data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001315
1316 /* Populate the memory. First, the padding. */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001317 result = fill_padding(writer,
1318 n_re_total + n_im_total + 1 + add_parens * 2,
Eric V. Smith2ea97122014-04-14 11:55:10 -04001319 format->fill_char, lpad, rpad);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001320 if (result == -1)
1321 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001322
Victor Stinnerd3f08822012-05-29 12:57:52 +02001323 if (add_parens) {
1324 PyUnicode_WRITE(rkind, rdata, writer->pos, '(');
1325 writer->pos++;
1326 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001327
1328 if (!skip_re) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001329 result = fill_number(writer, &re_spec,
1330 re_unicode_tmp, i_re, i_re + n_re_digits,
1331 NULL, 0,
1332 0,
1333 &locale, 0);
1334 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001335 goto done;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001336 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001337 result = fill_number(writer, &im_spec,
1338 im_unicode_tmp, i_im, i_im + n_im_digits,
1339 NULL, 0,
1340 0,
1341 &locale, 0);
1342 if (result == -1)
Victor Stinnerafbaa202011-09-28 21:50:16 +02001343 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001344 PyUnicode_WRITE(rkind, rdata, writer->pos, 'j');
1345 writer->pos++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001346
Victor Stinnerd3f08822012-05-29 12:57:52 +02001347 if (add_parens) {
1348 PyUnicode_WRITE(rkind, rdata, writer->pos, ')');
1349 writer->pos++;
1350 }
1351
1352 writer->pos += rpad;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001353
1354done:
1355 PyMem_Free(re_buf);
1356 PyMem_Free(im_buf);
1357 Py_XDECREF(re_unicode_tmp);
1358 Py_XDECREF(im_unicode_tmp);
Victor Stinner41a863c2012-02-24 00:37:51 +01001359 free_locale_info(&locale);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001360 return result;
1361}
1362
1363/************************************************************************/
1364/*********** built in formatters ****************************************/
1365/************************************************************************/
doko@ubuntu.com39378f72012-06-21 12:12:20 +02001366static int
Victor Stinnerd3f08822012-05-29 12:57:52 +02001367format_obj(PyObject *obj, _PyUnicodeWriter *writer)
1368{
1369 PyObject *str;
1370 int err;
1371
1372 str = PyObject_Str(obj);
1373 if (str == NULL)
1374 return -1;
1375 err = _PyUnicodeWriter_WriteStr(writer, str);
1376 Py_DECREF(str);
1377 return err;
1378}
1379
1380int
1381_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1382 PyObject *obj,
1383 PyObject *format_spec,
1384 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001385{
1386 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001387
1388 assert(PyUnicode_Check(obj));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001389
1390 /* check for the special case of zero length format spec, make
1391 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001392 if (start == end) {
1393 if (PyUnicode_CheckExact(obj))
1394 return _PyUnicodeWriter_WriteStr(writer, obj);
1395 else
1396 return format_obj(obj, writer);
1397 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001398
1399 /* parse the format_spec */
1400 if (!parse_internal_render_format_spec(format_spec, start, end,
1401 &format, 's', '<'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001402 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001403
1404 /* type conversion? */
1405 switch (format.type) {
1406 case 's':
1407 /* no type conversion needed, already a string. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001408 return format_string_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001409 default:
1410 /* unknown */
1411 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001412 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001413 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001414}
1415
Victor Stinnerd3f08822012-05-29 12:57:52 +02001416int
1417_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1418 PyObject *obj,
1419 PyObject *format_spec,
1420 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001421{
Victor Stinnerd3f08822012-05-29 12:57:52 +02001422 PyObject *tmp = NULL, *str = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001423 InternalFormatSpec format;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001424 int result = -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001425
1426 /* check for the special case of zero length format spec, make
1427 it equivalent to str(obj) */
1428 if (start == end) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001429 if (PyLong_CheckExact(obj))
1430 return _PyLong_FormatWriter(writer, obj, 10, 0);
1431 else
1432 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001433 }
1434
1435 /* parse the format_spec */
1436 if (!parse_internal_render_format_spec(format_spec, start, end,
1437 &format, 'd', '>'))
1438 goto done;
1439
1440 /* type conversion? */
1441 switch (format.type) {
1442 case 'b':
1443 case 'c':
1444 case 'd':
1445 case 'o':
1446 case 'x':
1447 case 'X':
1448 case 'n':
Serhiy Storchaka95949422013-08-27 19:40:23 +03001449 /* no type conversion needed, already an int. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001450 result = format_long_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001451 break;
1452
1453 case 'e':
1454 case 'E':
1455 case 'f':
1456 case 'F':
1457 case 'g':
1458 case 'G':
1459 case '%':
1460 /* convert to float */
1461 tmp = PyNumber_Float(obj);
1462 if (tmp == NULL)
1463 goto done;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001464 result = format_float_internal(tmp, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001465 break;
1466
1467 default:
1468 /* unknown */
1469 unknown_presentation_type(format.type, obj->ob_type->tp_name);
1470 goto done;
1471 }
1472
1473done:
1474 Py_XDECREF(tmp);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001475 Py_XDECREF(str);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001476 return result;
1477}
1478
Victor Stinnerd3f08822012-05-29 12:57:52 +02001479int
1480_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1481 PyObject *obj,
1482 PyObject *format_spec,
1483 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001484{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001485 InternalFormatSpec format;
1486
1487 /* check for the special case of zero length format spec, make
1488 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001489 if (start == end)
1490 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001491
1492 /* parse the format_spec */
1493 if (!parse_internal_render_format_spec(format_spec, start, end,
1494 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001495 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001496
1497 /* type conversion? */
1498 switch (format.type) {
1499 case '\0': /* No format code: like 'g', but with at least one decimal. */
1500 case 'e':
1501 case 'E':
1502 case 'f':
1503 case 'F':
1504 case 'g':
1505 case 'G':
1506 case 'n':
1507 case '%':
1508 /* no conversion, already a float. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001509 return format_float_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001510
1511 default:
1512 /* unknown */
1513 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001514 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001515 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001516}
1517
Victor Stinnerd3f08822012-05-29 12:57:52 +02001518int
1519_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
1520 PyObject *obj,
1521 PyObject *format_spec,
1522 Py_ssize_t start, Py_ssize_t end)
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001523{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001524 InternalFormatSpec format;
1525
1526 /* check for the special case of zero length format spec, make
1527 it equivalent to str(obj) */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001528 if (start == end)
1529 return format_obj(obj, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001530
1531 /* parse the format_spec */
1532 if (!parse_internal_render_format_spec(format_spec, start, end,
1533 &format, '\0', '>'))
Victor Stinnerd3f08822012-05-29 12:57:52 +02001534 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001535
1536 /* type conversion? */
1537 switch (format.type) {
1538 case '\0': /* No format code: like 'g', but with at least one decimal. */
1539 case 'e':
1540 case 'E':
1541 case 'f':
1542 case 'F':
1543 case 'g':
1544 case 'G':
1545 case 'n':
1546 /* no conversion, already a complex. do the formatting */
Victor Stinnerd3f08822012-05-29 12:57:52 +02001547 return format_complex_internal(obj, &format, writer);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001548
1549 default:
1550 /* unknown */
1551 unknown_presentation_type(format.type, obj->ob_type->tp_name);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001552 return -1;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001553 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001554}