Skip to content

Commit 1ecea7c

Browse files
committed
py: Make 'bytes' be a proper type, support standard constructor args.
1 parent be020c2 commit 1ecea7c

File tree

4 files changed

+105
-14
lines changed

4 files changed

+105
-14
lines changed

py/builtin.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,18 +375,6 @@ STATIC mp_obj_t mp_builtin_sorted(uint n_args, const mp_obj_t *args, mp_map_t *k
375375

376376
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, 1, mp_builtin_sorted);
377377

378-
// TODO: This should be type, this is just quick CPython compat hack
379-
STATIC mp_obj_t mp_builtin_bytes(uint n_args, const mp_obj_t *args) {
380-
if (!MP_OBJ_IS_QSTR(args[0]) && !MP_OBJ_IS_TYPE(args[0], &str_type)) {
381-
assert(0);
382-
}
383-
// Currently, MicroPython strings are mix between CPython byte and unicode
384-
// strings. So, conversion is null so far.
385-
return args[0];
386-
}
387-
388-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_bytes_obj, 1, 3, mp_builtin_bytes);
389-
390378
STATIC mp_obj_t mp_builtin_id(mp_obj_t o_in) {
391379
return mp_obj_new_int((machine_int_t)o_in);
392380
}

py/objstr.c

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ typedef struct _mp_obj_str_t {
1717
const byte *data;
1818
} mp_obj_str_t;
1919

20+
const mp_obj_t mp_const_empty_bytes;
21+
2022
// use this macro to extract the string hash
2123
#define GET_STR_HASH(str_obj_in, str_hash) uint str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) { str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_hash = ((mp_obj_str_t*)str_obj_in)->hash; }
2224

@@ -113,6 +115,75 @@ STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
113115
}
114116
}
115117

118+
STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
119+
if (n_args == 0) {
120+
return mp_const_empty_bytes;
121+
}
122+
123+
if (MP_OBJ_IS_STR(args[0])) {
124+
if (n_args < 2 || n_args > 3) {
125+
goto wrong_args;
126+
}
127+
GET_STR_DATA_LEN(args[0], str_data, str_len);
128+
GET_STR_HASH(args[0], str_hash);
129+
mp_obj_str_t *o = str_new(&bytes_type, NULL, str_len);
130+
o->data = str_data;
131+
o->hash = str_hash;
132+
return o;
133+
}
134+
135+
if (n_args > 1) {
136+
goto wrong_args;
137+
}
138+
139+
if (MP_OBJ_IS_SMALL_INT(args[0])) {
140+
uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
141+
byte *data;
142+
143+
mp_obj_t o = mp_obj_str_builder_start(&bytes_type, len, &data);
144+
memset(data, 0, len);
145+
return mp_obj_str_builder_end(o);
146+
}
147+
148+
int len;
149+
byte *data;
150+
vstr_t *vstr = NULL;
151+
mp_obj_t o = NULL;
152+
// Try to create array of exact len if initializer len is known
153+
mp_obj_t len_in = mp_obj_len_maybe(args[0]);
154+
if (len_in == MP_OBJ_NULL) {
155+
len = -1;
156+
vstr = vstr_new();
157+
} else {
158+
len = MP_OBJ_SMALL_INT_VALUE(len_in);
159+
o = mp_obj_str_builder_start(&bytes_type, len, &data);
160+
}
161+
162+
mp_obj_t iterable = rt_getiter(args[0]);
163+
mp_obj_t item;
164+
while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
165+
if (len == -1) {
166+
vstr_add_char(vstr, MP_OBJ_SMALL_INT_VALUE(item));
167+
} else {
168+
*data++ = MP_OBJ_SMALL_INT_VALUE(item);
169+
}
170+
}
171+
172+
if (len == -1) {
173+
vstr_shrink(vstr);
174+
// TODO: Optimize, borrow buffer from vstr
175+
len = vstr_len(vstr);
176+
o = mp_obj_str_builder_start(&bytes_type, len, &data);
177+
memcpy(data, vstr_str(vstr), len);
178+
vstr_free(vstr);
179+
}
180+
181+
return mp_obj_str_builder_end(o);
182+
183+
wrong_args:
184+
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "wrong number of arguments"));
185+
}
186+
116187
// like strstr but with specified length and allows \0 bytes
117188
// TODO replace with something more efficient/standard
118189
STATIC const byte *find_subbytes(const byte *haystack, uint hlen, const byte *needle, uint nlen) {
@@ -666,11 +737,16 @@ const mp_obj_type_t bytes_type = {
666737
{ &mp_type_type },
667738
.name = MP_QSTR_bytes,
668739
.print = str_print,
740+
.make_new = bytes_make_new,
669741
.binary_op = str_binary_op,
670742
.getiter = mp_obj_new_bytes_iterator,
671743
.methods = str_type_methods,
672744
};
673745

746+
// the zero-length bytes
747+
STATIC const mp_obj_str_t empty_bytes_obj = {{&bytes_type}, 0, 0, NULL};
748+
const mp_obj_t mp_const_empty_bytes = (mp_obj_t)&empty_bytes_obj;
749+
674750
mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
675751
mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
676752
o->base.type = type;
@@ -682,7 +758,6 @@ mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **da
682758
}
683759

684760
mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
685-
assert(MP_OBJ_IS_STR(o_in));
686761
mp_obj_str_t *o = o_in;
687762
o->hash = qstr_compute_hash(o->data, o->len);
688763
byte *p = (byte*)o->data;

py/runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ STATIC const mp_builtin_elem_t builtin_table[] = {
8989

9090
// built-in types
9191
{ MP_QSTR_bool, (mp_obj_t)&bool_type },
92+
{ MP_QSTR_bytes, (mp_obj_t)&bytes_type },
9293
#if MICROPY_ENABLE_FLOAT
9394
{ MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
9495
#endif
@@ -115,7 +116,6 @@ STATIC const mp_builtin_elem_t builtin_table[] = {
115116
{ MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
116117
{ MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
117118
{ MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
118-
{ MP_QSTR_bytes, (mp_obj_t)&mp_builtin_bytes_obj },
119119
{ MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
120120
{ MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
121121
{ MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },

tests/basics/bytes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,36 @@
44
print(repr(a))
55
print(a[0], a[2])
66
print(a[-1])
7+
print(str(a, "utf-8"))
8+
print(str(a, "utf-8", "ignore"))
9+
try:
10+
str(a, "utf-8", "ignore", "toomuch")
11+
except TypeError:
12+
print("TypeError")
713

814
s = 0
915
for i in a:
1016
s += i
1117
print(s)
18+
19+
20+
print(bytes("abc", "utf-8"))
21+
print(bytes("abc", "utf-8", "replace"))
22+
try:
23+
bytes("abc")
24+
except TypeError:
25+
print("TypeError")
26+
try:
27+
bytes("abc", "utf-8", "replace", "toomuch")
28+
except TypeError:
29+
print("TypeError")
30+
31+
print(bytes(3))
32+
33+
print(bytes([3, 2, 1]))
34+
print(bytes(range(5)))
35+
36+
def gen():
37+
for i in range(4):
38+
yield i
39+
print(bytes(gen()))

0 commit comments

Comments
 (0)