mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 16:57:31 +01:00
[python] Upgrade to micropython 1.11
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013, 2014 Damien P. George
|
||||
* Copyright (c) 2014 Paul Sokolovsky
|
||||
* Copyright (c) 2014-2018 Paul Sokolovsky
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -34,7 +34,9 @@
|
||||
#include "py/runtime.h"
|
||||
#include "py/stackctrl.h"
|
||||
|
||||
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
|
||||
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_t *args, mp_obj_t dict);
|
||||
#endif
|
||||
|
||||
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf);
|
||||
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
|
||||
@@ -116,7 +118,7 @@ STATIC void str_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
|
||||
}
|
||||
#endif
|
||||
#if !MICROPY_PY_BUILTINS_STR_UNICODE
|
||||
bool is_bytes = MP_OBJ_IS_TYPE(self_in, &mp_type_bytes);
|
||||
bool is_bytes = mp_obj_is_type(self_in, &mp_type_bytes);
|
||||
#else
|
||||
bool is_bytes = true;
|
||||
#endif
|
||||
@@ -153,7 +155,7 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
|
||||
|
||||
default: // 2 or 3 args
|
||||
// TODO: validate 2nd/3rd args
|
||||
if (MP_OBJ_IS_TYPE(args[0], &mp_type_bytes)) {
|
||||
if (mp_obj_is_type(args[0], &mp_type_bytes)) {
|
||||
GET_STR_DATA_LEN(args[0], str_data, str_len);
|
||||
GET_STR_HASH(args[0], str_hash);
|
||||
if (str_hash == 0) {
|
||||
@@ -203,7 +205,7 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
|
||||
return mp_const_empty_bytes;
|
||||
}
|
||||
|
||||
if (MP_OBJ_IS_STR(args[0])) {
|
||||
if (mp_obj_is_str(args[0])) {
|
||||
if (n_args < 2 || n_args > 3) {
|
||||
goto wrong_args;
|
||||
}
|
||||
@@ -222,7 +224,7 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size
|
||||
goto wrong_args;
|
||||
}
|
||||
|
||||
if (MP_OBJ_IS_SMALL_INT(args[0])) {
|
||||
if (mp_obj_is_small_int(args[0])) {
|
||||
mp_int_t len = MP_OBJ_SMALL_INT_VALUE(args[0]);
|
||||
if (len < 0) {
|
||||
mp_raise_ValueError(NULL);
|
||||
@@ -297,20 +299,24 @@ const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle,
|
||||
|
||||
// Note: this function is used to check if an object is a str or bytes, which
|
||||
// works because both those types use it as their binary_op method. Revisit
|
||||
// MP_OBJ_IS_STR_OR_BYTES if this fact changes.
|
||||
// mp_obj_is_str_or_bytes if this fact changes.
|
||||
mp_obj_t mp_obj_str_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||
// check for modulo
|
||||
if (op == MP_BINARY_OP_MODULO) {
|
||||
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
|
||||
mp_obj_t *args = &rhs_in;
|
||||
size_t n_args = 1;
|
||||
mp_obj_t dict = MP_OBJ_NULL;
|
||||
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple)) {
|
||||
if (mp_obj_is_type(rhs_in, &mp_type_tuple)) {
|
||||
// TODO: Support tuple subclasses?
|
||||
mp_obj_tuple_get(rhs_in, &n_args, &args);
|
||||
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
|
||||
} else if (mp_obj_is_type(rhs_in, &mp_type_dict)) {
|
||||
dict = rhs_in;
|
||||
}
|
||||
return str_modulo_format(lhs_in, n_args, args, dict);
|
||||
#else
|
||||
return MP_OBJ_NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
// from now on we need lhs type and data, so extract them
|
||||
@@ -419,7 +425,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||
if (value == MP_OBJ_SENTINEL) {
|
||||
// load
|
||||
#if MICROPY_PY_BUILTINS_SLICE
|
||||
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
|
||||
if (mp_obj_is_type(index, &mp_type_slice)) {
|
||||
mp_bound_slice_t slice;
|
||||
if (!mp_seq_get_fast_slice_indexes(self_len, index, &slice)) {
|
||||
mp_raise_NotImplementedError("only slices with step=1 (aka None) are supported");
|
||||
@@ -440,7 +446,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||
}
|
||||
|
||||
STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
|
||||
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(self_in));
|
||||
mp_check_self(mp_obj_is_str_or_bytes(self_in));
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
|
||||
|
||||
// get separation string
|
||||
@@ -450,7 +456,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
|
||||
size_t seq_len;
|
||||
mp_obj_t *seq_items;
|
||||
|
||||
if (!MP_OBJ_IS_TYPE(arg, &mp_type_list) && !MP_OBJ_IS_TYPE(arg, &mp_type_tuple)) {
|
||||
if (!mp_obj_is_type(arg, &mp_type_list) && !mp_obj_is_type(arg, &mp_type_tuple)) {
|
||||
// arg is not a list nor a tuple, try to convert it to a list
|
||||
// TODO: Try to optimize?
|
||||
arg = mp_type_list.make_new(&mp_type_list, 1, 0, &arg);
|
||||
@@ -680,7 +686,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit);
|
||||
|
||||
STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, bool is_index) {
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
|
||||
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
|
||||
// check argument type
|
||||
if (mp_obj_get_type(args[1]) != self_type) {
|
||||
@@ -778,7 +784,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith);
|
||||
enum { LSTRIP, RSTRIP, STRIP };
|
||||
|
||||
STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) {
|
||||
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
|
||||
|
||||
const byte *chars_to_del;
|
||||
@@ -904,7 +910,7 @@ STATIC bool istype(char ch) {
|
||||
}
|
||||
|
||||
STATIC bool arg_looks_integer(mp_obj_t arg) {
|
||||
return MP_OBJ_IS_TYPE(arg, &mp_type_bool) || MP_OBJ_IS_INT(arg);
|
||||
return mp_obj_is_type(arg, &mp_type_bool) || mp_obj_is_int(arg);
|
||||
}
|
||||
|
||||
STATIC bool arg_looks_numeric(mp_obj_t arg) {
|
||||
@@ -915,6 +921,7 @@ STATIC bool arg_looks_numeric(mp_obj_t arg) {
|
||||
;
|
||||
}
|
||||
|
||||
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
|
||||
STATIC mp_obj_t arg_as_int(mp_obj_t arg) {
|
||||
#if MICROPY_PY_BUILTINS_FLOAT
|
||||
if (mp_obj_is_float(arg)) {
|
||||
@@ -923,6 +930,7 @@ STATIC mp_obj_t arg_as_int(mp_obj_t arg) {
|
||||
#endif
|
||||
return arg;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
STATIC NORETURN void terse_str_format_value_error(void) {
|
||||
@@ -1327,7 +1335,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
|
||||
terse_str_format_value_error();
|
||||
} else {
|
||||
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 '%s'",
|
||||
type, mp_obj_get_type_str(arg)));
|
||||
}
|
||||
}
|
||||
@@ -1363,7 +1371,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
|
||||
terse_str_format_value_error();
|
||||
} else {
|
||||
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 '%s'",
|
||||
type, mp_obj_get_type_str(arg)));
|
||||
}
|
||||
}
|
||||
@@ -1374,21 +1382,22 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
|
||||
}
|
||||
|
||||
mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
||||
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
|
||||
GET_STR_DATA_LEN(args[0], str, len);
|
||||
int arg_i = 0;
|
||||
vstr_t vstr = mp_obj_str_format_helper((const char*)str, (const char*)str + len, &arg_i, n_args, args, kwargs);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
||||
return mp_obj_new_str_from_vstr(mp_obj_get_type(args[0]), &vstr);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(str_format_obj, 1, mp_obj_str_format);
|
||||
|
||||
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
|
||||
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_t *args, mp_obj_t dict) {
|
||||
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(pattern));
|
||||
mp_check_self(mp_obj_is_str_or_bytes(pattern));
|
||||
|
||||
GET_STR_DATA_LEN(pattern, str, len);
|
||||
const byte *start_str = str;
|
||||
bool is_bytes = MP_OBJ_IS_TYPE(pattern, &mp_type_bytes);
|
||||
bool is_bytes = mp_obj_is_type(pattern, &mp_type_bytes);
|
||||
size_t arg_i = 0;
|
||||
vstr_t vstr;
|
||||
mp_print_t print;
|
||||
@@ -1411,7 +1420,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
|
||||
// Dictionary value lookup
|
||||
if (*str == '(') {
|
||||
if (dict == MP_OBJ_NULL) {
|
||||
mp_raise_TypeError("format requires a dict");
|
||||
mp_raise_TypeError("format needs a dict");
|
||||
}
|
||||
arg_i = 1; // we used up the single dict argument
|
||||
const byte *key = ++str;
|
||||
@@ -1486,24 +1495,24 @@ incomplete_format:
|
||||
if (arg == MP_OBJ_NULL) {
|
||||
if (arg_i >= n_args) {
|
||||
not_enough_args:
|
||||
mp_raise_TypeError("not enough arguments for format string");
|
||||
mp_raise_TypeError("format string needs more arguments");
|
||||
}
|
||||
arg = args[arg_i++];
|
||||
}
|
||||
switch (*str) {
|
||||
case 'c':
|
||||
if (MP_OBJ_IS_STR(arg)) {
|
||||
if (mp_obj_is_str(arg)) {
|
||||
size_t slen;
|
||||
const char *s = mp_obj_str_get_data(arg, &slen);
|
||||
if (slen != 1) {
|
||||
mp_raise_TypeError("%%c requires int or char");
|
||||
mp_raise_TypeError("%%c needs int or char");
|
||||
}
|
||||
mp_print_strn(&print, s, 1, flags, ' ', width);
|
||||
} else if (arg_looks_integer(arg)) {
|
||||
char ch = mp_obj_get_int(arg);
|
||||
mp_print_strn(&print, &ch, 1, flags, ' ', width);
|
||||
} else {
|
||||
mp_raise_TypeError("integer required");
|
||||
mp_raise_TypeError("integer needed");
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1538,7 +1547,7 @@ not_enough_args:
|
||||
mp_print_t arg_print;
|
||||
vstr_init_print(&arg_vstr, 16, &arg_print);
|
||||
mp_print_kind_t print_kind = (*str == 'r' ? PRINT_REPR : PRINT_STR);
|
||||
if (print_kind == PRINT_STR && is_bytes && MP_OBJ_IS_TYPE(arg, &mp_type_bytes)) {
|
||||
if (print_kind == PRINT_STR && is_bytes && mp_obj_is_type(arg, &mp_type_bytes)) {
|
||||
// If we have something like b"%s" % b"1", bytes arg should be
|
||||
// printed undecorated.
|
||||
print_kind = PRINT_RAW;
|
||||
@@ -1573,16 +1582,17 @@ not_enough_args:
|
||||
}
|
||||
|
||||
if (arg_i != n_args) {
|
||||
mp_raise_TypeError("not all arguments converted during string formatting");
|
||||
mp_raise_TypeError("format string didn't convert all arguments");
|
||||
}
|
||||
|
||||
return mp_obj_new_str_from_vstr(is_bytes ? &mp_type_bytes : &mp_type_str, &vstr);
|
||||
}
|
||||
#endif
|
||||
|
||||
// The implementation is optimized, returning the original string if there's
|
||||
// nothing to replace.
|
||||
STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) {
|
||||
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
|
||||
mp_int_t max_rep = -1;
|
||||
if (n_args == 4) {
|
||||
@@ -1687,9 +1697,10 @@ STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) {
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
|
||||
|
||||
#if MICROPY_PY_BUILTINS_STR_COUNT
|
||||
STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
|
||||
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(args[0]));
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
|
||||
// check argument type
|
||||
if (mp_obj_get_type(args[1]) != self_type) {
|
||||
@@ -1727,10 +1738,11 @@ STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
|
||||
return MP_OBJ_NEW_SMALL_INT(num_occurrences);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
|
||||
#endif
|
||||
|
||||
#if MICROPY_PY_BUILTINS_STR_PARTITION
|
||||
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, int direction) {
|
||||
mp_check_self(MP_OBJ_IS_STR_OR_BYTES(self_in));
|
||||
mp_check_self(mp_obj_is_str_or_bytes(self_in));
|
||||
mp_obj_type_t *self_type = mp_obj_get_type(self_in);
|
||||
if (self_type != mp_obj_get_type(arg)) {
|
||||
bad_implicit_conversion(arg);
|
||||
@@ -1937,7 +1949,9 @@ STATIC const mp_rom_map_elem_t str8_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_rstrip), MP_ROM_PTR(&str_rstrip_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_format), MP_ROM_PTR(&str_format_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_replace), MP_ROM_PTR(&str_replace_obj) },
|
||||
#if MICROPY_PY_BUILTINS_STR_COUNT
|
||||
{ MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) },
|
||||
#endif
|
||||
#if MICROPY_PY_BUILTINS_STR_PARTITION
|
||||
{ MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) },
|
||||
@@ -2078,7 +2092,7 @@ mp_obj_t mp_obj_new_bytes(const byte* data, size_t len) {
|
||||
}
|
||||
|
||||
bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
|
||||
if (MP_OBJ_IS_QSTR(s1) && MP_OBJ_IS_QSTR(s2)) {
|
||||
if (mp_obj_is_qstr(s1) && mp_obj_is_qstr(s2)) {
|
||||
return s1 == s2;
|
||||
} else {
|
||||
GET_STR_HASH(s1, h1);
|
||||
@@ -2110,9 +2124,9 @@ STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in) {
|
||||
// use this if you will anyway convert the string to a qstr
|
||||
// will be more efficient for the case where it's already a qstr
|
||||
qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
|
||||
if (MP_OBJ_IS_QSTR(self_in)) {
|
||||
if (mp_obj_is_qstr(self_in)) {
|
||||
return MP_OBJ_QSTR_VALUE(self_in);
|
||||
} else if (MP_OBJ_IS_TYPE(self_in, &mp_type_str)) {
|
||||
} else if (mp_obj_is_type(self_in, &mp_type_str)) {
|
||||
mp_obj_str_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
return qstr_from_strn((char*)self->data, self->len);
|
||||
} else {
|
||||
@@ -2123,7 +2137,7 @@ qstr mp_obj_str_get_qstr(mp_obj_t self_in) {
|
||||
// only use this function if you need the str data to be zero terminated
|
||||
// at the moment all strings are zero terminated to help with C ASCIIZ compatibility
|
||||
const char *mp_obj_str_get_str(mp_obj_t self_in) {
|
||||
if (MP_OBJ_IS_STR_OR_BYTES(self_in)) {
|
||||
if (mp_obj_is_str_or_bytes(self_in)) {
|
||||
GET_STR_DATA_LEN(self_in, s, l);
|
||||
(void)l; // len unused
|
||||
return (const char*)s;
|
||||
@@ -2133,7 +2147,7 @@ const char *mp_obj_str_get_str(mp_obj_t self_in) {
|
||||
}
|
||||
|
||||
const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len) {
|
||||
if (MP_OBJ_IS_STR_OR_BYTES(self_in)) {
|
||||
if (mp_obj_is_str_or_bytes(self_in)) {
|
||||
GET_STR_DATA_LEN(self_in, s, l);
|
||||
*len = l;
|
||||
return (const char*)s;
|
||||
@@ -2144,7 +2158,7 @@ const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len) {
|
||||
|
||||
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
|
||||
const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, size_t *len) {
|
||||
if (MP_OBJ_IS_QSTR(self_in)) {
|
||||
if (mp_obj_is_qstr(self_in)) {
|
||||
return qstr_data(MP_OBJ_QSTR_VALUE(self_in), len);
|
||||
} else {
|
||||
*len = ((mp_obj_str_t*)self_in)->len;
|
||||
|
||||
Reference in New Issue
Block a user