This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: WindowsPath constructor does not check for invalid characters
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, pitrou
Priority: normal Keywords:

Created on 2014-07-13 01:20 by Antony.Lee, last changed 2022-04-11 14:58 by admin.

Messages (3)
msg222899 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-07-13 01:20
PureWindowsPath("foo*") returns a path object, even though it is an invalid one (e.g., open("foo*") on Windows throws an OSError for "invalid argument" rather than a FileNotFoundError).  Given the amount of checking that is done in (e.g.) with_name and with_suffix, it seems reasonable to throw an exception in this case too.
msg222904 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-07-13 03:13
Hmm, I am not sure. Is there a definite list of invalid Windows path characters, or can it be filesystem-specific?
msg222909 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-07-13 04:24
There is a list of always forbidden characters (http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions), and then a couple of obscure fs-dependent cases (http://en.wikipedia.org/wiki/Comparison_of_file_systems) but I believe excluding the main list should be enough for most purposes.
Note that PosixPath is not immune to this either, as there is one forbidden character: the null byte.

I would prefer if path.open() can only raise one of the OSError subclasses that correspond to errnos mentioned in "man 2 open".  Currently, on Windows, "Path('*').open()" raises an OSError ("invalid argument"); on Linux, "Path('\0').open()" raises a TypeError(!).
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 66168
2021-10-19 23:28:31iritkatrielsettype: enhancement
versions: + Python 3.11, - Python 3.4, Python 3.5
2014-07-13 04:24:06Antony.Leesetmessages: + msg222909
2014-07-13 03:13:48pitrousetmessages: + msg222904
2014-07-13 02:20:15ned.deilysetnosy: + pitrou
2014-07-13 01:20:03Antony.Leecreate