Skip to content

Commit d215ee1

Browse files
committed
py: Make MICROPY_PY_BUILTINS_STR_UNICODE=1 buildable.
1 parent 9731912 commit d215ee1

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

py/objstr.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t *args, mp_obj_t dict);
4444
const mp_obj_t mp_const_empty_bytes;
4545

46-
STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
46+
mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
4747
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
4848
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
4949
STATIC NORETURN void arg_type_mixup();
@@ -1649,7 +1649,14 @@ MP_DEFINE_CONST_FUN_OBJ_1(str_islower_obj, str_islower);
16491649
STATIC const mp_map_elem_t str_locals_dict_table[] = {
16501650
#if MICROPY_CPYTHON_COMPAT
16511651
{ MP_OBJ_NEW_QSTR(MP_QSTR_decode), (mp_obj_t)&bytes_decode_obj },
1652+
#if !MICROPY_PY_BUILTINS_STR_UNICODE
1653+
// If we have separate unicode type, then here we have methods only
1654+
// for bytes type, and it should not have encode() methods. Otherwise,
1655+
// we have non-compliant-but-practical bytestring type, which shares
1656+
// method table with bytes, so they both have encode() and decode()
1657+
// methods (which should do type checking at runtime).
16521658
{ MP_OBJ_NEW_QSTR(MP_QSTR_encode), (mp_obj_t)&str_encode_obj },
1659+
#endif
16531660
#endif
16541661
{ MP_OBJ_NEW_QSTR(MP_QSTR_find), (mp_obj_t)&str_find_obj },
16551662
{ MP_OBJ_NEW_QSTR(MP_QSTR_rfind), (mp_obj_t)&str_rfind_obj },
@@ -1679,6 +1686,7 @@ STATIC const mp_map_elem_t str_locals_dict_table[] = {
16791686

16801687
STATIC MP_DEFINE_CONST_DICT(str_locals_dict, str_locals_dict_table);
16811688

1689+
#if !MICROPY_PY_BUILTINS_STR_UNICODE
16821690
const mp_obj_type_t mp_type_str = {
16831691
{ &mp_type_type },
16841692
.name = MP_QSTR_str,
@@ -1690,6 +1698,7 @@ const mp_obj_type_t mp_type_str = {
16901698
.buffer_p = { .get_buffer = str_get_buffer },
16911699
.locals_dict = (mp_obj_t)&str_locals_dict,
16921700
};
1701+
#endif
16931702

16941703
// Reuses most of methods from str
16951704
const mp_obj_type_t mp_type_bytes = {
@@ -1857,6 +1866,7 @@ typedef struct _mp_obj_str_it_t {
18571866
machine_uint_t cur;
18581867
} mp_obj_str_it_t;
18591868

1869+
#if !MICROPY_PY_BUILTINS_STR_UNICODE
18601870
STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
18611871
mp_obj_str_it_t *self = self_in;
18621872
GET_STR_DATA_LEN(self->str, str, len);
@@ -1876,6 +1886,15 @@ STATIC const mp_obj_type_t mp_type_str_it = {
18761886
.iternext = str_it_iternext,
18771887
};
18781888

1889+
mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
1890+
mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
1891+
o->base.type = &mp_type_str_it;
1892+
o->str = str;
1893+
o->cur = 0;
1894+
return o;
1895+
}
1896+
#endif
1897+
18791898
STATIC mp_obj_t bytes_it_iternext(mp_obj_t self_in) {
18801899
mp_obj_str_it_t *self = self_in;
18811900
GET_STR_DATA_LEN(self->str, str, len);
@@ -1895,14 +1914,6 @@ STATIC const mp_obj_type_t mp_type_bytes_it = {
18951914
.iternext = bytes_it_iternext,
18961915
};
18971916

1898-
mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) {
1899-
mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
1900-
o->base.type = &mp_type_str_it;
1901-
o->str = str;
1902-
o->cur = 0;
1903-
return o;
1904-
}
1905-
19061917
mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) {
19071918
mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t);
19081919
o->base.type = &mp_type_bytes_it;

0 commit comments

Comments
 (0)