Skip to content

Commit 1801421

Browse files
committed
bytearray: Print objects properly.
1 parent 0b7e29c commit 1801421

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

py/objarray.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,20 @@ static void array_set_el(mp_obj_array_t *o, int index, mp_obj_t val_in) {
124124
static void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
125125
mp_obj_array_t *o = o_in;
126126
if (o->typecode == BYTEARRAY_TYPECODE) {
127-
print(env, "bytearray(", o->typecode);
127+
print(env, "bytearray(b", o->typecode);
128+
mp_str_print_quoted(print, env, o->items, o->len);
128129
} else {
129130
print(env, "array('%c'", o->typecode);
130-
}
131-
if (o->len > 0) {
132-
print(env, ", [", o->typecode);
133-
for (int i = 0; i < o->len; i++) {
134-
if (i > 0) {
135-
print(env, ", ");
131+
if (o->len > 0) {
132+
print(env, ", [", o->typecode);
133+
for (int i = 0; i < o->len; i++) {
134+
if (i > 0) {
135+
print(env, ", ");
136+
}
137+
print(env, "%d", array_get_el(o, i));
136138
}
137-
print(env, "%d", array_get_el(o, i));
139+
print(env, "]");
138140
}
139-
print(env, "]");
140141
}
141142
print(env, ")");
142143
}

tests/basics/bytearray1.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
a = bytearray([1, 2, 200])
22
print(a[0], a[2])
33
print(a[-1])
4+
print(a)
45
a[2] = 255
56
print(a[-1])
67
a.append(10)

0 commit comments

Comments
 (0)