@@ -522,7 +522,7 @@ def maybe_decode_store(store, chunks):
522
522
523
523
else :
524
524
ds2 = ds
525
- ds2 ._file_obj = ds ._file_obj
525
+ ds2 .set_close ( ds ._close )
526
526
return ds2
527
527
528
528
filename_or_obj = _normalize_path (filename_or_obj )
@@ -701,7 +701,7 @@ def open_dataarray(
701
701
else :
702
702
(data_array ,) = dataset .data_vars .values ()
703
703
704
- data_array ._file_obj = dataset ._file_obj
704
+ data_array .set_close ( dataset ._close )
705
705
706
706
# Reset names if they were changed during saving
707
707
# to ensure that we can 'roundtrip' perfectly
@@ -715,17 +715,6 @@ def open_dataarray(
715
715
return data_array
716
716
717
717
718
- class _MultiFileCloser :
719
- __slots__ = ("file_objs" ,)
720
-
721
- def __init__ (self , file_objs ):
722
- self .file_objs = file_objs
723
-
724
- def close (self ):
725
- for f in self .file_objs :
726
- f .close ()
727
-
728
-
729
718
def open_mfdataset (
730
719
paths ,
731
720
chunks = None ,
@@ -918,14 +907,14 @@ def open_mfdataset(
918
907
getattr_ = getattr
919
908
920
909
datasets = [open_ (p , ** open_kwargs ) for p in paths ]
921
- file_objs = [getattr_ (ds , "_file_obj " ) for ds in datasets ]
910
+ closers = [getattr_ (ds , "_close " ) for ds in datasets ]
922
911
if preprocess is not None :
923
912
datasets = [preprocess (ds ) for ds in datasets ]
924
913
925
914
if parallel :
926
915
# calling compute here will return the datasets/file_objs lists,
927
916
# the underlying datasets will still be stored as dask arrays
928
- datasets , file_objs = dask .compute (datasets , file_objs )
917
+ datasets , closers = dask .compute (datasets , closers )
929
918
930
919
# Combine all datasets, closing them in case of a ValueError
931
920
try :
@@ -963,7 +952,11 @@ def open_mfdataset(
963
952
ds .close ()
964
953
raise
965
954
966
- combined ._file_obj = _MultiFileCloser (file_objs )
955
+ def multi_file_closer ():
956
+ for closer in closers :
957
+ closer ()
958
+
959
+ combined .set_close (multi_file_closer )
967
960
968
961
# read global attributes from the attrs_file or from the first dataset
969
962
if attrs_file is not None :
0 commit comments