You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee=Noneclosed_at=<Date2019-03-20.00:52:19.850>created_at=<Date2018-05-24.12:15:27.517>labels= ['OS-mac', 'type-bug', 'library', 'expert-IO', '3.7']
title='OSError when using pathlib.Path.rglob() to list device files'updated_at=<Date2019-03-20.00:52:19.849>user='https://bugs.python.org/VictorDomingos'
This method fails with an error when it finds a character or block device (like those found in /dev on macOS). However, in the same machine, with the same Python version, the alternative os.walk() performs basically the same job with no errors. I am not sure if that error is the expected behaviour, but I wasn't able to find a clear explanation in the docs. Anyway, it seems to me, as a user, that the os.walk() error-less behaviour is more desirable.
Python 3.7.0b4 (v3.7.0b4:eb96c37699, May 2 2018, 04:13:13)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> from pathlib import Path
>>> [f for f in Path(os.path.expanduser('/dev')).rglob("*") if f.is_file()]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pathlib.py", line 1344, in is_file
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pathlib.py", line 1140, in stat
OSError: [Errno 9] Bad file descriptor: '/dev/fd/3'
This is fairly odd behaviour of macOS, the same error can be seen from a bash session:
$ stat /dev/fd/3
stat: /dev/fd/3: stat: Bad file descriptor
The reason os.walk() works while the Path().is_file doesn't is that os.walk() explicitly guards against OSError exceptions raised by os.stat().
Looking at the documentation this could be seen as a bug in macOS, as the manual page for stat(2) doesn't mention EBADF as a valid error for this system call.
I'm not sure at this point if we should add a workaround for this. An actual patch would be easy enough, "just" add EBADF to the list of ignored errno values in the implementation of is_file (and related method) in pathlib.py