Skip to content

Commit 7dffbfd

Browse files
committed
extmod/vfs_lfsx: Fix import_stat so it takes into account current dir.
CPython semantics require searching the current directory if the import is not absolute (when "" is in sys.path). Fixes issue micropython#6037.
1 parent 463c0fb commit 7dffbfd

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

extmod/vfs_lfsx.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "py/stream.h"
3232
#include "py/binary.h"
3333
#include "py/objarray.h"
34+
#include "py/objstr.h"
3435
#include "py/mperrno.h"
3536
#include "extmod/vfs.h"
3637

@@ -440,6 +441,8 @@ STATIC MP_DEFINE_CONST_DICT(MP_VFS_LFSx(locals_dict), MP_VFS_LFSx(locals_dict_ta
440441
STATIC mp_import_stat_t MP_VFS_LFSx(import_stat)(void *self_in, const char *path) {
441442
MP_OBJ_VFS_LFSx *self = self_in;
442443
struct LFSx_API (info) info;
444+
mp_obj_str_t path_obj = { { &mp_type_str }, 0, 0, (const byte *)path };
445+
path = MP_VFS_LFSx(make_path)(self, &path_obj);
443446
int ret = LFSx_API(stat)(&self->lfs, path, &info);
444447
if (ret == 0) {
445448
if (info.type == LFSx_MACRO(_TYPE_REG)) {

tests/extmod/vfs_lfs_mount.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def test(bdev, vfs_class):
5858
f.write('print("package")\n')
5959
import lfspkg
6060

61+
# chdir and import module from current directory (needs "" in sys.path)
62+
uos.mkdir("/lfs/subdir")
63+
uos.chdir("/lfs/subdir")
64+
uos.rename("/lfs/lfsmod.py", "/lfs/subdir/lfsmod2.py")
65+
import lfsmod2
66+
6167
# umount
6268
uos.umount("/lfs")
6369

@@ -72,6 +78,7 @@ def test(bdev, vfs_class):
7278

7379
sys.path.clear()
7480
sys.path.append("/lfs")
81+
sys.path.append("")
7582

7683
# run tests
7784
test(bdev, uos.VfsLfs1)

tests/extmod/vfs_lfs_mount.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
test <class 'VfsLfs1'>
22
hello from lfs
33
package
4+
hello from lfs
45
test <class 'VfsLfs2'>
56
hello from lfs
67
package
8+
hello from lfs

0 commit comments

Comments
 (0)