@@ -642,8 +642,8 @@ def cbuffer_sizes(src):
642
642
cpdef compress(src, int32_t typesize = 8 , int clevel = 9 , filter = blosc2.Filter.SHUFFLE, codec = blosc2.Codec.BLOSCLZ):
643
643
set_compressor(codec)
644
644
cdef int32_t len_src = < int32_t> len (src)
645
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
646
- PyObject_GetBuffer(src, buf, PyBUF_SIMPLE)
645
+ cdef Py_buffer buf
646
+ PyObject_GetBuffer(src, & buf, PyBUF_SIMPLE)
647
647
dest = bytes(buf.len + BLOSC2_MAX_OVERHEAD)
648
648
cdef int32_t len_dest = < int32_t> len (dest)
649
649
cdef int size
@@ -654,8 +654,7 @@ cpdef compress(src, int32_t typesize=8, int clevel=9, filter=blosc2.Filter.SHUFF
654
654
size = blosc2_compress(clevel, filter_, < int32_t> typesize, buf.buf, < int32_t> buf.len, _dest, len_dest)
655
655
else :
656
656
size = blosc2_compress(clevel, filter_, < int32_t> typesize, buf.buf, < int32_t> buf.len, < void * > < char * > dest, len_dest)
657
- PyBuffer_Release(buf)
658
- free(buf)
657
+ PyBuffer_Release(& buf)
659
658
if size > 0 :
660
659
return dest[:size]
661
660
else :
@@ -672,14 +671,13 @@ def decompress(src, dst=None, as_bytearray=False):
672
671
typed_view_src = mem_view_src.cast(' B' )
673
672
_check_comp_length(' src' , len (typed_view_src))
674
673
blosc2_cbuffer_sizes(< void * > & typed_view_src[0 ], & nbytes, & cbytes, & blocksize)
675
- cdef Py_buffer * buf
674
+ cdef Py_buffer buf
676
675
if dst is not None :
677
- buf = < Py_buffer * > malloc(sizeof(Py_buffer))
678
- PyObject_GetBuffer(dst, buf, PyBUF_SIMPLE)
676
+ PyObject_GetBuffer(dst, & buf, PyBUF_SIMPLE)
679
677
if buf.len == 0 :
680
678
raise ValueError (" The dst length must be greater than 0" )
681
679
size = blosc1_decompress(< void * > & typed_view_src[0 ], buf.buf, buf.len)
682
- PyBuffer_Release(buf)
680
+ PyBuffer_Release(& buf)
683
681
else :
684
682
dst = PyBytes_FromStringAndSize(NULL , nbytes)
685
683
if dst is None :
@@ -850,8 +848,8 @@ def compress2(src, **kwargs):
850
848
create_cparams_from_kwargs(& cparams, kwargs)
851
849
852
850
cdef blosc2_context * cctx
853
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
854
- PyObject_GetBuffer(src, buf, PyBUF_SIMPLE)
851
+ cdef Py_buffer buf
852
+ PyObject_GetBuffer(src, & buf, PyBUF_SIMPLE)
855
853
cdef int size
856
854
cdef int32_t len_dest = < int32_t> (buf.len + BLOSC2_MAX_OVERHEAD)
857
855
dest = bytes(len_dest)
@@ -865,8 +863,7 @@ def compress2(src, **kwargs):
865
863
else :
866
864
size = blosc2_compress_ctx(cctx, buf.buf, < int32_t> buf.len, _dest, len_dest)
867
865
blosc2_free_ctx(cctx)
868
- PyBuffer_Release(buf)
869
- free(buf)
866
+ PyBuffer_Release(& buf)
870
867
if size < 0 :
871
868
raise RuntimeError (" Could not compress the data" )
872
869
elif size == 0 :
@@ -900,10 +897,9 @@ def decompress2(src, dst=None, **kwargs):
900
897
cdef int32_t cbytes
901
898
cdef int32_t blocksize
902
899
blosc2_cbuffer_sizes(< void * > & typed_view_src[0 ], & nbytes, & cbytes, & blocksize)
903
- cdef Py_buffer * buf
900
+ cdef Py_buffer buf
904
901
if dst is not None :
905
- buf = < Py_buffer * > malloc(sizeof(Py_buffer))
906
- PyObject_GetBuffer(dst, buf, PyBUF_SIMPLE)
902
+ PyObject_GetBuffer(dst, & buf, PyBUF_SIMPLE)
907
903
if buf.len == 0 :
908
904
blosc2_free_ctx(dctx)
909
905
raise ValueError (" The dst length must be greater than 0" )
@@ -914,7 +910,7 @@ def decompress2(src, dst=None, **kwargs):
914
910
else :
915
911
size = blosc2_decompress_ctx(dctx, view, cbytes, buf.buf, nbytes)
916
912
blosc2_free_ctx(dctx)
917
- PyBuffer_Release(buf)
913
+ PyBuffer_Release(& buf)
918
914
else :
919
915
dst = PyBytes_FromStringAndSize(NULL , nbytes)
920
916
if dst is None :
@@ -980,7 +976,6 @@ cdef get_chunk_repeatval(blosc2_cparams cparams, const int32_t nbytes,
980
976
if blosc2_chunk_repeatval(cparams, nbytes, dest, destsize, repeatval.buf) < 0 :
981
977
free(dest)
982
978
PyBuffer_Release(repeatval)
983
- free(repeatval)
984
979
raise RuntimeError (" Problems when creating the repeated values chunk" )
985
980
986
981
@@ -1076,11 +1071,10 @@ cdef class SChunk:
1076
1071
self .schunk.chunksize = chunksize
1077
1072
cdef const uint8_t[:] typed_view
1078
1073
cdef int64_t index
1079
- cdef Py_buffer * buf
1074
+ cdef Py_buffer buf
1080
1075
cdef uint8_t * buf_ptr
1081
1076
if data is not None and len (data) > 0 :
1082
- buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1083
- PyObject_GetBuffer(data, buf, PyBUF_SIMPLE)
1077
+ PyObject_GetBuffer(data, & buf, PyBUF_SIMPLE)
1084
1078
buf_ptr = < uint8_t * > buf.buf
1085
1079
len_data = buf.len
1086
1080
nchunks = len_data // chunksize + 1 if len_data % chunksize != 0 else len_data // chunksize
@@ -1091,9 +1085,9 @@ cdef class SChunk:
1091
1085
index = i * chunksize
1092
1086
nchunks_ = blosc2_schunk_append_buffer(self .schunk, buf_ptr + index, len_chunk)
1093
1087
if nchunks_ != (i + 1 ):
1094
- PyBuffer_Release(buf)
1088
+ PyBuffer_Release(& buf)
1095
1089
raise RuntimeError (" An error occurred while appending the chunks" )
1096
- PyBuffer_Release(buf)
1090
+ PyBuffer_Release(& buf)
1097
1091
1098
1092
@property
1099
1093
def c_schunk (self ):
@@ -1223,11 +1217,10 @@ cdef class SChunk:
1223
1217
raise RuntimeError (" Could not create decompression context" )
1224
1218
1225
1219
def append_data (self , data ):
1226
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1227
- PyObject_GetBuffer(data, buf, PyBUF_SIMPLE)
1220
+ cdef Py_buffer buf
1221
+ PyObject_GetBuffer(data, & buf, PyBUF_SIMPLE)
1228
1222
rc = blosc2_schunk_append_buffer(self .schunk, buf.buf, < int32_t> buf.len)
1229
- PyBuffer_Release(buf)
1230
- free(buf)
1223
+ PyBuffer_Release(& buf)
1231
1224
if rc < 0 :
1232
1225
raise RuntimeError (" Could not append the buffer" )
1233
1226
return rc
@@ -1252,31 +1245,29 @@ cdef class SChunk:
1252
1245
else :
1253
1246
raise ValueError (" value size in bytes must match with typesize" )
1254
1247
array = np.array([value], dtype = dtype)
1255
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1256
- PyObject_GetBuffer(array, buf, PyBUF_SIMPLE)
1248
+ cdef Py_buffer buf
1249
+ PyObject_GetBuffer(array, & buf, PyBUF_SIMPLE)
1257
1250
# Create chunk with repeated values
1258
1251
nchunks = nitems // self .chunkshape
1259
1252
cdef blosc2_schunk * c_schunk = < blosc2_schunk * > self .c_schunk
1260
1253
cdef blosc2_cparams * cparams = self .schunk.storage.cparams
1261
1254
chunksize = BLOSC_EXTENDED_HEADER_LENGTH + self .typesize
1262
1255
cdef void * chunk = malloc(chunksize)
1263
- get_chunk_repeatval(dereference(cparams), self .chunksize, chunk, chunksize, buf)
1256
+ get_chunk_repeatval(dereference(cparams), self .chunksize, chunk, chunksize, & buf)
1264
1257
1265
1258
for i in range (nchunks):
1266
1259
if blosc2_schunk_append_chunk(self .schunk, < uint8_t * > chunk, True ) < 0 :
1267
1260
free(chunk)
1268
- PyBuffer_Release(buf)
1269
- free(buf)
1261
+ PyBuffer_Release(& buf)
1270
1262
raise RuntimeError (" Error while appending the chunk" )
1271
1263
# Create and append last chunk if it is smaller than chunkshape
1272
1264
remainder = nitems % self .chunkshape
1273
1265
rc = 0
1274
1266
if remainder != 0 :
1275
- get_chunk_repeatval(dereference(cparams), remainder * self .typesize, chunk, chunksize, buf)
1267
+ get_chunk_repeatval(dereference(cparams), remainder * self .typesize, chunk, chunksize, & buf)
1276
1268
rc = blosc2_schunk_append_chunk(self .schunk, < uint8_t * > chunk, True )
1277
1269
free(chunk)
1278
- PyBuffer_Release(buf)
1279
- free(buf)
1270
+ PyBuffer_Release(& buf)
1280
1271
if rc < 0 :
1281
1272
raise RuntimeError (" Error while appending the chunk" )
1282
1273
@@ -1297,14 +1288,13 @@ cdef class SChunk:
1297
1288
if needs_free:
1298
1289
free(chunk)
1299
1290
1300
- cdef Py_buffer * buf
1291
+ cdef Py_buffer buf
1301
1292
if dst is not None :
1302
- buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1303
- PyObject_GetBuffer(dst, buf, PyBUF_SIMPLE)
1293
+ PyObject_GetBuffer(dst, & buf, PyBUF_SIMPLE)
1304
1294
if buf.len == 0 :
1305
1295
raise ValueError (" The dst length must be greater than 0" )
1306
1296
size = blosc2_schunk_decompress_chunk(self .schunk, nchunk, buf.buf, < int32_t> buf.len)
1307
- PyBuffer_Release(buf)
1297
+ PyBuffer_Release(& buf)
1308
1298
else :
1309
1299
dst = PyBytes_FromStringAndSize(NULL , nbytes)
1310
1300
if dst is None :
@@ -1363,8 +1353,8 @@ cdef class SChunk:
1363
1353
1364
1354
def insert_data (self , nchunk , data , copy ):
1365
1355
cdef blosc2_context * cctx
1366
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1367
- PyObject_GetBuffer(data, buf, PyBUF_SIMPLE)
1356
+ cdef Py_buffer buf
1357
+ PyObject_GetBuffer(data, & buf, PyBUF_SIMPLE)
1368
1358
cdef int size
1369
1359
cdef int32_t len_chunk = < int32_t> (buf.len + BLOSC2_MAX_OVERHEAD)
1370
1360
cdef uint8_t* chunk = < uint8_t* > malloc(len_chunk)
@@ -1374,8 +1364,7 @@ cdef class SChunk:
1374
1364
size = blosc2_compress_ctx(self .schunk.cctx, buf.buf, < int32_t> buf.len, chunk, len_chunk)
1375
1365
else :
1376
1366
size = blosc2_compress_ctx(self .schunk.cctx, buf.buf, < int32_t> buf.len, chunk, len_chunk)
1377
- PyBuffer_Release(buf)
1378
- free(buf)
1367
+ PyBuffer_Release(& buf)
1379
1368
if size < 0 :
1380
1369
raise RuntimeError (" Could not compress the data" )
1381
1370
elif size == 0 :
@@ -1402,8 +1391,8 @@ cdef class SChunk:
1402
1391
return rc
1403
1392
1404
1393
def update_data (self , nchunk , data , copy ):
1405
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1406
- PyObject_GetBuffer(data, buf, PyBUF_SIMPLE)
1394
+ cdef Py_buffer buf
1395
+ PyObject_GetBuffer(data, & buf, PyBUF_SIMPLE)
1407
1396
cdef int size
1408
1397
cdef int32_t len_chunk = < int32_t> (buf.len + BLOSC2_MAX_OVERHEAD)
1409
1398
cdef uint8_t* chunk = < uint8_t* > malloc(len_chunk)
@@ -1413,8 +1402,7 @@ cdef class SChunk:
1413
1402
else :
1414
1403
size = blosc2_compress_ctx(self .schunk.cctx, buf.buf, < int32_t> buf.len, chunk, len_chunk)
1415
1404
1416
- PyBuffer_Release(buf)
1417
- free(buf)
1405
+ PyBuffer_Release(& buf)
1418
1406
if size < 0 :
1419
1407
raise RuntimeError (" Could not compress the data" )
1420
1408
elif size == 0 :
@@ -1437,14 +1425,13 @@ cdef class SChunk:
1437
1425
return b' '
1438
1426
1439
1427
cdef Py_ssize_t nbytes = (stop - start) * self .schunk.typesize
1440
- cdef Py_buffer * buf
1428
+ cdef Py_buffer buf
1441
1429
if out is not None :
1442
- buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1443
- PyObject_GetBuffer(out, buf, PyBUF_SIMPLE)
1430
+ PyObject_GetBuffer(out, & buf, PyBUF_SIMPLE)
1444
1431
if buf.len < nbytes:
1445
1432
raise ValueError (" Not enough space for writing the slice in out" )
1446
1433
rc = blosc2_schunk_get_slice_buffer(self .schunk, start, stop, buf.buf)
1447
- PyBuffer_Release(buf)
1434
+ PyBuffer_Release(& buf)
1448
1435
else :
1449
1436
out = PyBytes_FromStringAndSize(NULL , nbytes)
1450
1437
if out is None :
@@ -1463,8 +1450,8 @@ cdef class SChunk:
1463
1450
1464
1451
cdef int64_t nbytes = (stop - start) * self .schunk.typesize
1465
1452
1466
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1467
- PyObject_GetBuffer(value, buf, PyBUF_SIMPLE)
1453
+ cdef Py_buffer buf
1454
+ PyObject_GetBuffer(value, & buf, PyBUF_SIMPLE)
1468
1455
cdef uint8_t * buf_ptr = < uint8_t * > buf.buf
1469
1456
cdef int64_t buf_pos = 0
1470
1457
cdef uint8_t * data
@@ -1516,7 +1503,7 @@ cdef class SChunk:
1516
1503
buf_pos += chunksize
1517
1504
else :
1518
1505
rc = blosc2_schunk_set_slice_buffer(self .schunk, start, stop, buf.buf)
1519
- PyBuffer_Release(buf)
1506
+ PyBuffer_Release(& buf)
1520
1507
if rc < 0 :
1521
1508
raise RuntimeError (" Error while setting the slice" )
1522
1509
@@ -1797,18 +1784,17 @@ cdef int aux_udf(udf_udata *udata, int64_t nchunk, int32_t nblock,
1797
1784
cdef int64_t start[B2ND_MAX_DIM]
1798
1785
cdef int64_t slice_shape[B2ND_MAX_DIM]
1799
1786
cdef int64_t blockshape_int64[B2ND_MAX_DIM]
1800
- cdef Py_buffer * buf
1787
+ cdef Py_buffer buf
1801
1788
if padding:
1802
1789
for i in range (udata.array.ndim):
1803
1790
start[i] = 0
1804
1791
slice_shape[i] = blockshape[i]
1805
1792
blockshape_int64[i] = udata.array.blockshape[i]
1806
- buf = < Py_buffer * > malloc(sizeof(Py_buffer))
1807
- PyObject_GetBuffer(output, buf, PyBUF_SIMPLE)
1793
+ PyObject_GetBuffer(output, & buf, PyBUF_SIMPLE)
1808
1794
rc = b2nd_copy_buffer2(udata.array.ndim, typesize,
1809
1795
buf.buf, slice_shape, start, slice_shape,
1810
1796
params_output, blockshape_int64, start)
1811
- PyBuffer_Release(buf)
1797
+ PyBuffer_Release(& buf)
1812
1798
_check_rc(rc, " Could not copy the result into the buffer" )
1813
1799
1814
1800
return 0
@@ -2088,13 +2074,13 @@ cdef schunk_is_ndarray(blosc2_schunk* schunk):
2088
2074
2089
2075
2090
2076
def schunk_from_cframe (cframe , copy = False ):
2091
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
2092
- PyObject_GetBuffer(cframe, buf, PyBUF_SIMPLE)
2077
+ cdef Py_buffer buf
2078
+ PyObject_GetBuffer(cframe, & buf, PyBUF_SIMPLE)
2093
2079
cdef blosc2_schunk * schunk_ = blosc2_schunk_from_buffer(< uint8_t * > buf.buf, buf.len, copy)
2094
2080
if schunk_ == NULL :
2095
2081
raise RuntimeError (" Could not get the schunk from the cframe" )
2096
2082
schunk = blosc2.SChunk(_schunk = PyCapsule_New(schunk_, < char * > " blosc2_schunk*" , NULL ))
2097
- PyBuffer_Release(buf)
2083
+ PyBuffer_Release(& buf)
2098
2084
if not copy:
2099
2085
schunk._avoid_cframe_free(True )
2100
2086
return schunk
@@ -2441,8 +2427,8 @@ cdef class NDArray:
2441
2427
def set_slice (self , key , ndarray ):
2442
2428
ndim = self .ndim
2443
2429
start, stop = key
2444
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
2445
- PyObject_GetBuffer(ndarray, buf, PyBUF_SIMPLE)
2430
+ cdef Py_buffer buf
2431
+ PyObject_GetBuffer(ndarray, & buf, PyBUF_SIMPLE)
2446
2432
2447
2433
cdef int64_t[B2ND_MAX_DIM] buffershape_, start_, stop_
2448
2434
for i in range (ndim):
@@ -2452,7 +2438,7 @@ cdef class NDArray:
2452
2438
2453
2439
_check_rc(b2nd_set_slice_cbuffer(buf.buf, buffershape_, buf.len, start_, stop_, self .array),
2454
2440
" Error while setting the slice" )
2455
- PyBuffer_Release(buf)
2441
+ PyBuffer_Release(& buf)
2456
2442
2457
2443
return self
2458
2444
@@ -2715,12 +2701,12 @@ def full(shape, chunks, blocks, fill_value, dtype, **kwargs):
2715
2701
2716
2702
dtype = np.dtype(dtype)
2717
2703
nparr = np.array([fill_value], dtype = dtype)
2718
- cdef Py_buffer * val = < Py_buffer * > malloc(sizeof(Py_buffer))
2719
- PyObject_GetBuffer(nparr, val, PyBUF_SIMPLE)
2704
+ cdef Py_buffer val
2705
+ PyObject_GetBuffer(nparr, & val, PyBUF_SIMPLE)
2720
2706
2721
2707
cdef b2nd_array_t * array
2722
2708
_check_rc(b2nd_full(ctx, & array, val.buf), " Could not create full array" )
2723
- PyBuffer_Release(val)
2709
+ PyBuffer_Release(& val)
2724
2710
2725
2711
ndarray = blosc2.NDArray(_schunk = PyCapsule_New(array.sc, < char * > " blosc2_schunk*" , NULL ),
2726
2712
_array = PyCapsule_New(array, < char * > " b2nd_array_t*" , NULL ))
@@ -2748,8 +2734,8 @@ def from_buffer(buf, shape, chunks, blocks, dtype, **kwargs):
2748
2734
2749
2735
def asarray (ndarray , chunks , blocks , **kwargs ):
2750
2736
interface = ndarray.__array_interface__
2751
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
2752
- PyObject_GetBuffer(ndarray, buf, PyBUF_SIMPLE)
2737
+ cdef Py_buffer buf
2738
+ PyObject_GetBuffer(ndarray, & buf, PyBUF_SIMPLE)
2753
2739
2754
2740
shape = interface[" shape" ]
2755
2741
dtype = interface[" typestr" ]
@@ -2763,7 +2749,7 @@ def asarray(ndarray, chunks, blocks, **kwargs):
2763
2749
cdef b2nd_array_t * array
2764
2750
_check_rc(b2nd_from_cbuffer(ctx, & array, < void * > < char * > buf.buf, buf.len),
2765
2751
" Error while creating the NDArray" )
2766
- PyBuffer_Release(buf)
2752
+ PyBuffer_Release(& buf)
2767
2753
ndarray = blosc2.NDArray(_schunk = PyCapsule_New(array.sc, < char * > " blosc2_schunk*" , NULL ),
2768
2754
_array = PyCapsule_New(array, < char * > " b2nd_array_t*" , NULL ))
2769
2755
_check_rc(b2nd_free_ctx(ctx), " Error while freeing the context" )
@@ -2773,8 +2759,8 @@ def asarray(ndarray, chunks, blocks, **kwargs):
2773
2759
2774
2760
2775
2761
def ndarray_from_cframe (cframe , copy = False ):
2776
- cdef Py_buffer * buf = < Py_buffer * > malloc(sizeof(Py_buffer))
2777
- PyObject_GetBuffer(cframe, buf, PyBUF_SIMPLE)
2762
+ cdef Py_buffer buf
2763
+ PyObject_GetBuffer(cframe, & buf, PyBUF_SIMPLE)
2778
2764
cdef b2nd_array_t * array
2779
2765
cdef int rc
2780
2766
rc = b2nd_from_cframe(< uint8_t * > buf.buf, buf.len, copy, & array)
@@ -2783,7 +2769,7 @@ def ndarray_from_cframe(cframe, copy=False):
2783
2769
ndarray = blosc2.NDArray(_schunk = PyCapsule_New(array.sc, < char * > " blosc2_schunk*" , NULL ),
2784
2770
_array = PyCapsule_New(array, < char * > " b2nd_array_t*" , NULL ))
2785
2771
2786
- PyBuffer_Release(buf)
2772
+ PyBuffer_Release(& buf)
2787
2773
if not copy:
2788
2774
ndarray._schunk._avoid_cframe_free(True )
2789
2775
return ndarray
0 commit comments