Commit 0b1b3f00 authored by Mark Florisson's avatar Mark Florisson

Support acquiring slices from memoryviews

parent e31c8f6f
...@@ -119,11 +119,6 @@ cdef class array: ...@@ -119,11 +119,6 @@ cdef class array:
else: else:
info.format = NULL info.format = NULL
# info.obj = self
def __releasebuffer__(self, Py_buffer *info):
pass
def __dealloc__(array self): def __dealloc__(array self):
if self.callback_free_data != NULL: if self.callback_free_data != NULL:
self.callback_free_data(self.data) self.callback_free_data(self.data)
...@@ -183,6 +178,9 @@ cdef extern from *: ...@@ -183,6 +178,9 @@ cdef extern from *:
int __Pyx_GetBuffer(object, Py_buffer *, int) except -1 int __Pyx_GetBuffer(object, Py_buffer *, int) except -1
void __Pyx_ReleaseBuffer(Py_buffer *) void __Pyx_ReleaseBuffer(Py_buffer *)
void Py_INCREF(object)
void Py_DECREF(object)
ctypedef struct PyObject ctypedef struct PyObject
cdef struct __pyx_memoryview "__pyx_memoryview_obj": cdef struct __pyx_memoryview "__pyx_memoryview_obj":
...@@ -224,6 +222,7 @@ cdef class memoryview(object): ...@@ -224,6 +222,7 @@ cdef class memoryview(object):
cdef PyThread_type_lock lock cdef PyThread_type_lock lock
cdef int acquisition_count cdef int acquisition_count
cdef Py_buffer view cdef Py_buffer view
cdef int flags
def __cinit__(memoryview self, object obj, int flags): def __cinit__(memoryview self, object obj, int flags):
self.obj = obj self.obj = obj
...@@ -303,6 +302,10 @@ cdef class memoryview(object): ...@@ -303,6 +302,10 @@ cdef class memoryview(object):
for i, c in enumerate(bytesvalue): for i, c in enumerate(bytesvalue):
itemp[i] = c itemp[i] = c
def __getbuffer__(self, Py_buffer *info, int flags):
info[0] = self.view
info.obj = self
property T: property T:
@cname('__pyx_memoryview_transpose') @cname('__pyx_memoryview_transpose')
def __get__(self): def __get__(self):
...@@ -432,6 +435,7 @@ cdef memoryview memview_slice(memoryview memview, object indices): ...@@ -432,6 +435,7 @@ cdef memoryview memview_slice(memoryview memview, object indices):
else: else:
return memoryview_fromslice(&dst, new_ndim, NULL, NULL) return memoryview_fromslice(&dst, new_ndim, NULL, NULL)
# #
### Slicing in a single dimension of a memoryviewslice ### Slicing in a single dimension of a memoryviewslice
# #
...@@ -673,6 +677,9 @@ cdef class _memoryviewslice(memoryview): ...@@ -673,6 +677,9 @@ cdef class _memoryviewslice(memoryview):
def __get__(self): def __get__(self):
return self.from_object return self.from_object
@cname('__pyx_memoryviewslice_check')
cdef int __pyx_memoryviewslice_check(obj):
return isinstance(obj, _memoryviewslice)
@cname('__pyx_memoryview_fromslice') @cname('__pyx_memoryview_fromslice')
cdef memoryview_fromslice({{memviewslice_name}} *memviewslice, cdef memoryview_fromslice({{memviewslice_name}} *memviewslice,
......
...@@ -259,7 +259,7 @@ no_fail: ...@@ -259,7 +259,7 @@ no_fail:
} }
static CYTHON_INLINE void __pyx_fatalerror(char *fmt, ...) { static CYTHON_INLINE void __pyx_fatalerror(const char *fmt, ...) {
va_list vargs; va_list vargs;
char msg[200]; char msg[200];
......
...@@ -15,7 +15,7 @@ static {{struct_type_decl}} {{funcname}}(PyObject * o) { ...@@ -15,7 +15,7 @@ static {{struct_type_decl}} {{funcname}}(PyObject * o) {
{{for member in var_entries:}} {{for member in var_entries:}}
{{py:attr = "result." + member.cname}} {{py:attr = "result." + member.cname}}
value = PyMapping_GetItemString(o, "{{member.name}}"); value = PyMapping_GetItemString(o, (char *) "{{member.name}}");
if (!value) { if (!value) {
PyErr_SetString(PyExc_ValueError, "No value specified for struct " PyErr_SetString(PyExc_ValueError, "No value specified for struct "
"attribute '{{member.name}}'"); "attribute '{{member.name}}'");
......
...@@ -753,3 +753,51 @@ def test_oob(): ...@@ -753,3 +753,51 @@ def test_oob():
""" """
cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9)) cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9))
print a[:, 20] print a[:, 20]
def test_acquire_memoryview():
"""
>>> test_acquire_memoryview()
acquired A
22
<MemoryView of 'IntMockBuffer' object>
22
22
released A
"""
cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9))
cdef object b = a
print a[2, 4]
# Make sure we don't have to keep this around
del a
print b
cdef int[:, :] c = b
print b[2, 4]
print c[2, 4]
def test_acquire_memoryview_slice():
"""
>>> test_acquire_memoryview_slice()
acquired A
31
<MemoryView of 'IntMockBuffer' object>
31
31
released A
"""
cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9))
a = a[1:, :6]
cdef object b = a
print a[2, 4]
# Make sure we don't have to keep this around
del a
print b
cdef int[:, :] c = b
print b[2, 4]
print c[2, 4]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment