usb1: Enforce object finalization order.
Weak references are broken before the referent is destroyed. This opens
the possibility for the weak reference holder to get closed after the
referent cannot be reached from its WeakSet but before the it got destroyed,
which causes failures like this (on Windows):
Exception ignored in: <function USBDevice.__del__ at 0x000001BD8F7A8820>
Traceback (most recent call last):
File "...\usb1\__init__.py", line 1778, in __del__
self.close()
File "...\usb1\__init__.py", line 1790, in close
self.__libusb_unref_device(self.device_p)
OSError: exception: access violation writing 0x0000000000000024
Replace all weak references by python >=3.4 finalizers.
This approach has two good effects:
- it allows getting rid of __del__ methods, which should make the garbage-
collector's life easier (on older python versions at least)
- it moves (most of) the global bindings from class level to the finalizer's
creation, which is a bit better for code readability (definition is closer
to usage scope).
Also, backport finalizer class for versions which do not have it.
4 files changed