|
|
@@ -0,0 +1,42 @@ |
|
|
|
# Class decorator that warns on stderr at deletion time if the class's |
|
|
|
# close() member wasn't called. |
|
|
|
|
|
|
|
from nilmdb.utils.printf import * |
|
|
|
import sys |
|
|
|
|
|
|
|
def must_close(errorfile = sys.stderr): |
|
|
|
def decorator(cls): |
|
|
|
def dummy(*args, **kwargs): |
|
|
|
pass |
|
|
|
if "__init__" not in cls.__dict__: |
|
|
|
cls.__init__ = dummy |
|
|
|
if "__del__" not in cls.__dict__: |
|
|
|
cls.__del__ = dummy |
|
|
|
if "close" not in cls.__dict__: |
|
|
|
cls.close = dummy |
|
|
|
|
|
|
|
orig_init = cls.__init__ |
|
|
|
orig_del = cls.__del__ |
|
|
|
orig_close = cls.close |
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
ret = orig_init(self, *args, **kwargs) |
|
|
|
self.__dict__["_must_close"] = True |
|
|
|
return ret |
|
|
|
|
|
|
|
def __del__(self): |
|
|
|
if "_must_close" in self.__dict__: |
|
|
|
fprintf(errorfile, "error: %s.close() wasn't called!\n", |
|
|
|
self.__class__.__name__) |
|
|
|
return orig_del(self) |
|
|
|
|
|
|
|
def close(self, *args, **kwargs): |
|
|
|
del self._must_close |
|
|
|
return orig_close(self) |
|
|
|
|
|
|
|
cls.__init__ = __init__ |
|
|
|
cls.__del__ = __del__ |
|
|
|
cls.close = close |
|
|
|
|
|
|
|
return cls |
|
|
|
return decorator |