[Python-Dev] My summary of the scandir (PEP 471)

Jonas Wielicki j.wielicki at sotecware.net
Tue Jul 1 16:59:13 CEST 2014


On 01.07.2014 15:00, Ben Hoyt wrote:
> I'm leaning towards preferring #2 (Nick's proposal) because it solves
> or gets around the caching issue. My one concern is error handling. Is
> it an issue if scandir's __next__ can raise an OSError either from the
> readdir() call or the call to stat()? My thinking is probably not. In
> practice, would it ever really happen that readdir() would succeed but
> an os.stat() immediately after would fail? I guess it could if the
> file is deleted, but then if it were deleted a microsecond earlier the
> readdir() would fail anyway, or not? Or does readdir give you a
> consistent, "snap-shotted" view on things?

No need for a microsecond-timed deletion -- a directory with +r but
without +x will allow you to list the entries, but stat calls on the
files will fail with EPERM:

    $ ls -l
    drwxr--r--.   2 root     root        60  1. Jul 16:52 test

    $ sudo ls -l test
    total 0
    -rw-r--r--. 1 root root 0  1. Jul 16:52 foo

    $ ls test
    ls: cannot access test/foo: Permission denied
    total 0
    -????????? ? ? ? ?             ? foo

    $ stat test/foo
    stat: cannot stat ‘test/foo’: Permission denied

I had the idea to treat a failing lstat() inside scandir() as if the
entry wasn’t found at all, but in this context, this seems wrong too.

regards,
jwi




More information about the Python-Dev mailing list