Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afbaa20

Browse files
author
Victor Stinner
committed
fill_char() can now propagate an error
1 parent 157f83f commit afbaa20

1 file changed

Lines changed: 47 additions & 25 deletions

File tree

Python/formatter_unicode.c

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ fill_padding(PyObject *s, Py_ssize_t start, Py_ssize_t nchars,
341341

342342
/* Pad on right. */
343343
if (n_rpadding)
344-
unicode_fill(s, start + nchars + n_lpadding,
344+
unicode_fill(s, start + nchars + n_lpadding,
345345
start + nchars + n_lpadding + n_rpadding, fill_char);
346346

347347
/* Pointer to the user content. */
@@ -501,7 +501,7 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
501501
spec->n_grouped_digits = 0;
502502
else
503503
spec->n_grouped_digits = _PyUnicode_InsertThousandsGrouping(
504-
PyUnicode_1BYTE_KIND, NULL, 0, NULL,
504+
PyUnicode_1BYTE_KIND, NULL, 0, NULL,
505505
spec->n_digits, spec->n_min_width,
506506
locale->grouping, locale->thousands_sep);
507507

@@ -541,11 +541,12 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
541541

542542
/* Fill in the digit parts of a numbers's string representation,
543543
as determined in calc_number_widths().
544-
No error checking, since we know the buffer is the correct size. */
545-
static void
544+
Return -1 on error, or 0 on success. */
545+
static int
546546
fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
547547
PyObject *digits, Py_ssize_t d_start, Py_ssize_t d_end,
548-
PyObject *prefix, Py_ssize_t p_start, Py_UCS4 fill_char,
548+
PyObject *prefix, Py_ssize_t p_start,
549+
Py_UCS4 fill_char,
549550
LocaleInfo *locale, int toupper)
550551
{
551552
/* Used to keep track of digits, decimal, and remainder. */
@@ -589,11 +590,8 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
589590
char *pdigits = PyUnicode_DATA(digits);
590591
if (PyUnicode_KIND(digits) < kind) {
591592
pdigits = _PyUnicode_AsKind(digits, kind);
592-
if (pdigits == NULL) {
593-
/* XXX report exception */
594-
Py_FatalError("out of memory");
595-
return;
596-
}
593+
if (pdigits == NULL)
594+
return -1;
597595
}
598596
#ifndef NDEBUG
599597
r =
@@ -640,6 +638,7 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
640638
unicode_fill(out, pos, pos + spec->n_rpadding, fill_char);
641639
pos += spec->n_rpadding;
642640
}
641+
return 0;
643642
}
644643

645644
static char no_grouping[1] = {CHAR_MAX};
@@ -765,6 +764,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
765764
Py_ssize_t prefix;
766765
NumberFieldWidths spec;
767766
long x;
767+
int err;
768768

769769
/* Locale settings, either from the actual locale or
770770
from a hard-code pseudo-locale */
@@ -886,10 +886,13 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
886886
goto done;
887887

888888
/* Populate the memory. */
889-
fill_number(result, 0, &spec, tmp, inumeric_chars, inumeric_chars + n_digits,
890-
tmp, prefix,
891-
format->fill_char == '\0' ? ' ' : format->fill_char,
892-
&locale, format->type == 'X');
889+
err = fill_number(result, 0, &spec,
890+
tmp, inumeric_chars, inumeric_chars + n_digits,
891+
tmp, prefix,
892+
format->fill_char == '\0' ? ' ' : format->fill_char,
893+
&locale, format->type == 'X');
894+
if (err)
895+
Py_CLEAR(result);
893896

894897
done:
895898
Py_XDECREF(tmp);
@@ -929,6 +932,7 @@ format_float_internal(PyObject *value,
929932
Py_UCS4 sign_char = '\0';
930933
int float_type; /* Used to see if we have a nan, inf, or regular float. */
931934
PyObject *unicode_tmp = NULL;
935+
int err;
932936

933937
/* Locale settings, either from the actual locale or
934938
from a hard-code pseudo-locale */
@@ -1010,7 +1014,7 @@ format_float_internal(PyObject *value,
10101014
&locale);
10111015

10121016
/* Calculate how much memory we'll need. */
1013-
n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
1017+
n_total = calc_number_widths(&spec, 0, sign_char, unicode_tmp, index,
10141018
index + n_digits, n_remainder, has_decimal,
10151019
&locale, format);
10161020

@@ -1020,10 +1024,13 @@ format_float_internal(PyObject *value,
10201024
goto done;
10211025

10221026
/* Populate the memory. */
1023-
fill_number(result, 0, &spec, unicode_tmp, index, index + n_digits,
1024-
NULL, 0,
1025-
format->fill_char == '\0' ? ' ' : format->fill_char, &locale,
1026-
0);
1027+
err = fill_number(result, 0, &spec,
1028+
unicode_tmp, index, index + n_digits,
1029+
NULL, 0,
1030+
format->fill_char == '\0' ? ' ' : format->fill_char,
1031+
&locale, 0);
1032+
if (err)
1033+
Py_CLEAR(result);
10271034

10281035
done:
10291036
PyMem_Free(buf);
@@ -1077,6 +1084,7 @@ format_complex_internal(PyObject *value,
10771084
Py_ssize_t total;
10781085
PyObject *re_unicode_tmp = NULL;
10791086
PyObject *im_unicode_tmp = NULL;
1087+
int err;
10801088

10811089
/* Locale settings, either from the actual locale or
10821090
from a hard-code pseudo-locale */
@@ -1170,9 +1178,9 @@ format_complex_internal(PyObject *value,
11701178

11711179
/* Determine if we have any "remainder" (after the digits, might include
11721180
decimal or exponent or both (or neither)) */
1173-
parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
1181+
parse_number(re_unicode_tmp, i_re, i_re + n_re_digits,
11741182
&n_re_remainder, &re_has_decimal);
1175-
parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
1183+
parse_number(im_unicode_tmp, i_im, i_im + n_im_digits,
11761184
&n_im_remainder, &im_has_decimal);
11771185

11781186
/* Determine the grouping, separator, and decimal point, if any. */
@@ -1225,12 +1233,26 @@ format_complex_internal(PyObject *value,
12251233
PyUnicode_WRITE(rkind, rdata, index++, '(');
12261234

12271235
if (!skip_re) {
1228-
fill_number(result, index, &re_spec, re_unicode_tmp,
1229-
i_re, i_re + n_re_digits, NULL, 0, 0, &locale, 0);
1236+
err = fill_number(result, index, &re_spec,
1237+
re_unicode_tmp, i_re, i_re + n_re_digits,
1238+
NULL, 0,
1239+
0,
1240+
&locale, 0);
1241+
if (err) {
1242+
Py_CLEAR(result);
1243+
goto done;
1244+
}
12301245
index += n_re_total;
12311246
}
1232-
fill_number(result, index, &im_spec, im_unicode_tmp,
1233-
i_im, i_im + n_im_digits, NULL, 0, 0, &locale, 0);
1247+
err = fill_number(result, index, &im_spec,
1248+
im_unicode_tmp, i_im, i_im + n_im_digits,
1249+
NULL, 0,
1250+
0,
1251+
&locale, 0);
1252+
if (err) {
1253+
Py_CLEAR(result);
1254+
goto done;
1255+
}
12341256
index += n_im_total;
12351257
PyUnicode_WRITE(rkind, rdata, index++, 'j');
12361258

0 commit comments

Comments
 (0)