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 b2098f1 commit 684f084
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 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,39 @@ 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():
# BSD has 7, Linux has 6
for libc in ("libc.so.6", "libc.so.7"):
try:
return ctypes.CDLL(libc, use_errno=True)
except OSError:
pass
raise NotImplementedError("Not implemented for other libc.so")

@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 +749,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 684f084

Please sign in to comment.