Skip to content

Commit

Permalink
Merge pull request #93 from visr/close
Browse files Browse the repository at this point in the history
do not throw when closing closed NCDataset
  • Loading branch information
Alexander-Barth authored Aug 5, 2020
2 parents ef40775 + 43a0ed5 commit b8751a9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ function Base.show(io::IO, a::BaseAttributes; indent = " ")
return
end
end
rethrow
rethrow()
end
end
14 changes: 12 additions & 2 deletions src/dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,17 @@ to the disk.
"""
function Base.close(ds::NCDataset)
@debug "closing netCDF NCDataset $(ds.ncid) $(NCDatasets.path(ds))"
nc_close(ds.ncid)
try
nc_close(ds.ncid)
catch err
# like Base, allow close on closed file
if isa(err,NetCDFError)
if err.code == NC_EBADID
return ds
end
end
rethrow()
end
# prevent finalize to close file as ncid can reused for future files
ds.ncid = -1
return ds
Expand Down Expand Up @@ -440,7 +450,7 @@ function Base.show(io::IO,ds::AbstractDataset; indent="")
return
end
end
rethrow
rethrow()
end

print(io,indent,"Group: ",groupname(ds),"\n")
Expand Down
2 changes: 1 addition & 1 deletion src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ function Base.show(io::IO,v::AbstractVariable; indent="")
return
end
end
rethrow
rethrow()
end
sz = size(v)

Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ println("NetCDF version: ",NCDatasets.nc_inq_libvers())
# test sync
NCDatasets.sync(ds)
NCDatasets.close(ds)
# close on closed file should not throw
NCDatasets.close(ds)

# Load a file (with unknown structure)

Expand Down

0 comments on commit b8751a9

Please sign in to comment.