Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion xarray/backends/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import os
from collections.abc import (
Callable,
Expand Down Expand Up @@ -102,7 +103,8 @@ def _get_mtime(filename_or_obj):
path = None

if path and not is_remote_uri(path):
mtime = os.path.getmtime(os.path.expanduser(filename_or_obj))
with contextlib.suppress(OSError):
mtime = os.path.getmtime(os.path.expanduser(filename_or_obj))

return mtime

Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -8140,3 +8140,14 @@ def test_h5netcdf_storage_options() -> None:
storage_options={"skip_instance_cache": False},
) as ds:
assert_identical(xr.concat([ds1, ds2], dim="time", data_vars="all"), ds)


def test_get_mtime_non_file_paths() -> None:
"""_get_mtime should not raise on non-file paths (gh#11386)."""
from xarray.backends.api import _get_mtime

# Non-existent local path should return None, not crash
assert _get_mtime("/nonexistent/path.nc") is None

# GDAL virtual filesystem paths are not real files
assert _get_mtime("/vsicurl/https://example.com/file.nc") is None
Loading