New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pathlib.(r)glob stops on PermissionDenied exception #68308
Comments
Debian 8 Path.(r)glob stops when it encounters a PermissionDenied exception. However, os.walk just skips files/directories it does not have permission to access. In my opinion, the os.walk behavior is better because it doesn't prevent usage in situations where a single file/directory in a tree has restricted permissions. |
From Frank Woodall on python-ideas: mkdir /tmp/path_test && cd /tmp/path_test && mkdir dir1 dir2 dir2/dir3 && touch dir1/file1 dir1/file2 dir2/file1 dir2/file2 dir2/dir3/file1 python 3.4.1 from pathlib import Path
p = Path('/tmp/path_test')
for x in p.rglob('*') : print(x) |
The attached patch adds an unaccessible directory to the pathlib tests to provoke the problem and also fixes the cause in pathlib. It applies to at least 3.4 - 3.6 |
The glob module is not affected because it ignores all OSErrors (including "Permission denied" and "Too many levels of symbolic links"). >>> import glob
>>> glob.glob('**/*', recursive=True)
['dir2/file1', 'dir2/file2', 'dir2/dir3', 'dir1/file1', 'dir1/file2'] There is no special test for PermissionError. |
I'm not sure that Path.(r)glob() (and perhaps glob.glob()) should unconditionally ignore errors. Python has lover level than shell, and even bash has an option that controls this behavior.
|
Failing to find any matches with a pattern is an entirely different class of error than finding matches but hitting permission problems or broken links or suddenly deleted files or ... If glob doesn't already have a 'failglob' option we could add that, but in a different issue. |
Thank you Ulrich for the patch. I basically agree with the solution you proposed (I'm not a fan of the non-ASCII "ASCII art", by the way, but I won't block the patch on that :-)). Have you tested the patch and tests under Windows? Otherwise, I (or someone else) will have to do that before accepting it. |
Antoine, thanks for the review. I didn't realise that Unfortunately I don't have a Windows dev environment available at the moment, so I can't easily test for that. |
Ok, thanks. The tests are running ok on my Windows VM. |
May be slightly refactor the code? def _select_from(self, parent_path, is_dir, exists, listdir):
try:
if not is_dir(parent_path):
return
yield from self._select_from2(parent_path, is_dir, exists, listdir)
except PermissionError:
return
def _select_from2(self, parent_path, is_dir, exists, listdir):
... # implementation |
I just ran into this issue. What's keeping it from being committed, except perhaps that Antoine decided to leave core developments? |
I'm just going to commit this. |
New changeset bac18cb7b011 by Guido van Rossum in branch '3.4': New changeset 224a026b4ca1 by Guido van Rossum in branch '3.5': New changeset f6ae90450a4d by Guido van Rossum in branch 'default': |
thanks On 01/06/2016 06:42 PM, Guido van Rossum wrote:
|
I think this is not fixed yet. The try/except clause is currently around certain loops whereas it should be around specific calls (is_dir, exists, listdir. and is_symlink). As it currently is, the behavior is dependent on the ordering of the names returned by listdir(), which is just wrong. I think this is the root cause of the breakage reported in issue bpo-26012. |
New changeset 4043e08e6e52 by Guido van Rossum in branch '3.4': New changeset 8a3b0c1fb3d3 by Guido van Rossum in branch '3.5': New changeset 398cb8c183da by Guido van Rossum in branch 'default': |
Should be fixed for real now. |
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
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: