Skip to content

Commit 51b9a0d

Browse files
committed
py/objstr: Make string formatting 8-bit clean.
1 parent 1d350b8 commit 51b9a0d

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

py/objstr.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
851851
if (*str == '}') {
852852
str++;
853853
if (str < top && *str == '}') {
854-
vstr_add_char(&vstr, '}');
854+
vstr_add_byte(&vstr, '}');
855855
continue;
856856
}
857857
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
862862
}
863863
}
864864
if (*str != '{') {
865-
vstr_add_char(&vstr, *str);
865+
vstr_add_byte(&vstr, *str);
866866
continue;
867867
}
868868

869869
str++;
870870
if (str < top && *str == '{') {
871-
vstr_add_char(&vstr, '{');
871+
vstr_add_byte(&vstr, '{');
872872
continue;
873873
}
874874

@@ -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
881881
if (str < top && *str != '}' && *str != '!' && *str != ':') {
882882
field_name = vstr_new();
883883
while (str < top && *str != '}' && *str != '!' && *str != ':') {
884-
vstr_add_char(field_name, *str++);
884+
vstr_add_byte(field_name, *str++);
885885
}
886886
}
887887

@@ -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
911911
if (*str != '}') {
912912
format_spec = vstr_new();
913913
while (str < top && *str != '}') {
914-
vstr_add_char(format_spec, *str++);
914+
vstr_add_byte(format_spec, *str++);
915915
}
916916
}
917917
}
@@ -1290,14 +1290,14 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, mp_uint_t n_args, const mp_o
12901290
for (const byte *top = str + len; str < top; str++) {
12911291
mp_obj_t arg = MP_OBJ_NULL;
12921292
if (*str != '%') {
1293-
vstr_add_char(&vstr, *str);
1293+
vstr_add_byte(&vstr, *str);
12941294
continue;
12951295
}
12961296
if (++str >= top) {
12971297
break;
12981298
}
12991299
if (*str == '%') {
1300-
vstr_add_char(&vstr, '%');
1300+
vstr_add_byte(&vstr, '%');
13011301
continue;
13021302
}
13031303

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# test handling of unicode chars in format strings
2+
3+
print('α'.format())
4+
print('{α}'.format(α=1))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# test handling of unicode chars in string % formatting
2+
3+
print('α' % ())

0 commit comments

Comments
 (0)