From eccebc0aa6db1c2ba8c3e574742c00feac20c693 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Mon, 2 Sep 2024 15:11:01 +1000 Subject: [PATCH] test: plug a memleak in the python tests We need to free the list returned --- test/__init__.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/test/__init__.py b/test/__init__.py index 157b4f37..3dfdfa4c 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -45,7 +45,7 @@ class _Api: @property def basename(self) -> str: - return self.name[len(PREFIX) :] + return self.name.removeprefix(PREFIX) @dataclass @@ -58,6 +58,33 @@ def basename(self) -> str: return self.name.removeprefix("WACOM_").removeprefix("W") +class GlibC: + _lib = None + + _api_prototypes: List[_Api] = [ + _Api(name="free", args=(c_void_p,), return_type=None), + ] + + @staticmethod + def _cdll(): + return ctypes.CDLL("libc.so.6", use_errno=True) + + @classmethod + def _load(cls): + cls._lib = cls._cdll() + for api in cls._api_prototypes: + func = getattr(cls._lib, api.name) + func.argtypes = api.args + func.restype = api.return_type + setattr(cls, api.basename, func) + + @classmethod + def instance(cls): + if cls._lib is None: + cls._load() + return cls + + class LibWacom: """ libwacom.so wrapper. This is a singleton ctypes wrapper into libwacom.so with @@ -716,7 +743,9 @@ def new_from_builder( def list_devices(self) -> List[WacomDevice]: devices = self.libwacom_list_devices_from_database(self.db, 0) - return [ + devs = [ WacomDevice(d, destroy=False) for d in itertools.takewhile(lambda ptr: ptr is not None, devices) ] + GlibC.instance().free(devices) + return devs