osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why exception from os.path.exists()?


On 07-06-18 14:47, Steven D'Aprano wrote:
> On Thu, 07 Jun 2018 10:04:53 +0200, Antoon Pardon wrote:
>
>> On 07-06-18 05:55, Steven D'Aprano wrote:
>>> Python strings are rich objects which support the Unicode code point \0
>>> in them. The limitation of the Linux kernel that it relies on NULL-
>>> terminated byte strings is irrelevant to the question of what
>>> os.path.exists ought to do when given a path containing NUL. Other
>>> invalid path names return False.
>> It is not irrelevant. It makes the disctinction clear between possible
>> values and impossible values. 
> That is simply wrong. It is wrong in principle, and it is wrong in 
> practice, for reasons already covered to death in this thread.
>
> It is *wrong in practice* because other impossible values don't raise 
> ValueError, they simply return False:
>
> - illegal pathnames under Windows, those containing special 
>   characters like ? > < * etc, simply return False;
>
> - even on Linux, illegal pathnames like "" (the empty string)
>   return False;
>
> - invalid pathnames with too many path components, or too many
>   characters in a single component, simply return False;
>
> - the os.path.exists() function is not documented as making 
>   a three-way split between "exists, doesn't exist and invalid";

So? Maybe we should reconsider the above behaviour?

>
> - and it isn't even true to say that NULL is illegal in pathnames:
>   there are at least five file systems that allow either NUL bytes:
>   FAT-8, MFS, HFS, or Unicode \0 code points: HFS Plus and Apple
>   File System.

That doesn't matter much. sqrt(-1) gives a ValueError, while there
are numberdomains for which it has a value.


> And it is *wrong in principle* because in the most general case, there is 
> no way to tell which pathnames are valid or invalid without querying an 
> actual file system. In the case of Linux, any directory could be used as 
> a mount point.

I don't see how your first statement follows from that explanation. I don't
have a problem with needing to query the actual file system in order to find
out which pathnames are valid or invalid.

> Have you ever actually used this feature? When was the last time you?

This is irrelevant. You are now trying to argue the uselesness. The fact that
after consideration something turns out not very useful, is not a reason
to conclude that the factors that were taken into consideration were irrelevant.

Personaly I don't use os.path.exists because it tries to shoe horn too many
possibilities into a boolean result. Do you think os.stat("\0") should
raise FileNotFoundError?

-- 
Antoon.