Content-Length: 318537 | pFad | http://github.com/micropython/micropython/commit/e4051a1ca66703090383678a1747c3f99e993309

2B extmod/vfs_rom: Implement minimal VfsRom.getcwd() method. · micropython/micropython@e4051a1 · GitHub
Skip to content

Commit e4051a1

Browse files
committed
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>
1 parent c68a40a commit e4051a1

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

extmod/vfs_rom.c

+8
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ static mp_obj_t vfs_rom_chdir(mp_obj_t self_in, mp_obj_t path_in) {
300300
}
301301
static MP_DEFINE_CONST_FUN_OBJ_2(vfs_rom_chdir_obj, vfs_rom_chdir);
302302

303+
static mp_obj_t vfs_rom_getcwd(mp_obj_t self_in) {
304+
(void)self_in;
305+
// The current directory is always the root of the ROMFS.
306+
return MP_OBJ_NEW_QSTR(MP_QSTR_);
307+
}
308+
static MP_DEFINE_CONST_FUN_OBJ_1(vfs_rom_getcwd_obj, vfs_rom_getcwd);
309+
303310
typedef struct _vfs_rom_ilistdir_it_t {
304311
mp_obj_base_t base;
305312
mp_fun_1_t iternext;
@@ -436,6 +443,7 @@ static const mp_rom_map_elem_t vfs_rom_locals_dict_table[] = {
436443
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&vfs_rom_open_obj) },
437444

438445
{ MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&vfs_rom_chdir_obj) },
446+
{ MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&vfs_rom_getcwd_obj) },
439447
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&vfs_rom_ilistdir_obj) },
440448
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&vfs_rom_stat_obj) },
441449
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&vfs_rom_statvfs_obj) },

tests/extmod/vfs_rom.py

+2
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,11 @@ def test_listdir(self):
408408

409409
def test_chdir(self):
410410
os.chdir("/test_rom")
411+
self.assertEqual(os.getcwd(), "/test_rom")
411412
self.assertEqual(os.listdir(), self.romfs_listdir)
412413

413414
os.chdir("/test_rom/")
415+
self.assertEqual(os.getcwd(), "/test_rom")
414416
self.assertEqual(os.listdir(), self.romfs_listdir)
415417

416418
# chdir within the romfs is not implemented.

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/commit/e4051a1ca66703090383678a1747c3f99e993309

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy