Skip to content

Commit 8cf7e3c

Browse files
committed
interpreters/python: add wrapper to initialize Python
This wrapper application checks if the Python's modules are already mounted (and mounts them, if not), sets the necessary environment variables and, then, runs the Python interpreter.
1 parent 7009201 commit 8cf7e3c

File tree

3 files changed

+90
-38
lines changed

3 files changed

+90
-38
lines changed

interpreters/python/Kconfig

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ config INTERPRETER_CPYTHON_STACKSIZE
2727

2828
config INTERPRETER_CPYTHON_PRIORITY
2929
int "CPython task priority"
30-
default 150
30+
default 100
3131
---help---
3232
This is the priority of the CPython task.
3333

@@ -37,22 +37,4 @@ config INTERPRETER_CPYTHON_PROGNAME
3737
---help---
3838
This is the name of the program that will be used from the nsh.
3939

40-
config INTERPRETER_CPYTHON_MOUNT_MODULES_STACKSIZE
41-
int "CPython's Modules Mount stack size"
42-
default 4096
43-
---help---
44-
This is the stack size allocated when the CPython's Modules Mount task runs.
45-
46-
config INTERPRETER_CPYTHON_MOUNT_MODULES_PRIORITY
47-
int "CPython's Modules Mount task priority"
48-
default 150
49-
---help---
50-
This is the priority of the CPython's Modules Mount task.
51-
52-
config INTERPRETER_CPYTHON_MOUNT_MODULES_PROGNAME
53-
string "CPython's Modules Mount app name"
54-
default "python_mount_modules"
55-
---help---
56-
This is the name of the program that will be used from the nsh.
57-
5840
endif

interpreters/python/Makefile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,7 @@ PROGNAME += $(CONFIG_INTERPRETER_CPYTHON_PROGNAME)
175175
PRIORITY += $(CONFIG_INTERPRETER_CPYTHON_PRIORITY)
176176
STACKSIZE += $(CONFIG_INTERPRETER_CPYTHON_STACKSIZE)
177177

178-
MAINSRC += python.c
179-
180-
PROGNAME += $(CONFIG_INTERPRETER_CPYTHON_MOUNT_MODULES_PROGNAME)
181-
PRIORITY += $(CONFIG_INTERPRETER_CPYTHON_MOUNT_MODULES_PRIORITY)
182-
STACKSIZE += $(CONFIG_INTERPRETER_CPYTHON_MOUNT_MODULES_STACKSIZE)
183-
184-
MAINSRC += mount_modules.c
178+
MAINSRC += python_wrapper.c
185179

186180
checkgenromfs:
187181
@genromfs -h 1>/dev/null 2>&1 || { \

interpreters/python/mount_modules.c renamed to interpreters/python/python_wrapper.c

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* apps/interpreters/python/mount_modules.c
2+
* apps/interpreters/python/python_wrapper.c
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -41,11 +41,14 @@
4141
#include <fcntl.h>
4242
#include <dirent.h>
4343
#include <errno.h>
44+
#include <debug.h>
4445

4546
#include <nuttx/drivers/ramdisk.h>
4647

4748
#include "romfs_cpython_modules.h"
4849

50+
#include "Python.h"
51+
4952
/****************************************************************************
5053
* Pre-processor Definitions
5154
****************************************************************************/
@@ -61,7 +64,7 @@
6164
#endif
6265

6366
#ifndef CONFIG_CPYTHON_ROMFS_MOUNTPOINT
64-
# define CONFIG_CPYTHON_ROMFS_MOUNTPOINT "/usr/local/lib/"
67+
# define CONFIG_CPYTHON_ROMFS_MOUNTPOINT "/usr/local/lib"
6568
#endif
6669

6770
#ifdef CONFIG_DISABLE_MOUNTPOINT
@@ -85,24 +88,70 @@
8588
* Private Data
8689
****************************************************************************/
8790

88-
/****************************************************************************
89-
* Private Functions
90-
****************************************************************************/
91+
/* _PyRuntime occupies around 100kB of RAM in .data region, making it hard to
92+
* fit the interpreter into the available static RAM. Some devices may have
93+
* more RAM available on external PSRAM modules that requires to be properly
94+
* initialized before reading/writing data to it. Thus, _PyRuntime can be
95+
* moved to bss section and initialized before Python interpreter is started.
96+
*/
97+
98+
extern void _PyRuntime_Early_Init(void);
9199

92100
/****************************************************************************
93-
* Public Functions
101+
* Private Functions
94102
****************************************************************************/
95103

96104
/****************************************************************************
97-
* Name: mount_modules
105+
* Name: check_and_mount_romfs
106+
*
107+
* Description:
108+
* Check if the ROMFS is already mounted, and if not, mount it.
109+
*
110+
* Input Parameters:
111+
* None
112+
*
113+
* Returned Value:
114+
* 0 on success, 1 on failure
115+
*
98116
****************************************************************************/
99117

100-
int main(int argc, FAR char *argv[])
118+
static int check_and_mount_romfs(void)
101119
{
102-
int ret;
120+
int ret = OK;
103121
struct boardioc_romdisk_s desc;
122+
FILE *fp;
123+
char line[256];
124+
int is_mounted = 0;
125+
126+
/* Check if the device is already mounted */
104127

105-
/* Create a RAM disk for the test */
128+
fp = fopen("/proc/fs/mount", "r");
129+
if (fp == NULL)
130+
{
131+
printf("ERROR: Failed to open /proc/fs/mount\n");
132+
UNUSED(desc);
133+
return ret = ERROR;
134+
}
135+
136+
while (fgets(line, sizeof(line), fp))
137+
{
138+
if (strstr(line, CONFIG_CPYTHON_ROMFS_MOUNTPOINT) != NULL)
139+
{
140+
is_mounted = 1;
141+
break;
142+
}
143+
}
144+
145+
fclose(fp);
146+
147+
if (is_mounted)
148+
{
149+
_info("Device is already mounted at %s\n", CONFIG_CPYTHON_ROMFS_MOUNTPOINT);
150+
UNUSED(desc);
151+
return ret;
152+
}
153+
154+
/* Create a RAM disk */
106155

107156
desc.minor = CONFIG_CPYTHON_ROMFS_RAMDEVNO; /* Minor device number of the ROM disk. */
108157
desc.nsectors = NSECTORS(romfs_cpython_modules_img_len); /* The number of sectors in the ROM disk */
@@ -119,8 +168,8 @@ int main(int argc, FAR char *argv[])
119168

120169
/* Mount the test file system */
121170

122-
printf("Mounting ROMFS filesystem at target=%s with source=%s\n",
123-
CONFIG_CPYTHON_ROMFS_MOUNTPOINT, MOUNT_DEVNAME);
171+
_info("Mounting ROMFS filesystem at target=%s with source=%s\n",
172+
CONFIG_CPYTHON_ROMFS_MOUNTPOINT, MOUNT_DEVNAME);
124173

125174
ret = mount(MOUNT_DEVNAME, CONFIG_CPYTHON_ROMFS_MOUNTPOINT, "romfs",
126175
MS_RDONLY, NULL);
@@ -132,3 +181,30 @@ int main(int argc, FAR char *argv[])
132181

133182
return 0;
134183
}
184+
185+
/****************************************************************************
186+
* Public Functions
187+
****************************************************************************/
188+
189+
/****************************************************************************
190+
* Name: python_wrapper_main
191+
****************************************************************************/
192+
193+
int main(int argc, FAR char *argv[])
194+
{
195+
int ret;
196+
197+
ret = check_and_mount_romfs();
198+
if (ret != 0)
199+
{
200+
return ret;
201+
}
202+
203+
_PyRuntime_Early_Init();
204+
205+
setenv("PYTHONHOME", "/usr/local", 1);
206+
207+
setenv("PYTHON_BASIC_REPL", "1", 1);
208+
209+
return Py_BytesMain(argc, argv);
210+
}

0 commit comments

Comments
 (0)