Skip to content
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

Catch 'PermissionError' for unreadable directories #1706

Merged
merged 4 commits into from
Dec 7, 2020

Conversation

ColdGrub1384
Copy link
Contributor

@ColdGrub1384 ColdGrub1384 commented Dec 6, 2020

Hi. The master branch of Jedi raises a PermissionError on iOS. I added two try excepts on code that assumes a directory is readable. That may be useful on other platforms too. That was the error:

  File "/private/var/containers/Bundle/Application/2FB8EF9C-46B5-4A69-8D4F-0D1115D2BED0/Pyto.app/Lib/_codecompletion.py", line 59, in suggestForCode
    script = jedi.Script(code, path=path)
  File "/private/var/containers/Bundle/Application/2FB8EF9C-46B5-4A69-8D4F-0D1115D2BED0/Pyto.app/site-packages/jedi/api/__init__.py", line 145, in __init__
    project = get_default_project(None if self.path is None else self.path.parent)
  File "/private/var/containers/Bundle/Application/2FB8EF9C-46B5-4A69-8D4F-0D1115D2BED0/Pyto.app/site-packages/jedi/api/project.py", line 422, in get_default_project
    if probable_path is None and _is_potential_project(dir):
  File "/private/var/containers/Bundle/Application/2FB8EF9C-46B5-4A69-8D4F-0D1115D2BED0/Pyto.app/site-packages/jedi/api/project.py", line 369, in _is_potential_project
    if path.joinpath(name).exists():
  File "/var/mobile/Containers/Data/Application/4A635FFF-9668-44D3-9A19-6D54C2D289FF/Library/python38/pathlib.py", line 1370, in exists
    self.stat()
  File "/var/mobile/Containers/Data/Application/4A635FFF-9668-44D3-9A19-6D54C2D289FF/Library/python38/pathlib.py", line 1176, in stat
    return self._accessor.stat(self)
PermissionError: [Errno 1] Operation not permitted: '/private/var/mobile/Library/Mobile Documents/setup.py'

/private/var/mobile/Library/Mobile Documents is the directory where all iCloud Drive files are stored, but it's sandboxed so apps can only read their own container which are located in this directory.

Sorry, I don't think iOS is a priority here, but other computers with strict permissions could also raise this error so I think it would be better anyway to handle those cases.

@codecov-io
Copy link

Codecov Report

Merging #1706 (83d4ec9) into master (69750b9) will decrease coverage by 0.03%.
The diff coverage is 60.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1706      +/-   ##
==========================================
- Coverage   94.55%   94.52%   -0.04%     
==========================================
  Files          79       79              
  Lines       11754    11760       +6     
==========================================
+ Hits        11114    11116       +2     
- Misses        640      644       +4     
Impacted Files Coverage Δ
jedi/api/project.py 96.38% <60.00%> (-0.87%) ⬇️
jedi/inference/sys_path.py 92.66% <60.00%> (-1.22%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 69750b9...83d4ec9. Read the comment docs.

Copy link
Owner

@davidhalter davidhalter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch, I guess it makes sense. If you write a test that's obviously a plus, but I'm fine here without them as well.

try:
if path.joinpath(name).exists():
return True
except PermissionError:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just open for discussion: Shouldn't we use OSError here? I'm pretty sure that we do not care here if we can read it. If it's not readable, it should probably be ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just replaced PermissionError by OSError

if parent.joinpath(filename).is_file():
return parent
except PermissionError:
continue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

@ColdGrub1384
Copy link
Contributor Author

I just added some tests

]
)
def test_get_parent_dir_with_file(path, filename, expected):
assert sys_path._get_parent_dir_with_file(path, filename) == expected
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since os.path.basename(__file__), might be part of a directory ancestor, we should not test like this. Most of the time this will not be an issue for people, but if it is it's very confusing.

IMO this function is tested well enough with other means and we can probably just omit this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I removed it

@davidhalter davidhalter merged commit 42a759a into davidhalter:master Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants