diff --git a/libdatrie b/libdatrie index d1dfdb8..b174e65 160000 --- a/libdatrie +++ b/libdatrie @@ -1 +1 @@ -Subproject commit d1dfdb831093892541cae46eba82c46aec94f726 +Subproject commit b174e656ed365771303c8d5fb1342583f5b3c2a8 diff --git a/src/cdatrie.pxd b/src/cdatrie.pxd index b80ce99..78c99bb 100644 --- a/src/cdatrie.pxd +++ b/src/cdatrie.pxd @@ -54,6 +54,10 @@ cdef extern from "../libdatrie/datrie/trie.h": int trie_fwrite (Trie *trie, stdio.FILE *file) + size_t trie_get_serialized_size (Trie *trie); + + void trie_serialize (Trie *trie, unsigned char *ptr); + bint trie_is_dirty (Trie *trie) diff --git a/src/datrie.pyx b/src/datrie.pyx index 0f91a41..60fe43f 100644 --- a/src/datrie.pyx +++ b/src/datrie.pyx @@ -6,6 +6,7 @@ Cython wrapper for libdatrie. from cpython.version cimport PY_MAJOR_VERSION from cython.operator import dereference as deref from libc.stdlib cimport malloc, free +from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free from libc cimport stdio from libc cimport string cimport stdio_ext @@ -129,6 +130,16 @@ cdef class BaseTrie: stdio.fflush(f_ptr) + def __bytes__(self): + cdef Py_ssize_t size = cdatrie.trie_get_serialized_size(self._c_trie) + cdef unsigned char* data = PyMem_Malloc(size) + try: + cdatrie.trie_serialize (self._c_trie, data) + return data[:size] + finally: + PyMem_Free(data) + return res + @classmethod def load(cls, path): """