|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Mark Shannon |
| 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 | + |
| 28 | + |
| 29 | + |
| 30 | +#include "py/nlr.h" |
| 31 | +#include "py/obj.h" |
| 32 | +#include "filesystem.h" |
| 33 | +#include "py/stream.h" |
| 34 | + |
| 35 | + |
| 36 | +static mp_obj_t microbit_file_writable(mp_obj_t self) { |
| 37 | + return mp_obj_new_bool(((file_descriptor_obj *)self)->writable); |
| 38 | +} |
| 39 | +MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_writable_obj, microbit_file_writable); |
| 40 | + |
| 41 | +MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_name_obj, (mp_fun_1_t)microbit_file_name); |
| 42 | + |
| 43 | +static mp_obj_t microbit_file_close_func(mp_obj_t self_in) { |
| 44 | + microbit_file_close((file_descriptor_obj *)self_in); |
| 45 | + return mp_const_none; |
| 46 | +} |
| 47 | +MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_close_obj, microbit_file_close_func); |
| 48 | + |
| 49 | +STATIC mp_obj_t file___exit__(size_t n_args, const mp_obj_t *args) { |
| 50 | + (void)n_args; |
| 51 | + return microbit_file_close_func(args[0]); |
| 52 | +} |
| 53 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(file___exit___obj, 4, 4, file___exit__); |
| 54 | + |
| 55 | +static const mp_map_elem_t microbit_bytesio_locals_dict_table[] = { |
| 56 | + { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)µbit_file_close_obj }, |
| 57 | + { MP_OBJ_NEW_QSTR(MP_QSTR_name), (mp_obj_t)µbit_file_name_obj }, |
| 58 | + { MP_ROM_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj }, |
| 59 | + { MP_ROM_QSTR(MP_QSTR___exit__), (mp_obj_t)&file___exit___obj }, |
| 60 | + { MP_OBJ_NEW_QSTR(MP_QSTR_writable), (mp_obj_t)µbit_file_writable_obj }, |
| 61 | + /* Stream methods */ |
| 62 | + { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, |
| 63 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, |
| 64 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, |
| 65 | + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, |
| 66 | +}; |
| 67 | +static MP_DEFINE_CONST_DICT(microbit_bytesio_locals_dict, microbit_bytesio_locals_dict_table); |
| 68 | + |
| 69 | +STATIC const mp_stream_p_t bytesio_stream_p = { |
| 70 | + .read = microbit_file_read, |
| 71 | + .write = microbit_file_write, |
| 72 | +}; |
| 73 | + |
| 74 | +const mp_obj_type_t microbit_bytesio_type = { |
| 75 | + { &mp_type_type }, |
| 76 | + .name = MP_QSTR_BytesIO, |
| 77 | + .print = NULL, |
| 78 | + .make_new = NULL, |
| 79 | + .call = NULL, |
| 80 | + .unary_op = NULL, |
| 81 | + .binary_op = NULL, |
| 82 | + .attr = NULL, |
| 83 | + .subscr = NULL, |
| 84 | + .getiter = NULL, |
| 85 | + .iternext = NULL, |
| 86 | + .buffer_p = {NULL}, |
| 87 | + .stream_p = &bytesio_stream_p, |
| 88 | + .bases_tuple = NULL, |
| 89 | + .locals_dict = (mp_obj_dict_t*)µbit_bytesio_locals_dict, |
| 90 | +}; |
| 91 | + |
| 92 | +static const mp_map_elem_t microbit_textio_locals_dict_table[] = { |
| 93 | + { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)µbit_file_close_obj }, |
| 94 | + { MP_OBJ_NEW_QSTR(MP_QSTR_name), (mp_obj_t)µbit_file_name_obj }, |
| 95 | + { MP_ROM_QSTR(MP_QSTR___enter__), (mp_obj_t)&mp_identity_obj }, |
| 96 | + { MP_ROM_QSTR(MP_QSTR___exit__), (mp_obj_t)&file___exit___obj }, |
| 97 | + { MP_OBJ_NEW_QSTR(MP_QSTR_writable), (mp_obj_t)µbit_file_writable_obj }, |
| 98 | + /* Stream methods */ |
| 99 | + { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, |
| 100 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readall), (mp_obj_t)&mp_stream_readall_obj }, |
| 101 | + { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, |
| 102 | + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, |
| 103 | +}; |
| 104 | +static MP_DEFINE_CONST_DICT(microbit_textio_locals_dict, microbit_textio_locals_dict_table); |
| 105 | + |
| 106 | + |
| 107 | +STATIC const mp_stream_p_t textio_stream_p = { |
| 108 | + .read = microbit_file_read, |
| 109 | + .write = microbit_file_write, |
| 110 | + .is_text = true, |
| 111 | +}; |
| 112 | + |
| 113 | +const mp_obj_type_t microbit_textio_type = { |
| 114 | + { &mp_type_type }, |
| 115 | + .name = MP_QSTR_TextIO, |
| 116 | + .print = NULL, |
| 117 | + .make_new = NULL, |
| 118 | + .call = NULL, |
| 119 | + .unary_op = NULL, |
| 120 | + .binary_op = NULL, |
| 121 | + .attr = NULL, |
| 122 | + .subscr = NULL, |
| 123 | + .getiter = NULL, |
| 124 | + .iternext = NULL, |
| 125 | + .buffer_p = {NULL}, |
| 126 | + .stream_p = &textio_stream_p, |
| 127 | + .bases_tuple = NULL, |
| 128 | + .locals_dict = (mp_obj_dict_t*)µbit_textio_locals_dict, |
| 129 | +}; |
| 130 | + |
| 131 | + |
| 132 | +static mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args) { |
| 133 | + /// -1 means default; 0 explicitly false; 1 explicitly true. |
| 134 | + int read = -1; |
| 135 | + int text = -1; |
| 136 | + if (n_args == 2) { |
| 137 | + mp_uint_t len; |
| 138 | + const char *mode = mp_obj_str_get_data(args[1], &len); |
| 139 | + for (mp_uint_t i = 0; i < len; i++) { |
| 140 | + if (mode[i] == 'r' || mode[i] == 'w') { |
| 141 | + if (read >= 0) { |
| 142 | + goto mode_error; |
| 143 | + } |
| 144 | + read = (mode[i] == 'r'); |
| 145 | + } else if (mode[i] == 'b' || mode[i] == 't') { |
| 146 | + if (text >= 0) { |
| 147 | + goto mode_error; |
| 148 | + } |
| 149 | + text = (mode[i] == 't'); |
| 150 | + } else { |
| 151 | + goto mode_error; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + mp_uint_t name_len; |
| 156 | + const char *filename = mp_obj_str_get_data(args[0], &name_len); |
| 157 | + file_descriptor_obj *res = microbit_file_open(filename, name_len, read == 0, text == 0); |
| 158 | + if (res == NULL) { |
| 159 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "file not found")); |
| 160 | + } |
| 161 | + return res; |
| 162 | +mode_error: |
| 163 | + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "illegal mode")); |
| 164 | +} |
| 165 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open); |
0 commit comments