summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2022-05-25 12:04:27 +1000
committerDamien George <damien@micropython.org>2022-05-25 13:04:45 +1000
commit5956466c0ee08f7a08d274de65ff2d0ffc901137 (patch)
tree3df151037dc9f5b3ec69117c2153af2d11c938ef
parent26b1d31eda292d9cd5cfc68f14ce8055256fade8 (diff)
py/builtin: Clean up and simplify import_stat and builtin_open config.
The following changes are made: - If MICROPY_VFS is enabled then mp_vfs_import_stat and mp_vfs_open are automatically used for mp_import_stat and mp_builtin_open respectively. - If MICROPY_PY_IO is enabled then "open" is automatically included in the set of builtins, and points to mp_builtin_open_obj. This helps to clean up and simplify the most common port configuration. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--examples/embedding/hello-embed.c1
-rw-r--r--extmod/vfs.h2
-rw-r--r--mpy-cross/main.c1
-rw-r--r--ports/esp8266/main.c1
-rw-r--r--ports/javascript/main.c1
-rw-r--r--ports/minimal/main.c1
-rw-r--r--ports/nrf/modules/uos/microbitfs.h1
-rw-r--r--ports/powerpc/main.c1
-rw-r--r--ports/qemu-arm/test_main.c2
-rw-r--r--ports/teensy/lexerfrozen.c1
-rw-r--r--ports/zephyr/main.c1
-rw-r--r--py/builtin.h39
-rw-r--r--py/frozenmod.h2
-rw-r--r--py/lexer.h19
-rw-r--r--py/modbuiltins.c3
15 files changed, 56 insertions, 20 deletions
diff --git a/examples/embedding/hello-embed.c b/examples/embedding/hello-embed.c
index 9a90288cf..0b778986b 100644
--- a/examples/embedding/hello-embed.c
+++ b/examples/embedding/hello-embed.c
@@ -28,6 +28,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include "py/builtin.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/gc.h"
diff --git a/extmod/vfs.h b/extmod/vfs.h
index 23f2eac89..f577d3e33 100644
--- a/extmod/vfs.h
+++ b/extmod/vfs.h
@@ -26,7 +26,7 @@
#ifndef MICROPY_INCLUDED_EXTMOD_VFS_H
#define MICROPY_INCLUDED_EXTMOD_VFS_H
-#include "py/lexer.h"
+#include "py/builtin.h"
#include "py/obj.h"
// return values of mp_vfs_lookup_path
diff --git a/mpy-cross/main.c b/mpy-cross/main.c
index 390689fcb..f3ffff61f 100644
--- a/mpy-cross/main.c
+++ b/mpy-cross/main.c
@@ -29,6 +29,7 @@
#include <stdlib.h>
#include <unistd.h>
+#include "py/builtin.h"
#include "py/compile.h"
#include "py/persistentcode.h"
#include "py/runtime.h"
diff --git a/ports/esp8266/main.c b/ports/esp8266/main.c
index 1ae3f85bf..ded5e58ab 100644
--- a/ports/esp8266/main.c
+++ b/ports/esp8266/main.c
@@ -28,6 +28,7 @@
#include <stdio.h>
#include <string.h>
+#include "py/builtin.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
diff --git a/ports/javascript/main.c b/ports/javascript/main.c
index 04a853f66..36ea9e98b 100644
--- a/ports/javascript/main.c
+++ b/ports/javascript/main.c
@@ -29,6 +29,7 @@
#include <stdlib.h>
#include <string.h>
+#include "py/builtin.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/repl.h"
diff --git a/ports/minimal/main.c b/ports/minimal/main.c
index 006ca0a4d..dbde33915 100644
--- a/ports/minimal/main.c
+++ b/ports/minimal/main.c
@@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
+#include "py/builtin.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/repl.h"
diff --git a/ports/nrf/modules/uos/microbitfs.h b/ports/nrf/modules/uos/microbitfs.h
index 4157e8c33..d9efaabf1 100644
--- a/ports/nrf/modules/uos/microbitfs.h
+++ b/ports/nrf/modules/uos/microbitfs.h
@@ -27,6 +27,7 @@
#ifndef __MICROPY_INCLUDED_FILESYSTEM_H__
#define __MICROPY_INCLUDED_FILESYSTEM_H__
+#include "py/builtin.h"
#include "py/obj.h"
#include "py/lexer.h"
diff --git a/ports/powerpc/main.c b/ports/powerpc/main.c
index a66d737c1..ed82c517e 100644
--- a/ports/powerpc/main.c
+++ b/ports/powerpc/main.c
@@ -26,6 +26,7 @@
#include <stdio.h>
+#include "py/builtin.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/repl.h"
diff --git a/ports/qemu-arm/test_main.c b/ports/qemu-arm/test_main.c
index 284b28731..3a89b3f13 100644
--- a/ports/qemu-arm/test_main.c
+++ b/ports/qemu-arm/test_main.c
@@ -5,7 +5,7 @@
#include <malloc.h>
#include <setjmp.h>
-#include "py/obj.h"
+#include "py/builtin.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
diff --git a/ports/teensy/lexerfrozen.c b/ports/teensy/lexerfrozen.c
index 21e978dc7..672a0ce08 100644
--- a/ports/teensy/lexerfrozen.c
+++ b/ports/teensy/lexerfrozen.c
@@ -1,5 +1,6 @@
#include <stdio.h>
+#include "py/builtin.h"
#include "py/lexer.h"
#include "py/runtime.h"
#include "py/mperrno.h"
diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c
index 1275a4973..ed1ac560e 100644
--- a/ports/zephyr/main.c
+++ b/ports/zephyr/main.c
@@ -41,6 +41,7 @@
#include <storage/flash_map.h>
#include "py/mperrno.h"
+#include "py/builtin.h"
#include "py/compile.h"
#include "py/runtime.h"
#include "py/repl.h"
diff --git a/py/builtin.h b/py/builtin.h
index 162fed77c..a6f824ca2 100644
--- a/py/builtin.h
+++ b/py/builtin.h
@@ -28,8 +28,43 @@
#include "py/obj.h"
-mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args);
+typedef enum {
+ MP_IMPORT_STAT_NO_EXIST,
+ MP_IMPORT_STAT_DIR,
+ MP_IMPORT_STAT_FILE,
+} mp_import_stat_t;
+
+#if MICROPY_VFS
+
+// Delegate to the VFS for import stat and builtin open.
+
+#define mp_builtin_open_obj mp_vfs_open_obj
+
+mp_import_stat_t mp_vfs_import_stat(const char *path);
+mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
+
+MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_open_obj);
+
+static inline mp_import_stat_t mp_import_stat(const char *path) {
+ return mp_vfs_import_stat(path);
+}
+
+static inline mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
+ return mp_vfs_open(n_args, args, kwargs);
+}
+
+#else
+
+// A port can provide implementations of these functions.
+mp_import_stat_t mp_import_stat(const char *path);
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
+
+// A port can provide this object.
+MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
+
+#endif
+
+mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args);
mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args);
MP_DECLARE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj);
@@ -76,9 +111,7 @@ MP_DECLARE_CONST_FUN_OBJ_1(mp_builtin_repr_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj);
-// Defined by a port, but declared here for simplicity
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj);
-MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
MP_DECLARE_CONST_FUN_OBJ_2(mp_namedtuple_obj);
diff --git a/py/frozenmod.h b/py/frozenmod.h
index be735e85b..cff6c8616 100644
--- a/py/frozenmod.h
+++ b/py/frozenmod.h
@@ -27,7 +27,7 @@
#ifndef MICROPY_INCLUDED_PY_FROZENMOD_H
#define MICROPY_INCLUDED_PY_FROZENMOD_H
-#include "py/lexer.h"
+#include "py/builtin.h"
enum {
MP_FROZEN_NONE,
diff --git a/py/lexer.h b/py/lexer.h
index e16b9a8ce..8295dec0f 100644
--- a/py/lexer.h
+++ b/py/lexer.h
@@ -189,24 +189,15 @@ typedef struct _mp_lexer_t {
mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader);
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len, size_t free_len);
-void mp_lexer_free(mp_lexer_t *lex);
-void mp_lexer_to_next(mp_lexer_t *lex);
-
-/******************************************************************/
-// platform specific import function; must be implemented for a specific port
-// TODO tidy up, rename, or put elsewhere
-
-typedef enum {
- MP_IMPORT_STAT_NO_EXIST,
- MP_IMPORT_STAT_DIR,
- MP_IMPORT_STAT_FILE,
-} mp_import_stat_t;
-
-mp_import_stat_t mp_import_stat(const char *path);
+// If MICROPY_READER_POSIX or MICROPY_READER_VFS aren't enabled then
+// this function must be implemented by the port.
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
#if MICROPY_HELPER_LEXER_UNIX
mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd);
#endif
+void mp_lexer_free(mp_lexer_t *lex);
+void mp_lexer_to_next(mp_lexer_t *lex);
+
#endif // MICROPY_INCLUDED_PY_LEXER_H
diff --git a/py/modbuiltins.c b/py/modbuiltins.c
index 5631798fb..adb9ea40a 100644
--- a/py/modbuiltins.c
+++ b/py/modbuiltins.c
@@ -729,6 +729,9 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
#endif
{ MP_ROM_QSTR(MP_QSTR_next), MP_ROM_PTR(&mp_builtin_next_obj) },
{ MP_ROM_QSTR(MP_QSTR_oct), MP_ROM_PTR(&mp_builtin_oct_obj) },
+ #if MICROPY_PY_IO
+ { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
+ #endif
{ MP_ROM_QSTR(MP_QSTR_ord), MP_ROM_PTR(&mp_builtin_ord_obj) },
{ MP_ROM_QSTR(MP_QSTR_pow), MP_ROM_PTR(&mp_builtin_pow_obj) },
{ MP_ROM_QSTR(MP_QSTR_print), MP_ROM_PTR(&mp_builtin_print_obj) },