-
Notifications
You must be signed in to change notification settings - Fork 105
Closed
Labels
Description
I noticed while looking at #1241 that DataFile has unexpected (to me) behaviour inside a with statement.
BOUT-dev/tools/pylib/boututils/datafile.py
Lines 166 to 167 in 5dcf404
| def __enter__(self): | |
| return self.impl.__enter__() |
because
__enter__ returns self.impl.__enter__(), in a statement like
with DataFile('BOUT.dmp.0.nc') as f:
# do stuff here
f is a DataFile_netCDF object, not a DataFile object. In practice this doesn't make much difference because the interfaces are very similar, but DataFile.read does some work
BOUT-dev/tools/pylib/boututils/datafile.py
Lines 197 to 201 in 5dcf404
| if ranges is not None: | |
| for x in ranges: | |
| if isinstance(x, (list, tuple)): | |
| x = slice(*x) | |
| return self.impl.read(name, ranges=ranges, asBoutArray=asBoutArray) |
(although I think both netCDF and hdf5 can handle converting lists and tuples to slices) and
bout_type(self, varname) which returns self.attributes(varname)["bout_type"] would be missing.
In summary, I'd expect DataFile.__enter__ to return self so that f in the with-statement is a DataFile object not one of the implementations. Is this a bug?
Reactions are currently unavailable