Issue 29042: os.path.exists should not throw "Embedded NUL character" exception (original) (raw)

Issue29042

Created on 2016-12-22 04:36 by Dolda2000, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg283807 - (view) Author: (Dolda2000) Date: 2016-12-22 04:36
Currently, calling os.path.exists on a path which contains NUL characters behaves consistently with most file-system calls by throwing an exception: >>> os.path.exists('\0') Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.5/genericpath.py", line 19, in exists os.stat(path) ValueError: embedded null byte However, os.path.exists is supposed to be a predicate returning whether there exists a file named by the path; it does not specify any particular method or system call for doing the test, and so reflecting the behavior of the underlying syscall used is not obviously desirable. A path containing an embedded NUL character simply cannot name an existing file, and therefore os.path.exists should return False for such a path.
msg283821 - (view) Author: Christoph Reiter (lazka) * Date: 2016-12-22 08:54
Raising in case no valid path is passed seems fine to me. There are other cases where it fails: python3 -c "import os; os.path.exists('\ud83d')" Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.5/genericpath.py", line 19, in exists os.stat(path) UnicodeEncodeError: 'utf-8' codec can't encode character '\ud83d' in position 0: surrogates not allowed LANG=C python3 -c "import os; os.path.exists('\xff')" ~ Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.5/genericpath.py", line 19, in exists os.stat(path) UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 0: ordinal not in range(128)
msg308564 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-12-18 14:36
> A path containing an embedded NUL character simply cannot name an existing file, and therefore os.path.exists should return False for such a path. I disagree. Python doesn't call the syscall and so must raise a different exception. You must not pass a path with embedded NULL character/byte. That's all. Write your own wrapper to os.path.exists() if you want to a different behaviour.
History
Date User Action Args
2022-04-11 14:58:41 admin set github: 73228
2017-12-18 14:36:01 vstinner set status: open -> closednosy: + vstinnermessages: + resolution: not a bugstage: resolved
2016-12-22 08:54:01 lazka set nosy: + lazkamessages: +
2016-12-22 04:36:28 Dolda2000 create