Commit d88146d0 authored by Mark Florisson's avatar Mark Florisson

Merge cython array utility with memoryview utility

parent 23d7640e
......@@ -103,9 +103,6 @@ class CythonScope(ModuleScope):
cython_test_extclass_utility_code.declare_in_scope(
self, cython_scope=self)
MemoryView.cython_array_utility_code.declare_in_scope(
self, cython_scope=self)
#
# The view sub-scope
#
......@@ -115,13 +112,12 @@ class CythonScope(ModuleScope):
viewscope.pxd_file_loaded = True
cythonview_testscope_utility_code.declare_in_scope(
viewscope, cython_scope=self)
viewscope, cython_scope=self)
view_utility_scope = MemoryView.view_utility_code.declare_in_scope(
viewscope, cython_scope=self)
self.viewscope, cython_scope=self)
# MemoryView.memview_fromslice_utility_code.from_scope = view_utility_scope
# MemoryView.memview_fromslice_utility_code.declare_in_scope(viewscope)
self.entries["array"] = view_utility_scope.lookup("array")
def create_cython_scope(context):
......
......@@ -853,6 +853,10 @@ def _resolve_AttributeNode(env, node):
return entry
#
### Utility loading
#
def load_memview_cy_utility(util_code_name, context=None, **kwargs):
return CythonUtilityCode.load(util_code_name, "MemoryView.pyx",
context=context, **kwargs)
......@@ -865,9 +869,9 @@ def load_memview_c_utility(util_code_name, context=None, **kwargs):
context=context, **kwargs)
def use_cython_array_utility_code(env):
scope = env.global_scope().context.cython_scope
scope.lookup('array_cwrapper').used = True
env.use_utility_code(cython_array_utility_code)
cython_scope = env.global_scope().context.cython_scope
cython_scope.load_cythonscope()
cython_scope.viewscope.lookup('array_cwrapper').used = True
context = {
'memview_struct_name': memview_objstruct_cname,
......@@ -916,15 +920,4 @@ view_utility_code = load_memview_cy_utility(
copy_contents_new_utility],
)
cython_array_utility_code = load_memview_cy_utility(
"CythonArray",
context=context,
requires=[view_utility_code])
copy_contents_new_utility.requires.append(cython_array_utility_code)
# memview_fromslice_utility_code = load_memview_cy_utility(
# "MemviewFromSlice",
# context=context,
# requires=[view_utility_code],
# )
copy_contents_new_utility.requires.append(view_utility_code)
\ No newline at end of file
########## CythonArray ##########
#################### View.MemoryView ####################
cdef extern from "stdlib.h":
void *malloc(size_t)
void free(void *)
# This utility provides cython.array and cython.view.memoryview
import cython
# from cpython cimport ...
cdef extern from "Python.h":
int PyIndex_Check(object)
object PyLong_FromVoidPtr(void *)
cdef extern from "pythread.h":
ctypedef void *PyThread_type_lock
PyThread_type_lock PyThread_allocate_lock()
void PyThread_free_lock(PyThread_type_lock)
int PyThread_acquire_lock(PyThread_type_lock, int mode) nogil
void PyThread_release_lock(PyThread_type_lock) nogil
cdef extern from "string.h":
void *memset(void *b, int c, size_t len)
cdef extern from *:
int __Pyx_GetBuffer(object, Py_buffer *, int) except -1
void __Pyx_ReleaseBuffer(Py_buffer *)
ctypedef struct PyObject
void Py_INCREF(PyObject *)
void Py_DECREF(PyObject *)
cdef struct __pyx_memoryview "__pyx_memoryview_obj":
Py_buffer view
PyObject *obj
ctypedef struct {{memviewslice_name}}:
__pyx_memoryview *memview
char *data
Py_ssize_t shape[{{max_dims}}]
Py_ssize_t strides[{{max_dims}}]
Py_ssize_t suboffsets[{{max_dims}}]
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
cdef enum:
PyBUF_C_CONTIGUOUS,
......@@ -12,22 +53,36 @@ cdef extern from "Python.h":
PyBUF_ANY_CONTIGUOUS
PyBUF_FORMAT
PyBUF_WRITABLE
PyBUF_STRIDES
PyBUF_INDIRECT
PyBUF_RECORDS
ctypedef struct PyObject
PyObject *Py_None
void Py_INCREF(PyObject *)
cdef extern from *:
ctypedef int __pyx_atomic_int
{{memviewslice_name}} slice_copy_contig "__pyx_memoryview_copy_new_contig"(
__Pyx_memviewslice *from_mvs,
char *mode, int ndim,
size_t sizeof_dtype, int contig_flag,
bint dtype_is_object) nogil except *
bint slice_is_contig "__pyx_memviewslice_is_contig" (
{{memviewslice_name}} *mvs, char order, int ndim) nogil
bint slices_overlap "__pyx_slices_overlap" ({{memviewslice_name}} *slice1,
{{memviewslice_name}} *slice2,
int ndim, size_t itemsize) nogil
cdef extern from *:
object __pyx_memoryview_new(object obj, int flags, bint dtype_is_object)
Py_ssize_t fill_contig_strides_array "__pyx_fill_contig_strides_array" (
Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t stride,
int ndim, char order) nogil
cdef extern from "stdlib.h":
void *malloc(size_t) nogil
void free(void *) nogil
void *memcpy(void *dest, void *src, size_t n) nogil
cdef void refcount_objects_in_slice "__pyx_memoryview_refcount_objects_in_slice" (
char *data, Py_ssize_t *shape, Py_ssize_t *strides, int ndim, bint inc)
#
### cython.array class
#
@cname("__pyx_array")
cdef class array:
......@@ -154,14 +209,9 @@ cdef class array:
property memview:
@cname('__pyx_cython_array_get_memview')
def __get__(self):
# Make this a property as 'data' may be set after instantiation
#if self._memview is None:
# flags = PyBUF_ANY_CONTIGUOUS|PyBUF_FORMAT|PyBUF_WRITABLE
# self._memview = __pyx_memoryview_new(self, flags)
#return self._memview
# Make this a property as 'self.data' may be set after instantiation
flags = PyBUF_ANY_CONTIGUOUS|PyBUF_FORMAT|PyBUF_WRITABLE
return __pyx_memoryview_new(self, flags, self.dtype_is_object)
return memoryview(self, flags, self.dtype_is_object)
def __getattr__(self, attr):
......@@ -189,90 +239,9 @@ cdef array array_cwrapper(tuple shape, Py_ssize_t itemsize, char *format,
return result
#################### View.MemoryView ####################
import cython
# from cpython cimport ...
cdef extern from "Python.h":
int PyIndex_Check(object)
object PyLong_FromVoidPtr(void *)
cdef extern from "pythread.h":
ctypedef void *PyThread_type_lock
PyThread_type_lock PyThread_allocate_lock()
void PyThread_free_lock(PyThread_type_lock)
int PyThread_acquire_lock(PyThread_type_lock, int mode) nogil
void PyThread_release_lock(PyThread_type_lock) nogil
cdef extern from "string.h":
void *memset(void *b, int c, size_t len)
cdef extern from *:
int __Pyx_GetBuffer(object, Py_buffer *, int) except -1
void __Pyx_ReleaseBuffer(Py_buffer *)
ctypedef struct PyObject
void Py_INCREF(PyObject *)
void Py_DECREF(PyObject *)
cdef struct __pyx_memoryview "__pyx_memoryview_obj":
Py_buffer view
PyObject *obj
ctypedef struct {{memviewslice_name}}:
__pyx_memoryview *memview
char *data
Py_ssize_t shape[{{max_dims}}]
Py_ssize_t strides[{{max_dims}}]
Py_ssize_t suboffsets[{{max_dims}}]
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
cdef enum:
PyBUF_C_CONTIGUOUS,
PyBUF_F_CONTIGUOUS,
PyBUF_ANY_CONTIGUOUS
PyBUF_FORMAT
PyBUF_WRITABLE
PyBUF_STRIDES
PyBUF_INDIRECT
PyBUF_RECORDS
cdef extern from *:
ctypedef int __pyx_atomic_int
{{memviewslice_name}} slice_copy_contig "__pyx_memoryview_copy_new_contig"(
__Pyx_memviewslice *from_mvs,
char *mode, int ndim,
size_t sizeof_dtype, int contig_flag,
bint dtype_is_object) nogil except *
bint slice_is_contig "__pyx_memviewslice_is_contig" (
{{memviewslice_name}} *mvs, char order, int ndim) nogil
bint slices_overlap "__pyx_slices_overlap" ({{memviewslice_name}} *slice1,
{{memviewslice_name}} *slice2,
int ndim, size_t itemsize) nogil
cdef extern from "stdlib.h":
void *malloc(size_t) nogil
void free(void *) nogil
void *memcpy(void *dest, void *src, size_t n) nogil
@cname('__pyx_MemviewEnum')
cdef class Enum(object):
cdef object name
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
#
### Memoryview constants and cython.view.memoryview class
#
# Disable generic_contiguous, as it makes trouble verifying contiguity:
# - 'contiguous' or '::1' means the dimension is contiguous with dtype
......@@ -290,6 +259,14 @@ cdef class Enum(object):
# would mean you'd have assert dimension 0 to be indirect (and pointer contiguous) at runtime.
# So it doesn't bring any performance benefit, and it's only confusing.
@cname('__pyx_MemviewEnum')
cdef class Enum(object):
cdef object name
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
cdef generic = Enum("<strided and direct or indirect>")
cdef strided = Enum("<strided and direct>") # default
cdef indirect = Enum("<strided and indirect>")
......
......@@ -6,7 +6,10 @@ u'''
'''
# from cython.view cimport memoryview
from cython cimport array, PyBUF_C_CONTIGUOUS
from cython cimport array
cdef extern from "Python.h":
cdef int PyBUF_C_CONTIGUOUS
def f():
pass
......
......@@ -8,9 +8,12 @@ u'''
'''
from cython.view cimport memoryview
from cython cimport array, PyBUF_C_CONTIGUOUS
from cython cimport array
from cython cimport view
cdef extern from "Python.h":
cdef int PyBUF_C_CONTIGUOUS
include "mockbuffers.pxi"
#
......
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