Why exception from os.path.exists()?

Peter J. Holzer hjp-python at hjp.at
Mon Jun 11 06:31:09 EDT 2018


On 2018-06-11 01:06:37 +0000, Steven D'Aprano wrote:
> On Sun, 10 Jun 2018 23:57:35 +0200, Peter J. Holzer wrote:

[Note: I was talking about os.stat here, not os.path.exists. I agree
that os.path.exists (and the other boolean functions) should simply
return false]

> > I think this is worth keeping, and "I couldn't pass that file name to
> > the OS" is a different error than "the OS told me the file doesn't
> > exist", so I think it should be a different exception.
> 
> What makes you think that NUL bytes are a fundamental limitation that no 
> OS could every cope with?

What makes you think that I think that? We are talking about
platform-specific code here. On POSIX systems, there IS NO WAY to pass a
filename with an embedded NUL byte to the OS. On such systems Python
MUST NOT simply pass a pointer to the start of the (utf-8 encoded)
string to the OS, it must take special action. It could fake an ENOENT
error, but that would be confusing in many situations. Therefore it
should raise an exception which cannot be confused with an error
returned from the OS.


> Classic Mac OS takes file names as Pascal strings, with a length byte and 
> an array of arbitrary bytes, no NUL terminator required.

On such a system os.stat would have to check that filename is less than
256 bytes and raise an Exception otherwise. In this case it is even more
obvious, because there is no Python structure which it can simply pass
to the OS.

> Baking a limitation of some file systems into the high-level interface is 
> simply a *bad idea*.

We aren't talking about a high-level interface here. We are talking about
low-level code which is just above the OS. THAT code MUST make sure that
it calls the OS API with meaningful parameters or not at all. And it
should raise an Exception in the latter case. And that exception should
not be misleading. 

> How would you feel if there were a whole lot of ignorant Pascal 
> programmers arguing that it was fundamentally impossible for file names 
> to exceed 255 characters, and therefore os.path.exists() out to raise 
> ValueError when passed a file name of 256 characters?

You are barking up the wrong tree here.

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20180611/75196bdd/attachment.sig>


More information about the Python-list mailing list