diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-20 23:49:56 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-20 23:50:55 +0200 |
commit | 51ee44a718668964f2986096b3cb5ac45fb85439 (patch) | |
tree | da123108a7c91e58fd7db33f49ebc1ef28392f74 /unix/file.c | |
parent | 8965a5eb1e9d7df9018fc9537169f7bb9a523d9c (diff) |
unix file: Refactor and add sys.stdout/stdin/stderr.
Diffstat (limited to 'unix/file.c')
-rw-r--r-- | unix/file.c | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/unix/file.c b/unix/file.c index bbe84dfb4..a14f5895b 100644 --- a/unix/file.c +++ b/unix/file.c @@ -8,6 +8,7 @@ #include "mpconfig.h" #include "mpqstr.h" #include "obj.h" +#include "runtime.h" #include "stream.h" typedef struct _mp_obj_fdfile_t { @@ -15,6 +16,8 @@ typedef struct _mp_obj_fdfile_t { int fd; } mp_obj_fdfile_t; +static const mp_obj_type_t rawfile_type; + 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); @@ -45,6 +48,13 @@ static mp_obj_t fdfile_close(mp_obj_t self_in) { } static MP_DEFINE_CONST_FUN_OBJ_1(fdfile_close_obj, fdfile_close); +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; @@ -81,11 +91,11 @@ static mp_obj_t fdfile_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const } } - o->fd = open(fname, mode, 0644); - if (o->fd == -1) { - nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_OSError, "[Errno %d]", (const char *)(machine_int_t)errno)); + int fd = open(fname, mode, 0644); + if (fd == -1) { + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", errno)); } - return o; + return fdfile_new(fd); } static const mp_method_t rawfile_type_methods[] = { @@ -117,3 +127,12 @@ mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args) { return fdfile_make_new((mp_obj_t)&rawfile_type, n_args, 0, args); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_open_obj, 1, 2, mp_builtin_open); + +void file_init() { + rt_store_name(qstr_from_str_static("open"), (mp_obj_t)&mp_builtin_open_obj); + + mp_obj_t m_sys = mp_obj_new_module(qstr_from_str_static("sys")); + rt_store_attr(m_sys, qstr_from_str_static("stdin"), fdfile_new(STDIN_FILENO)); + rt_store_attr(m_sys, qstr_from_str_static("stdout"), fdfile_new(STDOUT_FILENO)); + rt_store_attr(m_sys, qstr_from_str_static("stderr"), fdfile_new(STDERR_FILENO)); +} |