Skip to content

Commit 11cc694

Browse files
committed
Merge pull request adafruit#173 from pfalcon/file-readall
Generic implementation if stream readall() method, immediately reused in unix io.FileIO implementation
2 parents 39eab8d + 5225450 commit 11cc694

7 files changed

Lines changed: 93 additions & 6 deletions

File tree

py/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ mp_lexer_t *mp_lexer_new(const char *src_name, void *stream_data, mp_lexer_strea
614614
lex->num_indent_level = 1;
615615
lex->indent_level = m_new(uint16_t, lex->alloc_indent_level);
616616
lex->indent_level[0] = 0;
617-
vstr_init(&lex->vstr);
617+
vstr_init(&lex->vstr, 32);
618618

619619
// preload characters
620620
lex->chr0 = stream_next_char(stream_data);

py/misc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@ typedef struct _vstr_t {
5959
bool had_error;
6060
} vstr_t;
6161

62-
void vstr_init(vstr_t *vstr);
62+
void vstr_init(vstr_t *vstr, int alloc);
6363
void vstr_clear(vstr_t *vstr);
6464
vstr_t *vstr_new(void);
65+
vstr_t *vstr_new_size(int alloc);
6566
void vstr_free(vstr_t *vstr);
6667
void vstr_reset(vstr_t *vstr);
6768
bool vstr_had_error(vstr_t *vstr);
6869
char *vstr_str(vstr_t *vstr);
6970
int vstr_len(vstr_t *vstr);
7071
void vstr_hint_size(vstr_t *vstr, int size);
72+
char *vstr_extend(vstr_t *vstr, int size);
73+
bool vstr_set_size(vstr_t *vstr, int size);
74+
bool vstr_shrink(vstr_t *vstr);
7175
char *vstr_add_len(vstr_t *vstr, int len);
7276
void vstr_add_byte(vstr_t *vstr, byte v);
7377
void vstr_add_char(vstr_t *vstr, unichar chr);

py/stream.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,47 @@ static mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
5151
}
5252
}
5353

54+
// TODO: should be in mpconfig.h
55+
#define READ_SIZE 256
56+
static mp_obj_t stream_readall(mp_obj_t self_in) {
57+
struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
58+
if (o->type->stream_p.read == NULL) {
59+
// CPython: io.UnsupportedOperation, OSError subclass
60+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
61+
}
62+
63+
int total_size = 0;
64+
vstr_t *vstr = vstr_new_size(READ_SIZE);
65+
char *buf = vstr_str(vstr);
66+
char *p = buf;
67+
int error;
68+
int current_read = READ_SIZE;
69+
while (true) {
70+
machine_int_t out_sz = o->type->stream_p.read(self_in, p, current_read, &error);
71+
if (out_sz == -1) {
72+
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
73+
}
74+
if (out_sz == 0) {
75+
break;
76+
}
77+
total_size += out_sz;
78+
if (out_sz < current_read) {
79+
current_read -= out_sz;
80+
p += out_sz;
81+
} else {
82+
current_read = READ_SIZE;
83+
p = vstr_extend(vstr, current_read);
84+
if (p == NULL) {
85+
// TODO
86+
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError/*MP_QSTR_RuntimeError*/, "Out of memory"));
87+
}
88+
}
89+
}
90+
vstr_set_size(vstr, total_size + 1); // TODO: for \0
91+
buf[total_size] = 0;
92+
return mp_obj_new_str(qstr_from_str_take(buf, total_size + 1));
93+
}
94+
5495
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_read_obj, stream_read);
96+
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
5597
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);

py/stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
extern const mp_obj_fun_native_t mp_stream_read_obj;
2+
extern const mp_obj_fun_native_t mp_stream_readall_obj;
23
extern const mp_obj_fun_native_t mp_stream_write_obj;

py/vstr.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// returned value is always at least 1 greater than argument
77
#define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8)
88

9-
void vstr_init(vstr_t *vstr) {
10-
vstr->alloc = 32;
9+
void vstr_init(vstr_t *vstr, int alloc) {
10+
vstr->alloc = alloc;
1111
vstr->len = 0;
1212
vstr->buf = m_new(char, vstr->alloc);
1313
if (vstr->buf == NULL) {
@@ -28,7 +28,16 @@ vstr_t *vstr_new(void) {
2828
if (vstr == NULL) {
2929
return NULL;
3030
}
31-
vstr_init(vstr);
31+
vstr_init(vstr, 32);
32+
return vstr;
33+
}
34+
35+
vstr_t *vstr_new_size(int alloc) {
36+
vstr_t *vstr = m_new(vstr_t, 1);
37+
if (vstr == NULL) {
38+
return NULL;
39+
}
40+
vstr_init(vstr, alloc);
3241
return vstr;
3342
}
3443

@@ -63,6 +72,36 @@ int vstr_len(vstr_t *vstr) {
6372
return vstr->len;
6473
}
6574

75+
// Extend vstr strictly to by requested size, return pointer to newly added chunk
76+
char *vstr_extend(vstr_t *vstr, int size) {
77+
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
78+
if (new_buf == NULL) {
79+
vstr->had_error = true;
80+
return NULL;
81+
}
82+
char *p = new_buf + vstr->alloc;
83+
vstr->alloc += size;
84+
vstr->buf = new_buf;
85+
return p;
86+
}
87+
88+
// Shrink vstr to be given size
89+
bool vstr_set_size(vstr_t *vstr, int size) {
90+
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, size);
91+
if (new_buf == NULL) {
92+
vstr->had_error = true;
93+
return false;
94+
}
95+
vstr->buf = new_buf;
96+
vstr->alloc = vstr->len = size;
97+
return true;
98+
}
99+
100+
// Shrink vstr allocation to its actual length
101+
bool vstr_shrink(vstr_t *vstr) {
102+
return vstr_set_size(vstr, vstr->len);
103+
}
104+
66105
bool vstr_ensure_extra(vstr_t *vstr, int size) {
67106
if (vstr->len + size + 1 > vstr->alloc) {
68107
int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2);

stm/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ void do_repl(void) {
390390
stdout_tx_str("Type \"help()\" for more information.\r\n");
391391

392392
vstr_t line;
393-
vstr_init(&line);
393+
vstr_init(&line, 32);
394394

395395
for (;;) {
396396
vstr_reset(&line);

unix/file.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ static mp_obj_t fdfile_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *ar
9090

9191
static const mp_method_t rawfile_type_methods[] = {
9292
{ "read", &mp_stream_read_obj },
93+
{ "readall", &mp_stream_readall_obj },
9394
{ "write", &mp_stream_write_obj },
9495
{ "close", &fdfile_close_obj },
9596
{ NULL, NULL },

0 commit comments

Comments
 (0)