From 927c81a7d6cc0a42d6a3b6487f661626af193f34 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 19 Mar 2013 06:06:01 +0100 Subject: cython: fixed missing class definition for PLIST_KEY type --- cython/plist.pxd | 2 +- cython/plist.pyx | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) (limited to 'cython') diff --git a/cython/plist.pxd b/cython/plist.pxd index 05f3e16..d7c70bd 100644 --- a/cython/plist.pxd +++ b/cython/plist.pxd @@ -21,7 +21,7 @@ cdef class Integer(Node): cdef class Key(Node): cpdef set_value(self, object value) - cpdef int get_value(self) + cpdef unicode get_value(self) cdef class Real(Node): cpdef set_value(self, object value) diff --git a/cython/plist.pyx b/cython/plist.pyx index 9b7eec9..746db4f 100644 --- a/cython/plist.pyx +++ b/cython/plist.pyx @@ -41,6 +41,10 @@ cdef extern from *: void plist_get_date_val(plist_t node, int32_t * sec, int32_t * usec) void plist_set_date_val(plist_t node, int32_t sec, int32_t usec) + plist_t plist_new_key(char *val) + void plist_get_key_val(plist_t node, char **val) + void plist_set_key_val(plist_t node, char *val) + plist_t plist_new_string(char *val) void plist_get_string_val(plist_t node, char **val) void plist_set_string_val(plist_t node, char *val) @@ -272,6 +276,77 @@ cdef Real Real_factory(plist_t c_node, bint managed=True): from cpython cimport PY_MAJOR_VERSION +cdef class Key(Node): + def __cinit__(self, object value=None, *args, **kwargs): + cdef: + char* c_utf8_data = NULL + bytes utf8_data + if value is None: + raise ValueError("Requires a value") + else: + if isinstance(value, unicode): + utf8_data = value.encode('utf-8') + elif (PY_MAJOR_VERSION < 3) and isinstance(value, str): + value.encode('ascii') # trial decode + utf8_data = value.encode('ascii') + else: + raise ValueError("Requires unicode input, got %s" % type(value)) + c_utf8_data = utf8_data + self._c_node = plist_new_string("") + plist_set_type(self._c_node, PLIST_KEY) + #plist_set_key_val(self._c_node, c_utf8_data) + + def __repr__(self): + s = self.get_value() + return '' % s.encode('utf-8') + + def __richcmp__(self, other, op): + cdef unicode s = self.get_value() + if op == 0: + return s < other + if op == 1: + return s <= other + if op == 2: + return s == other + if op == 3: + return s != other + if op == 4: + return s > other + if op == 5: + return s >= other + + cpdef set_value(self, object value): + cdef: + char* c_utf8_data = NULL + bytes utf8_data + if value is None: + plist_set_key_val(self._c_node, c_utf8_data) + else: + if isinstance(value, unicode): + utf8_data = value.encode('utf-8') + elif (PY_MAJOR_VERSION < 3) and isinstance(value, str): + value.encode('ascii') # trial decode + utf8_data = value.encode('ascii') + else: + raise ValueError("Requires unicode input, got %s" % type(value)) + c_utf8_data = utf8_data + plist_set_key_val(self._c_node, c_utf8_data) + + cpdef unicode get_value(self): + cdef: + char* c_value = NULL + plist_get_key_val(self._c_node, &c_value) + try: + return cpython.PyUnicode_DecodeUTF8(c_value, len(c_value), 'strict') + finally: + libc.stdlib.free(c_value) + +cdef Key Key_factory(plist_t c_node, bint managed=True): + cdef Key instance = Key.__new__(Key) + instance._c_managed = managed + instance._c_node = c_node + return instance + cdef class String(Node): def __cinit__(self, object value=None, *args, **kwargs): cdef: -- cgit v1.1-32-gdbae