[Python-Dev] os.path.exists() / os.path.isdir() inconsistency when dealing with gvfs directories

Cameron Simpson cs at zip.com.au
Wed Jun 27 02:02:38 CEST 2012


On 27Jun2012 01:49, Giampaolo Rodolà <g.rodola at gmail.com> wrote:
| I've just noticed a strange behavior when dealing with gvfs filesystems:
| 
| giampaolo at ubuntu:~$ python -c "import os;
| print(os.path.exists('/home/giampaolo/.gvfs'))"
| True
| giampaolo at ubuntu:~$ sudo su
| root at ubuntu:~# python -c "import os;
| print(os.path.exists('/home/giampaolo/.gvfs'))"
| False
| 
| This is due to os.stat() which internally fails with PermissionError (EACCES).
| The same problem exists with os.path.isdir() which will return True as
| limited user and False as root.
| I'm not sure what's best to do here nor I know if there are other
| cases other than when dealing with gvfs which can produce similar
| behaviors but here's an idea:
| 
| - make os.path.exists() return True in case of PermissionError because
| that's supposed to mean there's an existing path to deny access to

Definitely not.

Firstly, if I ask about "a/b/c" and am denied access to "a/b", then it
would be a lie to say "c" exists - it may not.

Secondly, that's not at all what the UNIX stat() call does, and these
library calls mirror stat() very closely, possibly identically. And that
it good, because people don't need to keep two models in their head: one
for what the OS actually does and one for what Python's close-to-the-OS
library calls do.

| - fix isdir(), islink(), isfile() documentation pointing out that in
| case of EACCES/EPERM or when dealing with exotic paths/fs it may
| return incorrect results.

I don't think False in incorrect. Arguably the docs should be clearer
that True means it exists and False means it does not, or could not be
accessed. A bit like the empty() tests on Queues etc; one side of the
test is strong (at least at the time of the test) and the other is weak.

So I'd be +0.5 for making the docs more clear that True is reliable and
False may merely mean "could not access".

And -1 on changing the semantics; I think they are correct.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

Against stupidity....the Gods themselves contend in vain!


More information about the Python-Dev mailing list