pip icon indicating copy to clipboard operation
pip copied to clipboard

Windows: Long filename detection

Open AA-Turner opened this issue 8 months ago • 4 comments

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

AA-Turner avatar Apr 23 '25 22:04 AA-Turner

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.)

bwoodsend avatar Apr 23 '25 22:04 bwoodsend

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'

AA-Turner avatar Apr 23 '25 22:04 AA-Turner

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).

zooba avatar Apr 23 '25 23:04 zooba

PRs are welcome :)

ichard26 avatar Apr 24 '25 20:04 ichard26

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.

sepehr-rs avatar Jun 30 '25 05:06 sepehr-rs