diff options
author | Bryan Forbes | 2010-03-17 10:27:44 -0500 |
---|---|---|
committer | Martin Szulecki | 2012-03-20 23:25:54 +0100 |
commit | 68c63cc1382326e7f0cb4e6bd863427f9069ca05 (patch) | |
tree | d4094cb7f98cdb081e614c519f6cf8d0f9080c18 /cython | |
parent | cfe6244f8954efce4ef12b9b4338cc0d41a9ff40 (diff) | |
download | libimobiledevice-68c63cc1382326e7f0cb4e6bd863427f9069ca05.tar.gz libimobiledevice-68c63cc1382326e7f0cb4e6bd863427f9069ca05.tar.bz2 |
Added base class for more efficient error handling.
Diffstat (limited to 'cython')
-rw-r--r-- | cython/Makefile.am | 8 | ||||
-rw-r--r-- | cython/imobiledevice.pxd | 8 | ||||
-rw-r--r-- | cython/imobiledevice.pyx | 80 | ||||
-rw-r--r-- | cython/mobilebackup.pxi | 28 | ||||
-rw-r--r-- | cython/mobilesync.pxi | 28 | ||||
-rw-r--r-- | cython/property_list_client.pxi | 13 | ||||
-rw-r--r-- | cython/property_list_service.pxi | 15 |
7 files changed, 113 insertions, 67 deletions
diff --git a/cython/Makefile.am b/cython/Makefile.am index f5f01ef..1a71fbe 100644 --- a/cython/Makefile.am +++ b/cython/Makefile.am @@ -7,7 +7,13 @@ if HAVE_CYTHON BUILT_SOURCES = imobiledevice.c PXDINCLUDES = imobiledevice.pxd stdint.pxi $(CYTHON_PLIST_INCLUDE_DIR)/plist.pxd -PXIINCLUDES = stdint.pxi mobilesync.pxi notification_proxy.pxi sbservices.pxi mobilebackup.pxi +PXIINCLUDES = \ + stdint.pxi \ + mobilesync.pxi \ + notification_proxy.pxi \ + sbservices.pxi \ + mobilebackup.pxi \ + property_list_client.pxi CLEANFILES = \ *.pyc \ diff --git a/cython/imobiledevice.pxd b/cython/imobiledevice.pxd index bdebe33..0557d37 100644 --- a/cython/imobiledevice.pxd +++ b/cython/imobiledevice.pxd @@ -10,6 +10,10 @@ cdef class BaseError(Exception): cdef dict _lookup_table cdef int16_t _c_errcode +cdef class Base: + cdef inline int handle_error(self, int16_t ret) except -1 + cdef inline BaseError _error(self, int16_t ret) + cdef class iDeviceError(BaseError): pass cdef extern from "libimobiledevice/libimobiledevice.h": @@ -29,7 +33,7 @@ cdef extern from "libimobiledevice/libimobiledevice.h": cdef class iDeviceEvent: cdef const_idevice_event_t _c_event -cdef class iDevice: +cdef class iDevice(Base): cdef idevice_t _c_dev cdef class LockdownError(BaseError): pass @@ -39,7 +43,7 @@ cdef extern from "libimobiledevice/lockdown.h": pass ctypedef lockdownd_client_int *lockdownd_client_t -cdef class LockdownClient: +cdef class LockdownClient(Base): cdef lockdownd_client_t _c_client cpdef int start_service(self, service) cpdef goodbye(self) diff --git a/cython/imobiledevice.pyx b/cython/imobiledevice.pyx index bbec7b8..c6a96dd 100644 --- a/cython/imobiledevice.pyx +++ b/cython/imobiledevice.pyx @@ -19,6 +19,16 @@ cdef class BaseError(Exception): def __repr__(self): return self.__str__() +cdef class Base: + cdef inline int handle_error(self, int16_t ret) except -1: + if ret == 0: + return 0 + cdef BaseError err = self._error(ret) + raise err + return -1 + + cdef inline BaseError _error(self, int16_t ret): pass + cdef extern from "libimobiledevice/libimobiledevice.h": int16_t IDEVICE_E_SUCCESS int16_t IDEVICE_E_INVALID_ARG @@ -70,44 +80,53 @@ cpdef event_unsubscribe(): if err: raise err cpdef get_device_list(): - cdef char** devices - cdef int count - cdef list result - cdef bytes device - cdef iDeviceError err = iDeviceError(idevice_get_device_list(&devices, &count)) + cdef: + char** devices + int count + list result + bytes device + iDeviceError err = iDeviceError(idevice_get_device_list(&devices, &count)) + + if err: raise err result = [] for i from 0 <= i < count: device = devices[i] result.append(device) - idevice_device_list_free(devices) + err = iDeviceError(idevice_device_list_free(devices)) + if err: raise err return result -cdef class iDevice: +cdef class iDevice(Base): def __cinit__(self, uuid=None, *args, **kwargs): - cdef char* c_uuid = NULL + cdef: + char* c_uuid = NULL + idevice_error_t err if uuid is not None: c_uuid = uuid - err = iDeviceError(idevice_new(&(self._c_dev), c_uuid)) - if err: raise err + err = idevice_new(&self._c_dev, c_uuid) + self.handle_error(err) def __dealloc__(self): if self._c_dev is not NULL: - err = iDeviceError(idevice_free(self._c_dev)) - if err: raise err + self.handle_error(idevice_free(self._c_dev)) + + cdef inline BaseError _error(self, int16_t ret): + return iDeviceError(ret) property uuid: def __get__(self): - cdef char* uuid - err = iDeviceError(idevice_get_uuid(self._c_dev, &uuid)) - if err: raise err + cdef: + char* uuid + idevice_error_t err + err = idevice_get_uuid(self._c_dev, &uuid) + self.handle_error(err) return uuid property handle: def __get__(self): cdef uint32_t handle - err = iDeviceError(idevice_get_handle(self._c_dev, &handle)) - if err: raise err + self.handle_error(idevice_get_handle(self._c_dev, &handle)) return handle cdef extern from "libimobiledevice/lockdown.h": @@ -166,27 +185,34 @@ cdef class LockdownError(BaseError): } BaseError.__init__(self, *args, **kwargs) -cdef class LockdownClient: +cdef class LockdownClient(Base): def __cinit__(self, iDevice device not None, char *label=NULL, *args, **kwargs): - cdef iDevice dev = device - err = LockdownError(lockdownd_client_new_with_handshake(dev._c_dev, &(self._c_client), label)) - if err: raise err + cdef: + iDevice dev = device + lockdownd_error_t err = lockdownd_client_new_with_handshake(dev._c_dev, &(self._c_client), label) + self.handle_error(err) def __dealloc__(self): + cdef lockdownd_error_t err if self._c_client is not NULL: - err = LockdownError(lockdownd_client_free(self._c_client)) - if err: raise err + err = lockdownd_client_free(self._c_client) + self.handle_error(err) + + cdef inline BaseError _error(self, int16_t ret): + return LockdownError(ret) cpdef int start_service(self, service): - cdef uint16_t port - err = LockdownError(lockdownd_start_service(self._c_client, service, &port)) - if err: raise err + cdef: + uint16_t port + lockdownd_error_t err + err = lockdownd_start_service(self._c_client, service, &port) + self.handle_error(err) return port cpdef goodbye(self): pass -include "property_list_service.pxi" +include "property_list_client.pxi" include "mobilesync.pxi" include "notification_proxy.pxi" include "sbservices.pxi" diff --git a/cython/mobilebackup.pxi b/cython/mobilebackup.pxi index 2bbaebe..f6b13a9 100644 --- a/cython/mobilebackup.pxi +++ b/cython/mobilebackup.pxi @@ -28,27 +28,33 @@ cdef class MobileBackupError(BaseError): } BaseError.__init__(self, *args, **kwargs) -cdef class MobileBackupClient(PropertyListService): +cdef class MobileBackupClient(PropertyListClient): cdef mobilebackup_client_t _c_client def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): - cdef iDevice dev = device - cdef LockdownClient lckd + cdef: + iDevice dev = device + LockdownClient lckd + mobilebackup_error_t err if lockdown is None: lckd = LockdownClient(dev) else: lckd = lockdown port = lckd.start_service("com.apple.mobilebackup") - err = MobileBackupError(mobilebackup_client_new(dev._c_dev, port, &self._c_client)) - if err: raise err + err = mobilebackup_client_new(dev._c_dev, port, &self._c_client) + self.handle_error(err) def __dealloc__(self): + cdef mobilebackup_error_t err if self._c_client is not NULL: - err = MobileBackupError(mobilebackup_client_free(self._c_client)) - if err: raise err + err = mobilebackup_client_free(self._c_client) + self.handle_error(err) - cdef _send(self, plist.plist_t node): - return MobileBackupError(mobilebackup_send(self._c_client, node)) + cdef inline int16_t _send(self, plist.plist_t node): + return mobilebackup_send(self._c_client, node) - cdef _receive(self, plist.plist_t* node): - return MobileBackupError(mobilebackup_receive(self._c_client, node)) + cdef inline int16_t _receive(self, plist.plist_t* node): + return mobilebackup_receive(self._c_client, node) + + cdef inline BaseError _error(self, int16_t ret): + return MobileBackupError(ret) diff --git a/cython/mobilesync.pxi b/cython/mobilesync.pxi index b9cf951..abd56b4 100644 --- a/cython/mobilesync.pxi +++ b/cython/mobilesync.pxi @@ -28,27 +28,33 @@ cdef class MobileSyncError(BaseError): } BaseError.__init__(self, *args, **kwargs) -cdef class MobileSyncClient(PropertyListService): +cdef class MobileSyncClient(PropertyListClient): cdef mobilesync_client_t _c_client def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): - cdef iDevice dev = device - cdef LockdownClient lckd + cdef: + iDevice dev = device + LockdownClient lckd + mobilesync_error_t err if lockdown is None: lckd = LockdownClient(dev) else: lckd = lockdown port = lckd.start_service("com.apple.mobilesync") - err = MobileSyncError(mobilesync_client_new(dev._c_dev, port, &(self._c_client))) - if err: raise err + err = mobilesync_client_new(dev._c_dev, port, &(self._c_client)) + self.handle_error(err) def __dealloc__(self): + cdef mobilesync_error_t err if self._c_client is not NULL: - err = MobileSyncError(mobilesync_client_free(self._c_client)) - if err: raise err + err = mobilesync_client_free(self._c_client) + self.handle_error(err) - cdef _send(self, plist.plist_t node): - return MobileSyncError(mobilesync_send(self._c_client, node)) + cdef inline int16_t _send(self, plist.plist_t node): + return mobilesync_send(self._c_client, node) - cdef _receive(self, plist.plist_t* node): - return MobileSyncError(mobilesync_receive(self._c_client, node)) + cdef inline int16_t _receive(self, plist.plist_t* node): + return mobilesync_receive(self._c_client, node) + + cdef inline BaseError _error(self, int16_t ret): + return MobileSyncError(ret) diff --git a/cython/property_list_client.pxi b/cython/property_list_client.pxi new file mode 100644 index 0000000..2689658 --- /dev/null +++ b/cython/property_list_client.pxi @@ -0,0 +1,13 @@ +cdef class PropertyListClient(Base): + cpdef send(self, plist.Node node): + cdef plist.Node n = node + self.handle_error(self._send(n._c_node)) + + cpdef plist.Node receive(self): + cdef plist.plist_t c_node = NULL + self.handle_error(self._receive(&c_node)) + + return plist.plist_t_to_node(c_node) + + cdef inline int16_t _send(self, plist.plist_t node): pass + cdef inline int16_t _receive(self, plist.plist_t* c_node): pass diff --git a/cython/property_list_service.pxi b/cython/property_list_service.pxi deleted file mode 100644 index 9b3f694..0000000 --- a/cython/property_list_service.pxi +++ /dev/null @@ -1,15 +0,0 @@ -cdef class PropertyListService: - cpdef send(self, plist.Node node): - cdef plist.Node n = node - cdef BaseError err = self._send(n._c_node) - if err: raise err - - cpdef plist.Node receive(self): - cdef plist.plist_t c_node = NULL - cdef BaseError err = self._receive(&c_node) - if err: raise err - - return plist.plist_t_to_node(c_node) - - cdef _send(self, plist.plist_t node): pass - cdef _receive(self, plist.plist_t* c_node): pass |