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

Skip to content

Commit ed27785

Browse files
author
Victor Stinner
committed
Issue #13706: Add assertions to detect bugs earlier
1 parent 5ea5b67 commit ed27785

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

Include/unicodeobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,17 @@ enum PyUnicode_Kind {
499499
do { \
500500
switch ((kind)) { \
501501
case PyUnicode_1BYTE_KIND: { \
502+
assert(value <= 0xff); \
502503
((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
503504
break; \
504505
} \
505506
case PyUnicode_2BYTE_KIND: { \
507+
assert(value <= 0xffff); \
506508
((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
507509
break; \
508510
} \
509511
default: { \
512+
assert(value <= 0x10ffff); \
510513
assert((kind) == PyUnicode_4BYTE_KIND); \
511514
((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
512515
} \

Python/formatter_unicode.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,9 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
559559
Py_ssize_t t;
560560
for (t = 0; t < spec->n_prefix; t++) {
561561
Py_UCS4 c = PyUnicode_READ(kind, data, pos + t);
562+
c = Py_TOUPPER(c);
562563
assert (c <= 127);
563-
PyUnicode_WRITE(kind, data, pos + t, Py_TOUPPER(c));
564+
PyUnicode_WRITE(kind, data, pos + t, c);
564565
}
565566
}
566567
pos += spec->n_prefix;
@@ -603,11 +604,12 @@ fill_number(PyObject *out, Py_ssize_t pos, const NumberFieldWidths *spec,
603604
Py_ssize_t t;
604605
for (t = 0; t < spec->n_grouped_digits; t++) {
605606
Py_UCS4 c = PyUnicode_READ(kind, data, pos + t);
607+
c = Py_TOUPPER(c);
606608
if (c > 127) {
607609
PyErr_SetString(PyExc_SystemError, "non-ascii grouped digit");
608610
return -1;
609611
}
610-
PyUnicode_WRITE(kind, data, pos + t, Py_TOUPPER(c));
612+
PyUnicode_WRITE(kind, data, pos + t, c);
611613
}
612614
}
613615
pos += spec->n_grouped_digits;
@@ -733,6 +735,7 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format)
733735
Py_CLEAR(result);
734736

735737
done:
738+
assert(!result || _PyUnicode_CheckConsistency(result, 1));
736739
return result;
737740
}
738741

@@ -759,7 +762,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
759762
produces non-digits */
760763
Py_ssize_t n_prefix = 0; /* Count of prefix chars, (e.g., '0x') */
761764
Py_ssize_t n_total;
762-
Py_ssize_t prefix;
765+
Py_ssize_t prefix = 0;
763766
NumberFieldWidths spec;
764767
long x;
765768
int err;
@@ -894,6 +897,7 @@ format_int_or_long_internal(PyObject *value, const InternalFormatSpec *format,
894897

895898
done:
896899
Py_XDECREF(tmp);
900+
assert(!result || _PyUnicode_CheckConsistency(result, 1));
897901
return result;
898902
}
899903

@@ -1036,6 +1040,7 @@ format_float_internal(PyObject *value,
10361040
done:
10371041
PyMem_Free(buf);
10381042
Py_DECREF(unicode_tmp);
1043+
assert(!result || _PyUnicode_CheckConsistency(result, 1));
10391044
return result;
10401045
}
10411046

@@ -1270,6 +1275,7 @@ format_complex_internal(PyObject *value,
12701275
PyMem_Free(im_buf);
12711276
Py_XDECREF(re_unicode_tmp);
12721277
Py_XDECREF(im_unicode_tmp);
1278+
assert(!result || _PyUnicode_CheckConsistency(result, 1));
12731279
return result;
12741280
}
12751281

0 commit comments

Comments
 (0)