Why exception from os.path.exists()?

Richard Damon Richard at Damon-Family.org
Fri Jun 1 09:41:04 EDT 2018


On 5/31/18 1:43 PM, Grant Edwards wrote:
> On 2018-05-31, Paul Moore <p.f.moore at gmail.com> wrote:
>> On 31 May 2018 at 15:01, Chris Angelico <rosuav at gmail.com> wrote:
>>
>> Honestly, I think the OP's point is correct. os.path.exists should
>> simply return False if the filename has an embedded \0 - at least on
>> Unix.
> Except on the platform in quetion filenames _don't_ contain an
> embedded \0.  What was passed was _not_ a path/filename.
>
> You might as well have passed a floating point number or a dict.
>
>
I think this is a key point. os.path.exists needs to pass a null
terminated string to the system to ask it about the file. The question
comes what should we do if we pass it a value that can't (or we won't)
represent as such a string.

The confusion is that in python, a string with an embedded null is
something pretty much like a string without an embedded null, so the
programmer might not think of it as being the wrong type. Thus we have
several options.

1) we can treat os.path.exists('foo\0bar') the same as
os.path.exists(1.5) and raise the exception.

2) we can treat os.path.exists('foo\0bar') as specifying a file that can
never exists and bypass the system call are return false.

3) we can process os.path.exists('foo\0bar') by just passing the string
to the system call, making it the same as os.path.exists('foo')

The last is probably the one that we can say is likely wrong, but
arguments could be made for either of the first two.

-- 
Richard Damon




More information about the Python-list mailing list