Skip to content

Commit 11de839

Browse files
committed
py: Small changes to objstr.c, including a bug fix.
Some small fixed: - Combine 'x' and 'X' cases in str format code. - Remove trailing spaces from some lines. - Make exception messages consistently begin with lower case (then needed to change those in objarray and objtuple so the same constant string data could be used). - Fix bug with exception message having %c instead of %%c.
1 parent c074cd3 commit 11de839

3 files changed

Lines changed: 25 additions & 37 deletions

File tree

py/objarray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
181181
mp_bound_slice_t slice;
182182
if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
183183
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
184-
"Only slices with step=1 (aka None) are supported"));
184+
"only slices with step=1 (aka None) are supported"));
185185
}
186186
mp_obj_array_t *res = array_new(o->typecode, slice.stop - slice.start);
187187
int sz = mp_binary_get_size('@', o->typecode, NULL);

py/objstr.c

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
358358
mp_bound_slice_t slice;
359359
if (!mp_seq_get_fast_slice_indexes(self_len, index, &slice)) {
360360
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
361-
"Only slices with step=1 (aka None) are supported"));
361+
"only slices with step=1 (aka None) are supported"));
362362
}
363363
return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start);
364364
}
@@ -781,7 +781,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
781781
vstr_add_char(vstr, '}');
782782
continue;
783783
}
784-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Single '}' encountered in format string"));
784+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "single '}' encountered in format string"));
785785
}
786786
if (*str != '{') {
787787
vstr_add_char(vstr, *str);
@@ -827,7 +827,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
827827
// '{:d}'.format(True) returns '1'
828828
// So we treat {:} as {} and this later gets treated to be {!s}
829829
if (*str != '}') {
830-
format_spec = vstr_new();
830+
format_spec = vstr_new();
831831
while (str < top && *str != '}') {
832832
vstr_add_char(format_spec, *str++);
833833
}
@@ -845,7 +845,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
845845

846846
if (field_name) {
847847
if (arg_i > 0) {
848-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "cannot switch from automatic field numbering to manual field specification"));
848+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "can't switch from automatic field numbering to manual field specification"));
849849
}
850850
int index = 0;
851851
if (str_to_int(vstr_str(field_name), &index) != vstr_len(field_name) - 1) {
@@ -860,7 +860,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
860860
field_name = NULL;
861861
} else {
862862
if (arg_i < 0) {
863-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "cannot switch from manual field specification to automatic field numbering"));
863+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "can't switch from manual field specification to automatic field numbering"));
864864
}
865865
if (arg_i >= n_args - 1) {
866866
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range"));
@@ -878,7 +878,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
878878
} else if (conversion == 'r') {
879879
print_kind = PRINT_REPR;
880880
} else {
881-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Unknown conversion specifier %c", conversion));
881+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "unknown conversion specifier %c", conversion));
882882
}
883883
vstr_t *arg_vstr = vstr_new();
884884
mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, arg_vstr, arg, print_kind);
@@ -1005,12 +1005,9 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
10051005
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 8, 'a', flags, fill, width);
10061006
continue;
10071007

1008-
case 'x':
1009-
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, 'a', flags, fill, width);
1010-
continue;
1011-
10121008
case 'X':
1013-
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, 'A', flags, fill, width);
1009+
case 'x':
1010+
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, type - ('X' - 'A'), flags, fill, width);
10141011
continue;
10151012

10161013
case 'e':
@@ -1026,7 +1023,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
10261023

10271024
default:
10281025
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
1029-
"Unknown format code '%c' for object of type '%s'", type, mp_obj_get_type_str(arg)));
1026+
"unknown format code '%c' for object of type '%s'", type, mp_obj_get_type_str(arg)));
10301027
}
10311028
}
10321029

@@ -1038,7 +1035,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
10381035
// Even though the docs say that an unspecified type is the same
10391036
// as 'g', there is one subtle difference, when the exponent
10401037
// is one less than the precision.
1041-
//
1038+
//
10421039
// '{:10.1}'.format(0.0) ==> '0e+00'
10431040
// '{:10.1g}'.format(0.0) ==> '0'
10441041
//
@@ -1077,7 +1074,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
10771074
case 'F':
10781075
case 'g':
10791076
case 'G':
1080-
pfenv_print_float(&pfenv_vstr, mp_obj_get_float(arg), type, flags, fill, width, precision);
1077+
pfenv_print_float(&pfenv_vstr, mp_obj_get_float(arg), type, flags, fill, width, precision);
10811078
break;
10821079

