[issue11406] There is no os.listdir() equivalent returning generator instead of list

Марк Коренберг report at bugs.python.org
Mon Mar 7 20:43:56 CET 2011


Марк Коренберг <socketpair at gmail.com> added the comment:

> Glibc's readdir() and readdir_r() already do caching
Yes, but glibc's readdir is the C analogue of python's generator. We do not need to create cache for cached values.
I think it's OK to make python's generator on top of readdir (instead of getdents).

Why not to create generator like this?
(pseudocode)
------------------
DIR *d;
struct dirent* entry, *e;
entry = malloc(offsetof(struct dirent, d_name) + pathconf(dirpath, _PC_NAME_MAX) + 1);
if (!e)
    raise Exception();
if (!(d= opendir(dirname)))
{
    free(e)
    raise IOException();
}

for (;;)
{
    if (readdir_r(d, entry, &e))
    {
        closedir(d);
        free(entry);
        raise IOException();
    }
    if (!e)
        break;
    yield e;
}
closedir(d);
free(entry);

------------------

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11406>
_______________________________________


More information about the Python-bugs-list mailing list