Windows: Long filename detection
Re: https://discuss.python.org/t/pep-773-a-python-installation-manager-for-windows/77900/160
https://github.com/pypa/pip/blob/24f4600851bbb3d7f22aed0ba6b1e2dcc4973412/src/pip/_internal/commands/install.py#L777-L791
try:
Path('a' * 300 + '.txt').touch()
except OSError as e:
exc = e
This gives exc as OSError(22, 'Invalid argument'), where exc.errno == errno.EINVAL, rather than ENOENT.
Python 3.13.2; Windows 11
A
What about a path with a few slashes in it? I vaguely remember that long paths and long names are treated differently. (Sorry, I'm a long way from the nearest Windows machine or I'd just check myself.)
It turns out I do have long paths enabled (via the registry rather than group policy), sorry for the false report. The problem seen here is when the filename itself is 255 characters or longer, which does still occur on PyPI.
>>> Path('a/'*300).mkdir(parents=True, exist_ok=True)
>>> Path('a/'*300 + 'a'*251 + '.txt').touch()
>>> Path('a/'*300 + 'a'*252 + '.txt').touch()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "...\Lib\pathlib\__init__.py", line 1010, in touch
fd = os.open(self, flags, mode)
OSError: [Errno 22] Invalid argument: 'a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\a\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt'
Yeah, there are a few different errors that can occur due to long paths. But that particular one is due to a long segment, which is still limited even with the OS setting changed.
You might want to look at the .winerror attribute (if it's there). Windows has much more fine grained error codes than POSIX, and we map a lot of different errors to ENOENT and EINVAL. Still pretty sure you can't tell specifically that the path was too long, though. Catching the error and checking the path argument that was passed in is the "best" way (it's not great).
PRs are welcome :)
Hi @ichard26, I've put together a PR for this issue. If you have a moment, I’d really appreciate it if you could take a look and provide any feedback or suggestions.