10831080
case '%':
@@ -1088,7 +1085,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
10881085

10891086
default:
10901087
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
1091-
"Unknown format code '%c' for object of type 'float'",
1088+
"unknown format code '%c' for object of type 'float'",
10921089
type, mp_obj_get_type_str(arg)));
10931090
}
10941091
} else {
@@ -1119,7 +1116,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
11191116

11201117
default:
11211118
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
1122-
"Unknown format code '%c' for object of type 'str'",
1119+
"unknown format code '%c' for object of type 'str'",
11231120
type, mp_obj_get_type_str(arg)));
11241121
}
11251122
}
@@ -1171,20 +1168,20 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
11711168

11721169
int flags = 0;
11731170
char fill = ' ';
1174-
bool alt = false;
1171+
int alt = 0;
11751172
while (str < top) {
11761173
if (*str == '-') flags |= PF_FLAG_LEFT_ADJUST;
11771174
else if (*str == '+') flags |= PF_FLAG_SHOW_SIGN;
11781175
else if (*str == ' ') flags |= PF_FLAG_SPACE_SIGN;
1179-
else if (*str == '#') alt = true;
1176+
else if (*str == '#') alt = PF_FLAG_SHOW_PREFIX;
11801177
else if (*str == '0') {
11811178
flags |= PF_FLAG_PAD_AFTER_SIGN;
11821179
fill = '0';
11831180
} else break;
11841181
str++;
11851182
}
11861183
// parse width, if it exists
1187-
int width = 0;
1184+
int width = 0;
11881185
if (str < top) {
11891186
if (*str == '*') {
11901187
if (arg_i >= n_args) {
@@ -1234,7 +1231,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
12341231
uint len;
12351232
const char *s = mp_obj_str_get_data(arg, &len);
12361233
if (len != 1) {
1237-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "%c requires int or char"));
1234+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "%%c requires int or char"));
12381235
break;
12391236
}
12401237
pfenv_print_strn(&pfenv_vstr, s, 1, flags, ' ', width);
@@ -1248,12 +1245,12 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
12481245
#if MICROPY_PY_BUILTINS_FLOAT
12491246
// This is what CPython reports, so we report the same.
12501247
if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
1251-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "integer argument expected, got float"));
1248+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "integer argument expected, got float"));
12521249

12531250
}
12541251
#endif
1255-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "an integer is required"));
1256-
break;
1252+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "an integer is required"));
1253+
break;
12571254

12581255
case 'd':
12591256
case 'i':
@@ -1276,7 +1273,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
12761273
if (alt) {
12771274
flags |= (PF_FLAG_SHOW_PREFIX | PF_FLAG_SHOW_OCTAL_LETTER);
12781275
}
1279-
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width);
1276+
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width);
12801277
break;
12811278

12821279
case 'r':
@@ -1297,18 +1294,9 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
12971294
break;
12981295
}
12991296

1300-
case 'x':
1301-
if (alt) {
1302-
flags |= PF_FLAG_SHOW_PREFIX;
1303-
}
1304-
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'a', flags, fill, width);
1305-
break;
1306-
13071297
case 'X':
1308-
if (alt) {
1309-
flags |= PF_FLAG_SHOW_PREFIX;
1310-
}
1311-
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'A', flags, fill, width);
1298+
case 'x':
1299+
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, *str - ('X' - 'A'), flags | alt, fill, width);
13121300
break;
13131301

13141302
default:

py/objtuple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
166166
mp_bound_slice_t slice;
167167
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
168168
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
169-
"Only slices with step=1 (aka None) are supported"));
169+
"only slices with step=1 (aka None) are supported"));
170170
}
171171
mp_obj_tuple_t *res = mp_obj_new_tuple(slice.stop - slice.start, NULL);
172172
mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);

0 commit comments

Comments
 (0)