This allows user to work-around race conditions in interpreter shutdown, where context might be freed before handle - for example. --- netmd/usb1.py | 19 ++++++++++++++++--- 1 files changed, 16 insertions(+), 3 deletions(-) diff --git a/netmd/usb1.py b/netmd/usb1.py index e9e3ea1..d22f52a 100644 --- a/netmd/usb1.py +++ b/netmd/usb1.py @@ -9,11 +9,19 @@ __all__ = ['LibUSBContext'] STRING_LENGTH = 256 class USBDeviceHandle(object): + handle = None + def __init__(self, handle): self.handle = handle def __del__(self): - libusb1.libusb_close(self.handle) + self.close() + + def close(self): + handle = self.handle + if handle is not None: + libusb1.libusb_close(handle) + self.handle = None def getConfiguration(self): configuration = c_int() @@ -313,8 +321,13 @@ class LibUSBContext(object): self.context_p = context_p def __del__(self): - if self.context_p is not None: - libusb1.libusb_exit(self.context_p) + self.exit() + + def exit(self): + context_p = self.context_p + if context_p is not None: + libusb1.libusb_exit(context_p) + self.context_p = None def getDeviceList(self): device_p_p = libusb1.libusb_device_p_p() -- 1.6.6