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): ...@@ -103,9 +103,6 @@ class CythonScope(ModuleScope):
cython_test_extclass_utility_code.declare_in_scope( cython_test_extclass_utility_code.declare_in_scope(
self, cython_scope=self) self, cython_scope=self)
MemoryView.cython_array_utility_code.declare_in_scope(
self, cython_scope=self)
# #
# The view sub-scope # The view sub-scope
# #
...@@ -115,13 +112,12 @@ class CythonScope(ModuleScope): ...@@ -115,13 +112,12 @@ class CythonScope(ModuleScope):
viewscope.pxd_file_loaded = True viewscope.pxd_file_loaded = True
cythonview_testscope_utility_code.declare_in_scope( 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( 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 self.entries["array"] = view_utility_scope.lookup("array")
# MemoryView.memview_fromslice_utility_code.declare_in_scope(viewscope)
def create_cython_scope(context): def create_cython_scope(context):
......
...@@ -853,6 +853,10 @@ def _resolve_AttributeNode(env, node): ...@@ -853,6 +853,10 @@ def _resolve_AttributeNode(env, node):
return entry return entry
#
### Utility loading
#
def load_memview_cy_utility(util_code_name, context=None, **kwargs): def load_memview_cy_utility(util_code_name, context=None, **kwargs):
return CythonUtilityCode.load(util_code_name, "MemoryView.pyx", return CythonUtilityCode.load(util_code_name, "MemoryView.pyx",
context=context, **kwargs) context=context, **kwargs)
...@@ -865,9 +869,9 @@ def load_memview_c_utility(util_code_name, context=None, **kwargs): ...@@ -865,9 +869,9 @@ def load_memview_c_utility(util_code_name, context=None, **kwargs):
context=context, **kwargs) context=context, **kwargs)
def use_cython_array_utility_code(env): def use_cython_array_utility_code(env):
scope = env.global_scope().context.cython_scope cython_scope = env.global_scope().context.cython_scope
scope.lookup('array_cwrapper').used = True cython_scope.load_cythonscope()
env.use_utility_code(cython_array_utility_code) cython_scope.viewscope.lookup('array_cwrapper').used = True
context = { context = {
'memview_struct_name': memview_objstruct_cname, 'memview_struct_name': memview_objstruct_cname,
...@@ -916,15 +920,4 @@ view_utility_code = load_memview_cy_utility( ...@@ -916,15 +920,4 @@ view_utility_code = load_memview_cy_utility(
copy_contents_new_utility], copy_contents_new_utility],
) )
cython_array_utility_code = load_memview_cy_utility( copy_contents_new_utility.requires.append(view_utility_code)
"CythonArray", \ No newline at end of file
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],
# )
########## CythonArray ########## #################### View.MemoryView ####################
cdef extern from "stdlib.h": # This utility provides cython.array and cython.view.memoryview
void *malloc(size_t)
void free(void *) import cython
# from cpython cimport ...
cdef extern from "Python.h": 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: cdef enum:
PyBUF_C_CONTIGUOUS, PyBUF_C_CONTIGUOUS,
...@@ -12,22 +53,36 @@ cdef extern from "Python.h": ...@@ -12,22 +53,36 @@ cdef extern from "Python.h":
PyBUF_ANY_CONTIGUOUS PyBUF_ANY_CONTIGUOUS
PyBUF_FORMAT PyBUF_FORMAT
PyBUF_WRITABLE PyBUF_WRITABLE
PyBUF_STRIDES
PyBUF_INDIRECT
PyBUF_RECORDS
ctypedef struct PyObject cdef extern from *:
PyObject *Py_None ctypedef int __pyx_atomic_int
void Py_INCREF(PyObject *) {{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 *: cdef extern from "stdlib.h":
object __pyx_memoryview_new(object obj, int flags, bint dtype_is_object) void *malloc(size_t) nogil
Py_ssize_t fill_contig_strides_array "__pyx_fill_contig_strides_array" ( void free(void *) nogil
Py_ssize_t *shape, Py_ssize_t *strides, Py_ssize_t stride, void *memcpy(void *dest, void *src, size_t n) nogil
int ndim, char order) 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") @cname("__pyx_array")
cdef class array: cdef class array:
...@@ -154,14 +209,9 @@ cdef class array: ...@@ -154,14 +209,9 @@ cdef class array:
property memview: property memview:
@cname('__pyx_cython_array_get_memview') @cname('__pyx_cython_array_get_memview')
def __get__(self): def __get__(self):
# Make this a property as 'data' may be set after instantiation # Make this a property as 'self.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
flags = PyBUF_ANY_CONTIGUOUS|PyBUF_FORMAT|PyBUF_WRITABLE 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): def __getattr__(self, attr):
...@@ -189,90 +239,9 @@ cdef array array_cwrapper(tuple shape, Py_ssize_t itemsize, char *format, ...@@ -189,90 +239,9 @@ cdef array array_cwrapper(tuple shape, Py_ssize_t itemsize, char *format,
return result return result
#################### View.MemoryView #################### #
### Memoryview constants and cython.view.memoryview class
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
# Disable generic_contiguous, as it makes trouble verifying contiguity: # Disable generic_contiguous, as it makes trouble verifying contiguity:
# - 'contiguous' or '::1' means the dimension is contiguous with dtype # - 'contiguous' or '::1' means the dimension is contiguous with dtype
...@@ -290,6 +259,14 @@ cdef class Enum(object): ...@@ -290,6 +259,14 @@ cdef class Enum(object):
# would mean you'd have assert dimension 0 to be indirect (and pointer contiguous) at runtime. # 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. # 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 generic = Enum("<strided and direct or indirect>")
cdef strided = Enum("<strided and direct>") # default cdef strided = Enum("<strided and direct>") # default
cdef indirect = Enum("<strided and indirect>") cdef indirect = Enum("<strided and indirect>")
......
...@@ -6,7 +6,10 @@ u''' ...@@ -6,7 +6,10 @@ u'''
''' '''
# from cython.view cimport memoryview # 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(): def f():
pass pass
......
...@@ -8,9 +8,12 @@ u''' ...@@ -8,9 +8,12 @@ u'''
''' '''
from cython.view cimport memoryview from cython.view cimport memoryview
from cython cimport array, PyBUF_C_CONTIGUOUS from cython cimport array
from cython cimport view from cython cimport view
cdef extern from "Python.h":
cdef int PyBUF_C_CONTIGUOUS
include "mockbuffers.pxi" 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