dsoprea / PyInotify

An efficient and elegant inotify (Linux filesystem activity monitor) library for Python. Python 2 and 3 compatible.
GNU General Public License v2.0
245 stars 73 forks source link

Context Manager? #50

Open Beefster09 opened 6 years ago

Beefster09 commented 6 years ago

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.

dsoprea commented 6 years ago

Please clarify or provide a supporting link.

Beefster09 commented 6 years ago

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.