hechth/fs-irods

Implement opening files with context manager or raw?

Closed this issue · 2 comments

@d-w-moore I'm not sure whether it is recommended to implement file opening as a context manager or just as a raw operation and relying on users to handle closing the files again properly?

References to open irods files (ie data objects) have been a sticky subject. If the associated session object goes out of scope before the open "file", you'll have problems because the connection to irods is now closed, so flushing, closing, etc. won't work.
I think the answer is for a session object to persist as long as the fs object. Users of fs-irods can do something of the form:

with fs.open_fs("irods://...") as abstract_file: 
  # do stuff with abstract_file

The ownership (and object lifetime management) hierarchy will be:

abstract_file

  • session
  • open data descriptor

or something similar, and we'll write our __del__, __enter__ and __exit__ methods appropriately.
This way, we'll allow people the flexibility to use the abstract_file object either as context manager or as mundane Python objects that follow the normal rules of object lifetimes. (ie expiring at the end of their scope, or at interpreter exit).

@d-w-moore yeah I figured that also now - I think it might be better to just pass the session to the fs and assume it is alive for the whole time.