Commit d82d4fc3 authored by Mark Florisson's avatar Mark Florisson

Move cython.array to cython.view.array to avoid conflicts with pure-mode cython.array

parent cc419833
...@@ -118,7 +118,7 @@ class CythonScope(ModuleScope): ...@@ -118,7 +118,7 @@ class CythonScope(ModuleScope):
self.viewscope, cython_scope=self, self.viewscope, cython_scope=self,
whitelist=MemoryView.view_utility_whitelist) whitelist=MemoryView.view_utility_whitelist)
self.entries["array"] = view_utility_scope.entries.pop("array") # self.entries["array"] = view_utility_scope.entries.pop("array")
def create_cython_scope(context): def create_cython_scope(context):
......
...@@ -7189,7 +7189,7 @@ class CythonArrayNode(ExprNode): ...@@ -7189,7 +7189,7 @@ class CythonArrayNode(ExprNode):
return self.get_cython_array_type(env) return self.get_cython_array_type(env)
def get_cython_array_type(self, env): def get_cython_array_type(self, env):
return env.global_scope().context.cython_scope.lookup("array").type return env.global_scope().context.cython_scope.viewscope.lookup("array").type
def generate_result_code(self, code): def generate_result_code(self, code):
import Buffer import Buffer
......
...@@ -140,9 +140,9 @@ does not "follow" the last one anymore (meaning, it was strided already, but it ...@@ -140,9 +140,9 @@ does not "follow" the last one anymore (meaning, it was strided already, but it
contiguous any longer), since it was sliced. contiguous any longer), since it was sliced.
Memoryview objects and cython.array Memoryview Objects and Cython Arrays
=================================== ====================================
These typed slices can be converted to Python objects (`cython.memoryview`), and are indexable, These typed slices can be converted to Python objects (`cython.view.memoryview`), and are indexable,
slicable and transposable in the same way that the slices are. They can also be converted back to typed slicable and transposable in the same way that the slices are. They can also be converted back to typed
slices at any time. slices at any time.
...@@ -176,18 +176,18 @@ the view was resliced in the meantime. ...@@ -176,18 +176,18 @@ the view was resliced in the meantime.
Cython Array Cython Array
============ ============
Whenever a slice is copied (using any of the `copy` or `copy_fortran` methods), you get a new Whenever a slice is copied (using any of the `copy` or `copy_fortran` methods), you get a new
memoryview slice of a newly created cython.array object. This array can also be used manually, memoryview slice of a newly created cython.view.array object. This array can also be used manually,
and will automatically allocate a block of data. It can later be assigned to a C or Fortran and will automatically allocate a block of data. It can later be assigned to a C or Fortran
contiguous slice (or a strided slice). It can be used like:: contiguous slice (or a strided slice). It can be used like::
import cython import cython.view
my_array = cython.array(shape=(10, 2), itemsize=sizeof(int), format="i") my_array = cython.view.array(shape=(10, 2), itemsize=sizeof(int), format="i")
cdef int[:, :] my_slice = my_array cdef int[:, :] my_slice = my_array
It also takes an optional argument `mode` ('c' or 'fortran') and a boolean `allocate_buffer`, that indicates whether a buffer should be allocated and freed when it goes out of scope:: It also takes an optional argument `mode` ('c' or 'fortran') and a boolean `allocate_buffer`, that indicates whether a buffer should be allocated and freed when it goes out of scope::
cdef cython.array my_array = cython.array(..., mode="fortran", allocate_buffer=False) cdef cython.view.array my_array = cython.view.array(..., mode="fortran", allocate_buffer=False)
my_array.data = <char *> my_data_pointer my_array.data = <char *> my_data_pointer
# define a function that can deallocate the data (if needed) # define a function that can deallocate the data (if needed)
...@@ -195,10 +195,10 @@ It also takes an optional argument `mode` ('c' or 'fortran') and a boolean `allo ...@@ -195,10 +195,10 @@ It also takes an optional argument `mode` ('c' or 'fortran') and a boolean `allo
You can also cast pointers to array, or C arrays to arrays:: You can also cast pointers to array, or C arrays to arrays::
cdef cython.array my_array = <int[:10, :2]> my_data_pointer cdef cython.view.array my_array = <int[:10, :2]> my_data_pointer
cdef cython.array my_array = <int[:, :]> my_c_array cdef cython.view.array my_array = <int[:, :]> my_c_array
Of course, you can also immediately assign a cython.array to a typed memoryview slice. A C array Of course, you can also immediately assign a cython.view.array to a typed memoryview slice. A C array
may be assigned directly to a memoryview slice:: may be assigned directly to a memoryview slice::
cdef int[:, ::1] myslice = my_2d_c_array cdef int[:, ::1] myslice = my_2d_c_array
......
# mode: run
u'''
>>> f()
>>> g()
'''
# from cython.view cimport memoryview
from cython cimport array
cdef extern from "Python.h":
cdef int PyBUF_C_CONTIGUOUS
def f():
pass
# cdef array arr = array(shape=(10,10), itemsize=sizeof(int), format='i')
# cdef memoryview mv = memoryview(arr, PyBUF_C_CONTIGUOUS)
def g():
# cdef int[::1] mview = array((10,), itemsize=sizeof(int), format='i')
cdef int[::1] mview = array((10,), itemsize=sizeof(int), format='i')
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from cython cimport array from cython.view cimport array
from cython cimport view as v
cimport cython as cy cimport cython as cy
include "cythonarrayutil.pxi" include "cythonarrayutil.pxi"
...@@ -19,7 +20,7 @@ def contiguity(): ...@@ -19,7 +20,7 @@ def contiguity():
2 3 2 3
2 2
''' '''
cdef cy.array cvarray = cy.array(shape=(2,3), itemsize=sizeof(int), format="i", mode='c') cdef v.array cvarray = v.array(shape=(2,3), itemsize=sizeof(int), format="i", mode='c')
assert cvarray.len == 2*3*sizeof(int), (cvarray.len, 2*3*sizeof(int)) assert cvarray.len == 2*3*sizeof(int), (cvarray.len, 2*3*sizeof(int))
assert cvarray.itemsize == sizeof(int) assert cvarray.itemsize == sizeof(int)
print cvarray.strides[0], cvarray.strides[1] print cvarray.strides[0], cvarray.strides[1]
...@@ -28,7 +29,7 @@ def contiguity(): ...@@ -28,7 +29,7 @@ def contiguity():
print print
cdef cy.array farray = cy.array(shape=(2,3), itemsize=sizeof(int), format="i", mode='fortran') cdef v.array farray = v.array(shape=(2,3), itemsize=sizeof(int), format="i", mode='fortran')
assert farray.len == 2*3*sizeof(int) assert farray.len == 2*3*sizeof(int)
assert farray.itemsize == sizeof(int) assert farray.itemsize == sizeof(int)
print farray.strides[0], farray.strides[1] print farray.strides[0], farray.strides[1]
...@@ -40,29 +41,29 @@ def acquire(): ...@@ -40,29 +41,29 @@ def acquire():
>>> acquire() >>> acquire()
''' '''
cdef object[int, ndim=1, mode="c"] buf1d = \ cdef object[int, ndim=1, mode="c"] buf1d = \
cy.array(shape=(10,), itemsize=sizeof(int), format='i', mode='c') array(shape=(10,), itemsize=sizeof(int), format='i', mode='c')
cdef object[int, ndim=2, mode="c"] buf2d = \ cdef object[int, ndim=2, mode="c"] buf2d = \
cy.array(shape=(10,10), itemsize=sizeof(int), format='i') array(shape=(10,10), itemsize=sizeof(int), format='i')
cdef object[unsigned long, ndim=3, mode='fortran'] buf3d = \ cdef object[unsigned long, ndim=3, mode='fortran'] buf3d = \
cy.array(shape=(1,2,3), itemsize=sizeof(unsigned long), format='L', mode='fortran') array(shape=(1,2,3), itemsize=sizeof(unsigned long), format='L', mode='fortran')
cdef object[long double, ndim=3, mode='fortran'] bufld = \ cdef object[long double, ndim=3, mode='fortran'] bufld = \
cy.array(shape=(1,2,3), itemsize=sizeof(long double), format='g', mode='fortran') array(shape=(1,2,3), itemsize=sizeof(long double), format='g', mode='fortran')
def full_or_strided(): def full_or_strided():
''' '''
>>> full_or_strided() >>> full_or_strided()
''' '''
cdef object[float, ndim=2, mode='full'] fullbuf = \ cdef object[float, ndim=2, mode='full'] fullbuf = \
cy.array(shape=(10,10), itemsize=sizeof(float), format='f', mode='c') array(shape=(10,10), itemsize=sizeof(float), format='f', mode='c')
cdef object[long long int, ndim=3, mode='strided'] stridedbuf = \ cdef object[long long int, ndim=3, mode='strided'] stridedbuf = \
cy.array(shape=(1,2,3), itemsize=sizeof(long long int), format='q', mode='fortran') array(shape=(1,2,3), itemsize=sizeof(long long int), format='q', mode='fortran')
def dont_allocate_buffer(): def dont_allocate_buffer():
""" """
>>> dont_allocate_buffer() >>> dont_allocate_buffer()
callback called callback called
""" """
cdef cy.array result = cy.array((10, 10), itemsize=sizeof(int), format='i', allocate_buffer=False) cdef array result = array((10, 10), itemsize=sizeof(int), format='i', allocate_buffer=False)
assert result.data == NULL assert result.data == NULL
result.callback_free_data = callback result.callback_free_data = callback
result = None result = None
......
from libc.stdlib cimport malloc, free from libc.stdlib cimport malloc, free
cimport cython cimport cython
from cython.view cimport array
cdef void callback(void *data): cdef void callback(void *data):
print "callback called" print "callback called"
free(data) free(data)
def create_array(shape, mode, use_callback=False): def create_array(shape, mode, use_callback=False):
cdef cython.array result = cython.array(shape, itemsize=sizeof(int), cdef array result = array(shape, itemsize=sizeof(int),
format='i', mode=mode) format='i', mode=mode)
cdef int *data = <int *> result.data cdef int *data = <int *> result.data
cdef int i, j, cidx, fidx cdef int i, j, cidx, fidx
......
...@@ -7,8 +7,7 @@ u''' ...@@ -7,8 +7,7 @@ u'''
>>> assignmvs() >>> assignmvs()
''' '''
from cython.view cimport memoryview from cython.view cimport memoryview, array
from cython cimport array
from cython cimport view from cython cimport view
cdef extern from "Python.h": cdef extern from "Python.h":
...@@ -68,11 +67,11 @@ cdef class TestExcClassExternalDtype(object): ...@@ -68,11 +67,11 @@ cdef class TestExcClassExternalDtype(object):
cdef td_h_double[:, :] arr_double cdef td_h_double[:, :] arr_double
def __init__(self): def __init__(self):
self.arr_float = cython.array((10, 10), itemsize=sizeof(ext_dtype), format='f') self.arr_float = array((10, 10), itemsize=sizeof(ext_dtype), format='f')
self.arr_float[:] = 0.0 self.arr_float[:] = 0.0
self.arr_float[4, 4] = 2.0 self.arr_float[4, 4] = 2.0
self.arr_double = cython.array((10, 10), itemsize=sizeof(td_h_double), format='d') self.arr_double = array((10, 10), itemsize=sizeof(td_h_double), format='d')
self.arr_double[:] = 0.0 self.arr_double[:] = 0.0
self.arr_double[4, 4] = 2.0 self.arr_double[4, 4] = 2.0
......
...@@ -9,7 +9,7 @@ def testcase(func): ...@@ -9,7 +9,7 @@ def testcase(func):
cimport cython cimport cython
from cython cimport array from cython.view cimport array
import numpy as np import numpy as np
cimport numpy as np cimport numpy as np
......
...@@ -6,6 +6,7 @@ from __future__ import unicode_literals ...@@ -6,6 +6,7 @@ from __future__ import unicode_literals
cimport cython cimport cython
from cython cimport view from cython cimport view
from cython.view cimport array
from cython.parallel cimport prange, parallel from cython.parallel cimport prange, parallel
import gc import gc
...@@ -1555,7 +1556,7 @@ def test_memslice_prange(arg): ...@@ -1555,7 +1556,7 @@ def test_memslice_prange(arg):
src = arg src = arg
dst = cython.array((<object> src).shape, sizeof(int), format="i") dst = array((<object> src).shape, sizeof(int), format="i")
cdef int i, j, k cdef int i, j, k
...@@ -1941,7 +1942,7 @@ class SingleObject(object): ...@@ -1941,7 +1942,7 @@ class SingleObject(object):
return self.value == getattr(other, 'value', None) or self.value == other return self.value == getattr(other, 'value', None) or self.value == other
cdef _get_empty_object_slice(fill=None): cdef _get_empty_object_slice(fill=None):
cdef cython.array a = cython.array((10,), sizeof(PyObject *), 'O') cdef array a = array((10,), sizeof(PyObject *), 'O')
assert a.dtype_is_object assert a.dtype_is_object
return a return a
...@@ -2086,7 +2087,7 @@ def test_dtype_object_scalar_assignment(): ...@@ -2086,7 +2087,7 @@ def test_dtype_object_scalar_assignment():
""" """
>>> test_dtype_object_scalar_assignment() >>> test_dtype_object_scalar_assignment()
""" """
cdef object[:] m = cython.array((10,), sizeof(PyObject *), 'O') cdef object[:] m = array((10,), sizeof(PyObject *), 'O')
m[:] = SingleObject(2) m[:] = SingleObject(2)
assert m[0] == m[4] == m[-1] == 2 assert m[0] == m[4] == m[-1] == 2
......
...@@ -240,7 +240,7 @@ ctypedef td_h_short td_h_cy_short ...@@ -240,7 +240,7 @@ ctypedef td_h_short td_h_cy_short
cdef void dealloc_callback(void *data): cdef void dealloc_callback(void *data):
print "deallocating..." print "deallocating..."
def index(cython.array array): def index(array array):
array.callback_free_data = dealloc_callback array.callback_free_data = dealloc_callback
print np.asarray(array)[3, 2] print np.asarray(array)[3, 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