Skip to content

Commit 54fd4b2

Browse files
committed
Add ensure_continguous_memoryview function
Include a function to ensure an object is converted into a contiguous `memoryview` object.
1 parent c17e314 commit 54fd4b2

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

numcodecs/compat_ext.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# cython: language_level=3
2+
3+
4+
cpdef memoryview ensure_continguous_memoryview(obj)

numcodecs/compat_ext.pyx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# cython: embedsignature=True
2+
# cython: profile=False
3+
# cython: linetrace=False
4+
# cython: binding=False
5+
# cython: language_level=3
6+
7+
from cpython.buffer cimport PyBuffer_IsContiguous
8+
from cpython.memoryview cimport PyMemoryView_GET_BUFFER
9+
10+
11+
cpdef memoryview ensure_continguous_memoryview(obj):
12+
cdef memoryview mv
13+
if type(obj) is memoryview:
14+
mv = <memoryview>obj
15+
else:
16+
mv = memoryview(obj)
17+
if not PyBuffer_IsContiguous(PyMemoryView_GET_BUFFER(mv), b'A'):
18+
raise BufferError("Expected contiguous memory")
19+
return mv

setup.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ def jenkins_extension():
276276
]
277277

278278

279+
def compat_extension():
280+
info('setting up compat extension')
281+
282+
extra_compile_args = base_compile_args.copy()
283+
284+
sources = ['numcodecs/compat_ext.pyx']
285+
286+
# define extension module
287+
return [
288+
Extension(
289+
'numcodecs.compat_ext',
290+
sources=sources,
291+
extra_compile_args=extra_compile_args,
292+
),
293+
]
294+
295+
279296
def shuffle_extension():
280297
info('setting up shuffle extension')
281298

@@ -344,6 +361,7 @@ def run_setup(with_extensions):
344361
blosc_extension()
345362
+ zstd_extension()
346363
+ lz4_extension()
364+
+ compat_extension()
347365
+ shuffle_extension()
348366
+ vlen_extension()
349367
+ fletcher_extension()

0 commit comments

Comments
 (0)