Skip to content

Commit

Permalink
Added serializing into bytes object
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Jan 26, 2021
1 parent ba96de5 commit e7feac4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/cdatrie.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
11 changes: 11 additions & 0 deletions src/datrie.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = <unsigned char*> PyMem_Malloc(size)
try:
cdatrie.trie_serialize (self._c_trie, data)
return <bytes> data[:size]
finally:
PyMem_Free(data)
return res

@classmethod
def load(cls, path):
"""
Expand Down

0 comments on commit e7feac4

Please sign in to comment.