From 0d255130984de6ba082f6c6e28356ef3e35984f6 Mon Sep 17 00:00:00 2001 From: Jon Foster Date: Mon, 1 Apr 2024 19:19:29 +0100 Subject: [PATCH 1/2] py/objstr.c: Add new mp_obj_new_str_0() method. There were lots of places where this pattern was duplicated, to convert a standard C string to a MicroPython string: x = mp_obj_new_str(s, strlen(s)); This commit provides a simpler method that removes this code duplication: x = mp_obj_new_str_0(s); The _0 suffix is because it accepts a NUL-terminated string as a parameter. This gives clearer, and probably smaller, code. Signed-off-by: Jon Foster --- py/obj.h | 1 + py/objstr.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/py/obj.h b/py/obj.h index 9f2bb46e41..0e56517ddb 100644 --- a/py/obj.h +++ b/py/obj.h @@ -971,6 +971,7 @@ mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, uns 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); // will check utf-8 (raises UnicodeError) +mp_obj_t mp_obj_new_str_0(const char *data); // will check utf-8 (raises UnicodeError) mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); // input data must be valid utf-8 mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); // will check utf-8 (raises UnicodeError) #if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK diff --git a/py/objstr.c b/py/objstr.c index c7e4ebf53b..ff6d481ec4 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -2308,6 +2308,10 @@ mp_obj_t mp_obj_new_str(const char *data, size_t len) { } } +mp_obj_t mp_obj_new_str_0(const char *data) { + return mp_obj_new_str(data, strlen(data)); +} + mp_obj_t mp_obj_str_intern(mp_obj_t str) { GET_STR_DATA_LEN(str, data, len); return mp_obj_new_str_via_qstr((const char *)data, len); From 6121f6890c7f6ea40597af050495b9c64a2953cf Mon Sep 17 00:00:00 2001 From: Jon Foster Date: Mon, 1 Apr 2024 19:23:49 +0100 Subject: [PATCH 2/2] all: Use new method mp_obj_new_str_0(). Use new method mp_obj_new_str_0() where appropriate. It simplifies the code, and probably makes it smaller too. Signed-off-by: Jon Foster --- extmod/modlwip.c | 2 +- extmod/modnetwork.c | 2 +- extmod/modopenamp.c | 2 +- extmod/modopenamp_remoteproc_store.c | 2 +- extmod/modtls_mbedtls.c | 6 +++--- extmod/modwebrepl.c | 2 +- extmod/network_esp_hosted.c | 2 +- extmod/network_lwip.c | 4 ++-- extmod/network_ninaw10.c | 2 +- extmod/vfs.c | 2 +- extmod/vfs_fat.c | 4 ++-- extmod/vfs_lfsx.c | 2 +- extmod/vfs_posix.c | 4 ++-- ports/cc3200/mods/modwlan.c | 6 +++--- ports/esp32/esp32_partition.c | 2 +- ports/esp32/modsocket.c | 4 ++-- ports/esp32/network_common.c | 2 +- ports/esp32/network_ppp.c | 2 +- ports/esp32/network_wlan.c | 2 +- ports/esp8266/network_wlan.c | 2 +- ports/unix/coverage.c | 8 ++++---- ports/unix/main.c | 2 +- ports/unix/modffi.c | 2 +- ports/unix/modjni.c | 2 +- ports/unix/modos.c | 2 +- ports/zephyr/main.c | 4 ++-- ports/zephyr/modsocket.c | 2 +- py/binary.c | 2 +- 28 files changed, 40 insertions(+), 40 deletions(-) diff --git a/extmod/modlwip.c b/extmod/modlwip.c index d4e25917a6..cb799a2ecf 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -361,7 +361,7 @@ mp_obj_t lwip_format_inet_addr(const ip_addr_t *ip, mp_uint_t port) { char ipstr[IPADDR_STRLEN_MAX]; ipaddr_ntoa_r(ip, ipstr, sizeof(ipstr)); mp_obj_t tuple[2] = { - tuple[0] = mp_obj_new_str(ipstr, strlen(ipstr)), + tuple[0] = mp_obj_new_str_0(ipstr), tuple[1] = mp_obj_new_int(port), }; return mp_obj_new_tuple(2, tuple); diff --git a/extmod/modnetwork.c b/extmod/modnetwork.c index aa237bd93c..fd7301cd79 100644 --- a/extmod/modnetwork.c +++ b/extmod/modnetwork.c @@ -127,7 +127,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_country_obj, 0, 1, network_count mp_obj_t mod_network_hostname(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { - return mp_obj_new_str(mod_network_hostname_data, strlen(mod_network_hostname_data)); + return mp_obj_new_str_0(mod_network_hostname_data); } else { size_t len; const char *str = mp_obj_str_get_data(args[0], &len); diff --git a/extmod/modopenamp.c b/extmod/modopenamp.c index 7a19e55a66..7faef06b70 100644 --- a/extmod/modopenamp.c +++ b/extmod/modopenamp.c @@ -263,7 +263,7 @@ static void openamp_ns_callback(struct rpmsg_device *rdev, const char *name, uin // the Name Service (NS) announcement containing the name of the channel. virtio_dev_obj_t *virtio_device = metal_container_of(rdev, virtio_dev_obj_t, rvdev); if (virtio_device->ns_callback != mp_const_none) { - mp_call_function_2(virtio_device->ns_callback, mp_obj_new_int(dest), mp_obj_new_str(name, strlen(name))); + mp_call_function_2(virtio_device->ns_callback, mp_obj_new_int(dest), mp_obj_new_str_0(name)); } } diff --git a/extmod/modopenamp_remoteproc_store.c b/extmod/modopenamp_remoteproc_store.c index 262e6f2ec7..3361dc6cd6 100644 --- a/extmod/modopenamp_remoteproc_store.c +++ b/extmod/modopenamp_remoteproc_store.c @@ -70,7 +70,7 @@ void *mp_openamp_remoteproc_store_alloc(void) { static int openamp_remoteproc_store_open(void *store, const char *path, const void **image_data) { metal_log(METAL_LOG_DEBUG, "store_open(): %s\n", path); mp_obj_t args[2] = { - mp_obj_new_str(path, strlen(path)), + mp_obj_new_str_0(path), MP_OBJ_NEW_QSTR(MP_QSTR_rb), }; diff --git a/extmod/modtls_mbedtls.c b/extmod/modtls_mbedtls.c index 6db6ac1958..e032ee5e8f 100644 --- a/extmod/modtls_mbedtls.c +++ b/extmod/modtls_mbedtls.c @@ -307,7 +307,7 @@ static mp_obj_t ssl_context_get_ciphers(mp_obj_t self_in) { mp_obj_t list = mp_obj_new_list(0, NULL); for (const int *cipher_list = mbedtls_ssl_list_ciphersuites(); *cipher_list; ++cipher_list) { const char *cipher_name = mbedtls_ssl_get_ciphersuite_name(*cipher_list); - mp_obj_list_append(list, MP_OBJ_FROM_PTR(mp_obj_new_str(cipher_name, strlen(cipher_name)))); + mp_obj_list_append(list, MP_OBJ_FROM_PTR(mp_obj_new_str_0(cipher_name))); } return list; } @@ -572,8 +572,8 @@ static mp_obj_t mod_ssl_cipher(mp_obj_t o_in) { mp_obj_ssl_socket_t *o = MP_OBJ_TO_PTR(o_in); const char *cipher_suite = mbedtls_ssl_get_ciphersuite(&o->ssl); const char *tls_version = mbedtls_ssl_get_version(&o->ssl); - mp_obj_t tuple[2] = {mp_obj_new_str(cipher_suite, strlen(cipher_suite)), - mp_obj_new_str(tls_version, strlen(tls_version))}; + mp_obj_t tuple[2] = {mp_obj_new_str_0(cipher_suite), + mp_obj_new_str_0(tls_version)}; return mp_obj_new_tuple(2, tuple); } diff --git a/extmod/modwebrepl.c b/extmod/modwebrepl.c index 9d09f65a37..8a5f342252 100644 --- a/extmod/modwebrepl.c +++ b/extmod/modwebrepl.c @@ -146,7 +146,7 @@ static void handle_op(mp_obj_webrepl_t *self) { // Handle operations requiring opened file mp_obj_t open_args[2] = { - mp_obj_new_str(self->hdr.fname, strlen(self->hdr.fname)), + mp_obj_new_str_0(self->hdr.fname), MP_OBJ_NEW_QSTR(MP_QSTR_rb) }; diff --git a/extmod/network_esp_hosted.c b/extmod/network_esp_hosted.c index 0747343d87..842e9216e2 100644 --- a/extmod/network_esp_hosted.c +++ b/extmod/network_esp_hosted.c @@ -230,7 +230,7 @@ static mp_obj_t network_esp_hosted_config(size_t n_args, const mp_obj_t *args, m case MP_QSTR_essid: { esp_hosted_netinfo_t netinfo; esp_hosted_wifi_netinfo(&netinfo); - return mp_obj_new_str(netinfo.ssid, strlen(netinfo.ssid)); + return mp_obj_new_str_0(netinfo.ssid); } case MP_QSTR_security: { esp_hosted_netinfo_t netinfo; diff --git a/extmod/network_lwip.c b/extmod/network_lwip.c index 97cb43902d..f2f9922067 100644 --- a/extmod/network_lwip.c +++ b/extmod/network_lwip.c @@ -113,7 +113,7 @@ mp_obj_t mod_network_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwa case MP_QSTR_dns: { char addr_str[IPADDR_STRLEN_MAX]; ipaddr_ntoa_r(dns_getserver(0), addr_str, sizeof(addr_str)); - return mp_obj_new_str(addr_str, strlen(addr_str)); + return mp_obj_new_str_0(addr_str); } case MP_QSTR_prefer: { return MP_OBJ_NEW_SMALL_INT(mp_mod_network_prefer_dns_use_ip_version); @@ -219,7 +219,7 @@ mp_obj_t mod_network_nic_ipconfig(struct netif *netif, size_t n_args, const mp_o char addr_str[IPADDR_STRLEN_MAX]; ipaddr_ntoa_r(netif_ip_addr6(netif, i), addr_str, sizeof(addr_str)); mp_obj_t tuple[4] = { - mp_obj_new_str(addr_str, strlen(addr_str)), + mp_obj_new_str_0(addr_str), MP_OBJ_NEW_SMALL_INT(netif_ip6_addr_state(netif, i)), MP_OBJ_NEW_SMALL_INT(netif_ip6_addr_pref_life(netif, i)), // preferred MP_OBJ_NEW_SMALL_INT(netif_ip6_addr_valid_life(netif, i)) diff --git a/extmod/network_ninaw10.c b/extmod/network_ninaw10.c index 926e228a35..bc952b5453 100644 --- a/extmod/network_ninaw10.c +++ b/extmod/network_ninaw10.c @@ -386,7 +386,7 @@ static mp_obj_t network_ninaw10_config(size_t n_args, const mp_obj_t *args, mp_m case MP_QSTR_ssid: { nina_netinfo_t netinfo; nina_netinfo(&netinfo); - return mp_obj_new_str(netinfo.ssid, strlen(netinfo.ssid)); + return mp_obj_new_str_0(netinfo.ssid); } case MP_QSTR_security: { nina_netinfo_t netinfo; diff --git a/extmod/vfs.c b/extmod/vfs.c index ae09b8afe8..827de7748c 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -139,7 +139,7 @@ mp_import_stat_t mp_vfs_import_stat(const char *path) { } // delegate to vfs.stat() method - mp_obj_t path_o = mp_obj_new_str(path_out, strlen(path_out)); + mp_obj_t path_o = mp_obj_new_str_0(path_out); mp_obj_t stat; nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 38c1663363..82694a0b10 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -144,7 +144,7 @@ static mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) { // make 4-tuple with info about this entry mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL)); if (self->is_str) { - t->items[0] = mp_obj_new_str(fn, strlen(fn)); + t->items[0] = mp_obj_new_str_0(fn); } else { t->items[0] = mp_obj_new_bytes((const byte *)fn, strlen(fn)); } @@ -291,7 +291,7 @@ static mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) { if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } - return mp_obj_new_str(buf, strlen(buf)); + return mp_obj_new_str_0(buf); } static MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd); diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index 19da4417e6..26f1d97abf 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -192,7 +192,7 @@ static mp_obj_t MP_VFS_LFSx(ilistdir_it_iternext)(mp_obj_t self_in) { // make 4-tuple with info about this entry mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL)); if (self->is_str) { - t->items[0] = mp_obj_new_str(info.name, strlen(info.name)); + t->items[0] = mp_obj_new_str_0(info.name); } else { t->items[0] = mp_obj_new_bytes((const byte *)info.name, strlen(info.name)); } diff --git a/extmod/vfs_posix.c b/extmod/vfs_posix.c index ed4c06e367..32ac97c4ac 100644 --- a/extmod/vfs_posix.c +++ b/extmod/vfs_posix.c @@ -194,7 +194,7 @@ static mp_obj_t vfs_posix_getcwd(mp_obj_t self_in) { } #endif } - return mp_obj_new_str(ret, strlen(ret)); + return mp_obj_new_str_0(ret); } static MP_DEFINE_CONST_FUN_OBJ_1(vfs_posix_getcwd_obj, vfs_posix_getcwd); @@ -234,7 +234,7 @@ static mp_obj_t vfs_posix_ilistdir_it_iternext(mp_obj_t self_in) { mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); if (self->is_str) { - t->items[0] = mp_obj_new_str(fn, strlen(fn)); + t->items[0] = mp_obj_new_str_0(fn); } else { t->items[0] = mp_obj_new_bytes((const byte *)fn, strlen(fn)); } diff --git a/ports/cc3200/mods/modwlan.c b/ports/cc3200/mods/modwlan.c index fc55201eaa..a6ac05773d 100644 --- a/ports/cc3200/mods/modwlan.c +++ b/ports/cc3200/mods/modwlan.c @@ -1074,7 +1074,7 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_mode_obj, 1, 2, wlan_mode); static mp_obj_t wlan_ssid(size_t n_args, const mp_obj_t *args) { wlan_obj_t *self = args[0]; if (n_args == 1) { - return mp_obj_new_str((const char *)self->ssid, strlen((const char *)self->ssid)); + return mp_obj_new_str_0((const char *)self->ssid); } else { size_t len; const char *ssid = mp_obj_str_get_data(args[1], &len); @@ -1094,7 +1094,7 @@ static mp_obj_t wlan_auth(size_t n_args, const mp_obj_t *args) { } else { mp_obj_t security[2]; security[0] = mp_obj_new_int(self->auth); - security[1] = mp_obj_new_str((const char *)self->key, strlen((const char *)self->key)); + security[1] = mp_obj_new_str_0((const char *)self->key); return mp_obj_new_tuple(2, security); } } else { @@ -1198,7 +1198,7 @@ static MP_DEFINE_CONST_FUN_OBJ_KW(wlan_irq_obj, 1, wlan_irq); // mp_obj_t connections = mp_obj_new_list(0, NULL); // // if (wlan_is_connected()) { -// device[0] = mp_obj_new_str((const char *)wlan_obj.ssid_o, strlen((const char *)wlan_obj.ssid_o)); +// device[0] = mp_obj_new_str_0((const char *)wlan_obj.ssid_o); // device[1] = mp_obj_new_bytes((const byte *)wlan_obj.bssid, SL_BSSID_LENGTH); // // add the device to the list // mp_obj_list_append(connections, mp_obj_new_tuple(MP_ARRAY_SIZE(device), device)); diff --git a/ports/esp32/esp32_partition.c b/ports/esp32/esp32_partition.c index e5d4809620..0288247a83 100644 --- a/ports/esp32/esp32_partition.c +++ b/ports/esp32/esp32_partition.c @@ -156,7 +156,7 @@ static mp_obj_t esp32_partition_info(mp_obj_t self_in) { MP_OBJ_NEW_SMALL_INT(self->part->subtype), mp_obj_new_int_from_uint(self->part->address), mp_obj_new_int_from_uint(self->part->size), - mp_obj_new_str(&self->part->label[0], strlen(&self->part->label[0])), + mp_obj_new_str_0(&self->part->label[0]), mp_obj_new_bool(self->part->encrypted), }; return mp_obj_new_tuple(6, tuple); diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 507609dfef..dc89ad74a1 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -941,7 +941,7 @@ static mp_obj_t esp_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { mp_obj_new_int(resi->ai_family), mp_obj_new_int(resi->ai_socktype), mp_obj_new_int(resi->ai_protocol), - mp_obj_new_str(resi->ai_canonname, strlen(resi->ai_canonname)), + mp_obj_new_str_0(resi->ai_canonname), mp_const_none }; @@ -952,7 +952,7 @@ static mp_obj_t esp_socket_getaddrinfo(size_t n_args, const mp_obj_t *args) { char buf[16]; ip4addr_ntoa_r(&ip4_addr, buf, sizeof(buf)); mp_obj_t inaddr_objs[2] = { - mp_obj_new_str(buf, strlen(buf)), + mp_obj_new_str_0(buf), mp_obj_new_int(ntohs(addr->sin_port)) }; addrinfo_objs[4] = mp_obj_new_tuple(2, inaddr_objs); diff --git a/ports/esp32/network_common.c b/ports/esp32/network_common.c index 223803a5d1..c1aafe0711 100644 --- a/ports/esp32/network_common.c +++ b/ports/esp32/network_common.c @@ -158,7 +158,7 @@ mp_obj_t esp_ifname(esp_netif_t *netif) { char ifname[NETIF_NAMESIZE + 1] = {0}; mp_obj_t ret = mp_const_none; if (esp_netif_get_netif_impl_name(netif, ifname) == ESP_OK && ifname[0] != 0) { - ret = mp_obj_new_str((char *)ifname, strlen(ifname)); + ret = mp_obj_new_str_0((char *)ifname); } return ret; } diff --git a/ports/esp32/network_ppp.c b/ports/esp32/network_ppp.c index 4e9b2a32ca..2c825339c0 100644 --- a/ports/esp32/network_ppp.c +++ b/ports/esp32/network_ppp.c @@ -308,7 +308,7 @@ static mp_obj_t ppp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs char ifname[NETIF_NAMESIZE + 1] = {0}; netif_index_to_name(netif_get_index(pppif), ifname); if (ifname[0] != 0) { - val = mp_obj_new_str((char *)ifname, strlen(ifname)); + val = mp_obj_new_str_0((char *)ifname); } } break; diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index eb2bcda03e..caf5e38fd7 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -640,7 +640,7 @@ static mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ case MP_QSTR_essid: switch (self->if_id) { case ESP_IF_WIFI_STA: - val = mp_obj_new_str((char *)cfg.sta.ssid, strlen((char *)cfg.sta.ssid)); + val = mp_obj_new_str_0((char *)cfg.sta.ssid); break; case ESP_IF_WIFI_AP: val = mp_obj_new_str((char *)cfg.ap.ssid, cfg.ap.ssid_len); diff --git a/ports/esp8266/network_wlan.c b/ports/esp8266/network_wlan.c index af1611a249..3e5991cd3d 100644 --- a/ports/esp8266/network_wlan.c +++ b/ports/esp8266/network_wlan.c @@ -456,7 +456,7 @@ static mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs case MP_QSTR_ssid: case MP_QSTR_essid: if (self->if_id == STATION_IF) { - val = mp_obj_new_str((char *)cfg.sta.ssid, strlen((char *)cfg.sta.ssid)); + val = mp_obj_new_str_0((char *)cfg.sta.ssid); } else { val = mp_obj_new_str((char *)cfg.ap.ssid, cfg.ap.ssid_len); } diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 803f849535..9033eb2b28 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -387,7 +387,7 @@ static mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "# str\n"); // intern string - mp_printf(&mp_plat_print, "%d\n", mp_obj_is_qstr(mp_obj_str_intern(mp_obj_new_str("intern me", 9)))); + mp_printf(&mp_plat_print, "%d\n", mp_obj_is_qstr(mp_obj_str_intern(mp_obj_new_str_0("intern me")))); } // bytearray @@ -463,12 +463,12 @@ static mp_obj_t extra_coverage(void) { // call mp_call_function_1_protected mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), MP_OBJ_NEW_SMALL_INT(1)); // call mp_call_function_1_protected with invalid args - mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), mp_obj_new_str("abc", 3)); + mp_call_function_1_protected(MP_OBJ_FROM_PTR(&mp_builtin_abs_obj), mp_obj_new_str_0("abc")); // call mp_call_function_2_protected mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), MP_OBJ_NEW_SMALL_INT(1), MP_OBJ_NEW_SMALL_INT(1)); // call mp_call_function_2_protected with invalid args - mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), mp_obj_new_str("abc", 3), mp_obj_new_str("abc", 3)); + mp_call_function_2_protected(MP_OBJ_FROM_PTR(&mp_builtin_divmod_obj), mp_obj_new_str_0("abc"), mp_obj_new_str_0("abc")); // mp_obj_int_get_uint_checked with non-negative small-int mp_printf(&mp_plat_print, "%d\n", (int)mp_obj_int_get_uint_checked(MP_OBJ_NEW_SMALL_INT(1))); @@ -722,7 +722,7 @@ static mp_obj_t extra_coverage(void) { // mp_obj_is_integer accepts ints and booleans mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_integer(MP_OBJ_NEW_SMALL_INT(1)), mp_obj_is_integer(mp_obj_new_int_from_ll(1))); mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_integer(mp_const_true), mp_obj_is_integer(mp_const_false)); - mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_integer(mp_obj_new_str("1", 1)), mp_obj_is_integer(mp_const_none)); + mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_integer(mp_obj_new_str_0("1")), mp_obj_is_integer(mp_const_none)); // mp_obj_is_int accepts small int and object ints mp_printf(&mp_plat_print, "%d %d\n", mp_obj_is_int(MP_OBJ_NEW_SMALL_INT(1)), mp_obj_is_int(mp_obj_new_int_from_ll(1))); diff --git a/ports/unix/main.c b/ports/unix/main.c index 0cf11d37cf..62328603c2 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -639,7 +639,7 @@ MP_NOINLINE int main_(int argc, char **argv) { return invalid_args(); } mp_obj_t import_args[4]; - import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1])); + import_args[0] = mp_obj_new_str_0(argv[a + 1]); import_args[1] = import_args[2] = mp_const_none; // Ask __import__ to handle imported module specially - set its __name__ // to __main__, and also return this leaf module, not top-level package diff --git a/ports/unix/modffi.c b/ports/unix/modffi.c index b7d03e84dd..25b32a0d00 100644 --- a/ports/unix/modffi.c +++ b/ports/unix/modffi.c @@ -174,7 +174,7 @@ static mp_obj_t return_ffi_value(ffi_union_t *val, char type) { if (!s) { return mp_const_none; } - return mp_obj_new_str(s, strlen(s)); + return mp_obj_new_str_0(s); } case 'v': return mp_const_none; diff --git a/ports/unix/modjni.c b/ports/unix/modjni.c index d953e7e015..c62762ae82 100644 --- a/ports/unix/modjni.c +++ b/ports/unix/modjni.c @@ -337,7 +337,7 @@ static mp_obj_t new_jobject(jobject jo) { return mp_const_none; } else if (JJ(IsInstanceOf, jo, String_class)) { const char *s = JJ(GetStringUTFChars, jo, NULL); - mp_obj_t ret = mp_obj_new_str(s, strlen(s)); + mp_obj_t ret = mp_obj_new_str_0(s); JJ(ReleaseStringUTFChars, jo, s); return ret; } else if (JJ(IsInstanceOf, jo, Class_class)) { diff --git a/ports/unix/modos.c b/ports/unix/modos.c index d5e963583b..9dd6823bd8 100644 --- a/ports/unix/modos.c +++ b/ports/unix/modos.c @@ -40,7 +40,7 @@ static mp_obj_t mp_os_getenv(size_t n_args, const mp_obj_t *args) { } return mp_const_none; } - return mp_obj_new_str(s, strlen(s)); + return mp_obj_new_str_0(s); } static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_os_getenv_obj, 1, 2, mp_os_getenv); diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index 314bbfd263..3d0cd85a54 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -103,7 +103,7 @@ static void vfs_init(void) { int ret = 0; #ifdef CONFIG_DISK_DRIVER_SDMMC - mp_obj_t args[] = { mp_obj_new_str(CONFIG_SDMMC_VOLUME_NAME, strlen(CONFIG_SDMMC_VOLUME_NAME)) }; + mp_obj_t args[] = { mp_obj_new_str_0(CONFIG_SDMMC_VOLUME_NAME) }; bdev = MP_OBJ_TYPE_GET_SLOT(&zephyr_disk_access_type, make_new)(&zephyr_disk_access_type, ARRAY_SIZE(args), 0, args); mount_point_str = "/sd"; #elif defined(CONFIG_FLASH_MAP) && FLASH_AREA_LABEL_EXISTS(storage) @@ -113,7 +113,7 @@ static void vfs_init(void) { #endif if ((bdev != NULL)) { - mount_point = mp_obj_new_str(mount_point_str, strlen(mount_point_str)); + mount_point = mp_obj_new_str_0(mount_point_str); ret = mp_vfs_mount_and_chdir_protected(bdev, mount_point); // TODO: if this failed, make a new file system and try to mount again } diff --git a/ports/zephyr/modsocket.c b/ports/zephyr/modsocket.c index 12c4c934bc..0cc945ca6f 100644 --- a/ports/zephyr/modsocket.c +++ b/ports/zephyr/modsocket.c @@ -92,7 +92,7 @@ static mp_obj_t format_inet_addr(struct sockaddr *addr, mp_obj_t port) { net_addr_ntop(addr->sa_family, &sockaddr_in6->sin6_addr, buf, sizeof(buf)); mp_obj_tuple_t *tuple = mp_obj_new_tuple(addr->sa_family == AF_INET ? 2 : 4, NULL); - tuple->items[0] = mp_obj_new_str(buf, strlen(buf)); + tuple->items[0] = mp_obj_new_str_0(buf); // We employ the fact that port offset is the same for IPv4 & IPv6 // not filled in // tuple->items[1] = mp_obj_new_int(ntohs(((struct sockaddr_in*)addr)->sin_port)); diff --git a/py/binary.c b/py/binary.c index 7c01cfa1c8..b998b74ed2 100644 --- a/py/binary.c +++ b/py/binary.c @@ -338,7 +338,7 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte * 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)); + return mp_obj_new_str_0(s_val); #if MICROPY_PY_BUILTINS_FLOAT } else if (val_type == 'e') { return mp_obj_new_float_from_f(mp_decode_half_float(val));