summaryrefslogtreecommitdiffstats
path: root/cython/imobiledevice.pyx
diff options
context:
space:
mode:
authorGravatar Bryan Forbes2010-03-17 10:27:44 -0500
committerGravatar Martin Szulecki2012-03-20 23:25:54 +0100
commit68c63cc1382326e7f0cb4e6bd863427f9069ca05 (patch)
treed4094cb7f98cdb081e614c519f6cf8d0f9080c18 /cython/imobiledevice.pyx
parentcfe6244f8954efce4ef12b9b4338cc0d41a9ff40 (diff)
downloadlibimobiledevice-68c63cc1382326e7f0cb4e6bd863427f9069ca05.tar.gz
libimobiledevice-68c63cc1382326e7f0cb4e6bd863427f9069ca05.tar.bz2
Added base class for more efficient error handling.
Diffstat (limited to 'cython/imobiledevice.pyx')
-rw-r--r--cython/imobiledevice.pyx80
1 files changed, 53 insertions, 27 deletions
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):
19 def __repr__(self): 19 def __repr__(self):
20 return self.__str__() 20 return self.__str__()
21 21
22cdef class Base:
23 cdef inline int handle_error(self, int16_t ret) except -1:
24 if ret == 0:
25 return 0
26 cdef BaseError err = self._error(ret)
27 raise err
28 return -1
29
30 cdef inline BaseError _error(self, int16_t ret): pass
31
22cdef extern from "libimobiledevice/libimobiledevice.h": 32cdef extern from "libimobiledevice/libimobiledevice.h":
23 int16_t IDEVICE_E_SUCCESS 33 int16_t IDEVICE_E_SUCCESS
24 int16_t IDEVICE_E_INVALID_ARG 34 int16_t IDEVICE_E_INVALID_ARG
@@ -70,44 +80,53 @@ cpdef event_unsubscribe():
70 if err: raise err 80 if err: raise err
71 81
72cpdef get_device_list(): 82cpdef get_device_list():
73 cdef char** devices 83 cdef:
74 cdef int count 84 char** devices
75 cdef list result 85 int count
76 cdef bytes device 86 list result
77 cdef iDeviceError err = iDeviceError(idevice_get_device_list(&devices, &count)) 87 bytes device
88 iDeviceError err = iDeviceError(idevice_get_device_list(&devices, &count))
89
90 if err: raise err
78 91
79 result = [] 92 result = []
80 for i from 0 <= i < count: 93 for i from 0 <= i < count:
81 device = devices[i] 94 device = devices[i]
82 result.append(device) 95 result.append(device)
83 96
84 idevice_device_list_free(devices) 97 err = iDeviceError(idevice_device_list_free(devices))
98 if err: raise err
85 return result 99 return result
86 100
87cdef class iDevice: 101cdef class iDevice(Base):
88 def __cinit__(self, uuid=None, *args, **kwargs): 102 def __cinit__(self, uuid=None, *args, **kwargs):
89 cdef char* c_uuid = NULL 103 cdef:
104 char* c_uuid = NULL
105 idevice_error_t err
90 if uuid is not None: 106 if uuid is not None:
91 c_uuid = uuid 107 c_uuid = uuid
92 err = iDeviceError(idevice_new(&(self._c_dev), c_uuid)) 108 err = idevice_new(&self._c_dev, c_uuid)
93 if err: raise err 109 self.handle_error(err)
94 110
95 def __dealloc__(self): 111 def __dealloc__(self):
96 if self._c_dev is not NULL: 112 if self._c_dev is not NULL:
97 err = iDeviceError(idevice_free(self._c_dev)) 113 self.handle_error(idevice_free(self._c_dev))
98 if err: raise err 114
115 cdef inline BaseError _error(self, int16_t ret):
116 return iDeviceError(ret)
99 117
100 property uuid: 118 property uuid:
101 def __get__(self): 119 def __get__(self):
102 cdef char* uuid 120 cdef:
103 err = iDeviceError(idevice_get_uuid(self._c_dev, &uuid)) 121 char* uuid
104 if err: raise err 122 idevice_error_t err
123 err = idevice_get_uuid(self._c_dev, &uuid)
124 self.handle_error(err)
105 return uuid 125 return uuid
106 property handle: 126 property handle:
107 def __get__(self): 127 def __get__(self):
108 cdef uint32_t handle 128 cdef uint32_t handle
109 err = iDeviceError(idevice_get_handle(self._c_dev, &handle)) 129 self.handle_error(idevice_get_handle(self._c_dev, &handle))
110 if err: raise err
111 return handle 130 return handle
112 131
113cdef extern from "libimobiledevice/lockdown.h": 132cdef extern from "libimobiledevice/lockdown.h":
@@ -166,27 +185,34 @@ cdef class LockdownError(BaseError):
166 } 185 }
167 BaseError.__init__(self, *args, **kwargs) 186 BaseError.__init__(self, *args, **kwargs)
168 187
169cdef class LockdownClient: 188cdef class LockdownClient(Base):
170 def __cinit__(self, iDevice device not None, char *label=NULL, *args, **kwargs): 189 def __cinit__(self, iDevice device not None, char *label=NULL, *args, **kwargs):
171 cdef iDevice dev = device 190 cdef:
172 err = LockdownError(lockdownd_client_new_with_handshake(dev._c_dev, &(self._c_client), label)) 191 iDevice dev = device
173 if err: raise err 192 lockdownd_error_t err = lockdownd_client_new_with_handshake(dev._c_dev, &(self._c_client), label)
193 self.handle_error(err)
174 194
175 def __dealloc__(self): 195 def __dealloc__(self):
196 cdef lockdownd_error_t err
176 if self._c_client is not NULL: 197 if self._c_client is not NULL:
177 err = LockdownError(lockdownd_client_free(self._c_client)) 198 err = lockdownd_client_free(self._c_client)
178 if err: raise err 199 self.handle_error(err)
200
201 cdef inline BaseError _error(self, int16_t ret):
202 return LockdownError(ret)
179 203
180 cpdef int start_service(self, service): 204 cpdef int start_service(self, service):
181 cdef uint16_t port 205 cdef:
182 err = LockdownError(lockdownd_start_service(self._c_client, service, &port)) 206 uint16_t port
183 if err: raise err 207 lockdownd_error_t err
208 err = lockdownd_start_service(self._c_client, service, &port)
209 self.handle_error(err)
184 return port 210 return port
185 211
186 cpdef goodbye(self): 212 cpdef goodbye(self):
187 pass 213 pass
188 214
189include "property_list_service.pxi" 215include "property_list_client.pxi"
190include "mobilesync.pxi" 216include "mobilesync.pxi"
191include "notification_proxy.pxi" 217include "notification_proxy.pxi"
192include "sbservices.pxi" 218include "sbservices.pxi"