diff options
Diffstat (limited to 'cython')
| -rw-r--r-- | cython/plist.pxd | 3 | ||||
| -rw-r--r-- | cython/plist.pyx | 55 |
2 files changed, 47 insertions, 11 deletions
diff --git a/cython/plist.pxd b/cython/plist.pxd index b11d80d..5a41bf8 100644 --- a/cython/plist.pxd +++ b/cython/plist.pxd | |||
| @@ -19,7 +19,8 @@ cdef class Bool(Node): | |||
| 19 | 19 | ||
| 20 | cdef class Integer(Node): | 20 | cdef class Integer(Node): |
| 21 | cpdef set_value(self, object value) | 21 | cpdef set_value(self, object value) |
| 22 | cpdef uint64_t get_value(self) | 22 | cpdef get_value(self) |
| 23 | cpdef bint is_negative(self) | ||
| 23 | 24 | ||
| 24 | cdef class Uid(Node): | 25 | cdef class Uid(Node): |
| 25 | cpdef set_value(self, object value) | 26 | cpdef set_value(self, object value) |
diff --git a/cython/plist.pyx b/cython/plist.pyx index 38415f9..5481308 100644 --- a/cython/plist.pyx +++ b/cython/plist.pyx | |||
| @@ -5,7 +5,7 @@ from libc.stdint cimport * | |||
| 5 | cdef extern from *: | 5 | cdef extern from *: |
| 6 | ctypedef enum plist_type: | 6 | ctypedef enum plist_type: |
| 7 | PLIST_BOOLEAN, | 7 | PLIST_BOOLEAN, |
| 8 | PLIST_UINT, | 8 | PLIST_INT, |
| 9 | PLIST_REAL, | 9 | PLIST_REAL, |
| 10 | PLIST_STRING, | 10 | PLIST_STRING, |
| 11 | PLIST_ARRAY, | 11 | PLIST_ARRAY, |
| @@ -14,6 +14,7 @@ cdef extern from *: | |||
| 14 | PLIST_DATA, | 14 | PLIST_DATA, |
| 15 | PLIST_KEY, | 15 | PLIST_KEY, |
| 16 | PLIST_UID, | 16 | PLIST_UID, |
| 17 | PLIST_NULL, | ||
| 17 | PLIST_NONE | 18 | PLIST_NONE |
| 18 | 19 | ||
| 19 | plist_t plist_new_bool(uint8_t val) | 20 | plist_t plist_new_bool(uint8_t val) |
| @@ -24,6 +25,10 @@ cdef extern from *: | |||
| 24 | void plist_get_uint_val(plist_t node, uint64_t *val) | 25 | void plist_get_uint_val(plist_t node, uint64_t *val) |
| 25 | void plist_set_uint_val(plist_t node, uint64_t val) | 26 | void plist_set_uint_val(plist_t node, uint64_t val) |
| 26 | 27 | ||
| 28 | plist_t plist_new_int(int64_t val) | ||
| 29 | void plist_get_int_val(plist_t node, int64_t *val) | ||
| 30 | void plist_set_int_val(plist_t node, int64_t val) | ||
| 31 | |||
| 27 | plist_t plist_new_real(double val) | 32 | plist_t plist_new_real(double val) |
| 28 | void plist_get_real_val(plist_t node, double *val) | 33 | void plist_get_real_val(plist_t node, double *val) |
| 29 | void plist_set_real_val(plist_t node, double val) | 34 | void plist_set_real_val(plist_t node, double val) |
| @@ -47,6 +52,8 @@ cdef extern from *: | |||
| 47 | void plist_get_data_val(plist_t node, char **val, uint64_t * length) | 52 | void plist_get_data_val(plist_t node, char **val, uint64_t * length) |
| 48 | void plist_set_data_val(plist_t node, char *val, uint64_t length) | 53 | void plist_set_data_val(plist_t node, char *val, uint64_t length) |
| 49 | 54 | ||
| 55 | plist_t plist_new_null(); | ||
| 56 | |||
| 50 | plist_t plist_new_dict() | 57 | plist_t plist_new_dict() |
| 51 | int plist_dict_get_size(plist_t node) | 58 | int plist_dict_get_size(plist_t node) |
| 52 | plist_t plist_dict_get_item(plist_t node, char* key) | 59 | plist_t plist_dict_get_item(plist_t node, char* key) |
| @@ -77,6 +84,8 @@ cdef extern from *: | |||
| 77 | void plist_from_xml(char *plist_xml, uint32_t length, plist_t * plist) | 84 | void plist_from_xml(char *plist_xml, uint32_t length, plist_t * plist) |
| 78 | void plist_from_bin(char *plist_bin, uint32_t length, plist_t * plist) | 85 | void plist_from_bin(char *plist_bin, uint32_t length, plist_t * plist) |
| 79 | 86 | ||
| 87 | int plist_int_val_is_negative(plist_t node); | ||
| 88 | |||
| 80 | cdef class Node: | 89 | cdef class Node: |
| 81 | def __init__(self, *args, **kwargs): | 90 | def __init__(self, *args, **kwargs): |
| 82 | self._c_managed = True | 91 | self._c_managed = True |
| @@ -177,13 +186,15 @@ cdef Bool Bool_factory(plist_t c_node, bint managed=True): | |||
| 177 | cdef class Integer(Node): | 186 | cdef class Integer(Node): |
| 178 | def __cinit__(self, object value=None, *args, **kwargs): | 187 | def __cinit__(self, object value=None, *args, **kwargs): |
| 179 | if value is None: | 188 | if value is None: |
| 180 | self._c_node = plist_new_uint(0) | 189 | self._c_node = plist_new_int(0) |
| 181 | else: | 190 | else: |
| 182 | self._c_node = plist_new_uint(int(value)) | 191 | if value < 0 or value <= INT64_MAX: |
| 192 | self._c_node = plist_new_int(int(value)) | ||
| 193 | else: | ||
| 194 | self._c_node = plist_new_uint(int(value)) | ||
| 183 | 195 | ||
| 184 | def __repr__(self): | 196 | def __repr__(self): |
| 185 | cdef uint64_t i = self.get_value() | 197 | return '<Integer: %s>' % self.get_value() |
| 186 | return '<Integer: %s>' % i | ||
| 187 | 198 | ||
| 188 | def __int__(self): | 199 | def __int__(self): |
| 189 | return self.get_value() | 200 | return self.get_value() |
| @@ -210,10 +221,18 @@ cdef class Integer(Node): | |||
| 210 | cpdef set_value(self, object value): | 221 | cpdef set_value(self, object value): |
| 211 | plist_set_uint_val(self._c_node, int(value)) | 222 | plist_set_uint_val(self._c_node, int(value)) |
| 212 | 223 | ||
| 213 | cpdef uint64_t get_value(self): | 224 | cpdef get_value(self): |
| 214 | cdef uint64_t value | 225 | cdef int64_t ivalue |
| 215 | plist_get_uint_val(self._c_node, &value) | 226 | cdef uint64_t uvalue |
| 216 | return value | 227 | if self.is_negative(): |
| 228 | plist_get_int_val(self._c_node, &ivalue) | ||
| 229 | return int(ivalue) | ||
| 230 | else: | ||
| 231 | plist_get_uint_val(self._c_node, &uvalue) | ||
| 232 | return int(uvalue) | ||
| 233 | |||
| 234 | cpdef bint is_negative(self): | ||
| 235 | return plist_int_val_is_negative(self._c_node); | ||
| 217 | 236 | ||
| 218 | cdef Integer Integer_factory(plist_t c_node, bint managed=True): | 237 | cdef Integer Integer_factory(plist_t c_node, bint managed=True): |
| 219 | cdef Integer instance = Integer.__new__(Integer) | 238 | cdef Integer instance = Integer.__new__(Integer) |
| @@ -314,6 +333,20 @@ cdef Uid Uid_factory(plist_t c_node, bint managed=True): | |||
| 314 | instance._c_node = c_node | 333 | instance._c_node = c_node |
| 315 | return instance | 334 | return instance |
| 316 | 335 | ||
| 336 | cdef class Null(Node): | ||
| 337 | def __cinit__(self, object value=None, *args, **kwargs): | ||
| 338 | self._c_node = plist_new_null() | ||
| 339 | |||
| 340 | def __repr__(self): | ||
| 341 | cdef uint64_t i = self.get_value() | ||
| 342 | return '<Null>' | ||
| 343 | |||
| 344 | cdef Null Null_factory(plist_t c_node, bint managed=True): | ||
| 345 | cdef Null instance = Null.__new__(Null) | ||
| 346 | instance._c_managed = managed | ||
| 347 | instance._c_node = c_node | ||
| 348 | return instance | ||
| 349 | |||
| 317 | from cpython cimport PY_MAJOR_VERSION | 350 | from cpython cimport PY_MAJOR_VERSION |
| 318 | 351 | ||
| 319 | cdef class Key(Node): | 352 | cdef class Key(Node): |
| @@ -833,7 +866,7 @@ cdef object plist_t_to_node(plist_t c_plist, bint managed=True): | |||
| 833 | cdef plist_type t = plist_get_node_type(c_plist) | 866 | cdef plist_type t = plist_get_node_type(c_plist) |
| 834 | if t == PLIST_BOOLEAN: | 867 | if t == PLIST_BOOLEAN: |
| 835 | return Bool_factory(c_plist, managed) | 868 | return Bool_factory(c_plist, managed) |
| 836 | if t == PLIST_UINT: | 869 | if t == PLIST_INT: |
| 837 | return Integer_factory(c_plist, managed) | 870 | return Integer_factory(c_plist, managed) |
| 838 | if t == PLIST_KEY: | 871 | if t == PLIST_KEY: |
| 839 | return Key_factory(c_plist, managed) | 872 | return Key_factory(c_plist, managed) |
| @@ -851,6 +884,8 @@ cdef object plist_t_to_node(plist_t c_plist, bint managed=True): | |||
| 851 | return Data_factory(c_plist, managed) | 884 | return Data_factory(c_plist, managed) |
| 852 | if t == PLIST_UID: | 885 | if t == PLIST_UID: |
| 853 | return Uid_factory(c_plist, managed) | 886 | return Uid_factory(c_plist, managed) |
| 887 | if t == PLIST_NULL: | ||
| 888 | return Null_factory(c_plist, managed) | ||
| 854 | if t == PLIST_NONE: | 889 | if t == PLIST_NONE: |
| 855 | return None | 890 | return None |
| 856 | 891 | ||
