Last active
December 21, 2015 01:09
-
-
Save jsam/6225931 to your computer and use it in GitHub Desktop.
pyobjs proposal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cdef class CArray: | |
"""Class for representing c-array. Due to lack of void ptr arithmetic support there is no void casting magic, therefore per type casting is needed.""" | |
cdef public list PyList | |
cdef public unsigned int PyListSize | |
def __init__(self, arr=None): | |
dprint("Initialize CArray in __init__") | |
if arr is not None: | |
self.PyList = self.fix_args(arr) | |
self.PyListSize = <unsigned int> len(self.PyList) | |
dprint("CArray values initialized: {0}".format(self.PyList, self.PyListSize)) | |
else: | |
dprint("CArray(arr=None)") | |
def fix_args(self, arr): | |
if type(arr) == list: | |
return arr | |
else: | |
return list(arr) | |
def get_from_ptr(self, unsigned long long ptr, char *type, unsigned long long arr_size): | |
dprint("CArray().get_from_ptr({0}, {1}, {2})".format(ptr, type, arr_size)) | |
ret = [] | |
if str(type) == "i": | |
arr_cast = ctypes.cast(ptr, ctypes.POINTER(ctypes.c_int)) | |
for i in xrange(arr_size): | |
ret.append(arr_cast[i]) | |
# TODO: remaining types | |
return ret | |
cdef int* as_int(self): | |
dprint("...converting to int array") | |
cdef int *int_t = <int*> malloc(cython.sizeof(int) * self.PyListSize) | |
if int_t is NULL: | |
raise MemoryError() | |
for i in xrange(self.PyListSize): | |
int_t[i] = self.PyList[i] | |
return int_t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment