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" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame^] | 6 | #include "qstr.h" |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 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 | } |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame^] | 26 | char *buf = m_new(char, sz); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 27 | int error; |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 28 | 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] | 29 | if (out_sz == -1) { |
Damien George | 6c73ca1 | 2014-01-08 18:11:23 +0000 | [diff] [blame] | 30 | 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] | 31 | } else { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame^] | 32 | // TODO don't intern this string |
| 33 | return mp_obj_new_str(qstr_from_strn_take(buf, sz, out_sz)); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | static 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 George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame^] | 44 | uint sz; |
| 45 | const byte *buf = qstr_data(mp_obj_get_qstr(arg), &sz); |
Paul Sokolovsky | e98cf40 | 2014-01-08 02:43:48 +0200 | [diff] [blame] | 46 | int error; |
| 47 | machine_int_t out_sz = o->type->stream_p.write(self_in, buf, sz, &error); |
| 48 | if (out_sz == -1) { |
Damien George | 6c73ca1 | 2014-01-08 18:11:23 +0000 | [diff] [blame] | 49 | 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] | 50 | } 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 Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 58 | // TODO: should be in mpconfig.h |
| 59 | #define READ_SIZE 256 |
| 60 | static 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 George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame^] | 94 | // 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 Sokolovsky | 5225450 | 2014-01-13 23:25:33 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 99 | // Unbuffered, inefficient implementation of readline() for raw I/O files. |
Damien George | a11ceca | 2014-01-19 16:02:09 +0000 | [diff] [blame] | 100 | 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] | 101 | 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 George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame^] | 114 | vstr = vstr_new_size(max_size); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 115 | } 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 George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame^] | 135 | // TODO don't intern this string |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 136 | vstr_shrink(vstr); |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame^] | 137 | return mp_obj_new_str(qstr_from_strn_take(vstr_str(vstr), vstr->alloc, vstr_len(vstr))); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Paul Sokolovsky | a671f89 | 2014-01-16 12:53:46 +0200 | [diff] [blame] | 141 | 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] | 142 | MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall); |
Paul Sokolovsky | 9953ca4 | 2014-01-15 23:39:44 +0200 | [diff] [blame] | 143 | 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] | 144 | MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write); |