Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added serializing into bytes object #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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