Skip to content

Commit c797f3c

Browse files
committed
shared-module: Fix os.listdir to correctly return list rather than
iterator.
1 parent a0058e6 commit c797f3c

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

shared-module/os/__init__.c

+19-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ STATIC mp_vfs_mount_t *lookup_path(const char* path, mp_obj_t *path_out) {
4444
const char *p_out;
4545
mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &p_out);
4646
if (vfs != MP_VFS_NONE && vfs != MP_VFS_ROOT) {
47-
*path_out = mp_obj_new_str_of_type(mp_obj_get_type(path),
47+
*path_out = mp_obj_new_str_of_type(&mp_type_str,
4848
(const byte*)p_out, strlen(p_out));
4949
}
5050
return vfs;
@@ -95,18 +95,28 @@ mp_obj_t common_hal_os_listdir(const char* path) {
9595
mp_obj_t path_out;
9696
mp_vfs_mount_t *vfs = lookup_path(path, &path_out);
9797

98+
mp_vfs_ilistdir_it_t iter;
99+
mp_obj_t iter_obj = MP_OBJ_FROM_PTR(&iter);
100+
98101
if (vfs == MP_VFS_ROOT) {
99102
// list the root directory
100-
mp_vfs_ilistdir_it_t *iter = m_new_obj(mp_vfs_ilistdir_it_t);
101-
iter->base.type = &mp_type_polymorph_iter;
102-
iter->iternext = mp_vfs_ilistdir_it_iternext;
103-
iter->cur.vfs = MP_STATE_VM(vfs_mount_table);
104-
iter->is_str = mp_obj_get_type(path) == &mp_type_str;
105-
iter->is_iter = false;
106-
return MP_OBJ_FROM_PTR(iter);
103+
iter.base.type = &mp_type_polymorph_iter;
104+
iter.iternext = mp_vfs_ilistdir_it_iternext;
105+
iter.cur.vfs = MP_STATE_VM(vfs_mount_table);
106+
iter.is_str = mp_obj_get_type(path) == &mp_type_str;
107+
iter.is_iter = false;
108+
} else {
109+
iter_obj = mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &path_out);
107110
}
108111

109-
return mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &path_out);
112+
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
113+
mp_obj_t next;
114+
while ((next = mp_iternext(iter_obj)) != MP_OBJ_STOP_ITERATION) {
115+
mp_obj_t *items;
116+
mp_obj_get_array_fixed_n(next, 3, &items);
117+
mp_obj_list_append(dir_list, items[0]);
118+
}
119+
return dir_list;
110120
}
111121

112122
void common_hal_os_mkdir(const char* path) {

0 commit comments

Comments
 (0)