Skip to content

Commit 1f7c1a5

Browse files
committed
usb1: Replace lambda in finalizer argument with partial
While it seems the lambda instance does not keep a reference to setPollFDNotifiers's stack cell (which would cause the finalizer to keep a reference to the object it is monitoring, preventing proper finalization) in cPython 3.11, I am not confident this would be also the case in other implementations and versions. I do not expect functools.partial instances to keep any reference to their call stack, so they seem safer in such reference-sensitive context.
1 parent 191c878 commit 1f7c1a5

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

usb1/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,10 @@ def setPollFDNotifiers(
24302430
self,
24312431
self.__finalizePollFDNotifiers, # Note: staticmethod
24322432
context_p=self.__context_p,
2433-
unregisterFinalizer=lambda: finalizer_dict.pop(finalizer_handle),
2433+
unregisterFinalizer=functools.partial(
2434+
finalizer_dict.pop,
2435+
finalizer_handle,
2436+
),
24342437
libusb_set_pollfd_notifiers=libusb1.libusb_set_pollfd_notifiers,
24352438
)
24362439
self.__registerFinalizer(finalizer_handle, finalizer)

0 commit comments

Comments
 (0)