blob: 88ddc5e6c9a35aad8cc12159e72c8d286e641d64 [file] [log] [blame]
Paul Sokolovskye98cf402014-01-08 02:43:48 +02001#include <string.h>
2
3#include "nlr.h"
4#include "misc.h"
5#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00006#include "qstr.h"
Paul Sokolovskye98cf402014-01-08 02:43:48 +02007#include "obj.h"
8#include "stream.h"
9
10// This file defines generic Python stream read/write methods which
11// dispatch to the underlying stream interface of an object.
12
Paul Sokolovskya671f892014-01-16 12:53:46 +020013static mp_obj_t stream_readall(mp_obj_t self_in);
14
Damien Georgea11ceca2014-01-19 16:02:09 +000015static mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
Paul Sokolovskya671f892014-01-16 12:53:46 +020016 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
Paul Sokolovskye98cf402014-01-08 02:43:48 +020017 if (o->type->stream_p.read == NULL) {
18 // CPython: io.UnsupportedOperation, OSError subclass
19 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
20 }
21
Paul Sokolovskya671f892014-01-16 12:53:46 +020022 machine_int_t sz;
23 if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
24 return stream_readall(args[0]);
25 }
Damien George55baff42014-01-21 21:40:13 +000026 char *buf = m_new(char, sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020027 int error;
Paul Sokolovskya671f892014-01-16 12:53:46 +020028 machine_int_t out_sz = o->type->stream_p.read(o, buf, sz, &error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020029 if (out_sz == -1) {
Damien George6c73ca12014-01-08 18:11:23 +000030 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020031 } else {
Damien George55baff42014-01-21 21:40:13 +000032 // TODO don't intern this string
33 return mp_obj_new_str(qstr_from_strn_take(buf, sz, out_sz));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020034 }
35}
36
37static mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
38 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
39 if (o->type->stream_p.write == NULL) {
40 // CPython: io.UnsupportedOperation, OSError subclass
41 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
42 }
43
Damien George55baff42014-01-21 21:40:13 +000044 uint sz;
45 const byte *buf = qstr_data(mp_obj_get_qstr(arg), &sz);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020046 int error;
47 machine_int_t out_sz = o->type->stream_p.write(self_in, buf, sz, &error);
48 if (out_sz == -1) {
Damien George6c73ca12014-01-08 18:11:23 +000049 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020050 } else {
51 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
52 // "None is returned if the raw stream is set not to block and no single byte could be readily written to it."
53 // Do they mean that instead of 0 they return None?
54 return MP_OBJ_NEW_SMALL_INT(out_sz);
55 }
56}
57
Paul Sokolovsky52254502014-01-13 23:25:33 +020058// TODO: should be in mpconfig.h
59#define READ_SIZE 256
60static mp_obj_t stream_readall(mp_obj_t self_in) {
61 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
62 if (o->type->stream_p.read == NULL) {
63 // CPython: io.UnsupportedOperation, OSError subclass
64 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
65 }
66
67 int total_size = 0;
68 vstr_t *vstr = vstr_new_size(READ_SIZE);
69 char *buf = vstr_str(vstr);
70 char *p = buf;
71 int error;
72 int current_read = READ_SIZE;
73 while (true) {
74 machine_int_t out_sz = o->type->stream_p.read(self_in, p, current_read, &error);
75 if (out_sz == -1) {
76 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
77 }
78 if (out_sz == 0) {
79 break;
80 }
81 total_size += out_sz;
82 if (out_sz < current_read) {
83 current_read -= out_sz;
84 p += out_sz;
85 } else {
86 current_read = READ_SIZE;
87 p = vstr_extend(vstr, current_read);
88 if (p == NULL) {
89 // TODO
90 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError/*MP_QSTR_RuntimeError*/, "Out of memory"));
91 }
92 }
93 }
Damien George55baff42014-01-21 21:40:13 +000094 // TODO don't intern this string
95 vstr_set_size(vstr, total_size);
96 return mp_obj_new_str(qstr_from_strn_take(vstr->buf, vstr->alloc, total_size));
Paul Sokolovsky52254502014-01-13 23:25:33 +020097}
98
Paul Sokolovsky9953ca42014-01-15 23:39:44 +020099// Unbuffered, inefficient implementation of readline() for raw I/O files.
Damien Georgea11ceca2014-01-19 16:02:09 +0000100static mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200101 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
102 if (o->type->stream_p.read == NULL) {
103 // CPython: io.UnsupportedOperation, OSError subclass
104 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
105 }
106
107 machine_int_t max_size = -1;
108 if (n_args > 1) {
109 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
110 }
111
112 vstr_t *vstr;
113 if (max_size != -1) {
Damien George55baff42014-01-21 21:40:13 +0000114 vstr = vstr_new_size(max_size);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200115 } else {
116 vstr = vstr_new();
117 }
118
119 int error;
120 while (max_size == -1 || max_size-- != 0) {
121 char *p = vstr_add_len(vstr, 1);
122 if (p == NULL) {
123 // TODO
124 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError/*MP_QSTR_RuntimeError*/, "Out of memory"));
125 }
126
127 machine_int_t out_sz = o->type->stream_p.read(o, p, 1, &error);
128 if (out_sz == -1) {
129 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
130 }
131 if (out_sz == 0 || *p == '\n') {
132 break;
133 }
134 }
Damien George55baff42014-01-21 21:40:13 +0000135 // TODO don't intern this string
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200136 vstr_shrink(vstr);
Damien George55baff42014-01-21 21:40:13 +0000137 return mp_obj_new_str(qstr_from_strn_take(vstr_str(vstr), vstr->alloc, vstr_len(vstr)));
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200138}
139
140
Paul Sokolovskya671f892014-01-16 12:53:46 +0200141MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200142MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200143MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200144MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);