Skip to content

Commit

Permalink
test: plug a memleak in the python tests
Browse files Browse the repository at this point in the history
We need to free the list returned
  • Loading branch information
whot committed Oct 4, 2024
1 parent be6ef19 commit eccebc0
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class _Api:

@property
def basename(self) -> str:
return self.name[len(PREFIX) :]
return self.name.removeprefix(PREFIX)


@dataclass
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit eccebc0

Please sign in to comment.