Numworks Epsilon  1.4.1
Graphing Calculator Operating System
modio.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <assert.h>
28 #include <string.h>
29 
30 #include "py/runtime.h"
31 #include "py/builtin.h"
32 #include "py/stream.h"
33 #include "py/objstringio.h"
34 #include "py/frozenmod.h"
35 
36 #if MICROPY_PY_IO
37 
38 extern const mp_obj_type_t mp_type_fileio;
39 extern const mp_obj_type_t mp_type_textio;
40 
41 #if MICROPY_PY_IO_BUFFEREDWRITER
42 typedef struct _mp_obj_bufwriter_t {
43  mp_obj_base_t base;
44  mp_obj_t stream;
45  size_t alloc;
46  size_t len;
47  byte buf[0];
48 } mp_obj_bufwriter_t;
49 
50 STATIC mp_obj_t bufwriter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
51  mp_arg_check_num(n_args, n_kw, 2, 2, false);
52  size_t alloc = mp_obj_get_int(args[1]);
53  mp_obj_bufwriter_t *o = m_new_obj_var(mp_obj_bufwriter_t, byte, alloc);
54  o->base.type = type;
55  o->stream = args[0];
56  o->alloc = alloc;
57  o->len = 0;
58  return o;
59 }
60 
61 STATIC mp_uint_t bufwriter_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
62  mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
63 
64  mp_uint_t org_size = size;
65 
66  while (size > 0) {
67  mp_uint_t rem = self->alloc - self->len;
68  if (size < rem) {
69  memcpy(self->buf + self->len, buf, size);
70  self->len += size;
71  return org_size;
72  }
73 
74  // Buffer flushing policy here is to flush entire buffer all the time.
75  // This allows e.g. to have a block device as backing storage and write
76  // entire block to it. memcpy below is not ideal and could be optimized
77  // in some cases. But the way it is now it at least ensures that buffer
78  // is word-aligned, to guard against obscure cases when it matters, e.g.
79  // https://github.com/micropython/micropython/issues/1863
80  memcpy(self->buf + self->len, buf, rem);
81  buf = (byte*)buf + rem;
82  size -= rem;
83  mp_uint_t out_sz = mp_stream_write_exactly(self->stream, self->buf, self->alloc, errcode);
84  if (*errcode != 0) {
85  return MP_STREAM_ERROR;
86  }
87  // TODO: try to recover from a case of non-blocking stream, e.g. move
88  // remaining chunk to the beginning of buffer.
89  assert(out_sz == self->alloc);
90  self->len = 0;
91  }
92 
93  return org_size;
94 }
95 
96 STATIC mp_obj_t bufwriter_flush(mp_obj_t self_in) {
97  mp_obj_bufwriter_t *self = MP_OBJ_TO_PTR(self_in);
98 
99  if (self->len != 0) {
100  int err;
101  mp_uint_t out_sz = mp_stream_write_exactly(self->stream, self->buf, self->len, &err);
102  // TODO: try to recover from a case of non-blocking stream, e.g. move
103  // remaining chunk to the beginning of buffer.
104  assert(out_sz == self->len);
105  self->len = 0;
106  if (err != 0) {
107  mp_raise_OSError(err);
108  }
109  }
110 
111  return mp_const_none;
112 }
113 STATIC MP_DEFINE_CONST_FUN_OBJ_1(bufwriter_flush_obj, bufwriter_flush);
114 
115 STATIC const mp_rom_map_elem_t bufwriter_locals_dict_table[] = {
116  { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
117  { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&bufwriter_flush_obj) },
118 };
119 STATIC MP_DEFINE_CONST_DICT(bufwriter_locals_dict, bufwriter_locals_dict_table);
120 
121 STATIC const mp_stream_p_t bufwriter_stream_p = {
122  .write = bufwriter_write,
123 };
124 
125 STATIC const mp_obj_type_t bufwriter_type = {
126  { &mp_type_type },
127  .name = MP_QSTR_BufferedWriter,
128  .make_new = bufwriter_make_new,
129  .protocol = &bufwriter_stream_p,
130  .locals_dict = (mp_obj_dict_t*)&bufwriter_locals_dict,
131 };
132 #endif // MICROPY_PY_IO_BUFFEREDWRITER
133 
134 #if MICROPY_MODULE_FROZEN_STR
135 STATIC mp_obj_t resource_stream(mp_obj_t package_in, mp_obj_t path_in) {
137  size_t len;
138 
139  // As an extension to pkg_resources.resource_stream(), we support
140  // package parameter being None, the path_in is interpreted as a
141  // raw path.
142  if (package_in != mp_const_none) {
143  mp_obj_t args[5];
144  args[0] = package_in;
145  args[1] = mp_const_none; // TODO should be globals
146  args[2] = mp_const_none; // TODO should be locals
147  args[3] = mp_const_true; // Pass sentinel "non empty" value to force returning of leaf module
148  args[4] = MP_OBJ_NEW_SMALL_INT(0);
149 
150  // TODO lookup __import__ and call that instead of going straight to builtin implementation
152 
153  mp_obj_t dest[2];
154  mp_load_method_maybe(pkg, MP_QSTR___path__, dest);
155  if (dest[0] == MP_OBJ_NULL) {
157  }
158 
159  const char *path = mp_obj_str_get_data(dest[0], &len);
160  vstr_add_strn(&path_buf, path, len);
161  vstr_add_byte(&path_buf, '/');
162  }
163 
164  const char *path = mp_obj_str_get_data(path_in, &len);
165  vstr_add_strn(&path_buf, path, len);
166 
167  len = path_buf.len;
168  const char *data = mp_find_frozen_str(path_buf.buf, &len);
169  if (data != NULL) {
171  o->base.type = &mp_type_bytesio;
172  o->vstr = m_new_obj(vstr_t);
173  vstr_init_fixed_buf(o->vstr, len + 1, (char*)data);
174  o->vstr->len = len;
175  o->pos = 0;
176  return MP_OBJ_FROM_PTR(o);
177  }
178 
179  mp_obj_t path_out = mp_obj_new_str(path_buf.buf, path_buf.len, false);
180  return mp_builtin_open(1, &path_out, (mp_map_t*)&mp_const_empty_map);
181 }
182 MP_DEFINE_CONST_FUN_OBJ_2(resource_stream_obj, resource_stream);
183 #endif
184 
185 STATIC const mp_rom_map_elem_t mp_module_io_globals_table[] = {
186  { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uio) },
187  // Note: mp_builtin_open_obj should be defined by port, it's not
188  // part of the core.
189  { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
190  #if MICROPY_PY_IO_RESOURCE_STREAM
191  { MP_ROM_QSTR(MP_QSTR_resource_stream), MP_ROM_PTR(&resource_stream_obj) },
192  #endif
193  #if MICROPY_PY_IO_FILEIO
194  { MP_ROM_QSTR(MP_QSTR_FileIO), MP_ROM_PTR(&mp_type_fileio) },
195  #if MICROPY_CPYTHON_COMPAT
196  { MP_ROM_QSTR(MP_QSTR_TextIOWrapper), MP_ROM_PTR(&mp_type_textio) },
197  #endif
198  #endif
199  { MP_ROM_QSTR(MP_QSTR_StringIO), MP_ROM_PTR(&mp_type_stringio) },
200  #if MICROPY_PY_IO_BYTESIO
201  { MP_ROM_QSTR(MP_QSTR_BytesIO), MP_ROM_PTR(&mp_type_bytesio) },
202  #endif
203  #if MICROPY_PY_IO_BUFFEREDWRITER
204  { MP_ROM_QSTR(MP_QSTR_BufferedWriter), MP_ROM_PTR(&bufwriter_type) },
205  #endif
206 };
207 
208 STATIC MP_DEFINE_CONST_DICT(mp_module_io_globals, mp_module_io_globals_table);
209 
211  .base = { &mp_type_module },
212  .globals = (mp_obj_dict_t*)&mp_module_io_globals,
213 };
214 
215 #endif
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf)
Definition: vstr.c:57
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
Definition: misc.h:142
void mp_load_method_maybe(mp_obj_t obj, qstr attr, mp_obj_t *dest)
Definition: runtime.c:1040
#define assert(e)
Definition: assert.h:9
#define mp_const_none
Definition: obj.h:614
#define MP_DEFINE_CONST_DICT(dict_name, table_name)
Definition: obj.h:317
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len)
Definition: vstr.c:179
#define mp_stream_write_exactly(stream, buf, size, err)
Definition: stream.h:95
def data
Definition: i18n.py:176
#define MP_ROM_QSTR(q)
Definition: obj.h:241
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw)
Definition: argcheck.c:32
#define MP_ROM_PTR(p)
Definition: obj.h:242
#define mp_const_true
Definition: obj.h:616
void vstr_add_byte(vstr_t *vstr, byte v)
Definition: vstr.c:141
size_t len
Definition: misc.h:144
mp_obj_base_t base
Definition: obj.h:814
mp_int_t mp_obj_get_int(mp_const_obj_t arg)
Definition: obj.c:225
#define VSTR_FIXED(vstr, alloc)
Definition: misc.h:150
const mp_map_t mp_const_empty_map
Definition: map.c:38
#define STATIC
Definition: mpconfig.h:1178
const mp_obj_type_t mp_type_stringio
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name)
Definition: obj.h:285
mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args)
#define MP_OBJ_NEW_SMALL_INT(small_int)
Definition: obj.h:87
const mp_obj_module_t mp_module_io
#define m_new_obj_var(obj_type, var_type, var_num)
Definition: misc.h:62
const char * mp_obj_str_get_data(mp_obj_t self_in, size_t *len)
Definition: objstr.c:2105
#define MICROPY_ALLOC_PATH_MAX
Definition: mpconfigport.h:13
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
NORETURN void mp_raise_OSError(int errno_)
Definition: runtime.c:1464
mp_obj_t mp_obj_new_str(const char *data, size_t len, bool make_qstr_if_not_already)
Definition: objstr.c:2025
mp_obj_base_t base
Definition: objstringio.h:32
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs)
Definition: builtins.c:6
args
Definition: i18n.py:175
Definition: obj.h:356
unsigned char byte
Definition: misc.h:37
#define MP_STREAM_ERROR
Definition: stream.h:32
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
const char * mp_find_frozen_str(const char *str, size_t *len)
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name)
Definition: obj.h:288
mp_uint_t(* write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode)
Definition: obj.h:468
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
const mp_obj_type_t mp_type_module
Definition: objmodule.c:94
const mp_obj_type_t mp_type_bytesio
qstr name
Definition: obj.h:478
uint64_t mp_obj_t
Definition: obj.h:39
NORETURN void mp_raise_TypeError(const char *msg)
Definition: runtime.c:1460
#define m_new_obj(type)
Definition: misc.h:60
void * memcpy(void *dst, const void *src, size_t n)
Definition: memcpy.c:3