extmod/vfs_rom: Implement minimal VfsRom.getcwd() method.
This is needed if you chdir to a ROMFS and want to query your current
directory.
Prior to this change, using `os.getcwd()` when in a ROMFS would raise:
AttributeError: 'VfsRom' object has no attribute 'getcwd'
Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
parent
c68a40ac94
commit
e4051a1ca6
2 changed files with 10 additions and 0 deletions
|
|
@ -300,6 +300,13 @@ static mp_obj_t vfs_rom_chdir(mp_obj_t self_in, mp_obj_t path_in) {
|
||||||
}
|
}
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_2(vfs_rom_chdir_obj, vfs_rom_chdir);
|
static MP_DEFINE_CONST_FUN_OBJ_2(vfs_rom_chdir_obj, vfs_rom_chdir);
|
||||||
|
|
||||||
|
static mp_obj_t vfs_rom_getcwd(mp_obj_t self_in) {
|
||||||
|
(void)self_in;
|
||||||
|
// The current directory is always the root of the ROMFS.
|
||||||
|
return MP_OBJ_NEW_QSTR(MP_QSTR_);
|
||||||
|
}
|
||||||
|
static MP_DEFINE_CONST_FUN_OBJ_1(vfs_rom_getcwd_obj, vfs_rom_getcwd);
|
||||||
|
|
||||||
typedef struct _vfs_rom_ilistdir_it_t {
|
typedef struct _vfs_rom_ilistdir_it_t {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
mp_fun_1_t iternext;
|
mp_fun_1_t iternext;
|
||||||
|
|
@ -436,6 +443,7 @@ static const mp_rom_map_elem_t vfs_rom_locals_dict_table[] = {
|
||||||
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&vfs_rom_open_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&vfs_rom_open_obj) },
|
||||||
|
|
||||||
{ MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&vfs_rom_chdir_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&vfs_rom_chdir_obj) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&vfs_rom_getcwd_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&vfs_rom_ilistdir_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&vfs_rom_ilistdir_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&vfs_rom_stat_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&vfs_rom_stat_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&vfs_rom_statvfs_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&vfs_rom_statvfs_obj) },
|
||||||
|
|
|
||||||
|
|
@ -408,9 +408,11 @@ class TestMounted(TestBase):
|
||||||
|
|
||||||
def test_chdir(self):
|
def test_chdir(self):
|
||||||
os.chdir("/test_rom")
|
os.chdir("/test_rom")
|
||||||
|
self.assertEqual(os.getcwd(), "/test_rom")
|
||||||
self.assertEqual(os.listdir(), self.romfs_listdir)
|
self.assertEqual(os.listdir(), self.romfs_listdir)
|
||||||
|
|
||||||
os.chdir("/test_rom/")
|
os.chdir("/test_rom/")
|
||||||
|
self.assertEqual(os.getcwd(), "/test_rom")
|
||||||
self.assertEqual(os.listdir(), self.romfs_listdir)
|
self.assertEqual(os.listdir(), self.romfs_listdir)
|
||||||
|
|
||||||
# chdir within the romfs is not implemented.
|
# chdir within the romfs is not implemented.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue