Skip to content

Commit 4601759

Browse files
committed
py/objstr: Remove "make_qstr_if_not_already" arg from mp_obj_new_str.
This patch simplifies the str creation API to favour the common case of creating a str object that is not forced to be interned. To force interning of a new str the new mp_obj_new_str_via_qstr function is added, and should only be used if warranted. Apart from simplifying the mp_obj_new_str function (and making it have the same signature as mp_obj_new_bytes), this patch also reduces code size by a bit (-16 bytes for bare-arm and roughly -40 bytes on the bare-metal archs).
1 parent 6bc55b6 commit 4601759

File tree

24 files changed

+55
-54
lines changed

24 files changed

+55
-54
lines changed

extmod/modujson.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ STATIC mp_obj_t mod_ujson_load(mp_obj_t stream_obj) {
166166
goto fail;
167167
}
168168
S_NEXT(s);
169-
next = mp_obj_new_str(vstr.buf, vstr.len, false);
169+
next = mp_obj_new_str(vstr.buf, vstr.len);
170170
break;
171171
case '-':
172172
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {

extmod/modwebrepl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ STATIC void handle_op(mp_obj_webrepl_t *self) {
141141
// Handle operations requiring opened file
142142

143143
mp_obj_t open_args[2] = {
144-
mp_obj_new_str(self->hdr.fname, strlen(self->hdr.fname), false),
144+
mp_obj_new_str(self->hdr.fname, strlen(self->hdr.fname)),
145145
MP_OBJ_NEW_QSTR(MP_QSTR_rb)
146146
};
147147

extmod/vfs_fat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) {
197197
if (res != FR_OK) {
198198
mp_raise_OSError(fresult_to_errno_table[res]);
199199
}
200-
return mp_obj_new_str(buf, strlen(buf), false);
200+
return mp_obj_new_str(buf, strlen(buf));
201201
}
202202
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd);
203203

extmod/vfs_fat_misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) {
5757
// make 3-tuple with info about this entry
5858
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
5959
if (self->is_str) {
60-
t->items[0] = mp_obj_new_str(fn, strlen(fn), false);
60+
t->items[0] = mp_obj_new_str(fn, strlen(fn));
6161
} else {
6262
t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn));
6363
}

