osdir.com


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

Why exception from os.path.exists()?


On Tue, 05 Jun 2018 23:27:16 +1000, Chris Angelico wrote:

> And an ASCIIZ string cannot contain a byte value of zero. The parallel
> is exact.

Why should we, as Python programmers, care one whit about ASCIIZ strings? 
They're not relevant. You might as well say that file names cannot 
contain the character "?" because ASCIIZ strings don't support it.

No they don't, and yet nevertheless file names can and do contain 
characters outside of the ASCIIZ range.

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.

As a Python programmer, how does treating NUL specially make our life 
better?

I don't know what the implementation of os.path.exists is precisely, but 
in pseudocode I expect it is something like this:


if "\0" in pathname:
    panic("OH NOES A NUL WHATEVER SHALL WE DO?!?!?!")
else:
    ask the OS to do a stat on pathname
    if an error occurs:
         return False
    else:
         return True


Why not just return False instead of panicking?





-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson