[linux-minidisc] [PATCH 05/17] Export methods to release resources.
- From: Vincent Pelletier <plr.vincent@gmail.com>
- To: <linux-minidisc@lists.fu-berlin.de>
- Date: Tue, 26 Jan 2010 21:14:07 +0100
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:in-reply-to:references:from:date:subject:to:mime-version :content-type:x-bogosity:x-uid; b=vWrf5DvhgJKyQETWmCRhRmA/jMJkhiXfbcoeD0pyE1+NR3zRKgup03IGkWDCmGBBVJ YAkvvMMtFyTBIHkGEW6oOOe/YdbSjk2rCqSQReDonDZu6aw/0R6nvlyFbid7rq3JkhBE HRtT1zo/zebV04k8j8SszY3v/EaUoHN/Gwf5c=
- Subject: [linux-minidisc] [PATCH 05/17] Export methods to release resources.
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()