From 51b9a0d0c4dfa265739708016ad85513d330987f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 26 Aug 2015 15:29:49 +0100 Subject: [PATCH] py/objstr: Make string formatting 8-bit clean. --- py/objstr.c | 14 +++++++------- tests/unicode/unicode_str_format.py | 4 ++++ tests/unicode/unicode_str_modulo.py | 3 +++ 3 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 tests/unicode/unicode_str_format.py create mode 100644 tests/unicode/unicode_str_modulo.py diff --git a/py/objstr.c b/py/objstr.c index 85b8fa3db3..b51636a6ca 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -851,7 +851,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa if (*str == '}') { str++; if (str < top && *str == '}') { - vstr_add_char(&vstr, '}'); + vstr_add_byte(&vstr, '}'); continue; } if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { @@ -862,13 +862,13 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa } } if (*str != '{') { - vstr_add_char(&vstr, *str); + vstr_add_byte(&vstr, *str); continue; } str++; if (str < top && *str == '{') { - vstr_add_char(&vstr, '{'); + vstr_add_byte(&vstr, '{'); continue; } @@ -881,7 +881,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa if (str < top && *str != '}' && *str != '!' && *str != ':') { field_name = vstr_new(); while (str < top && *str != '}' && *str != '!' && *str != ':') { - vstr_add_char(field_name, *str++); + vstr_add_byte(field_name, *str++); } } @@ -911,7 +911,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa if (*str != '}') { format_spec = vstr_new(); while (str < top && *str != '}') { - vstr_add_char(format_spec, *str++); + vstr_add_byte(format_spec, *str++); } } } @@ -1290,14 +1290,14 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o for (const byte *top = str + len; str < top; str++) { mp_obj_t arg = MP_OBJ_NULL; if (*str != '%') { - vstr_add_char(&vstr, *str); + vstr_add_byte(&vstr, *str); continue; } if (++str >= top) { break; } if (*str == '%') { - vstr_add_char(&vstr, '%'); + vstr_add_byte(&vstr, '%'); continue; } diff --git a/tests/unicode/unicode_str_format.py b/tests/unicode/unicode_str_format.py new file mode 100644 index 0000000000..bf8505a31a --- /dev/null +++ b/tests/unicode/unicode_str_format.py @@ -0,0 +1,4 @@ +# test handling of unicode chars in format strings + +print('α'.format()) +print('{α}'.format(α=1)) diff --git a/tests/unicode/unicode_str_modulo.py b/tests/unicode/unicode_str_modulo.py new file mode 100644 index 0000000000..e9b152473c --- /dev/null +++ b/tests/unicode/unicode_str_modulo.py @@ -0,0 +1,3 @@ +# test handling of unicode chars in string % formatting + +print('α' % ())