blob: d5884ae819864893c8f38c8ba97b39b72f67104f [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
Paul Sokolovskya671f892014-01-16 12:53:46 +020013static mp_obj_t stream_readall(mp_obj_t self_in);
14
15static mp_obj_t stream_read(int n_args, const mp_obj_t *args) {
16 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 }
Paul Sokolovskye98cf402014-01-08 02:43:48 +020026 // +1 because so far we mark end of string with \0
27 char *buf = m_new(char, sz + 1);
28 int error;
Paul Sokolovskya671f892014-01-16 12:53:46 +020029 machine_int_t out_sz = o->type->stream_p.read(o, buf, sz, &error);
Paul Sokolovskye98cf402014-01-08 02:43:48 +020030 if (out_sz == -1) {
Damien George6c73ca12014-01-08 18:11:23 +000031 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020032 } else {
33 buf[out_sz] = 0;
34 return mp_obj_new_str(qstr_from_str_take(buf, /*out_sz,*/ sz + 1));
35 }
36}
37
38static 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 George6c73ca12014-01-08 18:11:23 +000050 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", error));
Paul Sokolovskye98cf402014-01-08 02:43:48 +020051 } 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 Sokolovsky52254502014-01-13 23:25:33 +020059// TODO: should be in mpconfig.h
60#define READ_SIZE 256
61static 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 Sokolovsky323c09e2014-01-16 12:52:03 +020096 buf = vstr_str(vstr);
Paul Sokolovsky52254502014-01-13 23:25:33 +020097 buf[total_size] = 0;
98 return mp_obj_new_str(qstr_from_str_take(buf, total_size + 1));
99}
100
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200101// Unbuffered, inefficient implementation of readline() for raw I/O files.
102static mp_obj_t stream_unbuffered_readline(int n_args, const mp_obj_t *args) {
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);
140 return mp_obj_new_str(qstr_from_str_take(vstr_str(vstr), vstr_len(vstr)));
141}
142
143
Paul Sokolovskya671f892014-01-16 12:53:46 +0200144MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
Paul Sokolovsky52254502014-01-13 23:25:33 +0200145MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);
Paul Sokolovsky9953ca42014-01-15 23:39:44 +0200146MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj, 1, 2, stream_unbuffered_readline);
Paul Sokolovskye98cf402014-01-08 02:43:48 +0200147MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write_obj, stream_write);