From 06eac326104ba1e8734d28a27626b88b18dde600 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Thu, 23 Jul 2015 01:10:36 +0200 Subject: [PATCH] * Add cfile.pyx --- setup.py | 1 + spacy/cfile.pxd | 12 ++++++++++++ spacy/cfile.pyx | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 spacy/cfile.pxd create mode 100644 spacy/cfile.pyx diff --git a/setup.py b/setup.py index b29c01f60..eb3adde3e 100644 --- a/setup.py +++ b/setup.py @@ -162,6 +162,7 @@ MOD_NAMES = ['spacy.parts_of_speech', 'spacy.strings', 'spacy.gold', 'spacy.orth', 'spacy.tokens.doc', 'spacy.tokens.spans', 'spacy.tokens.token', 'spacy.serialize.packer', 'spacy.serialize.huffman', 'spacy.serialize.bits', + 'spacy.cfile', 'spacy.syntax.ner'] diff --git a/spacy/cfile.pxd b/spacy/cfile.pxd new file mode 100644 index 000000000..c9a6aec41 --- /dev/null +++ b/spacy/cfile.pxd @@ -0,0 +1,12 @@ +from libc.stdio cimport fopen, fclose, fread, fwrite, FILE +from cymem.cymem cimport Pool + +cdef class CFile: + cdef FILE* fp + cdef bint is_open + + cdef int read_into(self, void* dest, size_t number, size_t elem_size) except -1 + + cdef int write_from(self, void* src, size_t number, size_t elem_size) except -1 + + cdef void* alloc_read(self, Pool mem, size_t number, size_t elem_size) except * diff --git a/spacy/cfile.pyx b/spacy/cfile.pyx new file mode 100644 index 000000000..cb192dfab --- /dev/null +++ b/spacy/cfile.pyx @@ -0,0 +1,38 @@ +from libc.stdio cimport fopen, fclose, fread, fwrite, FILE + + +cdef class CFile: + def __init__(self, loc, bytes mode): + cdef bytes bytes_loc = loc.encode('utf8') if type(loc) == unicode else loc + self.fp = fopen(bytes_loc, mode) + if self.fp == NULL: + raise IOError("Could not open binary file %s" % bytes_loc) + self.is_open = True + + def __dealloc__(self): + if self.is_open: + fclose(self.fp) + + def close(self): + fclose(self.fp) + self.is_open = False + + cdef int read_into(self, void* dest, size_t number, size_t elem_size) except -1: + st = fread(dest, elem_size, number, self.fp) + if st != number: + raise IOError + + cdef int write_from(self, void* src, size_t number, size_t elem_size) except -1: + st = fwrite(src, elem_size, number, self.fp) + if st != number: + raise IOError + + cdef void* alloc_read(self, Pool mem, size_t number, size_t elem_size) except *: + cdef void* dest = mem.alloc(number, elem_size) + self.read_into(dest, number, elem_size) + return dest + + def write_unicode(self, unicode value): + cdef bytes py_bytes = value.encode('utf8') + cdef char* chars = py_bytes + self.write(sizeof(char), len(py_bytes), chars)