@@ -9,12 +9,15 @@ import threading
9
9
10
10
from numpy cimport ndarray
11
11
from cpython.ref cimport PyObject
12
+ from cpython cimport array
13
+ import array
12
14
from cpython.buffer cimport PyObject_GetBuffer, PyBuffer_Release, \
13
15
PyBUF_ANY_CONTIGUOUS
14
16
15
17
16
18
cdef extern from " Python.h" :
17
19
int PyByteArray_Resize(PyObject * bytearray, Py_ssize_t len )
20
+ PyObject* PyByteArray_FromObject(PyObject * o)
18
21
19
22
20
23
from zarr.compat import PY2, text_type
@@ -75,45 +78,45 @@ def set_nthreads(int nthreads):
75
78
return blosc_set_nthreads(nthreads)
76
79
77
80
78
- def decompress (cdata , ndarray array ):
79
- """ Decompress data into a numpy array .
81
+ def decompress (source , dest ):
82
+ """ Decompress data.
80
83
81
84
Parameters
82
85
----------
83
- cdata : bytes-like
86
+ source : object
84
87
Compressed data, including blosc header.
85
- array : ndarray
86
- Numpy array to decompress into.
88
+ dest : object
89
+ Object to decompress into.
87
90
88
91
Notes
89
92
-----
90
- Assumes that the size of the destination array is correct for the size of
93
+ Assumes that the size of the destination buffer is correct for the size of
91
94
the uncompressed data.
92
95
93
96
"""
94
97
cdef:
95
98
int ret
96
- char * source
97
- char * dest
99
+ char * source_ptr
100
+ char * dest_ptr
98
101
Py_buffer source_buffer
99
102
Py_buffer dest_buffer
100
- Py_ssize_t nbytes
103
+ size_t nbytes
101
104
102
- # setup
103
- PyObject_GetBuffer(cdata , & source_buffer, PyBUF_ANY_CONTIGUOUS)
104
- source = < char * > source_buffer.buf
105
- PyObject_GetBuffer(array , & dest_buffer, PyBUF_ANY_CONTIGUOUS)
106
- dest = < char * > dest_buffer.buf
107
- nbytes = array.nbytes
105
+ # setup buffers
106
+ PyObject_GetBuffer(source , & source_buffer, PyBUF_ANY_CONTIGUOUS)
107
+ source_ptr = < char * > source_buffer.buf
108
+ PyObject_GetBuffer(dest , & dest_buffer, PyBUF_ANY_CONTIGUOUS)
109
+ dest_ptr = < char * > dest_buffer.buf
110
+ nbytes = dest_buffer.len
108
111
109
112
# perform decompression
110
113
if _get_use_threads():
111
114
# allow blosc to use threads internally
112
115
with nogil:
113
- ret = blosc_decompress(source, dest , nbytes)
116
+ ret = blosc_decompress(source_ptr, dest_ptr , nbytes)
114
117
else :
115
118
with nogil:
116
- ret = blosc_decompress_ctx(source, dest , nbytes, 1 )
119
+ ret = blosc_decompress_ctx(source_ptr, dest_ptr , nbytes, 1 )
117
120
118
121
# release buffers
119
122
PyBuffer_Release(& source_buffer)
@@ -124,7 +127,7 @@ def decompress(cdata, ndarray array):
124
127
raise RuntimeError (' error during blosc decompression: %d ' % ret)
125
128
126
129
127
- def compress (ndarray array , char* cname , int clevel , int shuffle ):
130
+ def compress (source , char* cname , int clevel , int shuffle ):
128
131
""" Compress data in a numpy array.
129
132
130
133
Parameters
@@ -146,23 +149,23 @@ def compress(ndarray array, char* cname, int clevel, int shuffle):
146
149
"""
147
150
148
151
cdef:
149
- char * source
150
- char * dest
151
- bytearray cdata
152
+ char * source_ptr
153
+ char * dest_ptr
152
154
Py_buffer source_buffer
153
- Py_buffer dest_buffer
154
- Py_ssize_t nbytes, cbytes, itemsize
155
+ size_t nbytes, cbytes, itemsize
156
+ array.array char_array_template = array.array(' b' , [])
157
+ array.array dest
155
158
156
- # setup source
157
- PyObject_GetBuffer(array , & source_buffer, PyBUF_ANY_CONTIGUOUS)
158
- source = < char * > source_buffer.buf
159
+ # setup source buffer
160
+ PyObject_GetBuffer(source , & source_buffer, PyBUF_ANY_CONTIGUOUS)
161
+ source_ptr = < char * > source_buffer.buf
159
162
160
163
# setup destination
161
- nbytes = array.nbytes
162
- itemsize = array.dtype .itemsize
163
- cdata = bytearray( nbytes + BLOSC_MAX_OVERHEAD)
164
- PyObject_GetBuffer(cdata, & dest_buffer, PyBUF_ANY_CONTIGUOUS )
165
- dest = < char * > dest_buffer.buf
164
+ nbytes = source_buffer.len
165
+ itemsize = source_buffer .itemsize
166
+ dest = array.clone(char_array_template, nbytes + BLOSC_MAX_OVERHEAD,
167
+ zero = False )
168
+ dest_ptr = < char * > dest.data.as_voidptr
166
169
167
170
# perform compression
168
171
if _get_use_threads():
@@ -171,28 +174,28 @@ def compress(ndarray array, char* cname, int clevel, int shuffle):
171
174
if compressor_set < 0 :
172
175
raise ValueError (' compressor not supported: %r ' % cname)
173
176
with nogil:
174
- cbytes = blosc_compress(clevel, shuffle, itemsize, nbytes, source,
175
- dest, nbytes + BLOSC_MAX_OVERHEAD)
177
+ cbytes = blosc_compress(clevel, shuffle, itemsize, nbytes,
178
+ source_ptr, dest_ptr,
179
+ nbytes + BLOSC_MAX_OVERHEAD)
176
180
177
181
else :
178
182
with nogil:
179
183
cbytes = blosc_compress_ctx(clevel, shuffle, itemsize, nbytes,
180
- source, dest ,
184
+ source_ptr, dest_ptr ,
181
185
nbytes + BLOSC_MAX_OVERHEAD, cname,
182
186
0 , 1 )
183
187
184
- # release buffers
188
+ # release source buffer
185
189
PyBuffer_Release(& source_buffer)
186
- PyBuffer_Release(& dest_buffer)
187
190
188
191
# check compression was successful
189
192
if cbytes <= 0 :
190
193
raise RuntimeError (' error during blosc compression: %d ' % cbytes)
191
194
192
195
# resize after compression
193
- PyByteArray_Resize( < PyObject * > cdata , cbytes)
196
+ array.resize(dest , cbytes)
194
197
195
- return cdata
198
+ return dest
196
199
197
200
198
201
# set the value of this variable to True or False to override the
0 commit comments