summaryrefslogtreecommitdiffstatshomepage
path: root/unix/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'unix/file.c')
-rw-r--r--unix/file.c73
1 files changed, 49 insertions, 24 deletions
diff --git a/unix/file.c b/unix/file.c
index ada841ea2c..7f45ac4a33 100644
--- a/unix/file.c
+++ b/unix/file.c
@@ -54,11 +54,12 @@ void check_fd_is_open(const mp_obj_fdfile_t *o) {
#define check_fd_is_open(o)
#endif
-STATIC const mp_obj_type_t rawfile_type;
+extern const mp_obj_type_t mp_type_fileio;
+extern const mp_obj_type_t mp_type_textio;
STATIC void fdfile_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
mp_obj_fdfile_t *self = self_in;
- print(env, "<io.FileIO %d>", self->fd);
+ print(env, "<io.%s %d>", mp_obj_get_type_str(self), self->fd);
}
STATIC machine_int_t fdfile_read(mp_obj_t o_in, void *buf, machine_uint_t size, int *errcode) {
@@ -103,23 +104,10 @@ STATIC mp_obj_t fdfile_fileno(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fdfile_fileno_obj, fdfile_fileno);
-STATIC mp_obj_fdfile_t *fdfile_new(int fd) {
- mp_obj_fdfile_t *o = m_new_obj(mp_obj_fdfile_t);
- o->base.type = &rawfile_type;
- o->fd = fd;
- return o;
-}
-
STATIC mp_obj_t fdfile_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_obj_fdfile_t *o = m_new_obj(mp_obj_fdfile_t);
- o->base.type = type_in;
-
- if (MP_OBJ_IS_SMALL_INT(args[0])) {
- o->fd = MP_OBJ_SMALL_INT_VALUE(args[0]);
- return o;
- }
+ mp_const_obj_t type = type_in;
- const char *fname = mp_obj_str_get_str(args[0]);
const char *mode_s;
if (n_args > 1) {
mode_s = mp_obj_str_get_str(args[1]);
@@ -143,14 +131,32 @@ STATIC mp_obj_t fdfile_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
case '+':
mode |= O_RDWR;
break;
+ #if MICROPY_MOD_IO_FILEIO
+ // If we don't have io.FileIO, then files are in text mode implicitly
+ case 'b':
+ type = &mp_type_fileio;
+ break;
+ case 't':
+ type = &mp_type_textio;
+ break;
+ #endif
}
}
+ o->base.type = type;
+
+ if (MP_OBJ_IS_SMALL_INT(args[0])) {
+ o->fd = MP_OBJ_SMALL_INT_VALUE(args[0]);
+ return o;
+ }
+
+ const char *fname = mp_obj_str_get_str(args[0]);
int fd = open(fname, mode, 0644);
if (fd == -1) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT((machine_int_t)errno)));
}
- return fdfile_new(fd);
+ o->fd = fd;
+ return o;
}
STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
@@ -167,29 +173,48 @@ STATIC const mp_map_elem_t rawfile_locals_dict_table[] = {
STATIC MP_DEFINE_CONST_DICT(rawfile_locals_dict, rawfile_locals_dict_table);
-STATIC const mp_stream_p_t rawfile_stream_p = {
+#if MICROPY_MOD_IO_FILEIO
+STATIC const mp_stream_p_t fileio_stream_p = {
.read = fdfile_read,
.write = fdfile_write,
+ .is_bytes = true,
};
-STATIC const mp_obj_type_t rawfile_type = {
+const mp_obj_type_t mp_type_fileio = {
{ &mp_type_type },
.name = MP_QSTR_FileIO,
.print = fdfile_print,
.make_new = fdfile_make_new,
.getiter = mp_identity,
.iternext = mp_stream_unbuffered_iter,
- .stream_p = &rawfile_stream_p,
+ .stream_p = &fileio_stream_p,
+ .locals_dict = (mp_obj_t)&rawfile_locals_dict,
+};
+#endif
+
+STATIC const mp_stream_p_t textio_stream_p = {
+ .read = fdfile_read,
+ .write = fdfile_write,
+};
+
+const mp_obj_type_t mp_type_textio = {
+ { &mp_type_type },
+ .name = MP_QSTR_TextIOWrapper,
+ .print = fdfile_print,
+ .make_new = fdfile_make_new,
+ .getiter = mp_identity,
+ .iternext = mp_stream_unbuffered_iter,
+ .stream_p = &textio_stream_p,
.locals_dict = (mp_obj_t)&rawfile_locals_dict,
};
// Factory function for I/O stream classes
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args) {
// TODO: analyze mode and buffering args and instantiate appropriate type
- return fdfile_make_new((mp_obj_t)&rawfile_type, n_args, 0, args);
+ return fdfile_make_new((mp_obj_t)&mp_type_textio, n_args, 0, args);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open);
-const mp_obj_fdfile_t mp_sys_stdin_obj = { .base = {&rawfile_type}, .fd = STDIN_FILENO };
-const mp_obj_fdfile_t mp_sys_stdout_obj = { .base = {&rawfile_type}, .fd = STDOUT_FILENO };
-const mp_obj_fdfile_t mp_sys_stderr_obj = { .base = {&rawfile_type}, .fd = STDERR_FILENO };
+const mp_obj_fdfile_t mp_sys_stdin_obj = { .base = {&mp_type_textio}, .fd = STDIN_FILENO };
+const mp_obj_fdfile_t mp_sys_stdout_obj = { .base = {&mp_type_textio}, .fd = STDOUT_FILENO };
+const mp_obj_fdfile_t mp_sys_stderr_obj = { .base = {&mp_type_textio}, .fd = STDERR_FILENO };