Commit 54cbd0e8 authored by Mark Florisson's avatar Mark Florisson

Fix segfault & fix test

parent 349dd60e
......@@ -158,7 +158,7 @@ cdef array array_cwrapper(tuple shape, Py_ssize_t itemsize, char *format, char *
return result
########## View.MemoryView ##########
#################### View.MemoryView ####################
import cython
......@@ -180,6 +180,7 @@ cdef extern from *:
void Py_INCREF(object)
void Py_DECREF(object)
void Py_XINCREF(object)
ctypedef struct PyObject
......@@ -197,6 +198,10 @@ cdef extern from *:
void __PYX_INC_MEMVIEW({{memviewslice_name}} *memslice, int have_gil)
void __PYX_XDEC_MEMVIEW({{memviewslice_name}} *memslice, int have_gil)
ctypedef struct __pyx_buffer "Py_buffer":
PyObject *obj
PyObject *Py_None
@cname('__pyx_MemviewEnum')
cdef class Enum(object):
......@@ -246,6 +251,9 @@ cdef class memoryview(object):
if type(self) is memoryview or obj is not None:
__Pyx_GetBuffer(obj, &self.view, flags)
if <PyObject *> self.view.obj == NULL:
(<__pyx_buffer *> &self.view).obj = Py_None
Py_INCREF(None)
self.lock = PyThread_allocate_lock()
if self.lock == NULL:
......@@ -320,6 +328,8 @@ cdef class memoryview(object):
itemp[i] = c
def __getbuffer__(self, Py_buffer *info, int flags):
# Note: self.view.obj must not be NULL
Py_INCREF(self.view.obj)
info[0] = self.view
info.obj = self
......@@ -333,10 +343,6 @@ cdef class memoryview(object):
property object:
@cname('__pyx_memoryview__get__object')
def __get__(self):
if (self.obj is None and <PyObject *> self.view.obj != NULL and
self.view.obj is not None):
return <object> self.view.obj
return self.obj
property shape:
......@@ -459,6 +465,7 @@ cdef memoryview memview_slice(memoryview memview, object indices):
cdef extern from "stdlib.h":
void abort() nogil
void printf(char *s, ...) nogil
cdef extern from "stdio.h":
ctypedef struct FILE
......@@ -715,6 +722,8 @@ cdef memoryview_fromslice({{memviewslice_name}} *memviewslice,
result.view = memviewslice.memview.view
result.view.buf = <void *> memviewslice.data
result.view.ndim = ndim
(<__pyx_buffer *> &result.view).obj = Py_None
Py_INCREF(None)
result.view.shape = <Py_ssize_t *> result.from_slice.shape
result.view.strides = <Py_ssize_t *> result.from_slice.strides
......
# mode: error
from cython.parallel cimport prange
def invalid_closure_reduction():
sum = 0
def inner():
nonlocal sum
cdef int i
for i in prange(10, nogil=True):
with gil:
sum += i
_ERRORS = u"""
e_cython_parallel_pyobject_reduction.pyx:10:23: Python objects cannot be reductions
"""
......@@ -588,13 +588,16 @@ def assign_temporary_to_object(object[:] mslice):
buf = mslice
buf[1] = {3-2: 2+(2*4)-2}
def print_int_offsets(*args):
def print_offsets(*args, size=0, newline=True):
for item in args:
print item / sizeof(int),
print item / size,
print
if newline: print
def test_generic_slicing(arg):
def print_int_offsets(*args, newline=True):
print_offsets(*args, size=sizeof(int), newline=newline)
def test_generic_slicing(arg, indirect=False):
"""
Test simple slicing
>>> test_generic_slicing(IntMockBuffer("A", range(8 * 14 * 11), shape=(8, 14, 11)))
......@@ -614,7 +617,7 @@ def test_generic_slicing(arg):
Test indirect slicing
>>> L = [[range(k * 12 + j * 4, k * 12 + j * 4 + 4) for j in xrange(3)] for k in xrange(5)]
>>> test_generic_slicing(IntMockBuffer("A", L, shape=(5, 3, 4)))
>>> test_generic_slicing(IntMockBuffer("A", L, shape=(5, 3, 4)), indirect=True)
acquired A
(2, 0, 2)
0 1 -1
......@@ -623,10 +626,10 @@ def test_generic_slicing(arg):
>>> stride1 = 21 * 14
>>> stride2 = 21
>>> L = [[range(k * stride1 + j * stride2, k * stride1 + j * stride2 + 21) for j in xrange(14)] for k in xrange(9)]
>>> test_generic_slicing(IntMockBuffer("A", L, shape=(9, 14, 21)))
>>> test_generic_slicing(IntMockBuffer("A", L, shape=(9, 14, 21)), indirect=True)
acquired A
(3, 9, 2)
20 1 -1
10 1 -1
released A
"""
......@@ -635,9 +638,14 @@ def test_generic_slicing(arg):
b = a[2:8:2, -4:1:-1, 1:3]
print b.shape
if b.suboffsets[0] < 0:
print_int_offsets(*b.strides)
print_int_offsets(*b.suboffsets)
if indirect:
print b.suboffsets[0] / sizeof(int *),
print b.suboffsets[1] / sizeof(int),
print b.suboffsets[2]
else:
print_int_offsets(b.strides[0], b.strides[1], b.strides[2])
print_int_offsets(b.suboffsets[0], b.suboffsets[1], b.suboffsets[2])
cdef int i, j, k
for i in range(b.shape[0]):
......
......@@ -1162,14 +1162,17 @@ def test_cdef_function2():
cdef_function2(global_A, global_B)
def print_int_offsets(*args):
def print_offsets(*args, size=0, newline=True):
for item in args:
print item / sizeof(int),
print item / size,
print
if newline: print
def print_int_offsets(*args, newline=True):
print_offsets(*args, size=sizeof(int), newline=newline)
@testcase
def test_generic_slicing(arg):
def test_generic_slicing(arg, indirect=False):
"""
Test simple slicing
>>> test_generic_slicing(IntMockBuffer("A", range(8 * 14 * 11), shape=(8, 14, 11)))
......@@ -1189,7 +1192,7 @@ def test_generic_slicing(arg):
Test indirect slicing
>>> L = [[range(k * 12 + j * 4, k * 12 + j * 4 + 4) for j in xrange(3)] for k in xrange(5)]
>>> test_generic_slicing(IntMockBuffer("A", L, shape=(5, 3, 4)))
>>> test_generic_slicing(IntMockBuffer("A", L, shape=(5, 3, 4)), indirect=True)
acquired A
2 0 2
0 1 -1
......@@ -1198,10 +1201,10 @@ def test_generic_slicing(arg):
>>> stride1 = 21 * 14
>>> stride2 = 21
>>> L = [[range(k * stride1 + j * stride2, k * stride1 + j * stride2 + 21) for j in xrange(14)] for k in xrange(9)]
>>> test_generic_slicing(IntMockBuffer("A", L, shape=(9, 14, 21)))
>>> test_generic_slicing(IntMockBuffer("A", L, shape=(9, 14, 21)), indirect=True)
acquired A
3 9 2
20 1 -1
10 1 -1
released A
"""
......@@ -1209,7 +1212,12 @@ def test_generic_slicing(arg):
cdef int[::view.generic, ::view.generic, :] b = a[2:8:2, -4:1:-1, 1:3]
print b.shape[0], b.shape[1], b.shape[2]
if b.suboffsets[0] < 0:
if indirect:
print b.suboffsets[0] / sizeof(int *),
print b.suboffsets[1] / sizeof(int),
print b.suboffsets[2]
else:
print_int_offsets(b.strides[0], b.strides[1], b.strides[2])
print_int_offsets(b.suboffsets[0], b.suboffsets[1], b.suboffsets[2])
......@@ -1250,7 +1258,9 @@ def test_indirect_slicing(arg):
cdef int[::view.indirect, ::view.indirect] c = b[..., 0]
print b.shape[0], b.shape[1], b.shape[2]
print_int_offsets(b.suboffsets[0], b.suboffsets[1], b.suboffsets[2])
print b.suboffsets[0] / sizeof(int *),
print b.suboffsets[1] / sizeof(int),
print b.suboffsets[2]
print b[4, 2, 1]
print c[4, 2]
......
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