summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/binary.c2
-rw-r--r--py/builtinhelp.c2
-rw-r--r--py/builtinimport.c2
-rw-r--r--py/modbuiltins.c4
-rw-r--r--py/modio.c2
-rw-r--r--py/obj.h3
-rw-r--r--py/objstr.c36
-rw-r--r--py/objstrunicode.c4
8 files changed, 28 insertions, 27 deletions
diff --git a/py/binary.c b/py/binary.c
index 870a0942b..f509ff010 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -206,7 +206,7 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
return (mp_obj_t)(mp_uint_t)val;
} else if (val_type == 'S') {
const char *s_val = (const char*)(uintptr_t)(mp_uint_t)val;
- return mp_obj_new_str(s_val, strlen(s_val), false);
+ return mp_obj_new_str(s_val, strlen(s_val));
#if MICROPY_PY_BUILTINS_FLOAT
} else if (val_type == 'f') {
union { uint32_t i; float f; } fpu = {val};
diff --git a/py/builtinhelp.c b/py/builtinhelp.c
index c9992906d..7106f3ced 100644
--- a/py/builtinhelp.c
+++ b/py/builtinhelp.c
@@ -69,7 +69,7 @@ STATIC void mp_help_add_from_names(mp_obj_t list, const char *name) {
while (*name) {
size_t l = strlen(name);
// name should end in '.py' and we strip it off
- mp_obj_list_append(list, mp_obj_new_str(name, l - 3, false));
+ mp_obj_list_append(list, mp_obj_new_str(name, l - 3));
name += l + 1;
}
}
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 04ce66723..9235e946c 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -434,7 +434,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
DEBUG_printf("%.*s is dir\n", vstr_len(&path), vstr_str(&path));
// https://docs.python.org/3/reference/import.html
// "Specifically, any module that contains a __path__ attribute is considered a package."
- mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false));
+ mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path)));
size_t orig_path_len = path.len;
vstr_add_char(&path, PATH_SEP_CHAR);
vstr_add_str(&path, "__init__.py");
diff --git a/py/modbuiltins.c b/py/modbuiltins.c
index 65c03d523..ebff5f5c0 100644
--- a/py/modbuiltins.c
+++ b/py/modbuiltins.c
@@ -159,12 +159,12 @@ STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
} else {
mp_raise_ValueError("chr() arg not in range(0x110000)");
}
- return mp_obj_new_str(str, len, true);
+ return mp_obj_new_str_via_qstr(str, len);
#else
mp_int_t ord = mp_obj_get_int(o_in);
if (0 <= ord && ord <= 0xff) {
char str[1] = {ord};
- return mp_obj_new_str(str, 1, true);
+ return mp_obj_new_str_via_qstr(str, 1);
} else {
mp_raise_ValueError("chr() arg not in range(256)");
}
diff --git a/py/modio.c b/py/modio.c
index 353a00286..828bcec46 100644
--- a/py/modio.c
+++ b/py/modio.c
@@ -176,7 +176,7 @@ STATIC mp_obj_t resource_stream(mp_obj_t package_in, mp_obj_t path_in) {
return MP_OBJ_FROM_PTR(o);
}
- mp_obj_t path_out = mp_obj_new_str(path_buf.buf, path_buf.len, false);
+ mp_obj_t path_out = mp_obj_new_str(path_buf.buf, path_buf.len);
return mp_builtin_open(1, &path_out, (mp_map_t*)&mp_const_empty_map);
}
MP_DEFINE_CONST_FUN_OBJ_2(resource_stream_obj, resource_stream);
diff --git a/py/obj.h b/py/obj.h
index 77f0f2298..31c3ce95c 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -637,7 +637,8 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base);
mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
-mp_obj_t mp_obj_new_str(const char* data, size_t len, bool make_qstr_if_not_already);
+mp_obj_t mp_obj_new_str(const char* data, size_t len);
+mp_obj_t mp_obj_new_str_via_qstr(const char* data, size_t len);
mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr);
mp_obj_t mp_obj_new_bytes(const byte* data, size_t len);
mp_obj_t mp_obj_new_bytearray(size_t n, void *items);
diff --git a/py/objstr.c b/py/objstr.c
index 51da7a418..5c464ba7d 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -176,7 +176,7 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
mp_raise_msg(&mp_type_UnicodeError, NULL);
}
#endif
- return mp_obj_new_str(bufinfo.buf, bufinfo.len, false);
+ return mp_obj_new_str(bufinfo.buf, bufinfo.len);
}
}
}
@@ -423,7 +423,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
if (MICROPY_PY_BUILTINS_STR_UNICODE || type == &mp_type_bytes) {
return MP_OBJ_NEW_SMALL_INT(self_data[index_val]);
} else {
- return mp_obj_new_str((char*)&self_data[index_val], 1, true);
+ return mp_obj_new_str_via_qstr((char*)&self_data[index_val], 1);
}
} else {
return MP_OBJ_NULL; // op not supported
@@ -1046,7 +1046,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
} else {
const char *lookup;
for (lookup = field_name; lookup < field_name_top && *lookup != '.' && *lookup != '['; lookup++);
- mp_obj_t field_q = mp_obj_new_str(field_name, lookup - field_name, true/*?*/);
+ mp_obj_t field_q = mp_obj_new_str_via_qstr(field_name, lookup - field_name); // should it be via qstr?
field_name = lookup;
mp_map_elem_t *key_elem = mp_map_lookup(kwargs, field_q, MP_MAP_LOOKUP);
if (key_elem == NULL) {
@@ -1413,7 +1413,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
}
++str;
}
- mp_obj_t k_obj = mp_obj_new_str((const char*)key, str - key, true);
+ mp_obj_t k_obj = mp_obj_new_str_via_qstr((const char*)key, str - key);
arg = mp_obj_dict_get(dict, k_obj);
str++;
}
@@ -1992,6 +1992,11 @@ mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, siz
return MP_OBJ_FROM_PTR(o);
}
+// Create a str using a qstr to store the data; may use existing or new qstr.
+mp_obj_t mp_obj_new_str_via_qstr(const char* data, size_t len) {
+ return MP_OBJ_NEW_QSTR(qstr_from_strn(data, len));
+}
+
// Create a str/bytes object from the given vstr. The vstr buffer is resized to
// the exact length required and then reused for the str/bytes object. The vstr
// is cleared and can safely be passed to vstr_free if it was heap allocated.
@@ -2022,25 +2027,20 @@ mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) {
return MP_OBJ_FROM_PTR(o);
}
-mp_obj_t mp_obj_new_str(const char* data, size_t len, bool make_qstr_if_not_already) {
- if (make_qstr_if_not_already) {
- // use existing, or make a new qstr
- return MP_OBJ_NEW_QSTR(qstr_from_strn(data, len));
+mp_obj_t mp_obj_new_str(const char* data, size_t len) {
+ qstr q = qstr_find_strn(data, len);
+ if (q != MP_QSTR_NULL) {
+ // qstr with this data already exists
+ return MP_OBJ_NEW_QSTR(q);
} else {
- qstr q = qstr_find_strn(data, len);
- if (q != MP_QSTR_NULL) {
- // qstr with this data already exists
- return MP_OBJ_NEW_QSTR(q);
- } else {
- // no existing qstr, don't make one
- return mp_obj_new_str_of_type(&mp_type_str, (const byte*)data, len);
- }
+ // no existing qstr, don't make one
+ return mp_obj_new_str_of_type(&mp_type_str, (const byte*)data, len);
}
}
mp_obj_t mp_obj_str_intern(mp_obj_t str) {
GET_STR_DATA_LEN(str, data, len);
- return MP_OBJ_NEW_QSTR(qstr_from_strn((const char*)data, len));
+ return mp_obj_new_str_via_qstr((const char*)data, len);
}
mp_obj_t mp_obj_new_bytes(const byte* data, size_t len) {
@@ -2138,7 +2138,7 @@ STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
mp_obj_str8_it_t *self = MP_OBJ_TO_PTR(self_in);
GET_STR_DATA_LEN(self->str, str, len);
if (self->cur < len) {
- mp_obj_t o_out = mp_obj_new_str((const char*)str + self->cur, 1, true);
+ mp_obj_t o_out = mp_obj_new_str_via_qstr((const char*)str + self->cur, 1);
self->cur += 1;
return o_out;
} else {
diff --git a/py/objstrunicode.c b/py/objstrunicode.c
index 29f7695b7..a1f54b8a2 100644
--- a/py/objstrunicode.c
+++ b/py/objstrunicode.c
@@ -216,7 +216,7 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
++len;
}
}
- return mp_obj_new_str((const char*)s, len, true); // This will create a one-character string
+ return mp_obj_new_str_via_qstr((const char*)s, len); // This will create a one-character string
} else {
return MP_OBJ_NULL; // op not supported
}
@@ -291,7 +291,7 @@ STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) {
if (self->cur < len) {
const byte *cur = str + self->cur;
const byte *end = utf8_next_char(str + self->cur);
- mp_obj_t o_out = mp_obj_new_str((const char*)cur, end - cur, true);
+ mp_obj_t o_out = mp_obj_new_str_via_qstr((const char*)cur, end - cur);
self->cur += end - cur;
return o_out;
} else {