Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
| 3 | #include "nlr.h" |
| 4 | #include "misc.h" |
| 5 | #include "mpconfig.h" |
| 6 | #include "mpqstr.h" |
| 7 | #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 Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 13 | static mp_obj_t stream_readall(mp_obj_t self_in); |
| 14 | |
Damien George | a11ceca | 2014-01-19 16:02:09 +0000 | [diff] [blame] | 15 | static mp_obj_t stream_read(uint n_args, const mp_obj_t *args) { |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 16 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 17 | 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 Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 22 | machine_int_t sz; |
| 23 | if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) { |
| 24 | return stream_readall(args[0]); |
| 25 | } |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 26 | // +1 because so far we mark end of string with \0 |
| 27 | char *buf = m_new(char, sz + 1); |
| 28 | int error; |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 29 | machine_int_t out_sz = o->type->stream_p.read(o, buf, sz, &error); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 30 | if (out_sz == -1) { |
Damien George | 6c73ca1 | 2014-01-08 18:11:23 +0000 | [diff] [blame] | 31 | nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error)); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 32 | } else { |
| 33 | buf[out_sz] = 0; |
| 34 | return mp_obj_new_str(qstr_from_str_take(buf, /*out_sz,*/ sz + 1)); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) { |
| 39 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; |
| 40 | if (o->type->stream_p.write == NULL) { |
| 41 | // CPython: io.UnsupportedOperation, OSError subclass |
| 42 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported")); |
| 43 | } |
| 44 | |
| 45 | const char *buf = qstr_str(mp_obj_get_qstr(arg)); |
| 46 | machine_int_t sz = strlen(buf); |
| 47 | int error; |
| 48 | machine_int_t out_sz = o->type->stream_p.write(self_in, buf, sz, &error); |
| 49 | if (out_sz == -1) { |
Damien George | 6c73ca1 | 2014-01-08 18:11:23 +0000 | [diff] [blame] | 50 | nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error)); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 51 | } else { |
| 52 | // http://docs.python.org/3/library/io.html#io.RawIOBase.write |
| 53 | // "None is returned if the raw stream is set not to block and no single byte could be readily written to it." |
| 54 | // Do they mean that instead of 0 they return None? |
| 55 | return MP_OBJ_NEW_SMALL_INT(out_sz); |
| 56 | } |
| 57 | } |
| 58 | |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 59 | // TODO: should be in mpconfig.h |
| 60 | #define READ_SIZE 256 |
| 61 | static mp_obj_t stream_readall(mp_obj_t self_in) { |
| 62 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in; |
| 63 | if (o->type->stream_p.read == NULL) { |
| 64 | // CPython: io.UnsupportedOperation, OSError subclass |
| 65 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported")); |
| 66 | } |
| 67 | |
| 68 | int total_size = 0; |
| 69 | vstr_t *vstr = vstr_new_size(READ_SIZE); |
| 70 | char *buf = vstr_str(vstr); |
| 71 | char *p = buf; |
| 72 | int error; |
| 73 | int current_read = READ_SIZE; |
| 74 | while (true) { |
| 75 | machine_int_t out_sz = o->type->stream_p.read(self_in, p, current_read, &error); |
| 76 | if (out_sz == -1) { |
| 77 | nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error)); |
| 78 | } |
| 79 | if (out_sz == 0) { |
| 80 | break; |
| 81 | } |
| 82 | total_size += out_sz; |
| 83 | if (out_sz < current_read) { |
| 84 | current_read -= out_sz; |
| 85 | p += out_sz; |
| 86 | } else { |
| 87 | current_read = READ_SIZE; |
| 88 | p = vstr_extend(vstr, current_read); |
| 89 | if (p == NULL) { |
| 90 | // TODO |
| 91 | nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError/*MP_QSTR_RuntimeError*/, "Out of memory")); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | vstr_set_size(vstr, total_size + 1); // TODO: for \0 |
Paul Sokolovsky | 323c09e | 2014-01-16 12:52:03 +0200 | [diff] [blame] | 96 | buf = vstr_str(vstr); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 97 | buf[total_size] = 0; |
| 98 | return mp_obj_new_str(qstr_from_str_take(buf, total_size + 1)); |
| 99 | } |
| 100 | |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 101 | // Unbuffered, inefficient implementation of readline() for raw I/O files. |
Damien George | a11ceca | 2014-01-19 16:02:09 +0000 | [diff] [blame] | 102 | static mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) { |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 103 | struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0]; |
| 104 | if (o->type->stream_p.read == NULL) { |
| 105 | // CPython: io.UnsupportedOperation, OSError subclass |
| 106 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported")); |
| 107 | } |
| 108 | |
| 109 | machine_int_t max_size = -1; |
| 110 | if (n_args > 1) { |
| 111 | max_size = MP_OBJ_SMALL_INT_VALUE(args[1]); |
| 112 | } |
| 113 | |
| 114 | vstr_t *vstr; |
| 115 | if (max_size != -1) { |
| 116 | vstr = vstr_new_size(max_size + 1); // TODO: \0 |
| 117 | } else { |
| 118 | vstr = vstr_new(); |
| 119 | } |
| 120 | |
| 121 | int error; |
| 122 | while (max_size == -1 || max_size-- != 0) { |
| 123 | char *p = vstr_add_len(vstr, 1); |
| 124 | if (p == NULL) { |
| 125 | // TODO |
| 126 | nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError/*MP_QSTR_RuntimeError*/, "Out of memory")); |
| 127 | } |
| 128 | |
| 129 | machine_int_t out_sz = o->type->stream_p.read(o, p, 1, &error); |
| 130 | if (out_sz == -1) { |
| 131 | nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error)); |
| 132 | } |
| 133 | if (out_sz == 0 || *p == '\n') { |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | // TODO: \0 |
| 138 | vstr_add_byte(vstr, 0); |
| 139 | vstr_shrink(vstr); |
Paul Sokolovsky | d54bef7 | 2014-01-20 18:35:32 +0200 | [diff] [blame^] | 140 | return MP_OBJ_NEW_QSTR(qstr_from_str_take(vstr_str(vstr), vstr_len(vstr))); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Paul Sokolovsky | d54bef7 | 2014-01-20 18:35:32 +0200 | [diff] [blame^] | 143 | mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) { |
| 144 | mp_obj_t l_in = stream_unbuffered_readline(1, &self); |
| 145 | const char *l = qstr_str(MP_OBJ_QSTR_VALUE(l_in)); |
| 146 | // TODO: \0 |
| 147 | if (*l != 0) { |
| 148 | return l_in; |
| 149 | } |
| 150 | return mp_const_stop_iteration; |
| 151 | } |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 152 | |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 153 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read); |
Paul Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 154 | MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 155 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 156 | MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write); |