Skip to content

Commit 3fa7d8d

Browse files
committed
Added serializing into bytes object
1 parent ba96de5 commit 3fa7d8d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/cdatrie.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ cdef extern from "../libdatrie/datrie/trie.h":
5454

5555
int trie_fwrite (Trie *trie, stdio.FILE *file)
5656

57+
size_t get_trie_serialized_size (Trie *trie);
58+
59+
void trie_serialize (Trie *trie, unsigned char *ptr);
60+
5761
bint trie_is_dirty (Trie *trie)
5862

5963

src/datrie.pyx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Cython wrapper for libdatrie.
66
from cpython.version cimport PY_MAJOR_VERSION
77
from cython.operator import dereference as deref
88
from libc.stdlib cimport malloc, free
9+
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
910
from libc cimport stdio
1011
from libc cimport string
1112
cimport stdio_ext
@@ -129,6 +130,16 @@ cdef class BaseTrie:
129130

130131
stdio.fflush(f_ptr)
131132

133+
def __bytes__(self):
134+
cdef Py_ssize_t size = cdatrie.get_trie_serialized_size(self._c_trie)
135+
cdef unsigned char* data = <unsigned char*> PyMem_Malloc(size)
136+
try:
137+
cdatrie.trie_serialize (self._c_trie, data)
138+
return <bytes> data[:size]
139+
finally:
140+
PyMem_Free(data)
141+
return res
142+
132143
@classmethod
133144
def load(cls, path):
134145
"""

0 commit comments

Comments
 (0)