extmod/vfs_reader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ STATIC void mp_reader_vfs_close(void *data) {
7171

7272
void mp_reader_new_file(mp_reader_t *reader, const char *filename) {
7373
mp_reader_vfs_t *rf = m_new_obj(mp_reader_vfs_t);
74-
mp_obj_t arg = mp_obj_new_str(filename, strlen(filename), false);
74+
mp_obj_t arg = mp_obj_new_str(filename, strlen(filename));
7575
rf->file = mp_vfs_open(1, &arg, (mp_map_t*)&mp_const_empty_map);
7676
int errcode;
7777
rf->len = mp_stream_rw(rf->file, rf->buf, sizeof(rf->buf), &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE);

lib/netutils/netutils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mp_obj_t netutils_format_ipv4_addr(uint8_t *ip, netutils_endian_t endian) {
4141
} else {
4242
ip_len = snprintf(ip_str, 16, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
4343
}
44-
return mp_obj_new_str(ip_str, ip_len, false);
44+
return mp_obj_new_str(ip_str, ip_len);
4545
}
4646

4747
// Takes an array with a raw IP address, and a port, and returns a net-address

ports/cc3200/mods/modwlan.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ STATIC mp_obj_t wlan_scan(mp_obj_t self_in) {
885885
}
886886

887887
mp_obj_t tuple[5];
888-
tuple[0] = mp_obj_new_str((const char *)wlanEntry.ssid, wlanEntry.ssid_len, false);
888+
tuple[0] = mp_obj_new_str((const char *)wlanEntry.ssid, wlanEntry.ssid_len);
889889
tuple[1] = mp_obj_new_bytes((const byte *)wlanEntry.bssid, SL_BSSID_LENGTH);
890890
// 'normalize' the security type
891891
if (wlanEntry.sec_type > 2) {
@@ -1075,7 +1075,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wlan_mode_obj, 1, 2, wlan_mode);
10751075
STATIC mp_obj_t wlan_ssid(size_t n_args, const mp_obj_t *args) {
10761076
wlan_obj_t *self = args[0];
10771077
if (n_args == 1) {
1078-
return mp_obj_new_str((const char *)self->ssid, strlen((const char *)self->ssid), false);
1078+
return mp_obj_new_str((const char *)self->ssid, strlen((const char *)self->ssid));
10791079
} else {
10801080
size_t len;
10811081
const char *ssid = mp_obj_str_get_data(args[1], &len);
@@ -1095,7 +1095,7 @@ STATIC mp_obj_t wlan_auth(size_t n_args, const mp_obj_t *args) {
10951095
} else {
10961096
mp_obj_t security[2];
10971097
security[0] = mp_obj_new_int(self->auth);
1098-
security[1] = mp_obj_new_str((const char *)self->key, strlen((const char *)self->key), false);
1098+
security[1] = mp_obj_new_str((const char *)self->key, strlen((const char *)self->key));
10991099
return mp_obj_new_tuple(2, security);
11001100
}
11011101
} else {
@@ -1199,7 +1199,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_irq_obj, 1, wlan_irq);
11991199
// mp_obj_t connections = mp_obj_new_list(0, NULL);
12001200
//
12011201
// if (wlan_is_connected()) {
1202-
// device[0] = mp_obj_new_str((const char *)wlan_obj.ssid_o, strlen((const char *)wlan_obj.ssid_o), false);
1202+
// device[0] = mp_obj_new_str((const char *)wlan_obj.ssid_o, strlen((const char *)wlan_obj.ssid_o));
12031203
// device[1] = mp_obj_new_bytes((const byte *)wlan_obj.bssid, SL_BSSID_LENGTH);
12041204
// // add the device to the list
12051205
// mp_obj_list_append(connections, mp_obj_new_tuple(MP_ARRAY_SIZE(device), device));
@@ -1232,7 +1232,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wlan_irq_obj, 1, wlan_irq);
12321232
// if (sl_NetAppGet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, &len, (uint8_t *)urn) < 0) {
12331233
// mp_raise_OSError(MP_EIO);
12341234
// }
1235-
// return mp_obj_new_str(urn, (len - 1), false);
1235+
// return mp_obj_new_str(urn, (len - 1));
12361236
// }
12371237
//
12381238
// return mp_const_none;

ports/esp8266/modnetwork.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
411411
}
412412
case QS(MP_QSTR_essid):
413413
req_if = SOFTAP_IF;
414-
val = mp_obj_new_str((char*)cfg.ap.ssid, cfg.ap.ssid_len, false);
414+
val = mp_obj_new_str((char*)cfg.ap.ssid, cfg.ap.ssid_len);
415415
break;
416416
case QS(MP_QSTR_hidden):
417417
req_if = SOFTAP_IF;
@@ -428,7 +428,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
428428
case QS(MP_QSTR_dhcp_hostname): {
429429
req_if = STATION_IF;
430430
char* s = wifi_station_get_hostname();
431-
val = mp_obj_new_str(s, strlen(s), false);
431+
val = mp_obj_new_str(s, strlen(s));
432432
break;
433433
}
434434
default:

ports/esp8266/moduos.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ STATIC mp_obj_tuple_t os_uname_info_obj = {
6060
STATIC mp_obj_t os_uname(void) {
6161
// We must populate the "release" field each time in case it was GC'd since the last call.
6262
const char *ver = system_get_sdk_version();
63-
os_uname_info_obj.items[2] = mp_obj_new_str(ver, strlen(ver), false);
63+
os_uname_info_obj.items[2] = mp_obj_new_str(ver, strlen(ver));
6464
return (mp_obj_t)&os_uname_info_obj;
6565
}
6666
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);

ports/stm32/modnwcc3k.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ STATIC mp_obj_t cc3k_ifconfig(mp_obj_t self_in) {
531531
netutils_format_ipv4_addr(ipconfig.aucDefaultGateway, NETUTILS_LITTLE),
532532
netutils_format_ipv4_addr(ipconfig.aucDNSServer, NETUTILS_LITTLE),
533533
netutils_format_ipv4_addr(ipconfig.aucDHCPServer, NETUTILS_LITTLE),
534-
mp_obj_new_str(mac_vstr.buf, mac_vstr.len, false),
535-
mp_obj_new_str((const char*)ipconfig.uaSSID, strlen((const char*)ipconfig.uaSSID), false),
534+
mp_obj_new_str(mac_vstr.buf, mac_vstr.len),
535+
mp_obj_new_str((const char*)ipconfig.uaSSID, strlen((const char*)ipconfig.uaSSID)),
536536
};
537537
return mp_obj_new_tuple(MP_ARRAY_SIZE(tuple), tuple);
538538
}

0 commit comments

Comments
 (0)