@@ -306,40 +306,44 @@ def compress(source, char* cname, int clevel, int shuffle=SHUFFLE,
306
306
raise ValueError (' invalid shuffle argument; expected -1, 0, 1 or 2, found %r ' %
307
307
shuffle)
308
308
309
- # setup destination
310
- dest = PyBytes_FromStringAndSize(NULL , nbytes + BLOSC_MAX_OVERHEAD)
311
- dest_ptr = PyBytes_AS_STRING(dest)
312
-
313
- # perform compression
314
- if _get_use_threads():
315
- # allow blosc to use threads internally
316
-
317
- # N.B., we are using blosc's global context, and so we need to use a lock
318
- # to ensure no-one else can modify the global context while we're setting it
319
- # up and using it.
320
- with get_mutex():
321
-
322
- # set compressor
323
- compressor_set = blosc_set_compressor(cname)
324
- if compressor_set < 0 :
325
- # shouldn't happen if we checked against list of compressors
326
- # already, but just in case
327
- _err_bad_cname(cname_str)
328
-
329
- # set blocksize
330
- blosc_set_blocksize(blocksize)
331
-
332
- # perform compression
333
- with nogil:
334
- cbytes = blosc_compress(clevel, shuffle, itemsize, nbytes, source_ptr,
335
- dest_ptr, nbytes + BLOSC_MAX_OVERHEAD)
309
+ try :
336
310
337
- else :
338
- with nogil:
339
- cbytes = blosc_compress_ctx(clevel, shuffle, itemsize, nbytes, source_ptr,
340
- dest_ptr, nbytes + BLOSC_MAX_OVERHEAD,
341
- cname, blocksize, 1 )
311
+ # setup destination
312
+ dest = PyBytes_FromStringAndSize(NULL , nbytes + BLOSC_MAX_OVERHEAD)
313
+ dest_ptr = PyBytes_AS_STRING(dest)
314
+
315
+ # perform compression
316
+ if _get_use_threads():
317
+ # allow blosc to use threads internally
318
+
319
+ # N.B., we are using blosc's global context, and so we need to use a lock
320
+ # to ensure no-one else can modify the global context while we're setting it
321
+ # up and using it.
322
+ with get_mutex():
342
323
324
+ # set compressor
325
+ compressor_set = blosc_set_compressor(cname)
326
+ if compressor_set < 0 :
327
+ # shouldn't happen if we checked against list of compressors
328
+ # already, but just in case
329
+ _err_bad_cname(cname_str)
330
+
331
+ # set blocksize
332
+ blosc_set_blocksize(blocksize)
333
+
334
+ # perform compression
335
+ with nogil:
336
+ cbytes = blosc_compress(clevel, shuffle, itemsize, nbytes, source_ptr,
337
+ dest_ptr, nbytes + BLOSC_MAX_OVERHEAD)
338
+
339
+ else :
340
+ with nogil:
341
+ cbytes = blosc_compress_ctx(clevel, shuffle, itemsize, nbytes, source_ptr,
342
+ dest_ptr, nbytes + BLOSC_MAX_OVERHEAD,
343
+ cname, blocksize, 1 )
344
+
345
+ finally :
346
+ pass
343
347
344
348
# check compression was successful
345
349
if cbytes <= 0 :
@@ -403,19 +407,23 @@ def decompress(source, dest=None):
403
407
dest_ptr = < char * > dest_pb.buf
404
408
dest_nbytes = dest_pb.len
405
409
406
- # guard condition
407
- if dest_nbytes < nbytes:
408
- raise ValueError (' destination buffer too small; expected at least %s , '
409
- ' got %s ' % (nbytes, dest_nbytes))
410
+ try :
410
411
411
- # perform decompression
412
- if _get_use_threads():
413
- # allow blosc to use threads internally
414
- with nogil:
415
- ret = blosc_decompress(source_ptr, dest_ptr, nbytes)
416
- else :
417
- with nogil:
418
- ret = blosc_decompress_ctx(source_ptr, dest_ptr, nbytes, 1 )
412
+ # guard condition
413
+ if dest_nbytes < nbytes:
414
+ raise ValueError (' destination buffer too small; expected at least %s , '
415
+ ' got %s ' % (nbytes, dest_nbytes))
416
+
417
+ # perform decompression
418
+ if _get_use_threads():
419
+ # allow blosc to use threads internally
420
+ with nogil:
421
+ ret = blosc_decompress(source_ptr, dest_ptr, nbytes)
422
+ else :
423
+ with nogil:
424
+ ret = blosc_decompress_ctx(source_ptr, dest_ptr, nbytes, 1 )
425
+ finally :
426
+ pass
419
427
420
428
# handle errors
421
429
if ret <= 0 :
@@ -490,10 +498,13 @@ def _decompress_partial(source, start, nitems, dest=None):
490
498
dest_nbytes = dest_pb.len
491
499
492
500
# try decompression
493
- if dest_nbytes < nitems_bytes:
494
- raise ValueError (' destination buffer too small; expected at least %s , '
495
- ' got %s ' % (nitems_bytes, dest_nbytes))
496
- ret = blosc_getitem(source_ptr, start, nitems, dest_ptr)
501
+ try :
502
+ if dest_nbytes < nitems_bytes:
503
+ raise ValueError (' destination buffer too small; expected at least %s , '
504
+ ' got %s ' % (nitems_bytes, dest_nbytes))
505
+ ret = blosc_getitem(source_ptr, start, nitems, dest_ptr)
506
+ finally :
507
+ pass
497
508
498
509
# ret refers to the number of bytes returned from blosc_getitem.
499
510
if ret <= 0 :
0 commit comments