예제 #1
0
void file_init() {
    rt_store_name(MP_QSTR_open, (mp_obj_t)&mp_builtin_open_obj);

    mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
    rt_store_attr(m_sys, MP_QSTR_stdin, fdfile_new(STDIN_FILENO));
    rt_store_attr(m_sys, MP_QSTR_stdout, fdfile_new(STDOUT_FILENO));
    rt_store_attr(m_sys, MP_QSTR_stderr, fdfile_new(STDERR_FILENO));
}
예제 #2
0
파일: file.c 프로젝트: xbe/micropython
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;
    }

    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]);
    } else {
        mode_s = "r";
    }

    int mode = 0;
    while (*mode_s) {
        switch (*mode_s++) {
            // Note: these assume O_RDWR = O_RDONLY | O_WRONLY
            case 'r':
                mode |= O_RDONLY;
                break;
            case 'w':
                mode |= O_WRONLY | O_CREAT | O_TRUNC;
                break;
            case 'a':
                mode |= O_APPEND;
                break;
            case '+':
                mode |= O_RDWR;
                break;
        }
    }

    int fd = open(fname, mode, 0644);
    if (fd == -1) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", errno));
    }
    return fdfile_new(fd);
}
예제 #3
0
파일: file.c 프로젝트: xbe/micropython
void file_init() {
    mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
    mp_store_attr(m_sys, MP_QSTR_stdin, fdfile_new(STDIN_FILENO));
    mp_store_attr(m_sys, MP_QSTR_stdout, fdfile_new(STDOUT_FILENO));
    mp_store_attr(m_sys, MP_QSTR_stderr, fdfile_new(STDERR_FILENO));
}