#include "Python.h"
#include "pycore_fileutils.h"
#include "pycore_long.h"
#include <locale.h>
static void
unknown_presentation_type(Py_UCS4 presentation_type,
const char* type_name)
{
if (presentation_type > 32 && presentation_type < 128)
PyErr_Format(PyExc_ValueError,
"Unknown format code '%c' "
"for object of type '%.200s'",
(char)presentation_type,
type_name);
else
PyErr_Format(PyExc_ValueError,
"Unknown format code '\\x%x' "
"for object of type '%.200s'",
(unsigned int)presentation_type,
type_name);
}
static void
invalid_thousands_separator_type(char specifier, Py_UCS4 presentation_type)
{
assert(specifier == ',' || specifier == '_');
if (presentation_type > 32 && presentation_type < 128)
PyErr_Format(PyExc_ValueError,
"Cannot specify '%c' with '%c'.",
specifier, (char)presentation_type);
else
PyErr_Format(PyExc_ValueError,
"Cannot specify '%c' with '\\x%x'.",
specifier, (unsigned int)presentation_type);
}
static void
invalid_comma_and_underscore(void)
{
PyErr_Format(PyExc_ValueError, "Cannot specify both ',' and '_'.");
}
static int
get_integer(PyObject *str, Py_ssize_t *ppos, Py_ssize_t end,
Py_ssize_t *result)
{
Py_ssize_t accumulator, digitval, pos = *ppos;
int numdigits;
int kind = PyUnicode_KIND(str);
const void *data = PyUnicode_DATA(str);
accumulator = numdigits = 0;
for (; pos < end; pos++, numdigits++) {
digitval = Py_UNICODE_TODECIMAL(PyUnicode_READ(kind, data, pos));
if (digitval < 0)
break;
if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
PyErr_Format(PyExc_ValueError,
"Too many decimal digits in format string");
*ppos = pos;
return -1;
}
accumulator = accumulator * 10 + digitval;
}
*ppos = pos;
*result = accumulator;
return numdigits;
}
Py_LOCAL_INLINE(int)
is_alignment_token(Py_UCS4 c)
{
switch (c) {
case '<': case '>': case '=': case '^':
return 1;
default:
return 0;
}
}
Py_LOCAL_INLINE(int)
is_sign_element(Py_UCS4 c)
{
switch (c) {
case ' ': case '+': case '-':
return 1;
default:
return 0;
}
}
enum LocaleType {
LT_NO_LOCALE = 0,
LT_DEFAULT_LOCALE = ',',
LT_UNDERSCORE_LOCALE = '_',
LT_UNDER_FOUR_LOCALE,
LT_CURRENT_LOCALE
};
typedef struct {
Py_UCS4 fill_char;
Py_UCS4 align;
int alternate;
int no_neg_0;
Py_UCS4 sign;
Py_ssize_t width;
enum LocaleType thousands_separators;
Py_ssize_t precision;
Py_UCS4 type;
} InternalFormatSpec;
static int
parse_internal_render_format_spec(PyObject *obj,
PyObject *format_spec,
Py_ssize_t start, Py_ssize_t end,
InternalFormatSpec *format,
char default_type,
char default_align)
{
Py_ssize_t pos = start;
int kind = PyUnicode_KIND(format_spec);
const void *data = PyUnicode_DATA(format_spec);
#define READ_spec(index) PyUnicode_READ(kind, data, index)
Py_ssize_t consumed;
int align_specified = 0;
int fill_char_specified = 0;
format->fill_char = ' ';
format->align = default_align;
format->alternate = 0;
format->no_neg_0 = 0;
format->sign = '\0';
format->width = -1;
format->thousands_separators = LT_NO_LOCALE;
format->precision = -1;
format->type = default_type;
if (end-pos >= 2 && is_alignment_token(READ_spec(pos+1))) {
format->align = READ_spec(pos+1);
format->fill_char = READ_spec(pos);
fill_char_specified = 1;
align_specified = 1;
pos += 2;
}
else if (end-pos >= 1 && is_alignment_token(READ_spec(pos))) {
format->align = READ_spec(pos);
align_specified = 1;
++pos;
}
if (end-pos >= 1 && is_sign_element(READ_spec(pos))) {
format->sign = READ_spec(pos);
++pos;
}
if (end-pos >= 1 && READ_spec(pos) == 'z') {
format->no_neg_0 = 1;
++pos;
}
if (end-pos >= 1 && READ_spec(pos) == '#') {
format->alternate = 1;
++pos;
}
if (!fill_char_specified && end-pos >= 1 && READ_spec(pos) == '0') {
format->fill_char = '0';
if (!align_specified && default_align == '>') {
format->align = '=';
}
++pos;
}
consumed = get_integer(format_spec, &pos, end, &format->width);
if (consumed == -1)
return 0;
if (consumed == 0)
format->width = -1;
if (end-pos && READ_spec(pos) == ',') {
format->thousands_separators = LT_DEFAULT_LOCALE;
++pos;
}
if (end-pos && READ_spec(pos) == '_') {
if (format->thousands_separators != LT_NO_LOCALE) {
invalid_comma_and_underscore();
return 0;
}
format->thousands_separators = LT_UNDERSCORE_LOCALE;
++pos;
}
if (end-pos && READ_spec(pos) == ',') {
if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
invalid_comma_and_underscore();
return 0;
}
}
if (end-pos && READ_spec(pos) == '.') {
++pos;
consumed = get_integer(format_spec, &pos, end, &format->precision);
if (consumed == -1)
return 0;
if (consumed == 0) {
PyErr_Format(PyExc_ValueError,
"Format specifier missing precision");
return 0;
}
}
if (end-pos > 1) {
PyObject* actual_format_spec = PyUnicode_FromKindAndData(kind,
(char*)data + kind*start,
end-start);
if (actual_format_spec != NULL) {
PyErr_Format(PyExc_ValueError,
"Invalid format specifier '%U' for object of type '%.200s'",
actual_format_spec, Py_TYPE(obj)->tp_name);
Py_DECREF(actual_format_spec);
}
return 0;
}
if (end-pos == 1) {
format->type = READ_spec(pos);
++pos;
}
if (format->thousands_separators) {
switch (format->type) {
case 'd':
case 'e':
case 'f':
case 'g':
case 'E':
case 'G':
case '%':
case 'F':
case '\0':
break;
case 'b':
case 'o':
case 'x':
case 'X':
if (format->thousands_separators == LT_UNDERSCORE_LOCALE) {
format->thousands_separators = LT_UNDER_FOUR_LOCALE;
break;
}
default:
invalid_thousands_separator_type(format->thousands_separators, format->type);
return 0;
}
}
assert (format->align <= 127);
assert (format->sign <= 127);
return 1;
}
static void
calc_padding(Py_ssize_t nchars, Py_ssize_t width, Py_UCS4 align,
Py_ssize_t *n_lpadding, Py_ssize_t *n_rpadding,
Py_ssize_t *n_total)
{
if (width >= 0) {
if (nchars > width)
*n_total = nchars;
else
*n_total = width;
}
else {
*n_total = nchars;
}
if (align == '>')
*n_lpadding = *n_total - nchars;
else if (align == '^')
*n_lpadding = (*n_total - nchars) / 2;
else if (align == '<' || align == '=')
*n_lpadding = 0;
else {
Py_UNREACHABLE();
}
*n_rpadding = *n_total - nchars - *n_lpadding;
}
static int
fill_padding(_PyUnicodeWriter *writer,
Py_ssize_t nchars,
Py_UCS4 fill_char, Py_ssize_t n_lpadding,
Py_ssize_t n_rpadding)
{
Py_ssize_t pos;
if (n_lpadding) {
pos = writer->pos;
_PyUnicode_FastFill(writer->buffer, pos, n_lpadding, fill_char);
}
if (n_rpadding) {
pos = writer->pos + nchars + n_lpadding;
_PyUnicode_FastFill(writer->buffer, pos, n_rpadding, fill_char);
}
writer->pos += n_lpadding;
return 0;
}
typedef struct {
PyObject *decimal_point;
PyObject *thousands_sep;
const char *grouping;
char *grouping_buffer;
} LocaleInfo;
#define LocaleInfo_STATIC_INIT {0, 0, 0, 0}
typedef struct {
Py_ssize_t n_lpadding;
Py_ssize_t n_prefix;
Py_ssize_t n_spadding;
Py_ssize_t n_rpadding;
char sign;
Py_ssize_t n_sign;
Py_ssize_t n_grouped_digits;
Py_ssize_t n_decimal;
Py_ssize_t n_remainder;
Py_ssize_t n_digits;
Py_ssize_t n_min_width;
} NumberFieldWidths;
static void
parse_number(PyObject *s, Py_ssize_t pos, Py_ssize_t end,
Py_ssize_t *n_remainder, int *has_decimal)
{
Py_ssize_t remainder;
int kind = PyUnicode_KIND(s);
const void *data = PyUnicode_DATA(s);
while (pos<end && Py_ISDIGIT(PyUnicode_READ(kind, data, pos)))
++pos;
remainder = pos;
*has_decimal = pos<end && PyUnicode_READ(kind, data, remainder) == '.';
if (*has_decimal)
remainder++;
*n_remainder = end - remainder;
}
static Py_ssize_t
calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
Py_UCS4 sign_char, Py_ssize_t n_start,
Py_ssize_t n_end, Py_ssize_t n_remainder,
int has_decimal, const LocaleInfo *locale,
const InternalFormatSpec *format, Py_UCS4 *maxchar)
{
Py_ssize_t n_non_digit_non_padding;
Py_ssize_t n_padding;
spec->n_digits = n_end - n_start - n_remainder - (has_decimal?1:0);
spec->n_lpadding = 0;
spec->n_prefix = n_prefix;
spec->n_decimal = has_decimal ? PyUnicode_GET_LENGTH(locale->decimal_point) : 0;
spec->n_remainder = n_remainder;
spec->n_spadding = 0;
spec->n_rpadding = 0;
spec->sign = '\0';
spec->n_sign = 0;
switch (format->sign) {
case '+':
spec->n_sign = 1;
spec->sign = (sign_char == '-' ? '-' : '+');
break;
case ' ':
spec->n_sign = 1;
spec->sign = (sign_char == '-' ? '-' : ' ');
break;
default:
if (sign_char == '-') {
spec->n_sign = 1;
spec->sign = '-';
}
}
n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
spec->n_remainder;
if (format->fill_char == '0' && format->align == '=')
spec->n_min_width = format->width - n_non_digit_non_padding;
else
spec->n_min_width = 0;
if (spec->n_digits == 0)
spec->n_grouped_digits = 0;
else {
Py_UCS4 grouping_maxchar;
spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
NULL, 0,
NULL, 0, spec->n_digits,
spec->n_min_width,
locale->grouping, locale->thousands_sep, &grouping_maxchar);
if (spec->n_grouped_digits == -1) {
return -1;
}
*maxchar = Py_MAX(*maxchar, grouping_maxchar);
}
n_padding = format->width -
(n_non_digit_non_padding + spec->n_grouped_digits);
if (n_padding > 0) {
switch (format->align) {
case '<':
spec->n_rpadding = n_padding;
break;
case '^':
spec->n_lpadding = n_padding / 2;
spec->n_rpadding = n_padding - spec->n_lpadding;
break;
case '=':
spec->n_spadding = n_padding;
break;
case '>':
spec->n_lpadding = n_padding;
break;
default:
Py_UNREACHABLE();
}
}
if (spec->n_lpadding || spec->n_spadding || spec->n_rpadding)
*maxchar = Py_MAX(*maxchar, format->fill_char);
if (spec->n_decimal)
*maxchar = Py_MAX(*maxchar, PyUnicode_MAX_CHAR_VALUE(locale->decimal_point));
return spec->n_lpadding + spec->n_sign + spec->n_prefix +
spec->n_spadding + spec->n_grouped_digits + spec->n_decimal +
spec->n_remainder + spec->n_rpadding;
}
static int
fill_number(_PyUnicodeWriter *writer, const NumberFieldWidths *spec,
PyObject *digits, Py_ssize_t d_start,
PyObject *prefix, Py_ssize_t p_start,
Py_UCS4 fill_char,
LocaleInfo *locale, int toupper)
{
Py_ssize_t d_pos = d_start;
const int kind = writer->kind;
const void *data = writer->data;
Py_ssize_t r;
if (spec->n_lpadding) {
_PyUnicode_FastFill(writer->buffer,
writer->pos, spec->n_lpadding, fill_char);
writer->pos += spec->n_lpadding;
}
if (spec->n_sign == 1) {
PyUnicode_WRITE(kind, data, writer->pos, spec->sign);
writer->pos++;
}
if (spec->n_prefix) {
_PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
prefix, p_start,
spec->n_prefix);
if (toupper) {
Py_ssize_t t;
for (t = 0; t < spec->n_prefix; t++) {
Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
c = Py_TOUPPER(c);
assert (c <= 127);
PyUnicode_WRITE(kind, data, writer->pos + t, c);
}
}
writer->pos += spec->n_prefix;
}
if (spec->n_spadding) {
_PyUnicode_FastFill(writer->buffer,
writer->pos, spec->n_spadding, fill_char);
writer->pos += spec->n_spadding;
}
if (spec->n_digits != 0) {
r = _PyUnicode_InsertThousandsGrouping(
writer, spec->n_grouped_digits,
digits, d_pos, spec->n_digits,
spec->n_min_width,
locale->grouping, locale->thousands_sep, NULL);
if (r == -1)
return -1;
assert(r == spec->n_grouped_digits);
d_pos += spec->n_digits;
}
if (toupper) {
Py_ssize_t t;
for (t = 0; t < spec->n_grouped_digits; t++) {
Py_UCS4 c = PyUnicode_READ(kind, data, writer->pos + t);
c = Py_TOUPPER(c);
if (c > 127) {
PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
return -1;
}
PyUnicode_WRITE(kind, data, writer->pos + t, c);
}
}
writer->pos += spec->n_grouped_digits;
if (spec->n_decimal) {
_PyUnicode_FastCopyCharacters(
writer->buffer, writer->pos,
locale->decimal_point, 0, spec->n_decimal);
writer->pos += spec->n_decimal;
d_pos += 1;
}
if (spec->n_remainder) {
_PyUnicode_FastCopyCharacters(
writer->buffer, writer->pos,
digits, d_pos, spec->n_remainder);
writer->pos += spec->n_remainder;
}
if (spec->n_rpadding) {
_PyUnicode_FastFill(writer->buffer,
writer->pos, spec->n_rpadding,
fill_char);
writer->pos += spec->n_rpadding;
}
return 0;
}
static const char no_grouping[1] = {CHAR_MAX};
static int
get_locale_info(enum LocaleType type, LocaleInfo *locale_info)
{
switch (type) {
case LT_CURRENT_LOCALE: {
struct lconv *lc = localeconv();
if (_Py_GetLocaleconvNumeric(lc,
&locale_info->decimal_point,
&locale_info->thousands_sep) < 0) {
return -1;
}
locale_info->grouping_buffer = _PyMem_Strdup(lc->grouping);
if (locale_info->grouping_buffer == NULL) {
PyErr_NoMemory();
return -1;
}
locale_info->grouping = locale_info->grouping_buffer;
break;
}
case LT_DEFAULT_LOCALE:
case LT_UNDERSCORE_LOCALE:
case LT_UNDER_FOUR_LOCALE:
locale_info->decimal_point = PyUnicode_FromOrdinal('.');
locale_info->thousands_sep = PyUnicode_FromOrdinal(
type == LT_DEFAULT_LOCALE ? ',' : '_');
if (!locale_info->decimal_point || !locale_info->thousands_sep)
return -1;
if (type != LT_UNDER_FOUR_LOCALE)
locale_info->grouping = "\3";
else
locale_info->grouping = "\4";
break;
case LT_NO_LOCALE:
locale_info->decimal_point = PyUnicode_FromOrdinal('.');
locale_info->thousands_sep = PyUnicode_New(0, 0);
if (!locale_info->decimal_point || !locale_info->thousands_sep)
return -1;
locale_info->grouping = no_grouping;
break;
}
return 0;
}
static void
free_locale_info(LocaleInfo *locale_info)
{
Py_XDECREF(locale_info->decimal_point);
Py_XDECREF(locale_info->thousands_sep);
PyMem_Free(locale_info->grouping_buffer);
}
static int
format_string_internal(PyObject *value, const InternalFormatSpec *format,
_PyUnicodeWriter *writer)
{
Py_ssize_t lpad;
Py_ssize_t rpad;
Py_ssize_t total;
Py_ssize_t len;
int result = -1;
Py_UCS4 maxchar;
assert(PyUnicode_IS_READY(value));
len = PyUnicode_GET_LENGTH(value);
if (format->sign != '\0') {
if (format->sign == ' ') {
PyErr_SetString(PyExc_ValueError,
"Space not allowed in string format specifier");
}
else {
PyErr_SetString(PyExc_ValueError,
"Sign not allowed in string format specifier");
}
goto done;
}
if (format->no_neg_0) {
PyErr_SetString(PyExc_ValueError,
"Negative zero coercion (z) not allowed in string format "
"specifier");
goto done;
}
if (format->alternate) {
PyErr_SetString(PyExc_ValueError,
"Alternate form (#) not allowed in string format "
"specifier");
goto done;
}
if (format->align == '=') {
PyErr_SetString(PyExc_ValueError,
"'=' alignment not allowed "
"in string format specifier");
goto done;
}
if ((format->width == -1 || format->width <= len)
&& (format->precision == -1 || format->precision >= len)) {
return _PyUnicodeWriter_WriteStr(writer, value);
}
if (format->precision >= 0 && len >= format->precision) {
len = format->precision;
}
calc_padding(len, format->width, format->align, &lpad, &rpad, &total);
maxchar = writer->maxchar;
if (lpad != 0 || rpad != 0)
maxchar = Py_MAX(maxchar, format->fill_char);
if (PyUnicode_MAX_CHAR_VALUE(value) > maxchar) {
Py_UCS4 valmaxchar = _PyUnicode_FindMaxChar(value, 0, len);
maxchar = Py_MAX(maxchar, valmaxchar);
}
if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
goto done;
result = fill_padding(writer, len, format->fill_char, lpad, rpad);
if (result == -1)
goto done;
if (len) {
_PyUnicode_FastCopyCharacters(writer->buffer, writer->pos,
value, 0, len);
}
writer->pos += (len + rpad);
result = 0;
done:
return result;
}
static int
format_long_internal(PyObject *value, const InternalFormatSpec *format,
_PyUnicodeWriter *writer)
{
int result = -1;
Py_UCS4 maxchar = 127;
PyObject *tmp = NULL;
Py_ssize_t inumeric_chars;
Py_UCS4 sign_char = '\0';
Py_ssize_t n_digits;
Py_ssize_t n_remainder = 0;
Py_ssize_t n_prefix = 0;
Py_ssize_t n_total;
Py_ssize_t prefix = 0;
NumberFieldWidths spec;
long x;
LocaleInfo locale = LocaleInfo_STATIC_INIT;
if (format->precision != -1) {
PyErr_SetString(PyExc_ValueError,
"Precision not allowed in integer format specifier");
goto done;
}
if (format->no_neg_0) {
PyErr_SetString(PyExc_ValueError,
"Negative zero coercion (z) not allowed in integer"
" format specifier");
goto done;
}
if (format->type == 'c') {
if (format->sign != '\0') {
PyErr_SetString(PyExc_ValueError,
"Sign not allowed with integer"
" format specifier 'c'");
goto done;
}
if (format->alternate) {
PyErr_SetString(PyExc_ValueError,
"Alternate form (#) not allowed with integer"
" format specifier 'c'");
goto done;
}
x = PyLong_AsLong(value);
if (x == -1 && PyErr_Occurred())
goto done;
if (x < 0 || x > 0x10ffff) {
PyErr_SetString(PyExc_OverflowError,
"%c arg not in range(0x110000)");
goto done;
}
tmp = PyUnicode_FromOrdinal(x);
inumeric_chars = 0;
n_digits = 1;
maxchar = Py_MAX(maxchar, (Py_UCS4)x);
n_remainder = 1;
}
else {
int base;
int leading_chars_to_skip = 0;
switch (format->type) {
case 'b':
base = 2;
leading_chars_to_skip = 2;
break;
case 'o':
base = 8;
leading_chars_to_skip = 2;
break;
case 'x':
case 'X':
base = 16;
leading_chars_to_skip = 2;
break;
default:
case 'd':
case 'n':
base = 10;
break;
}
if (format->sign != '+' && format->sign != ' '
&& format->width == -1
&& format->type != 'X' && format->type != 'n'
&& !format->thousands_separators
&& PyLong_CheckExact(value))
{
return _PyLong_FormatWriter(writer, value, base, format->alternate);
}
if (format->alternate)
n_prefix = leading_chars_to_skip;
tmp = _PyLong_Format(value, base);
if (tmp == NULL)
goto done;
inumeric_chars = 0;
n_digits = PyUnicode_GET_LENGTH(tmp);
prefix = inumeric_chars;
if (PyUnicode_READ_CHAR(tmp, inumeric_chars) == '-') {
sign_char = '-';
++prefix;
++leading_chars_to_skip;
}
n_digits -= leading_chars_to_skip;
inumeric_chars += leading_chars_to_skip;
}
if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
format->thousands_separators,
&locale) == -1)
goto done;
n_total = calc_number_widths(&spec, n_prefix, sign_char, inumeric_chars,
inumeric_chars + n_digits, n_remainder, 0,
&locale, format, &maxchar);
if (n_total == -1) {
goto done;
}
if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
goto done;
result = fill_number(writer, &spec,
tmp, inumeric_chars,
tmp, prefix, format->fill_char,
&locale, format->type == 'X');
done:
Py_XDECREF(tmp);
free_locale_info(&locale);
return result;
}
static int
format_float_internal(PyObject *value,
const InternalFormatSpec *format,
_PyUnicodeWriter *writer)
{
char *buf = NULL;
Py_ssize_t n_digits;
Py_ssize_t n_remainder;
Py_ssize_t n_total;
int has_decimal;
double val;
int precision, default_precision = 6;
Py_UCS4 type = format->type;
int add_pct = 0;
Py_ssize_t index;
NumberFieldWidths spec;
int flags = 0;
int result = -1;
Py_UCS4 maxchar = 127;
Py_UCS4 sign_char = '\0';
int float_type;
PyObject *unicode_tmp = NULL;
LocaleInfo locale = LocaleInfo_STATIC_INIT;
if (format->precision > INT_MAX) {
PyErr_SetString(PyExc_ValueError, "precision too big");
goto done;
}
precision = (int)format->precision;
if (format->alternate)
flags |= Py_DTSF_ALT;
if (format->no_neg_0)
flags |= Py_DTSF_NO_NEG_0;
if (type == '\0') {
flags |= Py_DTSF_ADD_DOT_0;
type = 'r';
default_precision = 0;
}
if (type == 'n')
type = 'g';
val = PyFloat_AsDouble(value);
if (val == -1.0 && PyErr_Occurred())
goto done;
if (type == '%') {
type = 'f';
val *= 100;
add_pct = 1;
}
if (precision < 0)
precision = default_precision;
else if (type == 'r')
type = 'g';
buf = PyOS_double_to_string(val, (char)type, precision, flags,
&float_type);
if (buf == NULL)
goto done;
n_digits = strlen(buf);
if (add_pct) {
buf[n_digits] = '%';
n_digits += 1;
}
if (format->sign != '+' && format->sign != ' '
&& format->width == -1
&& format->type != 'n'
&& !format->thousands_separators)
{
result = _PyUnicodeWriter_WriteASCIIString(writer, buf, n_digits);
PyMem_Free(buf);
return result;
}
unicode_tmp = _PyUnicode_FromASCII(buf, n_digits);
PyMem_Free(buf);
if (unicode_tmp == NULL)
goto done;
index = 0;
if (PyUnicode_READ_CHAR(unicode_tmp, index) == '-') {
sign_char = '-';
++index;
--n_digits;
}
parse_number(unicode_tmp, index, index + n_digits, &n_remainder, &has_decimal);
if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
format->thousands_separators,
&locale) == -1)
goto done;
n_total = calc_number_widths(&spec, 0, sign_char, index,
index + n_digits, n_remainder, has_decimal,
&locale, format, &maxchar);
if (n_total == -1) {
goto done;
}
if (_PyUnicodeWriter_Prepare(writer, n_total, maxchar) == -1)
goto done;
result = fill_number(writer, &spec,
unicode_tmp, index,
NULL, 0, format->fill_char,
&locale, 0);
done:
Py_XDECREF(unicode_tmp);
free_locale_info(&locale);
return result;
}
static int
format_complex_internal(PyObject *value,
const InternalFormatSpec *format,
_PyUnicodeWriter *writer)
{
double re;
double im;
char *re_buf = NULL;
char *im_buf = NULL;
InternalFormatSpec tmp_format = *format;
Py_ssize_t n_re_digits;
Py_ssize_t n_im_digits;
Py_ssize_t n_re_remainder;
Py_ssize_t n_im_remainder;
Py_ssize_t n_re_total;
Py_ssize_t n_im_total;
int re_has_decimal;
int im_has_decimal;
int precision, default_precision = 6;
Py_UCS4 type = format->type;
Py_ssize_t i_re;
Py_ssize_t i_im;
NumberFieldWidths re_spec;
NumberFieldWidths im_spec;
int flags = 0;
int result = -1;
Py_UCS4 maxchar = 127;
int rkind;
void *rdata;
Py_UCS4 re_sign_char = '\0';
Py_UCS4 im_sign_char = '\0';
int re_float_type;
int im_float_type;
int add_parens = 0;
int skip_re = 0;
Py_ssize_t lpad;
Py_ssize_t rpad;
Py_ssize_t total;
PyObject *re_unicode_tmp = NULL;
PyObject *im_unicode_tmp = NULL;
LocaleInfo locale = LocaleInfo_STATIC_INIT;
if (format->precision > INT_MAX) {
PyErr_SetString(PyExc_ValueError, "precision too big");
goto done;
}
precision = (int)format->precision;
if (format->fill_char == '0') {
PyErr_SetString(PyExc_ValueError,
"Zero padding is not allowed in complex format "
"specifier");
goto done;
}
if (format->align == '=') {
PyErr_SetString(PyExc_ValueError,
"'=' alignment flag is not allowed in complex format "
"specifier");
goto done;
}
re = PyComplex_RealAsDouble(value);
if (re == -1.0 && PyErr_Occurred())
goto done;
im = PyComplex_ImagAsDouble(value);
if (im == -1.0 && PyErr_Occurred())
goto done;
if (format->alternate)
flags |= Py_DTSF_ALT;
if (format->no_neg_0)
flags |= Py_DTSF_NO_NEG_0;
if (type == '\0') {
type = 'r';
default_precision = 0;
if (re == 0.0 && copysign(1.0, re) == 1.0)
skip_re = 1;
else
add_parens = 1;
}
if (type == 'n')
type = 'g';
if (precision < 0)
precision = default_precision;
else if (type == 'r')
type = 'g';
re_buf = PyOS_double_to_string(re, (char)type, precision, flags,
&re_float_type);
if (re_buf == NULL)
goto done;
im_buf = PyOS_double_to_string(im, (char)type, precision, flags,
&im_float_type);
if (im_buf == NULL)
goto done;
n_re_digits = strlen(re_buf);
n_im_digits = strlen(im_buf);
re_unicode_tmp = _PyUnicode_FromASCII(re_buf, n_re_digits);
if (re_unicode_tmp == NULL)
goto done;
i_re = 0;
im_unicode_tmp = _PyUnicode_FromASCII(im_buf, n_im_digits);
if (im_unicode_tmp == NULL)
goto done;
i_im = 0;
if (PyUnicode_READ_CHAR(re_unicode_tmp, i_re) == '-') {
re_sign_char = '-';
++i_re;
--n_re_digits;
}
if (PyUnicode_READ_CHAR(im_unicode_tmp, i_im) == '-') {
im_sign_char = '-';
++i_im;
--n_im_digits;
}
parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
&n_re_remainder, &re_has_decimal);
parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
&n_im_remainder, &im_has_decimal);
if (get_locale_info(format->type == 'n' ? LT_CURRENT_LOCALE :
format->thousands_separators,
&locale) == -1)
goto done;
tmp_format.fill_char = '\0';
tmp_format.align = '<';
tmp_format.width = -1;
n_re_total = calc_number_widths(&re_spec, 0, re_sign_char,
i_re, i_re + n_re_digits, n_re_remainder,
re_has_decimal, &locale, &tmp_format,
&maxchar);
if (n_re_total == -1) {
goto done;
}
if (!skip_re)
tmp_format.sign = '+';
n_im_total = calc_number_widths(&im_spec, 0, im_sign_char,
i_im, i_im + n_im_digits, n_im_remainder,
im_has_decimal, &locale, &tmp_format,
&maxchar);
if (n_im_total == -1) {
goto done;
}
if (skip_re)
n_re_total = 0;
calc_padding(n_re_total + n_im_total + 1 + add_parens * 2,
format->width, format->align, &lpad, &rpad, &total);
if (lpad || rpad)
maxchar = Py_MAX(maxchar, format->fill_char);
if (_PyUnicodeWriter_Prepare(writer, total, maxchar) == -1)
goto done;
rkind = writer->kind;
rdata = writer->data;
result = fill_padding(writer,
n_re_total + n_im_total + 1 + add_parens * 2,
format->fill_char, lpad, rpad);
if (result == -1)
goto done;
if (add_parens) {
PyUnicode_WRITE(rkind, rdata, writer->pos, '(');
writer->pos++;
}
if (!skip_re) {
result = fill_number(writer, &re_spec,
re_unicode_tmp, i_re,
NULL, 0,
0,
&locale, 0);
if (result == -1)
goto done;
}
result = fill_number(writer, &im_spec,
im_unicode_tmp, i_im,
NULL, 0,
0,
&locale, 0);
if (result == -1)
goto done;
PyUnicode_WRITE(rkind, rdata, writer->pos, 'j');
writer->pos++;
if (add_parens) {
PyUnicode_WRITE(rkind, rdata, writer->pos, ')');
writer->pos++;
}
writer->pos += rpad;
done:
PyMem_Free(re_buf);
PyMem_Free(im_buf);
Py_XDECREF(re_unicode_tmp);
Py_XDECREF(im_unicode_tmp);
free_locale_info(&locale);
return result;
}
static int
format_obj(PyObject *obj, _PyUnicodeWriter *writer)
{
PyObject *str;
int err;
str = PyObject_Str(obj);
if (str == NULL)
return -1;
err = _PyUnicodeWriter_WriteStr(writer, str);
Py_DECREF(str);
return err;
}
int
_PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
PyObject *obj,
PyObject *format_spec,
Py_ssize_t start, Py_ssize_t end)
{
InternalFormatSpec format;
assert(PyUnicode_Check(obj));
if (start == end) {
if (PyUnicode_CheckExact(obj))
return _PyUnicodeWriter_WriteStr(writer, obj);
else
return format_obj(obj, writer);
}
if (!parse_internal_render_format_spec(obj, format_spec, start, end,
&format, 's', '<'))
return -1;
switch (format.type) {
case 's':
return format_string_internal(obj, &format, writer);
default:
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
return -1;
}
}
int
_PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
PyObject *obj,
PyObject *format_spec,
Py_ssize_t start, Py_ssize_t end)
{
PyObject *tmp = NULL;
InternalFormatSpec format;
int result = -1;
if (start == end) {
if (PyLong_CheckExact(obj))
return _PyLong_FormatWriter(writer, obj, 10, 0);
else
return format_obj(obj, writer);
}
if (!parse_internal_render_format_spec(obj, format_spec, start, end,
&format, 'd', '>'))
goto done;
switch (format.type) {
case 'b':
case 'c':
case 'd':
case 'o':
case 'x':
case 'X':
case 'n':
result = format_long_internal(obj, &format, writer);
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case '%':
tmp = PyNumber_Float(obj);
if (tmp == NULL)
goto done;
result = format_float_internal(tmp, &format, writer);
break;
default:
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
goto done;
}
done:
Py_XDECREF(tmp);
return result;
}
int
_PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
PyObject *obj,
PyObject *format_spec,
Py_ssize_t start, Py_ssize_t end)
{
InternalFormatSpec format;
if (start == end)
return format_obj(obj, writer);
if (!parse_internal_render_format_spec(obj, format_spec, start, end,
&format, '\0', '>'))
return -1;
switch (format.type) {
case '\0':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'n':
case '%':
return format_float_internal(obj, &format, writer);
default:
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
return -1;
}
}
int
_PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
PyObject *obj,
PyObject *format_spec,
Py_ssize_t start, Py_ssize_t end)
{
InternalFormatSpec format;
if (start == end)
return format_obj(obj, writer);
if (!parse_internal_render_format_spec(obj, format_spec, start, end,
&format, '\0', '>'))
return -1;
switch (format.type) {
case '\0':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'n':
return format_complex_internal(obj, &format, writer);
default:
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
return -1;
}
}