How to best pass file ownership from imopen to a plugin?
See original GitHub issueThe basic idea behind the file-management of the new API is simple:
with iio.imopen(...) as img_file:
img = img_file.read(...)
...
The file is opened by imopen
, held by img_file
during the context, and closed once the context manager exits. This works nicely.
A problem occurs when we use imopen
outside a context manager:
instance = iio.imopen(...)
# ... program crashes or returns here
instance.read(...)
In this case, the open file may not be cleaned up properly or could cause a dangling reference. How can we smooth this out?
One option would be to have imopen
close the file after it is done selecting a plugin and have the plugin reopen it. This might work, but is undesirable because it slows down the default case of using imopen
inside a context manager (as intended)
Another option would be to use a promise-style setup where imopen
returns a PluginPromise
which gets turned into the correct plugin by its __enter__
method. This would ensure that all file access is properly guarded. Promise-based interfaces are more common in JS than they are in python though, so I don’t know if this idea is easily maintainable.
A third option could be to make the destructor of imopen
close the file. However, this may limit applications, because they cant use the file outside imopen
’s scope.
All the options have pros and cons. The question here is which option is the best.
Issue Analytics
- State:
- Created 2 years ago
- Comments:16 (16 by maintainers)
Top GitHub Comments
No, but maybe we should make sure that we don’t with a test, so that it stays that way
@almarklein I think a
Plugin
class could be nice, indeed. That way, we can avoid boilerplate, but also get some typing support, becauseimopen
can now definePlugin
as a return type.