Commit 11de8399 authored by Damien George's avatar Damien George
Browse files

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.
parent c074cd38
...@@ -181,7 +181,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value ...@@ -181,7 +181,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
mp_bound_slice_t slice; mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) { if (!mp_seq_get_fast_slice_indexes(o->len, index_in, &slice)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"Only slices with step=1 (aka None) are supported")); "only slices with step=1 (aka None) are supported"));
} }
mp_obj_array_t *res = array_new(o->typecode, slice.stop - slice.start); mp_obj_array_t *res = array_new(o->typecode, slice.stop - slice.start);
int sz = mp_binary_get_size('@', o->typecode, NULL); int sz = mp_binary_get_size('@', o->typecode, NULL);
......
...@@ -358,7 +358,7 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { ...@@ -358,7 +358,7 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
mp_bound_slice_t slice; mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(self_len, index, &slice)) { if (!mp_seq_get_fast_slice_indexes(self_len, index, &slice)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"Only slices with step=1 (aka None) are supported")); "only slices with step=1 (aka None) are supported"));
} }
return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start); return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start);
} }
...@@ -781,7 +781,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { ...@@ -781,7 +781,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
vstr_add_char(vstr, '}'); vstr_add_char(vstr, '}');
continue; continue;
} }
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Single '}' encountered in format string")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "single '}' encountered in format string"));
} }
if (*str != '{') { if (*str != '{') {
vstr_add_char(vstr, *str); vstr_add_char(vstr, *str);
...@@ -827,7 +827,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { ...@@ -827,7 +827,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
// '{:d}'.format(True) returns '1' // '{:d}'.format(True) returns '1'
// So we treat {:} as {} and this later gets treated to be {!s} // So we treat {:} as {} and this later gets treated to be {!s}
if (*str != '}') { if (*str != '}') {
format_spec = vstr_new(); format_spec = vstr_new();
while (str < top && *str != '}') { while (str < top && *str != '}') {
vstr_add_char(format_spec, *str++); vstr_add_char(format_spec, *str++);
} }
...@@ -845,7 +845,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { ...@@ -845,7 +845,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
if (field_name) { if (field_name) {
if (arg_i > 0) { if (arg_i > 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "cannot switch from automatic field numbering to manual field specification")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "can't switch from automatic field numbering to manual field specification"));
} }
int index = 0; int index = 0;
if (str_to_int(vstr_str(field_name), &index) != vstr_len(field_name) - 1) { 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) { ...@@ -860,7 +860,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
field_name = NULL; field_name = NULL;
} else { } else {
if (arg_i < 0) { if (arg_i < 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "cannot switch from manual field specification to automatic field numbering")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "can't switch from manual field specification to automatic field numbering"));
} }
if (arg_i >= n_args - 1) { if (arg_i >= n_args - 1) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "tuple index out of range")); 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) { ...@@ -878,7 +878,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
} else if (conversion == 'r') { } else if (conversion == 'r') {
print_kind = PRINT_REPR; print_kind = PRINT_REPR;
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Unknown conversion specifier %c", conversion)); nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "unknown conversion specifier %c", conversion));
} }
vstr_t *arg_vstr = vstr_new(); vstr_t *arg_vstr = vstr_new();
mp_obj_print_helper((void (*)(void*, const char*, ...))vstr_printf, arg_vstr, arg, print_kind); 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) { ...@@ -1005,12 +1005,9 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 8, 'a', flags, fill, width); pfenv_print_mp_int(&pfenv_vstr, arg, 1, 8, 'a', flags, fill, width);
continue; continue;
case 'x':
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, 'a', flags, fill, width);
continue;
case 'X': case 'X':
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, 'A', flags, fill, width); case 'x':
pfenv_print_mp_int(&pfenv_vstr, arg, 1, 16, type - ('X' - 'A'), flags, fill, width);
continue; continue;
case 'e': case 'e':
...@@ -1026,7 +1023,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { ...@@ -1026,7 +1023,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
default: default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Unknown format code '%c' for object of type '%s'", type, mp_obj_get_type_str(arg))); "unknown format code '%c' for object of type '%s'", type, mp_obj_get_type_str(arg)));
} }
} }
...@@ -1038,7 +1035,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { ...@@ -1038,7 +1035,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
// Even though the docs say that an unspecified type is the same // Even though the docs say that an unspecified type is the same
// as 'g', there is one subtle difference, when the exponent // as 'g', there is one subtle difference, when the exponent
// is one less than the precision. // is one less than the precision.
// //
// '{:10.1}'.format(0.0) ==> '0e+00' // '{:10.1}'.format(0.0) ==> '0e+00'
// '{:10.1g}'.format(0.0) ==> '0' // '{:10.1g}'.format(0.0) ==> '0'
// //
...@@ -1077,7 +1074,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { ...@@ -1077,7 +1074,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
case 'F': case 'F':
case 'g': case 'g':
case 'G': case 'G':
pfenv_print_float(&pfenv_vstr, mp_obj_get_float(arg), type, flags, fill, width, precision); pfenv_print_float(&pfenv_vstr, mp_obj_get_float(arg), type, flags, fill, width, precision);
break; break;
case '%': case '%':
...@@ -1088,7 +1085,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { ...@@ -1088,7 +1085,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
default: default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Unknown format code '%c' for object of type 'float'", "unknown format code '%c' for object of type 'float'",
type, mp_obj_get_type_str(arg))); type, mp_obj_get_type_str(arg)));
} }
} else { } else {
...@@ -1119,7 +1116,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) { ...@@ -1119,7 +1116,7 @@ mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args) {
default: default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"Unknown format code '%c' for object of type 'str'", "unknown format code '%c' for object of type 'str'",
type, mp_obj_get_type_str(arg))); type, mp_obj_get_type_str(arg)));
} }
} }
...@@ -1171,12 +1168,12 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t ...@@ -1171,12 +1168,12 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
int flags = 0; int flags = 0;
char fill = ' '; char fill = ' ';
bool alt = false; int alt = 0;
while (str < top) { while (str < top) {
if (*str == '-') flags |= PF_FLAG_LEFT_ADJUST; if (*str == '-') flags |= PF_FLAG_LEFT_ADJUST;
else if (*str == '+') flags |= PF_FLAG_SHOW_SIGN; else if (*str == '+') flags |= PF_FLAG_SHOW_SIGN;
else if (*str == ' ') flags |= PF_FLAG_SPACE_SIGN; else if (*str == ' ') flags |= PF_FLAG_SPACE_SIGN;
else if (*str == '#') alt = true; else if (*str == '#') alt = PF_FLAG_SHOW_PREFIX;
else if (*str == '0') { else if (*str == '0') {
flags |= PF_FLAG_PAD_AFTER_SIGN; flags |= PF_FLAG_PAD_AFTER_SIGN;
fill = '0'; fill = '0';
...@@ -1184,7 +1181,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t ...@@ -1184,7 +1181,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t
str++; str++;
} }
// parse width, if it exists // parse width, if it exists
int width = 0; int width = 0;
if (str < top) { if (str < top) {
if (*str == '*') { if (*str == '*') {
if (arg_i >= n_args) { if (arg_i >= n_args) {
...@@ -1234,7 +1231,7 @@ not_enough_args: ...@@ -1234,7 +1231,7 @@ not_enough_args:
uint len; uint len;
const char *s = mp_obj_str_get_data(arg, &len); const char *s = mp_obj_str_get_data(arg, &len);
if (len != 1) { if (len != 1) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "%c requires int or char")); nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "%%c requires int or char"));
break; break;
} }
pfenv_print_strn(&pfenv_vstr, s, 1, flags, ' ', width); pfenv_print_strn(&pfenv_vstr, s, 1, flags, ' ', width);
...@@ -1248,12 +1245,12 @@ not_enough_args: ...@@ -1248,12 +1245,12 @@ not_enough_args:
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
// This is what CPython reports, so we report the same. // This is what CPython reports, so we report the same.
if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) { if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "integer argument expected, got float")); nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "integer argument expected, got float"));
} }
#endif #endif
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "an integer is required")); nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "an integer is required"));
break; break;
case 'd': case 'd':
case 'i': case 'i':
...@@ -1276,7 +1273,7 @@ not_enough_args: ...@@ -1276,7 +1273,7 @@ not_enough_args:
if (alt) { if (alt) {
flags |= (PF_FLAG_SHOW_PREFIX | PF_FLAG_SHOW_OCTAL_LETTER); flags |= (PF_FLAG_SHOW_PREFIX | PF_FLAG_SHOW_OCTAL_LETTER);
} }
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width); pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width);
break; break;
case 'r': case 'r':
...@@ -1297,18 +1294,9 @@ not_enough_args: ...@@ -1297,18 +1294,9 @@ not_enough_args:
break; break;
} }
case 'x':
if (alt) {
flags |= PF_FLAG_SHOW_PREFIX;
}
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'a', flags, fill, width);
break;
case 'X': case 'X':
if (alt) { case 'x':
flags |= PF_FLAG_SHOW_PREFIX; pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, *str - ('X' - 'A'), flags | alt, fill, width);
}
pfenv_print_mp_int(&pfenv_vstr, arg_as_int(arg), 1, 16, 'A', flags, fill, width);
break; break;
default: default:
......
...@@ -166,7 +166,7 @@ mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { ...@@ -166,7 +166,7 @@ mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
mp_bound_slice_t slice; mp_bound_slice_t slice;
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) { if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"Only slices with step=1 (aka None) are supported")); "only slices with step=1 (aka None) are supported"));
} }
mp_obj_tuple_t *res = mp_obj_new_tuple(slice.stop - slice.start, NULL); mp_obj_tuple_t *res = mp_obj_new_tuple(slice.stop - slice.start, NULL);
mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t); mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment