PyInotify icon indicating copy to clipboard operation
PyInotify copied to clipboard

Context Manager?

Open Beefster09 opened this issue 7 years ago • 2 comments

I find it a bit wonky that closing the watch can only be done through the destructor. Maybe it isn't the end of the world, but this sorta locks it to cPython.

Beefster09 avatar Jun 04 '18 16:06 Beefster09

Please clarify or provide a supporting link.

dsoprea avatar Jul 07 '18 06:07 dsoprea

Most things of this nature do their resource management through __enter__ and __exit__ (so that it works with with) rather than relying on refcounting and the immediate object destruction and __del__ call. Since only cPython has the immediate destruction behavior, this would cause severe resource leaks on other implementations of Python that do not use refcounting for object lifetime.

Even on cPython, you can create a resource leak by keeping a reference to the watch in an object cycle, making it only get closed if and when the garbage collector runs.

It's true that this isn't really an issue for most use cases, but it's not particularly responsible IMO to depend on destructors for resource cleanup. That's a C++ thing.

Beefster09 avatar Jul 25 '18 15:07 Beefster09