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

Remove the references to _file_obj outside low level code paths, change to _close #4809

Merged
merged 18 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion xarray/backends/rasterio_.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None, loc
dims=("band", "y", "x"),
coords=coords,
attrs=attrs,
close=manager.close,
)
result.set_close(manager.close)

if chunks is not None:
from dask.base import tokenize
Expand Down
3 changes: 2 additions & 1 deletion xarray/backends/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def open_backend_dataset_store(
decode_timedelta=decode_timedelta,
)

ds = Dataset(vars, attrs=attrs, close=store.close)
ds = Dataset(vars, attrs=attrs)
ds.set_close(store.close)
ds = ds.set_coords(coord_names.intersection(vars))
ds.encoding = encoding

Expand Down
3 changes: 2 additions & 1 deletion xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ def decode_cf(
use_cftime=use_cftime,
decode_timedelta=decode_timedelta,
)
ds = Dataset(vars, attrs=attrs, close=close)
ds = Dataset(vars, attrs=attrs)
ds.set_close(close)
ds = ds.set_coords(coord_names.union(extra_coords).intersection(vars))
ds.encoding = encoding

Expand Down
8 changes: 6 additions & 2 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ def get_squeeze_dims(
class DataWithCoords(SupportsArithmetic, AttrAccessMixin):
"""Shared base class for Dataset and DataArray."""

__slots__ = ()
_close: Optional[Callable[[], None]]

__slots__ = ("_close",)

_rolling_exp_cls = RollingExp

Expand Down Expand Up @@ -1264,9 +1266,11 @@ def where(self, cond, other=dtypes.NA, drop: bool = False):

return ops.where_method(self, cond, other)

def set_close(self, close: Optional[Callable[[], None]]) -> None:
alexamici marked this conversation as resolved.
Show resolved Hide resolved
self._close = close

def close(self: Any) -> None:
"""Close any files linked to this object"""
self._close: Optional[Callable[[], None]]
if self._close is not None:
self._close()
self._close = None
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ def __init__(
# internal parameters
indexes: Dict[Hashable, pd.Index] = None,
fastpath: bool = False,
close: Optional[Callable[[], None]] = None,
):
if fastpath:
variable = data
Expand Down Expand Up @@ -423,7 +422,7 @@ def __init__(
# public interface.
self._indexes = indexes

self._close = close
self._close = None

def _replace(
self,
Expand All @@ -442,8 +441,9 @@ def _replace(
if close is _default:
close = self._close
replaced = type(self)(
variable, coords, name=name, fastpath=True, indexes=indexes, close=close
variable, coords, name=name, fastpath=True, indexes=indexes
)
replaced.set_close(close)
return replaced

def _replace_maybe_drop_dims(
Expand Down
7 changes: 3 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ def __init__(
data_vars: Mapping[Hashable, Any] = None,
coords: Mapping[Hashable, Any] = None,
attrs: Mapping[Hashable, Any] = None,
close: Optional[Callable[[], None]] = None,
):
# TODO(shoyer): expose indexes as a public argument in __init__

Expand All @@ -689,7 +688,7 @@ def __init__(
)

self._attrs = dict(attrs) if attrs is not None else None
self._close = close
self._close = None
self._encoding = None
self._variables = variables
self._coord_names = coord_names
Expand Down Expand Up @@ -1343,8 +1342,8 @@ def _construct_dataarray(self, name: Hashable) -> "DataArray":
name=name,
indexes=indexes,
fastpath=True,
close=self._close,
)
da.set_close(self._close)
return da

def __copy__(self) -> "Dataset":
Expand Down Expand Up @@ -4808,8 +4807,8 @@ def to_array(self, dim="variable", name=None):
attrs=self.attrs,
name=name,
indexes=indexes,
close=self._close,
)
da.set_close(self._close)
return da

def _normalize_dim_order(
Expand Down