Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1 | /* 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öwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 6 | #include <locale.h> |
| 7 | |
| 8 | /* Raises an exception about an unknown presentation type for this |
| 9 | * type. */ |
| 10 | |
| 11 | static void |
| 12 | unknown_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 | |
| 30 | static void |
| 31 | invalid_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 | */ |
| 50 | static int |
| 51 | get_integer(PyObject *str, Py_ssize_t *pos, Py_ssize_t end, |
| 52 | Py_ssize_t *result) |
| 53 | { |
Mark Dickinson | 47862d4 | 2011-12-01 15:27:04 +0000 | [diff] [blame] | 54 | Py_ssize_t accumulator, digitval; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 55 | 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 Dickinson | 47862d4 | 2011-12-01 15:27:04 +0000 | [diff] [blame] | 64 | 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öwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 68 | */ |
Mark Dickinson | 47862d4 | 2011-12-01 15:27:04 +0000 | [diff] [blame] | 69 | if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 70 | PyErr_Format(PyExc_ValueError, |
| 71 | "Too many decimal digits in format string"); |
| 72 | return -1; |
| 73 | } |
Mark Dickinson | 47862d4 | 2011-12-01 15:27:04 +0000 | [diff] [blame] | 74 | accumulator = accumulator * 10 + digitval; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 75 | } |
| 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 */ |
| 85 | Py_LOCAL_INLINE(int) |
| 86 | is_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 */ |
| 97 | Py_LOCAL_INLINE(int) |
| 98 | is_sign_element(Py_UCS4 c) |
| 99 | { |
| 100 | switch (c) { |
| 101 | case ' ': case '+': case '-': |
| 102 | return 1; |
| 103 | default: |
| 104 | return 0; |
| 105 | } |
| 106 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 107 | |
Eric Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 108 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 109 | typedef 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 Smith | 4a7d76d | 2008-05-30 18:10:19 +0000 | [diff] [blame] | 119 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 120 | #if 0 |
| 121 | /* Occassionally useful for debugging. Should normally be commented out. */ |
| 122 | static void |
| 123 | DEBUG_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 | */ |
| 145 | static int |
| 146 | parse_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; |
| 159 | |
| 160 | format->fill_char = '\0'; |
| 161 | format->align = default_align; |
| 162 | format->alternate = 0; |
| 163 | format->sign = '\0'; |
| 164 | format->width = -1; |
| 165 | format->thousands_separators = 0; |
| 166 | format->precision = -1; |
| 167 | format->type = default_type; |
| 168 | |
| 169 | /* If the second char is an alignment token, |
| 170 | then parse the fill char */ |
| 171 | if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) { |
| 172 | format->align = READ_spec(pos+1); |
| 173 | format->fill_char = READ_spec(pos); |
| 174 | align_specified = 1; |
| 175 | pos += 2; |
| 176 | } |
| 177 | else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) { |
| 178 | format->align = READ_spec(pos); |
| 179 | align_specified = 1; |
| 180 | ++pos; |
| 181 | } |
| 182 | |
| 183 | /* Parse the various sign options */ |
| 184 | if (end-pos >= 1 && is_sign_element(READ_spec(pos))) { |
| 185 | format->sign = READ_spec(pos); |
| 186 | ++pos; |
| 187 | } |
| 188 | |
| 189 | /* If the next character is #, we're in alternate mode. This only |
| 190 | applies to integers. */ |
| 191 | if (end-pos >= 1 && READ_spec(pos) == '#') { |
| 192 | format->alternate = 1; |
| 193 | ++pos; |
| 194 | } |
| 195 | |
| 196 | /* The special case for 0-padding (backwards compat) */ |
| 197 | if (format->fill_char == '\0' && end-pos >= 1 && READ_spec(pos) == '0') { |
| 198 | format->fill_char = '0'; |
| 199 | if (!align_specified) { |
| 200 | format->align = '='; |
| 201 | } |
| 202 | ++pos; |
| 203 | } |
| 204 | |
| 205 | consumed = get_integer(format_spec, &pos, end, &format->width); |
| 206 | if (consumed == -1) |
| 207 | /* Overflow error. Exception already set. */ |
| 208 | return 0; |
| 209 | |
| 210 | /* If consumed is 0, we didn't consume any characters for the |
| 211 | width. In that case, reset the width to -1, because |
| 212 | get_integer() will have set it to zero. -1 is how we record |
| 213 | that the width wasn't specified. */ |
| 214 | if (consumed == 0) |
| 215 | format->width = -1; |
| 216 | |
| 217 | /* Comma signifies add thousands separators */ |
| 218 | if (end-pos && READ_spec(pos) == ',') { |
| 219 | format->thousands_separators = 1; |
| 220 | ++pos; |
| 221 | } |
| 222 | |
| 223 | /* Parse field precision */ |
| 224 | if (end-pos && READ_spec(pos) == '.') { |
| 225 | ++pos; |
| 226 | |
| 227 | consumed = get_integer(format_spec, &pos, end, &format->precision); |
| 228 | if (consumed == -1) |
| 229 | /* Overflow error. Exception already set. */ |
| 230 | return 0; |
| 231 | |
| 232 | /* Not having a precision after a dot is an error. */ |
| 233 | if (consumed == 0) { |
| 234 | PyErr_Format(PyExc_ValueError, |
| 235 | "Format specifier missing precision"); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | } |
| 240 | |
| 241 | /* Finally, parse the type field. */ |
| 242 | |
| 243 | if (end-pos > 1) { |
Eric V. Smith | d25cfe6 | 2012-01-19 20:04:28 -0500 | [diff] [blame] | 244 | /* More than one char remain, invalid format specifier. */ |
| 245 | PyErr_Format(PyExc_ValueError, "Invalid format specifier"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | if (end-pos == 1) { |
| 250 | format->type = READ_spec(pos); |
| 251 | ++pos; |
| 252 | } |
| 253 | |
| 254 | /* Do as much validating as we can, just by looking at the format |
| 255 | specifier. Do not take into account what type of formatting |
| 256 | we're doing (int, float, string). */ |
| 257 | |
| 258 | if (format->thousands_separators) { |
| 259 | switch (format->type) { |
| 260 | case 'd': |
| 261 | case 'e': |
| 262 | case 'f': |
| 263 | case 'g': |
| 264 | case 'E': |
| 265 | case 'G': |
| 266 | case '%': |
| 267 | case 'F': |
| 268 | case '\0': |
| 269 | /* These are allowed. See PEP 378.*/ |
| 270 | break; |
| 271 | default: |
| 272 | invalid_comma_type(format->type); |
| 273 | return 0; |
| 274 | } |
| 275 | } |
| 276 | |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 277 | assert (format->align <= 127); |
| 278 | assert (format->sign <= 127); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | /* Calculate the padding needed. */ |
| 283 | static void |
| 284 | calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align, |
| 285 | Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding, |
| 286 | Py_ssize_t *n_total) |
| 287 | { |
| 288 | if (width >= 0) { |
| 289 | if (nchars > width) |
| 290 | *n_total = nchars; |
| 291 | else |
| 292 | *n_total = width; |
| 293 | } |
| 294 | else { |
| 295 | /* not specified, use all of the chars and no more */ |
| 296 | *n_total = nchars; |
| 297 | } |
| 298 | |
| 299 | /* Figure out how much leading space we need, based on the |
| 300 | aligning */ |
| 301 | if (align == '>') |
| 302 | *n_lpadding = *n_total - nchars; |
| 303 | else if (align == '^') |
| 304 | *n_lpadding = (*n_total - nchars) / 2; |
| 305 | else if (align == '<' || align == '=') |
| 306 | *n_lpadding = 0; |
| 307 | else { |
| 308 | /* We should never have an unspecified alignment. */ |
| 309 | *n_lpadding = 0; |
| 310 | assert(0); |
| 311 | } |
| 312 | |
| 313 | *n_rpadding = *n_total - nchars - *n_lpadding; |
| 314 | } |
| 315 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 316 | /* Do the padding, and return a pointer to where the caller-supplied |
| 317 | content goes. */ |
| 318 | static Py_ssize_t |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 319 | fill_padding(_PyUnicodeWriter *writer, |
| 320 | Py_ssize_t nchars, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 321 | Py_UCS4 fill_char, Py_ssize_t n_lpadding, |
| 322 | Py_ssize_t n_rpadding) |
| 323 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 324 | Py_ssize_t pos; |
| 325 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 326 | /* Pad on left. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 327 | if (n_lpadding) { |
| 328 | pos = writer->pos; |
| 329 | _PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char); |
| 330 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 331 | |
| 332 | /* Pad on right. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 333 | if (n_rpadding) { |
| 334 | pos = writer->pos + nchars + n_lpadding; |
| 335 | _PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char); |
| 336 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 337 | |
| 338 | /* Pointer to the user content. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 339 | writer->pos += n_lpadding; |
| 340 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /************************************************************************/ |
| 344 | /*********** common routines for numeric formatting *********************/ |
| 345 | /************************************************************************/ |
| 346 | |
| 347 | /* Locale type codes. */ |
| 348 | #define LT_CURRENT_LOCALE 0 |
| 349 | #define LT_DEFAULT_LOCALE 1 |
| 350 | #define LT_NO_LOCALE 2 |
| 351 | |
| 352 | /* Locale info needed for formatting integers and the part of floats |
| 353 | before and including the decimal. Note that locales only support |
| 354 | 8-bit chars, not unicode. */ |
| 355 | typedef struct { |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 356 | PyObject *decimal_point; |
| 357 | PyObject *thousands_sep; |
| 358 | const char *grouping; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 359 | } LocaleInfo; |
| 360 | |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 361 | #define STATIC_LOCALE_INFO_INIT {0, 0, 0} |
| 362 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 363 | /* describes the layout for an integer, see the comment in |
| 364 | calc_number_widths() for details */ |
| 365 | typedef struct { |
| 366 | Py_ssize_t n_lpadding; |
| 367 | Py_ssize_t n_prefix; |
| 368 | Py_ssize_t n_spadding; |
| 369 | Py_ssize_t n_rpadding; |
| 370 | char sign; |
| 371 | Py_ssize_t n_sign; /* number of digits needed for sign (0/1) */ |
| 372 | Py_ssize_t n_grouped_digits; /* Space taken up by the digits, including |
| 373 | any grouping chars. */ |
| 374 | Py_ssize_t n_decimal; /* 0 if only an integer */ |
| 375 | Py_ssize_t n_remainder; /* Digits in decimal and/or exponent part, |
| 376 | excluding the decimal itself, if |
| 377 | present. */ |
| 378 | |
| 379 | /* These 2 are not the widths of fields, but are needed by |
| 380 | STRINGLIB_GROUPING. */ |
| 381 | Py_ssize_t n_digits; /* The number of digits before a decimal |
| 382 | or exponent. */ |
| 383 | Py_ssize_t n_min_width; /* The min_width we used when we computed |
| 384 | the n_grouped_digits width. */ |
| 385 | } NumberFieldWidths; |
| 386 | |
| 387 | |
| 388 | /* Given a number of the form: |
| 389 | digits[remainder] |
| 390 | where ptr points to the start and end points to the end, find where |
| 391 | the integer part ends. This could be a decimal, an exponent, both, |
| 392 | or neither. |
| 393 | If a decimal point is present, set *has_decimal and increment |
| 394 | remainder beyond it. |
| 395 | Results are undefined (but shouldn't crash) for improperly |
| 396 | formatted strings. |
| 397 | */ |
| 398 | static void |
| 399 | parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end, |
| 400 | Py_ssize_t *n_remainder, int *has_decimal) |
| 401 | { |
| 402 | Py_ssize_t remainder; |
| 403 | |
Antoine Pitrou | c73c561 | 2013-02-09 23:14:42 +0100 | [diff] [blame] | 404 | while (pos<end && Py_ISDIGIT(PyUnicode_READ_CHAR(s, pos))) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 405 | ++pos; |
| 406 | remainder = pos; |
| 407 | |
| 408 | /* Does remainder start with a decimal point? */ |
| 409 | *has_decimal = pos<end && PyUnicode_READ_CHAR(s, remainder) == '.'; |
| 410 | |
| 411 | /* Skip the decimal point. */ |
| 412 | if (*has_decimal) |
| 413 | remainder++; |
| 414 | |
| 415 | *n_remainder = end - remainder; |
| 416 | } |
| 417 | |
| 418 | /* not all fields of format are used. for example, precision is |
| 419 | unused. should this take discrete params in order to be more clear |
| 420 | about what it does? or is passing a single format parameter easier |
| 421 | and more efficient enough to justify a little obfuscation? */ |
| 422 | static Py_ssize_t |
| 423 | calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix, |
| 424 | Py_UCS4 sign_char, PyObject *number, Py_ssize_t n_start, |
| 425 | Py_ssize_t n_end, Py_ssize_t n_remainder, |
| 426 | int has_decimal, const LocaleInfo *locale, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 427 | const InternalFormatSpec *format, Py_UCS4 *maxchar) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 428 | { |
| 429 | Py_ssize_t n_non_digit_non_padding; |
| 430 | Py_ssize_t n_padding; |
| 431 | |
| 432 | spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0); |
| 433 | spec->n_lpadding = 0; |
| 434 | spec->n_prefix = n_prefix; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 435 | spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 436 | spec->n_remainder = n_remainder; |
| 437 | spec->n_spadding = 0; |
| 438 | spec->n_rpadding = 0; |
| 439 | spec->sign = '\0'; |
| 440 | spec->n_sign = 0; |
| 441 | |
| 442 | /* the output will look like: |
| 443 | | | |
| 444 | | <lpadding> <sign> <prefix> <spadding> <grouped_digits> <decimal> <remainder> <rpadding> | |
| 445 | | | |
| 446 | |
| 447 | sign is computed from format->sign and the actual |
| 448 | sign of the number |
| 449 | |
| 450 | prefix is given (it's for the '0x' prefix) |
| 451 | |
| 452 | digits is already known |
| 453 | |
| 454 | the total width is either given, or computed from the |
| 455 | actual digits |
| 456 | |
| 457 | only one of lpadding, spadding, and rpadding can be non-zero, |
| 458 | and it's calculated from the width and other fields |
| 459 | */ |
| 460 | |
| 461 | /* compute the various parts we're going to write */ |
| 462 | switch (format->sign) { |
| 463 | case '+': |
| 464 | /* always put a + or - */ |
| 465 | spec->n_sign = 1; |
| 466 | spec->sign = (sign_char == '-' ? '-' : '+'); |
| 467 | break; |
| 468 | case ' ': |
| 469 | spec->n_sign = 1; |
| 470 | spec->sign = (sign_char == '-' ? '-' : ' '); |
| 471 | break; |
| 472 | default: |
| 473 | /* Not specified, or the default (-) */ |
| 474 | if (sign_char == '-') { |
| 475 | spec->n_sign = 1; |
| 476 | spec->sign = '-'; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /* The number of chars used for non-digits and non-padding. */ |
| 481 | n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal + |
| 482 | spec->n_remainder; |
| 483 | |
| 484 | /* min_width can go negative, that's okay. format->width == -1 means |
| 485 | we don't care. */ |
| 486 | if (format->fill_char == '0' && format->align == '=') |
| 487 | spec->n_min_width = format->width - n_non_digit_non_padding; |
| 488 | else |
| 489 | spec->n_min_width = 0; |
| 490 | |
| 491 | if (spec->n_digits == 0) |
| 492 | /* This case only occurs when using 'c' formatting, we need |
| 493 | to special case it because the grouping code always wants |
| 494 | to have at least one character. */ |
| 495 | spec->n_grouped_digits = 0; |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 496 | else { |
| 497 | Py_UCS4 grouping_maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 498 | spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping( |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 499 | NULL, 0, |
| 500 | 0, NULL, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 501 | spec->n_digits, spec->n_min_width, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 502 | locale->grouping, locale->thousands_sep, &grouping_maxchar); |
| 503 | *maxchar = Py_MAX(*maxchar, grouping_maxchar); |
| 504 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 505 | |
| 506 | /* Given the desired width and the total of digit and non-digit |
| 507 | space we consume, see if we need any padding. format->width can |
| 508 | be negative (meaning no padding), but this code still works in |
| 509 | that case. */ |
| 510 | n_padding = format->width - |
| 511 | (n_non_digit_non_padding + spec->n_grouped_digits); |
| 512 | if (n_padding > 0) { |
| 513 | /* Some padding is needed. Determine if it's left, space, or right. */ |
| 514 | switch (format->align) { |
| 515 | case '<': |
| 516 | spec->n_rpadding = n_padding; |
| 517 | break; |
| 518 | case '^': |
| 519 | spec->n_lpadding = n_padding / 2; |
| 520 | spec->n_rpadding = n_padding - spec->n_lpadding; |
| 521 | break; |
| 522 | case '=': |
| 523 | spec->n_spadding = n_padding; |
| 524 | break; |
| 525 | case '>': |
| 526 | spec->n_lpadding = n_padding; |
| 527 | break; |
| 528 | default: |
| 529 | /* Shouldn't get here, but treat it as '>' */ |
| 530 | spec->n_lpadding = n_padding; |
| 531 | assert(0); |
| 532 | break; |
| 533 | } |
| 534 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 535 | |
| 536 | if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding) |
| 537 | *maxchar = Py_MAX(*maxchar, format->fill_char); |
| 538 | |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 539 | if (spec->n_decimal) |
| 540 | *maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point)); |
| 541 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 542 | return spec->n_lpadding + spec->n_sign + spec->n_prefix + |
| 543 | spec->n_spadding + spec->n_grouped_digits + spec->n_decimal + |
| 544 | spec->n_remainder + spec->n_rpadding; |
| 545 | } |
| 546 | |
| 547 | /* Fill in the digit parts of a numbers's string representation, |
| 548 | as determined in calc_number_widths(). |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 549 | Return -1 on error, or 0 on success. */ |
| 550 | static int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 551 | fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 552 | PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end, |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 553 | PyObject *prefix, Py_ssize_t p_start, |
| 554 | Py_UCS4 fill_char, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 555 | LocaleInfo *locale, int toupper) |
| 556 | { |
| 557 | /* Used to keep track of digits, decimal, and remainder. */ |
| 558 | Py_ssize_t d_pos = d_start; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 559 | const enum PyUnicode_Kind kind = writer->kind; |
| 560 | const void *data = writer->data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 561 | Py_ssize_t r; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 562 | |
| 563 | if (spec->n_lpadding) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 564 | _PyUnicode_FastFill(writer->buffer, |
| 565 | writer->pos, spec->n_lpadding, fill_char); |
| 566 | writer->pos += spec->n_lpadding; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 567 | } |
| 568 | if (spec->n_sign == 1) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 569 | PyUnicode_WRITE(kind, data, writer->pos, spec->sign); |
| 570 | writer->pos++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 571 | } |
| 572 | if (spec->n_prefix) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 573 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 574 | prefix, p_start, |
| 575 | spec->n_prefix); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 576 | if (toupper) { |
| 577 | Py_ssize_t t; |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 578 | for (t = 0; t < spec->n_prefix; t++) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 579 | Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t); |
Victor Stinner | ed27785 | 2012-02-01 00:22:23 +0100 | [diff] [blame] | 580 | c = Py_TOUPPER(c); |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 581 | assert (c <= 127); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 582 | PyUnicode_WRITE(kind, data, writer->pos + t, c); |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 583 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 584 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 585 | writer->pos += spec->n_prefix; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 586 | } |
| 587 | if (spec->n_spadding) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 588 | _PyUnicode_FastFill(writer->buffer, |
| 589 | writer->pos, spec->n_spadding, fill_char); |
| 590 | writer->pos += spec->n_spadding; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* Only for type 'c' special case, it has no digits. */ |
| 594 | if (spec->n_digits != 0) { |
| 595 | /* Fill the digits with InsertThousandsGrouping. */ |
Victor Stinner | dba2dee | 2011-09-28 21:50:42 +0200 | [diff] [blame] | 596 | char *pdigits; |
| 597 | if (PyUnicode_READY(digits)) |
| 598 | return -1; |
| 599 | pdigits = PyUnicode_DATA(digits); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 600 | if (PyUnicode_KIND(digits) < kind) { |
| 601 | pdigits = _PyUnicode_AsKind(digits, kind); |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 602 | if (pdigits == NULL) |
| 603 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 604 | } |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 605 | r = _PyUnicode_InsertThousandsGrouping( |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 606 | writer->buffer, writer->pos, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 607 | spec->n_grouped_digits, |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 608 | pdigits + kind * d_pos, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 609 | spec->n_digits, spec->n_min_width, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 610 | locale->grouping, locale->thousands_sep, NULL); |
Victor Stinner | 90f50d4 | 2012-02-24 01:44:47 +0100 | [diff] [blame] | 611 | if (r == -1) |
| 612 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 613 | assert(r == spec->n_grouped_digits); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 614 | if (PyUnicode_KIND(digits) < kind) |
| 615 | PyMem_Free(pdigits); |
| 616 | d_pos += spec->n_digits; |
| 617 | } |
| 618 | if (toupper) { |
| 619 | Py_ssize_t t; |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 620 | for (t = 0; t < spec->n_grouped_digits; t++) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 621 | Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t); |
Victor Stinner | ed27785 | 2012-02-01 00:22:23 +0100 | [diff] [blame] | 622 | c = Py_TOUPPER(c); |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 623 | if (c > 127) { |
| 624 | PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit"); |
| 625 | return -1; |
| 626 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 627 | PyUnicode_WRITE(kind, data, writer->pos + t, c); |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 628 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 629 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 630 | writer->pos += spec->n_grouped_digits; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 631 | |
| 632 | if (spec->n_decimal) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 633 | _PyUnicode_FastCopyCharacters( |
| 634 | writer->buffer, writer->pos, |
| 635 | locale->decimal_point, 0, spec->n_decimal); |
| 636 | writer->pos += spec->n_decimal; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 637 | d_pos += 1; |
| 638 | } |
| 639 | |
| 640 | if (spec->n_remainder) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 641 | _PyUnicode_FastCopyCharacters( |
| 642 | writer->buffer, writer->pos, |
| 643 | digits, d_pos, spec->n_remainder); |
| 644 | writer->pos += spec->n_remainder; |
Brett Cannon | 8a250fa | 2012-06-25 16:13:44 -0400 | [diff] [blame] | 645 | /* d_pos += spec->n_remainder; */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | if (spec->n_rpadding) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 649 | _PyUnicode_FastFill(writer->buffer, |
| 650 | writer->pos, spec->n_rpadding, |
| 651 | fill_char); |
| 652 | writer->pos += spec->n_rpadding; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 653 | } |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 654 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static char no_grouping[1] = {CHAR_MAX}; |
| 658 | |
| 659 | /* Find the decimal point character(s?), thousands_separator(s?), and |
| 660 | grouping description, either for the current locale if type is |
| 661 | LT_CURRENT_LOCALE, a hard-coded locale if LT_DEFAULT_LOCALE, or |
| 662 | none if LT_NO_LOCALE. */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 663 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 664 | get_locale_info(int type, LocaleInfo *locale_info) |
| 665 | { |
| 666 | switch (type) { |
| 667 | case LT_CURRENT_LOCALE: { |
| 668 | struct lconv *locale_data = localeconv(); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 669 | locale_info->decimal_point = PyUnicode_DecodeLocale( |
| 670 | locale_data->decimal_point, |
| 671 | NULL); |
| 672 | if (locale_info->decimal_point == NULL) |
| 673 | return -1; |
| 674 | locale_info->thousands_sep = PyUnicode_DecodeLocale( |
| 675 | locale_data->thousands_sep, |
| 676 | NULL); |
| 677 | if (locale_info->thousands_sep == NULL) { |
| 678 | Py_DECREF(locale_info->decimal_point); |
| 679 | return -1; |
| 680 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 681 | locale_info->grouping = locale_data->grouping; |
| 682 | break; |
| 683 | } |
| 684 | case LT_DEFAULT_LOCALE: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 685 | locale_info->decimal_point = PyUnicode_FromOrdinal('.'); |
| 686 | locale_info->thousands_sep = PyUnicode_FromOrdinal(','); |
| 687 | if (!locale_info->decimal_point || !locale_info->thousands_sep) { |
| 688 | Py_XDECREF(locale_info->decimal_point); |
| 689 | Py_XDECREF(locale_info->thousands_sep); |
| 690 | return -1; |
| 691 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 692 | locale_info->grouping = "\3"; /* Group every 3 characters. The |
| 693 | (implicit) trailing 0 means repeat |
| 694 | infinitely. */ |
| 695 | break; |
| 696 | case LT_NO_LOCALE: |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 697 | locale_info->decimal_point = PyUnicode_FromOrdinal('.'); |
| 698 | locale_info->thousands_sep = PyUnicode_New(0, 0); |
| 699 | if (!locale_info->decimal_point || !locale_info->thousands_sep) { |
| 700 | Py_XDECREF(locale_info->decimal_point); |
| 701 | Py_XDECREF(locale_info->thousands_sep); |
| 702 | return -1; |
| 703 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 704 | locale_info->grouping = no_grouping; |
| 705 | break; |
| 706 | default: |
| 707 | assert(0); |
| 708 | } |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static void |
| 713 | free_locale_info(LocaleInfo *locale_info) |
| 714 | { |
| 715 | Py_XDECREF(locale_info->decimal_point); |
| 716 | Py_XDECREF(locale_info->thousands_sep); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /************************************************************************/ |
| 720 | /*********** string formatting ******************************************/ |
| 721 | /************************************************************************/ |
| 722 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 723 | static int |
| 724 | format_string_internal(PyObject *value, const InternalFormatSpec *format, |
| 725 | _PyUnicodeWriter *writer) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 726 | { |
| 727 | Py_ssize_t lpad; |
| 728 | Py_ssize_t rpad; |
| 729 | Py_ssize_t total; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 730 | Py_ssize_t len; |
| 731 | int result = -1; |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 732 | Py_UCS4 maxchar; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 733 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 734 | assert(PyUnicode_IS_READY(value)); |
| 735 | len = PyUnicode_GET_LENGTH(value); |
| 736 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 737 | /* sign is not allowed on strings */ |
| 738 | if (format->sign != '\0') { |
| 739 | PyErr_SetString(PyExc_ValueError, |
| 740 | "Sign not allowed in string format specifier"); |
| 741 | goto done; |
| 742 | } |
| 743 | |
| 744 | /* alternate is not allowed on strings */ |
| 745 | if (format->alternate) { |
| 746 | PyErr_SetString(PyExc_ValueError, |
| 747 | "Alternate form (#) not allowed in string format " |
| 748 | "specifier"); |
| 749 | goto done; |
| 750 | } |
| 751 | |
| 752 | /* '=' alignment not allowed on strings */ |
| 753 | if (format->align == '=') { |
| 754 | PyErr_SetString(PyExc_ValueError, |
| 755 | "'=' alignment not allowed " |
| 756 | "in string format specifier"); |
| 757 | goto done; |
| 758 | } |
| 759 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 760 | if (format->width == -1 && format->precision == -1) { |
| 761 | /* Fast path */ |
| 762 | return _PyUnicodeWriter_WriteStr(writer, value); |
| 763 | } |
| 764 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 765 | /* if precision is specified, output no more that format.precision |
| 766 | characters */ |
| 767 | if (format->precision >= 0 && len >= format->precision) { |
| 768 | len = format->precision; |
| 769 | } |
| 770 | |
| 771 | calc_padding(len, format->width, format->align, &lpad, &rpad, &total); |
| 772 | |
Victor Stinner | ece58de | 2012-04-23 23:36:38 +0200 | [diff] [blame] | 773 | maxchar = _PyUnicode_FindMaxChar(value, 0, len); |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 774 | if (lpad != 0 || rpad != 0) |
| 775 | maxchar = Py_MAX(maxchar, format->fill_char); |
| 776 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 777 | /* allocate the resulting string */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 778 | if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 779 | goto done; |
| 780 | |
| 781 | /* Write into that space. First the padding. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 782 | result = fill_padding(writer, len, |
| 783 | format->fill_char=='\0'?' ':format->fill_char, |
| 784 | lpad, rpad); |
| 785 | if (result == -1) |
| 786 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 787 | |
| 788 | /* Then the source string. */ |
Victor Stinner | c9d369f | 2012-06-16 02:22:37 +0200 | [diff] [blame] | 789 | if (len) { |
| 790 | _PyUnicode_FastCopyCharacters(writer->buffer, writer->pos, |
| 791 | value, 0, len); |
| 792 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 793 | writer->pos += (len + rpad); |
| 794 | result = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 795 | |
| 796 | done: |
| 797 | return result; |
| 798 | } |
| 799 | |
| 800 | |
| 801 | /************************************************************************/ |
| 802 | /*********** long formatting ********************************************/ |
| 803 | /************************************************************************/ |
| 804 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 805 | static int |
| 806 | format_long_internal(PyObject *value, const InternalFormatSpec *format, |
| 807 | _PyUnicodeWriter *writer) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 808 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 809 | int result = -1; |
Amaury Forgeot d'Arc | cd27df3 | 2012-01-23 22:42:19 +0100 | [diff] [blame] | 810 | Py_UCS4 maxchar = 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 811 | PyObject *tmp = NULL; |
| 812 | Py_ssize_t inumeric_chars; |
| 813 | Py_UCS4 sign_char = '\0'; |
| 814 | Py_ssize_t n_digits; /* count of digits need from the computed |
| 815 | string */ |
| 816 | Py_ssize_t n_remainder = 0; /* Used only for 'c' formatting, which |
| 817 | produces non-digits */ |
| 818 | Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */ |
| 819 | Py_ssize_t n_total; |
Victor Stinner | ed27785 | 2012-02-01 00:22:23 +0100 | [diff] [blame] | 820 | Py_ssize_t prefix = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 821 | NumberFieldWidths spec; |
| 822 | long x; |
| 823 | |
| 824 | /* Locale settings, either from the actual locale or |
| 825 | from a hard-code pseudo-locale */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 826 | LocaleInfo locale = STATIC_LOCALE_INFO_INIT; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 827 | |
| 828 | /* no precision allowed on integers */ |
| 829 | if (format->precision != -1) { |
| 830 | PyErr_SetString(PyExc_ValueError, |
| 831 | "Precision not allowed in integer format specifier"); |
| 832 | goto done; |
| 833 | } |
| 834 | |
| 835 | /* special case for character formatting */ |
| 836 | if (format->type == 'c') { |
| 837 | /* error to specify a sign */ |
| 838 | if (format->sign != '\0') { |
| 839 | PyErr_SetString(PyExc_ValueError, |
| 840 | "Sign not allowed with integer" |
| 841 | " format specifier 'c'"); |
| 842 | goto done; |
| 843 | } |
| 844 | |
| 845 | /* taken from unicodeobject.c formatchar() */ |
| 846 | /* Integer input truncated to a character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 847 | x = PyLong_AsLong(value); |
| 848 | if (x == -1 && PyErr_Occurred()) |
| 849 | goto done; |
| 850 | if (x < 0 || x > 0x10ffff) { |
| 851 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 852 | "%c arg not in range(0x110000)"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 853 | goto done; |
| 854 | } |
| 855 | tmp = PyUnicode_FromOrdinal(x); |
| 856 | inumeric_chars = 0; |
| 857 | n_digits = 1; |
Amaury Forgeot d'Arc | 6d766fc | 2012-01-23 23:20:43 +0100 | [diff] [blame] | 858 | maxchar = Py_MAX(maxchar, (Py_UCS4)x); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 859 | |
| 860 | /* As a sort-of hack, we tell calc_number_widths that we only |
| 861 | have "remainder" characters. calc_number_widths thinks |
| 862 | these are characters that don't get formatted, only copied |
| 863 | into the output string. We do this for 'c' formatting, |
| 864 | because the characters are likely to be non-digits. */ |
| 865 | n_remainder = 1; |
| 866 | } |
| 867 | else { |
| 868 | int base; |
| 869 | int leading_chars_to_skip = 0; /* Number of characters added by |
| 870 | PyNumber_ToBase that we want to |
| 871 | skip over. */ |
| 872 | |
| 873 | /* Compute the base and how many characters will be added by |
| 874 | PyNumber_ToBase */ |
| 875 | switch (format->type) { |
| 876 | case 'b': |
| 877 | base = 2; |
| 878 | leading_chars_to_skip = 2; /* 0b */ |
| 879 | break; |
| 880 | case 'o': |
| 881 | base = 8; |
| 882 | leading_chars_to_skip = 2; /* 0o */ |
| 883 | break; |
| 884 | case 'x': |
| 885 | case 'X': |
| 886 | base = 16; |
| 887 | leading_chars_to_skip = 2; /* 0x */ |
| 888 | break; |
| 889 | default: /* shouldn't be needed, but stops a compiler warning */ |
| 890 | case 'd': |
| 891 | case 'n': |
| 892 | base = 10; |
| 893 | break; |
| 894 | } |
| 895 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 896 | if (format->sign != '+' && format->sign != ' ' |
| 897 | && format->width == -1 |
| 898 | && format->type != 'X' && format->type != 'n' |
| 899 | && !format->thousands_separators |
| 900 | && PyLong_CheckExact(value)) |
| 901 | { |
| 902 | /* Fast path */ |
| 903 | return _PyLong_FormatWriter(writer, value, base, format->alternate); |
| 904 | } |
| 905 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 906 | /* The number of prefix chars is the same as the leading |
| 907 | chars to skip */ |
| 908 | if (format->alternate) |
| 909 | n_prefix = leading_chars_to_skip; |
| 910 | |
| 911 | /* Do the hard part, converting to a string in a given base */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 912 | tmp = _PyLong_Format(value, base); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 913 | if (tmp == NULL || PyUnicode_READY(tmp) == -1) |
| 914 | goto done; |
| 915 | |
| 916 | inumeric_chars = 0; |
| 917 | n_digits = PyUnicode_GET_LENGTH(tmp); |
| 918 | |
| 919 | prefix = inumeric_chars; |
| 920 | |
| 921 | /* Is a sign character present in the output? If so, remember it |
| 922 | and skip it */ |
| 923 | if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') { |
| 924 | sign_char = '-'; |
| 925 | ++prefix; |
| 926 | ++leading_chars_to_skip; |
| 927 | } |
| 928 | |
| 929 | /* Skip over the leading chars (0x, 0b, etc.) */ |
| 930 | n_digits -= leading_chars_to_skip; |
| 931 | inumeric_chars += leading_chars_to_skip; |
| 932 | } |
| 933 | |
| 934 | /* Determine the grouping, separator, and decimal point, if any. */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 935 | if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : |
| 936 | (format->thousands_separators ? |
| 937 | LT_DEFAULT_LOCALE : |
| 938 | LT_NO_LOCALE), |
| 939 | &locale) == -1) |
| 940 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 941 | |
| 942 | /* Calculate how much memory we'll need. */ |
| 943 | n_total = calc_number_widths(&spec, n_prefix, sign_char, tmp, inumeric_chars, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 944 | inumeric_chars + n_digits, n_remainder, 0, |
| 945 | &locale, format, &maxchar); |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 946 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 947 | /* Allocate the memory. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 948 | if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 949 | goto done; |
| 950 | |
| 951 | /* Populate the memory. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 952 | result = fill_number(writer, &spec, |
| 953 | tmp, inumeric_chars, inumeric_chars + n_digits, |
| 954 | tmp, prefix, |
| 955 | format->fill_char == '\0' ? ' ' : format->fill_char, |
| 956 | &locale, format->type == 'X'); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 957 | |
| 958 | done: |
| 959 | Py_XDECREF(tmp); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 960 | free_locale_info(&locale); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 961 | return result; |
| 962 | } |
| 963 | |
| 964 | /************************************************************************/ |
| 965 | /*********** float formatting *******************************************/ |
| 966 | /************************************************************************/ |
| 967 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 968 | /* much of this is taken from unicodeobject.c */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 969 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 970 | format_float_internal(PyObject *value, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 971 | const InternalFormatSpec *format, |
| 972 | _PyUnicodeWriter *writer) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 973 | { |
| 974 | char *buf = NULL; /* buffer returned from PyOS_double_to_string */ |
| 975 | Py_ssize_t n_digits; |
| 976 | Py_ssize_t n_remainder; |
| 977 | Py_ssize_t n_total; |
| 978 | int has_decimal; |
| 979 | double val; |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 980 | Py_ssize_t precision; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 981 | Py_ssize_t default_precision = 6; |
| 982 | Py_UCS4 type = format->type; |
| 983 | int add_pct = 0; |
| 984 | Py_ssize_t index; |
| 985 | NumberFieldWidths spec; |
| 986 | int flags = 0; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 987 | int result = -1; |
Amaury Forgeot d'Arc | cd27df3 | 2012-01-23 22:42:19 +0100 | [diff] [blame] | 988 | Py_UCS4 maxchar = 127; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 989 | Py_UCS4 sign_char = '\0'; |
| 990 | int float_type; /* Used to see if we have a nan, inf, or regular float. */ |
| 991 | PyObject *unicode_tmp = NULL; |
| 992 | |
| 993 | /* Locale settings, either from the actual locale or |
| 994 | from a hard-code pseudo-locale */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 995 | LocaleInfo locale = STATIC_LOCALE_INFO_INIT; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 996 | |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 997 | if (format->precision > INT_MAX) { |
| 998 | PyErr_SetString(PyExc_ValueError, "precision too big"); |
| 999 | goto done; |
| 1000 | } |
| 1001 | precision = (int)format->precision; |
| 1002 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1003 | if (format->alternate) |
| 1004 | flags |= Py_DTSF_ALT; |
| 1005 | |
| 1006 | if (type == '\0') { |
| 1007 | /* Omitted type specifier. Behaves in the same way as repr(x) |
| 1008 | and str(x) if no precision is given, else like 'g', but with |
| 1009 | at least one digit after the decimal point. */ |
| 1010 | flags |= Py_DTSF_ADD_DOT_0; |
| 1011 | type = 'r'; |
| 1012 | default_precision = 0; |
| 1013 | } |
| 1014 | |
| 1015 | if (type == 'n') |
| 1016 | /* 'n' is the same as 'g', except for the locale used to |
| 1017 | format the result. We take care of that later. */ |
| 1018 | type = 'g'; |
| 1019 | |
| 1020 | val = PyFloat_AsDouble(value); |
| 1021 | if (val == -1.0 && PyErr_Occurred()) |
| 1022 | goto done; |
| 1023 | |
| 1024 | if (type == '%') { |
| 1025 | type = 'f'; |
| 1026 | val *= 100; |
| 1027 | add_pct = 1; |
| 1028 | } |
| 1029 | |
| 1030 | if (precision < 0) |
| 1031 | precision = default_precision; |
| 1032 | else if (type == 'r') |
| 1033 | type = 'g'; |
| 1034 | |
| 1035 | /* Cast "type", because if we're in unicode we need to pass a |
| 1036 | 8-bit char. This is safe, because we've restricted what "type" |
| 1037 | can be. */ |
| 1038 | buf = PyOS_double_to_string(val, (char)type, precision, flags, |
| 1039 | &float_type); |
| 1040 | if (buf == NULL) |
| 1041 | goto done; |
| 1042 | n_digits = strlen(buf); |
| 1043 | |
| 1044 | if (add_pct) { |
| 1045 | /* We know that buf has a trailing zero (since we just called |
| 1046 | strlen() on it), and we don't use that fact any more. So we |
| 1047 | can just write over the trailing zero. */ |
| 1048 | buf[n_digits] = '%'; |
| 1049 | n_digits += 1; |
| 1050 | } |
| 1051 | |
| 1052 | /* Since there is no unicode version of PyOS_double_to_string, |
| 1053 | just use the 8 bit version and then convert to unicode. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1054 | unicode_tmp = _PyUnicode_FromASCII(buf, n_digits); |
| 1055 | PyMem_Free(buf); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1056 | if (unicode_tmp == NULL) |
| 1057 | goto done; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1058 | |
| 1059 | if (format->sign != '+' && format->sign != ' ' |
| 1060 | && format->width == -1 |
| 1061 | && format->type != 'n' |
| 1062 | && !format->thousands_separators) |
| 1063 | { |
| 1064 | /* Fast path */ |
| 1065 | result = _PyUnicodeWriter_WriteStr(writer, unicode_tmp); |
| 1066 | Py_DECREF(unicode_tmp); |
| 1067 | return result; |
| 1068 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1069 | |
| 1070 | /* Is a sign character present in the output? If so, remember it |
| 1071 | and skip it */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1072 | index = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1073 | if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') { |
| 1074 | sign_char = '-'; |
| 1075 | ++index; |
| 1076 | --n_digits; |
| 1077 | } |
| 1078 | |
| 1079 | /* Determine if we have any "remainder" (after the digits, might include |
| 1080 | decimal or exponent or both (or neither)) */ |
| 1081 | parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal); |
| 1082 | |
| 1083 | /* Determine the grouping, separator, and decimal point, if any. */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1084 | if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE : |
| 1085 | (format->thousands_separators ? |
| 1086 | LT_DEFAULT_LOCALE : |
| 1087 | LT_NO_LOCALE), |
| 1088 | &locale) == -1) |
| 1089 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1090 | |
| 1091 | /* Calculate how much memory we'll need. */ |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1092 | n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1093 | index + n_digits, n_remainder, has_decimal, |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1094 | &locale, format, &maxchar); |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 1095 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1096 | /* Allocate the memory. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1097 | if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1098 | goto done; |
| 1099 | |
| 1100 | /* Populate the memory. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1101 | result = fill_number(writer, &spec, |
| 1102 | unicode_tmp, index, index + n_digits, |
| 1103 | NULL, 0, |
| 1104 | format->fill_char == '\0' ? ' ' : format->fill_char, |
| 1105 | &locale, 0); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1106 | |
| 1107 | done: |
Stefan Krah | d9c1bf7 | 2012-09-06 13:02:46 +0200 | [diff] [blame] | 1108 | Py_XDECREF(unicode_tmp); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1109 | free_locale_info(&locale); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1110 | return result; |
| 1111 | } |
| 1112 | |
| 1113 | /************************************************************************/ |
| 1114 | /*********** complex formatting *****************************************/ |
| 1115 | /************************************************************************/ |
| 1116 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1117 | static int |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1118 | format_complex_internal(PyObject *value, |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1119 | const InternalFormatSpec *format, |
| 1120 | _PyUnicodeWriter *writer) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1121 | { |
| 1122 | double re; |
| 1123 | double im; |
| 1124 | char *re_buf = NULL; /* buffer returned from PyOS_double_to_string */ |
| 1125 | char *im_buf = NULL; /* buffer returned from PyOS_double_to_string */ |
| 1126 | |
| 1127 | InternalFormatSpec tmp_format = *format; |
| 1128 | Py_ssize_t n_re_digits; |
| 1129 | Py_ssize_t n_im_digits; |
| 1130 | Py_ssize_t n_re_remainder; |
| 1131 | Py_ssize_t n_im_remainder; |
| 1132 | Py_ssize_t n_re_total; |
| 1133 | Py_ssize_t n_im_total; |
| 1134 | int re_has_decimal; |
| 1135 | int im_has_decimal; |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 1136 | int precision; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1137 | Py_ssize_t default_precision = 6; |
| 1138 | 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 Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1144 | int result = -1; |
Amaury Forgeot d'Arc | cd27df3 | 2012-01-23 22:42:19 +0100 | [diff] [blame] | 1145 | Py_UCS4 maxchar = 127; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1146 | enum PyUnicode_Kind rkind; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | void *rdata; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1148 | 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 Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1162 | LocaleInfo locale = STATIC_LOCALE_INFO_INIT; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1163 | |
Victor Stinner | 2f084ec | 2013-06-23 14:54:30 +0200 | [diff] [blame] | 1164 | 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öwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1170 | /* 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 | |
| 1216 | /* Cast "type", because if we're in unicode we need to pass a |
| 1217 | 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 Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1233 | re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1234 | if (re_unicode_tmp == NULL) |
| 1235 | goto done; |
| 1236 | i_re = 0; |
| 1237 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1238 | im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1239 | 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 Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1258 | parse_number(re_unicode_tmp, i_re, i_re + n_re_digits, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1259 | &n_re_remainder, &re_has_decimal); |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1260 | parse_number(im_unicode_tmp, i_im, i_im + n_im_digits, |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1261 | &n_im_remainder, &im_has_decimal); |
| 1262 | |
| 1263 | /* Determine the grouping, separator, and decimal point, if any. */ |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1264 | 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öwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1270 | |
| 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 Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1280 | re_has_decimal, &locale, &tmp_format, |
| 1281 | &maxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1282 | |
| 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 Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1290 | im_has_decimal, &locale, &tmp_format, |
| 1291 | &maxchar); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1292 | |
| 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 Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1300 | if (lpad || rpad) |
Victor Stinner | a4ac600 | 2012-01-21 15:50:49 +0100 | [diff] [blame] | 1301 | maxchar = Py_MAX(maxchar, format->fill_char); |
| 1302 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1303 | if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1304 | goto done; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1305 | rkind = writer->kind; |
| 1306 | rdata = writer->data; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1307 | |
| 1308 | /* Populate the memory. First, the padding. */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1309 | result = fill_padding(writer, |
| 1310 | n_re_total + n_im_total + 1 + add_parens * 2, |
| 1311 | format->fill_char=='\0' ? ' ' : format->fill_char, |
| 1312 | lpad, rpad); |
| 1313 | if (result == -1) |
| 1314 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1315 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1316 | if (add_parens) { |
| 1317 | PyUnicode_WRITE(rkind, rdata, writer->pos, '('); |
| 1318 | writer->pos++; |
| 1319 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1320 | |
| 1321 | if (!skip_re) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1322 | result = fill_number(writer, &re_spec, |
| 1323 | re_unicode_tmp, i_re, i_re + n_re_digits, |
| 1324 | NULL, 0, |
| 1325 | 0, |
| 1326 | &locale, 0); |
| 1327 | if (result == -1) |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1328 | goto done; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1329 | } |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1330 | result = fill_number(writer, &im_spec, |
| 1331 | im_unicode_tmp, i_im, i_im + n_im_digits, |
| 1332 | NULL, 0, |
| 1333 | 0, |
| 1334 | &locale, 0); |
| 1335 | if (result == -1) |
Victor Stinner | afbaa20 | 2011-09-28 21:50:16 +0200 | [diff] [blame] | 1336 | goto done; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1337 | PyUnicode_WRITE(rkind, rdata, writer->pos, 'j'); |
| 1338 | writer->pos++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1339 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1340 | if (add_parens) { |
| 1341 | PyUnicode_WRITE(rkind, rdata, writer->pos, ')'); |
| 1342 | writer->pos++; |
| 1343 | } |
| 1344 | |
| 1345 | writer->pos += rpad; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1346 | |
| 1347 | done: |
| 1348 | PyMem_Free(re_buf); |
| 1349 | PyMem_Free(im_buf); |
| 1350 | Py_XDECREF(re_unicode_tmp); |
| 1351 | Py_XDECREF(im_unicode_tmp); |
Victor Stinner | 41a863c | 2012-02-24 00:37:51 +0100 | [diff] [blame] | 1352 | free_locale_info(&locale); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1353 | return result; |
| 1354 | } |
| 1355 | |
| 1356 | /************************************************************************/ |
| 1357 | /*********** built in formatters ****************************************/ |
| 1358 | /************************************************************************/ |
doko@ubuntu.com | 39378f7 | 2012-06-21 12:12:20 +0200 | [diff] [blame] | 1359 | static int |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1360 | format_obj(PyObject *obj, _PyUnicodeWriter *writer) |
| 1361 | { |
| 1362 | PyObject *str; |
| 1363 | int err; |
| 1364 | |
| 1365 | str = PyObject_Str(obj); |
| 1366 | if (str == NULL) |
| 1367 | return -1; |
| 1368 | err = _PyUnicodeWriter_WriteStr(writer, str); |
| 1369 | Py_DECREF(str); |
| 1370 | return err; |
| 1371 | } |
| 1372 | |
| 1373 | int |
| 1374 | _PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer, |
| 1375 | PyObject *obj, |
| 1376 | PyObject *format_spec, |
| 1377 | Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1378 | { |
| 1379 | InternalFormatSpec format; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1380 | |
| 1381 | assert(PyUnicode_Check(obj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1382 | |
| 1383 | /* check for the special case of zero length format spec, make |
| 1384 | it equivalent to str(obj) */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1385 | if (start == end) { |
| 1386 | if (PyUnicode_CheckExact(obj)) |
| 1387 | return _PyUnicodeWriter_WriteStr(writer, obj); |
| 1388 | else |
| 1389 | return format_obj(obj, writer); |
| 1390 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1391 | |
| 1392 | /* parse the format_spec */ |
| 1393 | if (!parse_internal_render_format_spec(format_spec, start, end, |
| 1394 | &format, 's', '<')) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1395 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1396 | |
| 1397 | /* type conversion? */ |
| 1398 | switch (format.type) { |
| 1399 | case 's': |
| 1400 | /* no type conversion needed, already a string. do the formatting */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1401 | return format_string_internal(obj, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1402 | default: |
| 1403 | /* unknown */ |
| 1404 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1405 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1406 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1407 | } |
| 1408 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1409 | int |
| 1410 | _PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer, |
| 1411 | PyObject *obj, |
| 1412 | PyObject *format_spec, |
| 1413 | Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1414 | { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1415 | PyObject *tmp = NULL, *str = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1416 | InternalFormatSpec format; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1417 | int result = -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1418 | |
| 1419 | /* check for the special case of zero length format spec, make |
| 1420 | it equivalent to str(obj) */ |
| 1421 | if (start == end) { |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1422 | if (PyLong_CheckExact(obj)) |
| 1423 | return _PyLong_FormatWriter(writer, obj, 10, 0); |
| 1424 | else |
| 1425 | return format_obj(obj, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | /* parse the format_spec */ |
| 1429 | if (!parse_internal_render_format_spec(format_spec, start, end, |
| 1430 | &format, 'd', '>')) |
| 1431 | goto done; |
| 1432 | |
| 1433 | /* type conversion? */ |
| 1434 | switch (format.type) { |
| 1435 | case 'b': |
| 1436 | case 'c': |
| 1437 | case 'd': |
| 1438 | case 'o': |
| 1439 | case 'x': |
| 1440 | case 'X': |
| 1441 | case 'n': |
| 1442 | /* no type conversion needed, already an int (or long). do |
| 1443 | the formatting */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1444 | result = format_long_internal(obj, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1445 | break; |
| 1446 | |
| 1447 | case 'e': |
| 1448 | case 'E': |
| 1449 | case 'f': |
| 1450 | case 'F': |
| 1451 | case 'g': |
| 1452 | case 'G': |
| 1453 | case '%': |
| 1454 | /* convert to float */ |
| 1455 | tmp = PyNumber_Float(obj); |
| 1456 | if (tmp == NULL) |
| 1457 | goto done; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1458 | result = format_float_internal(tmp, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1459 | break; |
| 1460 | |
| 1461 | default: |
| 1462 | /* unknown */ |
| 1463 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
| 1464 | goto done; |
| 1465 | } |
| 1466 | |
| 1467 | done: |
| 1468 | Py_XDECREF(tmp); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1469 | Py_XDECREF(str); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1470 | return result; |
| 1471 | } |
| 1472 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1473 | int |
| 1474 | _PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer, |
| 1475 | PyObject *obj, |
| 1476 | PyObject *format_spec, |
| 1477 | Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1478 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1479 | InternalFormatSpec format; |
| 1480 | |
| 1481 | /* check for the special case of zero length format spec, make |
| 1482 | it equivalent to str(obj) */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1483 | if (start == end) |
| 1484 | return format_obj(obj, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1485 | |
| 1486 | /* parse the format_spec */ |
| 1487 | if (!parse_internal_render_format_spec(format_spec, start, end, |
| 1488 | &format, '\0', '>')) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1489 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1490 | |
| 1491 | /* type conversion? */ |
| 1492 | switch (format.type) { |
| 1493 | case '\0': /* No format code: like 'g', but with at least one decimal. */ |
| 1494 | case 'e': |
| 1495 | case 'E': |
| 1496 | case 'f': |
| 1497 | case 'F': |
| 1498 | case 'g': |
| 1499 | case 'G': |
| 1500 | case 'n': |
| 1501 | case '%': |
| 1502 | /* no conversion, already a float. do the formatting */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1503 | return format_float_internal(obj, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1504 | |
| 1505 | default: |
| 1506 | /* unknown */ |
| 1507 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1508 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1509 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1512 | int |
| 1513 | _PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer, |
| 1514 | PyObject *obj, |
| 1515 | PyObject *format_spec, |
| 1516 | Py_ssize_t start, Py_ssize_t end) |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1517 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1518 | InternalFormatSpec format; |
| 1519 | |
| 1520 | /* check for the special case of zero length format spec, make |
| 1521 | it equivalent to str(obj) */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1522 | if (start == end) |
| 1523 | return format_obj(obj, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1524 | |
| 1525 | /* parse the format_spec */ |
| 1526 | if (!parse_internal_render_format_spec(format_spec, start, end, |
| 1527 | &format, '\0', '>')) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1528 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1529 | |
| 1530 | /* type conversion? */ |
| 1531 | switch (format.type) { |
| 1532 | case '\0': /* No format code: like 'g', but with at least one decimal. */ |
| 1533 | case 'e': |
| 1534 | case 'E': |
| 1535 | case 'f': |
| 1536 | case 'F': |
| 1537 | case 'g': |
| 1538 | case 'G': |
| 1539 | case 'n': |
| 1540 | /* no conversion, already a complex. do the formatting */ |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1541 | return format_complex_internal(obj, &format, writer); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1542 | |
| 1543 | default: |
| 1544 | /* unknown */ |
| 1545 | unknown_presentation_type(format.type, obj->ob_type->tp_name); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 1546 | return -1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1547 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1548 | } |