blob: 0e2cccad33d8de948c81012ff5d64138b9104c78 [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"
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
13static mp_obj_t stream_read(mp_obj_t self_in, mp_obj_t arg) {
14 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
15 if (o->type->stream_p.read == NULL) {
16 // CPython: io.UnsupportedOperation, OSError subclass
17 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
18 }
19
20 machine_int_t sz = mp_obj_get_int(arg);
21 // +1 because so far we mark end of string with \0
22 char *buf = m_new(char, sz + 1);
23 int error;
24 machine_int_t out_sz = o->type->stream_p.read(self_in, buf, sz, &error);
25 if (out_sz == -1) {
Damien George6c73ca12014-01-08 18:11:23 +000026 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020027 } else {
28 buf[out_sz] = 0;
29 return mp_obj_new_str(qstr_from_str_take(buf, /*out_sz,*/ sz + 1));
30 }
31}
32
33static mp_obj_t stream_write(mp_obj_t self_in, mp_obj_t arg) {
34 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)self_in;
35 if (o->type->stream_p.write == NULL) {
36 // CPython: io.UnsupportedOperation, OSError subclass
37 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
38 }
39
40 const char *buf = qstr_str(mp_obj_get_qstr(arg));
41 machine_int_t sz = strlen(buf);
42 int error;
43 machine_int_t out_sz = o->type->stream_p.write(self_in, buf, sz, &error);
44 if (out_sz == -1) {
Damien George6c73ca12014-01-08 18:11:23 +000045 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020046 } else {
47 // http://docs.python.org/3/library/io.html#io.RawIOBase.write
48 // "None is returned if the raw stream is set not to block and no single byte could be readily written to it."
49 // Do they mean that instead of 0 they return None?
50 return MP_OBJ_NEW_SMALL_INT(out_sz);
51 }
52}
53
Paul Sokolovsky52254502014-01-13 23:25:33 +020054// TODO: should be in mpconfig.h
55#define READ_SIZE 256
56static 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
Paul Sokolovsky323c09e2014-01-16 12:52:03 +020091 buf = vstr_str(vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +020092 buf[total_size] = 0;
93 return mp_obj_new_str(qstr_from_str_take(buf, total_size + 1));
94}
95
Paul Sokolovsky9953ca42014-01-15 23:39:44 +020096// Unbuffered, inefficient implementation of readline() for raw I/O files.
97static mp_obj_t stream_unbuffered_readline(int n_args, const mp_obj_t *args) {
98 struct _mp_obj_base_t *o = (struct _mp_obj_base_t *)args[0];
99 if (o->type->stream_p.read == NULL) {
100 // CPython: io.UnsupportedOperation, OSError subclass
101 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_OSError, "Operation not supported"));
102 }
103
104 machine_int_t max_size = -1;
105 if (n_args > 1) {
106 max_size = MP_OBJ_SMALL_INT_VALUE(args[1]);
107 }
108
109 vstr_t *vstr;
110 if (max_size != -1) {
111 vstr = vstr_new_size(max_size + 1); // TODO: \0
112 } else {
113 vstr = vstr_new();
114 }
115
116 int error;
117 while (max_size == -1 || max_size-- != 0) {
118 char *p = vstr_add_len(vstr, 1);
119 if (p == NULL) {
120 // TODO
121 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError/*MP_QSTR_RuntimeError*/, "Out of memory"));
122 }
123
124 machine_int_t out_sz = o->type->stream_p.read(o, p, 1, &error);
125 if (out_sz == -1) {
126 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
127 }
128 if (out_sz == 0 || *p == '\n') {
129 break;
130 }
131 }
132 // TODO: \0
133 vstr_add_byte(vstr, 0);
134 vstr_shrink(vstr);
135 return mp_obj_new_str(qstr_from_str_take(vstr_str(vstr), vstr_len(vstr)));
136}
137
138
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200139MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_read_obj, stream_read);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200140MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200141MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200142MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);