Why exception from os.path.exists()?

Peter J. Holzer hjp-python at hjp.at
Sat Jun 2 11:20:59 EDT 2018


On 2018-06-02 01:51:07 +0000, Grant Edwards wrote:
> On 2018-06-01, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
> > But since "\0" is the correct type (a string), and the fact that it 
> > happens to be illegal on POSIX is a platform-dependent detail of no more 
> > importance than the fact that "?" is illegal on Windows, it should be 
> > treated as any other platform-dependent illegal file and return False.
> 
> That sounds reasonable.
> 
> What about the case where somebody calls
> 
> os.path.exists("/tmp/foo\x00bar")
> 
> If /tmp/foo exists should it return True?

No.

> That's what would happen if you passed that string directly to the
> libc call.

You can't really pass that string directly to the libc call. The libc
calling convention uses \0 as the string delimiter, so you are really
passing "/tmp/foo" to the libc (there happen to be more bytes after the
terminator, but they are not part of the string as far as libc is
concerned), which is not the same as "/tmp/foo\x00bar".

So that has to be handled by Python before calling libc.

        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/20180602/dea4ab19/attachment.sig>


More information about the Python-list mailing list