[mpy/files] implemented open

This commit is contained in:
M4x1m3
2020-07-05 21:15:58 +02:00
parent 61a9e492bd
commit efcf9903a9
3 changed files with 38 additions and 1 deletions

View File

@@ -1,9 +1,19 @@
#include "py/builtin.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "mod/ion/file.h"
#include <string.h>
#include "mphalport.h"
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
mp_arg_check_num(n_args, kwargs->used, 1, 2, false);
if (n_args == 2) {
return file_open_mode(args[0], args[1]);
} else {
return file_open(args[0]);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);

View File

@@ -7,6 +7,8 @@ extern "C" {
#include <py/stream.h>
#include <py/builtin.h>
#include <py/obj.h>
#include <py/misc.h>
#include "file.h"
}
#include <algorithm>
@@ -156,7 +158,8 @@ extern const mp_obj_type_t file_type = {
0, // flags
MP_QSTR_file, // name
file_print, // __repr__, __srt__
file_make_new, // __new__, __init__
// file_make_new, // __new__, __init__
nullptr, // __new__, __init__
nullptr, // __call__
nullptr, // unary operations
nullptr, // binary operations
@@ -998,3 +1001,18 @@ STATIC mp_obj_t file_truncate(size_t n_args, const mp_obj_t* args) {
return mp_const_none;
}
mp_obj_t file_open(mp_obj_t file_name) {
mp_obj_t args[1];
args[0] = file_name;
return file_make_new(nullptr, 1, 0, args);
}
mp_obj_t file_open_mode(mp_obj_t file_name, mp_obj_t file_mode) {
mp_obj_t args[2];
args[0] = file_name;
args[1] = file_mode;
return file_make_new(nullptr, 2, 0, args);
}

View File

@@ -0,0 +1,9 @@
#ifndef MP_MOD_FILE_H
#define MP_MOD_FILE_H
#include <py/obj.h>
mp_obj_t file_open(mp_obj_t file_name);
mp_obj_t file_open_mode(mp_obj_t file_name, mp_obj_t file_mode);
#endif