Skip to content

Commit 5956466

Browse files
committed
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 <[email protected]>
1 parent 26b1d31 commit 5956466

File tree

15 files changed

+56
-20
lines changed

15 files changed

+56
-20
lines changed

examples/embedding/hello-embed.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdio.h>
2929
#include <stdlib.h>
3030

31+
#include "py/builtin.h"
3132
#include "py/compile.h"
3233
#include "py/runtime.h"
3334
#include "py/gc.h"

extmod/vfs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#ifndef MICROPY_INCLUDED_EXTMOD_VFS_H
2727
#define MICROPY_INCLUDED_EXTMOD_VFS_H
2828

29-
#include "py/lexer.h"
29+
#include "py/builtin.h"
3030
#include "py/obj.h"
3131

3232
// return values of mp_vfs_lookup_path

mpy-cross/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <stdlib.h>
3030
#include <unistd.h>
3131

32+
#include "py/builtin.h"
3233
#include "py/compile.h"
3334
#include "py/persistentcode.h"
3435
#include "py/runtime.h"

ports/esp8266/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdio.h>
2929
#include <string.h>
3030

31+
#include "py/builtin.h"
3132
#include "py/compile.h"
3233
#include "py/runtime.h"
3334
#include "py/stackctrl.h"

ports/javascript/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <stdlib.h>
3030
#include <string.h>
3131

32+
#include "py/builtin.h"
3233
#include "py/compile.h"
3334
#include "py/runtime.h"
3435
#include "py/repl.h"

ports/minimal/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdio.h>
33
#include <string.h>
44

5+
#include "py/builtin.h"
56
#include "py/compile.h"
67
#include "py/runtime.h"
78
#include "py/repl.h"

ports/nrf/modules/uos/microbitfs.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#ifndef __MICROPY_INCLUDED_FILESYSTEM_H__
2828
#define __MICROPY_INCLUDED_FILESYSTEM_H__
2929

30+
#include "py/builtin.h"
3031
#include "py/obj.h"
3132
#include "py/lexer.h"
3233

ports/powerpc/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdio.h>
2828

29+
#include "py/builtin.h"
2930
#include "py/compile.h"
3031
#include "py/runtime.h"
3132
#include "py/repl.h"

ports/qemu-arm/test_main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <malloc.h>
66
#include <setjmp.h>
77

8-
#include "py/obj.h"
8+
#include "py/builtin.h"
99
#include "py/compile.h"
1010
#include "py/runtime.h"
1111
#include "py/stackctrl.h"

ports/teensy/lexerfrozen.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22

3+
#include "py/builtin.h"
34
#include "py/lexer.h"
45
#include "py/runtime.h"
56
#include "py/mperrno.h"

ports/zephyr/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <storage/flash_map.h>
4242

4343
#include "py/mperrno.h"
44+
#include "py/builtin.h"
4445
#include "py/compile.h"
4546
#include "py/runtime.h"
4647
#include "py/repl.h"

py/builtin.h

+36-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,43 @@
2828

2929
#include "py/obj.h"
3030

31-
mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args);
31+
typedef enum {
32+
MP_IMPORT_STAT_NO_EXIST,
33+
MP_IMPORT_STAT_DIR,
34+
MP_IMPORT_STAT_FILE,
35+
} mp_import_stat_t;
36+
37+
#if MICROPY_VFS
38+
39+
// Delegate to the VFS for import stat and builtin open.
40+
41+
#define mp_builtin_open_obj mp_vfs_open_obj
42+
43+
mp_import_stat_t mp_vfs_import_stat(const char *path);
44+
mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
45+
46+
MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_open_obj);
47+
48+
static inline mp_import_stat_t mp_import_stat(const char *path) {
49+
return mp_vfs_import_stat(path);
50+
}
51+
52+
static inline mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
53+
return mp_vfs_open(n_args, args, kwargs);
54+
}
55+
56+
#else
57+
58+
// A port can provide implementations of these functions.
59+
mp_import_stat_t mp_import_stat(const char *path);
3260
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
61+
62+
// A port can provide this object.
63+
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
64+
65+
#endif
66+
67+
mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args);
3368
mp_obj_t mp_micropython_mem_info(size_t n_args, const mp_obj_t *args);
3469

3570
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);
76111
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj);
77112
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj);
78113
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_sum_obj);
79-
// Defined by a port, but declared here for simplicity
80114
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj);
81-
MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj);
82115

83116
MP_DECLARE_CONST_FUN_OBJ_2(mp_namedtuple_obj);
84117

py/frozenmod.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#ifndef MICROPY_INCLUDED_PY_FROZENMOD_H
2828
#define MICROPY_INCLUDED_PY_FROZENMOD_H
2929

30-
#include "py/lexer.h"
30+
#include "py/builtin.h"
3131

3232
enum {
3333
MP_FROZEN_NONE,

py/lexer.h

+5-14
Original file line numberDiff line numberDiff line change
@@ -189,24 +189,15 @@ typedef struct _mp_lexer_t {
189189
mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader);
190190
mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len, size_t free_len);
191191

192-
void mp_lexer_free(mp_lexer_t *lex);
193-
void mp_lexer_to_next(mp_lexer_t *lex);
194-
195-
/******************************************************************/
196-
// platform specific import function; must be implemented for a specific port
197-
// TODO tidy up, rename, or put elsewhere
198-
199-
typedef enum {
200-
MP_IMPORT_STAT_NO_EXIST,
201-
MP_IMPORT_STAT_DIR,
202-
MP_IMPORT_STAT_FILE,
203-
} mp_import_stat_t;
204-
205-
mp_import_stat_t mp_import_stat(const char *path);
192+
// If MICROPY_READER_POSIX or MICROPY_READER_VFS aren't enabled then
193+
// this function must be implemented by the port.
206194
mp_lexer_t *mp_lexer_new_from_file(const char *filename);
207195

208196
#if MICROPY_HELPER_LEXER_UNIX
209197
mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd);
210198
#endif
211199

200+
void mp_lexer_free(mp_lexer_t *lex);
201+
void mp_lexer_to_next(mp_lexer_t *lex);
202+
212203
#endif // MICROPY_INCLUDED_PY_LEXER_H

py/modbuiltins.c

+3
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
729729
#endif
730730
{ MP_ROM_QSTR(MP_QSTR_next), MP_ROM_PTR(&mp_builtin_next_obj) },
731731
{ MP_ROM_QSTR(MP_QSTR_oct), MP_ROM_PTR(&mp_builtin_oct_obj) },
732+
#if MICROPY_PY_IO
733+
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
734+
#endif
732735
{ MP_ROM_QSTR(MP_QSTR_ord), MP_ROM_PTR(&mp_builtin_ord_obj) },
733736
{ MP_ROM_QSTR(MP_QSTR_pow), MP_ROM_PTR(&mp_builtin_pow_obj) },
734737
{ MP_ROM_QSTR(MP_QSTR_print), MP_ROM_PTR(&mp_builtin_print_obj) },

0 commit comments

Comments
 (0)