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

Raise PermissionError when insufficient permissions #7629

Conversation

ColemanTom
Copy link
Contributor

@ColemanTom ColemanTom commented Mar 15, 2023

Previously

(xr) $ chmod 000 testfile.nc
(xr) $ python -c 'import xarray as xr
f=xr.open_dataarray("testfile.nc").load()'
MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/plugins.py:139: RuntimeWarning: 'netcdf4' fails while guessing
  warnings.warn(f"{engine!r} fails while guessing", RuntimeWarning)
MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/plugins.py:148: RuntimeWarning: 'netcdf4' fails while guessing
  warnings.warn(f"{engine!r} fails while guessing", RuntimeWarning)
MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/plugins.py:148: RuntimeWarning: 'h5netcdf' fails while guessing
  warnings.warn(f"{engine!r} fails while guessing", RuntimeWarning)
MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/plugins.py:148: RuntimeWarning: 'scipy' fails while guessing
  warnings.warn(f"{engine!r} fails while guessing", RuntimeWarning)
Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/api.py", line 687, in open_dataarray
    dataset = open_dataset(
  File "MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/api.py", line 510, in open_dataset
    engine = plugins.guess_engine(filename_or_obj)
  File "MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/plugins.py", line 177, in guess_engine
    raise ValueError(error_msg)
ValueError: did not find a match in any of xarray's currently installed IO backends ['netcdf4']. Consider explicitly selecting one of the installed engines via the ``engine`` parameter, or installing additional IO dependencies, see:
https://docs.xarray.dev/en/stable/getting-started-guide/installing.html
https://docs.xarray.dev/en/stable/user-guide/io.html

Now

(xr) $ chmod 000 testfile.nc
(xr) $ python -c 'import xarray as xr
f=xr.open_dataarray("testfile.nc").load()'
Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/api.py", line 687, in open_dataarray
    dataset = open_dataset(
  File "MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/api.py", line 510, in open_dataset
    engine = plugins.guess_engine(filename_or_obj)
  File "MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/plugins.py", line 136, in guess_engine
    if backend.guess_can_open(store_spec):
  File "MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/backends/netCDF4_.py", line 547, in guess_can_open
    magic_number = try_read_magic_number_from_path(filename_or_obj)
  File "MY_HOME/miniconda3/envs/xr/lib/python3.9/site-packages/xarray/core/utils.py", line 673, in try_read_magic_number_from_path
    with open(path, "rb") as f:
PermissionError: [Errno 13] Permission denied: 'testfile.nc'

@ColemanTom ColemanTom marked this pull request as ready for review March 15, 2023 00:58
@ColemanTom ColemanTom force-pushed the 6523-insufficient-permissions-do-not-raise-permissionerror branch from 09260fb to 90dadc8 Compare March 15, 2023 01:19
Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

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

I think that's a good idea.

Hopefully this does not break too many try-exept ValueError blocks for users.
But I think a deprecation cycle won't have much effect for something that occurs so rarely.

@ColemanTom ColemanTom force-pushed the 6523-insufficient-permissions-do-not-raise-permissionerror branch from 90dadc8 to 0af200d Compare March 20, 2023 00:13
@ColemanTom
Copy link
Contributor Author

Just resolved merge conflicts in whats-new.rst.

Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

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

Looks good.

doc/whats-new.rst Outdated Show resolved Hide resolved
@ColemanTom ColemanTom force-pushed the 6523-insufficient-permissions-do-not-raise-permissionerror branch from 0af200d to c7bac02 Compare March 23, 2023 00:19
doc/whats-new.rst Outdated Show resolved Hide resolved
@dcherian
Copy link
Contributor

Thanks @ColemanTom !

@dcherian dcherian enabled auto-merge (squash) March 26, 2023 20:01
@dcherian dcherian added the plan to merge Final call for comments label Mar 26, 2023
@dcherian dcherian merged commit 86f3f21 into pydata:main Mar 26, 2023
dcherian added a commit to kmsquire/xarray that referenced this pull request Mar 29, 2023
* upstream/main:
  Save groupby codes after factorizing, pass to flox (pydata#7206)
  [skip-ci] Add compute to groupby benchmarks (pydata#7690)
  Delete built-in cfgrib backend (pydata#7670)
  Added a pronunciation guide to the word Xarray in the README.MD fil… (pydata#7677)
  boundarynorm fix (pydata#7553)
  Fix lazy negative slice rewriting. (pydata#7586)
  [pre-commit.ci] pre-commit autoupdate (pydata#7687)
  Adjust sidebar font colors (pydata#7674)
  Bump pypa/gh-action-pypi-publish from 1.8.1 to 1.8.3 (pydata#7682)
  Raise PermissionError when insufficient permissions (pydata#7629)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Insufficient permissions raise IO ValueError instead of PermissionError
3 participants