64
64
65
65
SUPPORT_MULTITHREADING = False
66
66
67
+ C_INT_MIN = - (2 ** 31 )
68
+ C_INT_MAX = (2 ** 31 ) - 1
69
+
70
+
67
71
def setUpModule ():
68
72
global SUPPORT_MULTITHREADING
69
73
SUPPORT_MULTITHREADING = CompressionParameter .nb_workers .bounds () != (0 , 0 )
@@ -195,14 +199,21 @@ def test_simple_compress_bad_args(self):
195
199
self .assertRaises (TypeError , ZstdCompressor , zstd_dict = b"abcd1234" )
196
200
self .assertRaises (TypeError , ZstdCompressor , zstd_dict = {1 : 2 , 3 : 4 })
197
201
198
- with self .assertRaises (ValueError ):
199
- ZstdCompressor (2 ** 31 )
200
- with self .assertRaises (ValueError ):
201
- ZstdCompressor (options = {2 ** 31 : 100 })
202
+ # valid range for compression level is [-(1<<17), 22]
203
+ msg = r'illegal compression level {}; the valid range is \[-?\d+, -?\d+\]'
204
+ with self .assertRaisesRegex (ValueError , msg .format (C_INT_MAX )):
205
+ ZstdCompressor (C_INT_MAX )
206
+ with self .assertRaisesRegex (ValueError , msg .format (C_INT_MIN )):
207
+ ZstdCompressor (C_INT_MIN )
208
+ msg = r'illegal compression level; the valid range is \[-?\d+, -?\d+\]'
209
+ with self .assertRaisesRegex (ValueError , msg ):
210
+ ZstdCompressor (level = - (2 ** 1000 ))
211
+ with self .assertRaisesRegex (ValueError , msg ):
212
+ ZstdCompressor (level = 2 ** 1000 )
202
213
203
- with self .assertRaises (ZstdError ):
214
+ with self .assertRaises (ValueError ):
204
215
ZstdCompressor (options = {CompressionParameter .window_log : 100 })
205
- with self .assertRaises (ZstdError ):
216
+ with self .assertRaises (ValueError ):
206
217
ZstdCompressor (options = {3333 : 100 })
207
218
208
219
# Method bad arguments
@@ -253,18 +264,32 @@ def test_compress_parameters(self):
253
264
}
254
265
ZstdCompressor (options = d )
255
266
256
- # larger than signed int, ValueError
257
267
d1 = d .copy ()
258
- d1 [CompressionParameter .ldm_bucket_size_log ] = 2 ** 31
259
- self .assertRaises (ValueError , ZstdCompressor , options = d1 )
268
+ # larger than signed int
269
+ d1 [CompressionParameter .ldm_bucket_size_log ] = C_INT_MAX
270
+ with self .assertRaises (ValueError ):
271
+ ZstdCompressor (options = d1 )
272
+ # smaller than signed int
273
+ d1 [CompressionParameter .ldm_bucket_size_log ] = C_INT_MIN
274
+ with self .assertRaises (ValueError ):
275
+ ZstdCompressor (options = d1 )
260
276
261
- # clamp compressionLevel
277
+ # out of bounds compression level
262
278
level_min , level_max = CompressionParameter .compression_level .bounds ()
263
- compress (b'' , level_max + 1 )
264
- compress (b'' , level_min - 1 )
265
-
266
- compress (b'' , options = {CompressionParameter .compression_level :level_max + 1 })
267
- compress (b'' , options = {CompressionParameter .compression_level :level_min - 1 })
279
+ with self .assertRaises (ValueError ):
280
+ compress (b'' , level_max + 1 )
281
+ with self .assertRaises (ValueError ):
282
+ compress (b'' , level_min - 1 )
283
+ with self .assertRaises (ValueError ):
284
+ compress (b'' , 2 ** 1000 )
285
+ with self .assertRaises (ValueError ):
286
+ compress (b'' , - (2 ** 1000 ))
287
+ with self .assertRaises (ValueError ):
288
+ compress (b'' , options = {
289
+ CompressionParameter .compression_level : level_max + 1 })
290
+ with self .assertRaises (ValueError ):
291
+ compress (b'' , options = {
292
+ CompressionParameter .compression_level : level_min - 1 })
268
293
269
294
# zstd lib doesn't support MT compression
270
295
if not SUPPORT_MULTITHREADING :
@@ -277,19 +302,19 @@ def test_compress_parameters(self):
277
302
278
303
# out of bounds error msg
279
304
option = {CompressionParameter .window_log :100 }
280
- with self .assertRaisesRegex (ZstdError ,
281
- (r'Error when setting zstd compression parameter "window_log", '
282
- r'it should \d+ <= value <= \d+, provided value is 100\. '
283
- r'\((?:32|64)-bit build\)' )):
305
+ with self .assertRaisesRegex (
306
+ ValueError ,
307
+ "compression parameter 'window_log' received an illegal value 100; "
308
+ r'the valid range is \[-?\d+, -?\d+\]' ,
309
+ ):
284
310
compress (b'' , options = option )
285
311
286
312
def test_unknown_compression_parameter (self ):
287
313
KEY = 100001234
288
314
option = {CompressionParameter .compression_level : 10 ,
289
315
KEY : 200000000 }
290
- pattern = (r'Invalid zstd compression parameter.*?'
291
- fr'"unknown parameter \(key { KEY } \)"' )
292
- with self .assertRaisesRegex (ZstdError , pattern ):
316
+ pattern = rf"invalid compression parameter 'unknown parameter \(key { KEY } \)'"
317
+ with self .assertRaisesRegex (ValueError , pattern ):
293
318
ZstdCompressor (options = option )
294
319
295
320
@unittest .skipIf (not SUPPORT_MULTITHREADING ,
@@ -384,12 +409,22 @@ def test_simple_decompress_bad_args(self):
384
409
self .assertRaises (TypeError , ZstdDecompressor , options = b'abc' )
385
410
386
411
with self .assertRaises (ValueError ):
387
- ZstdDecompressor (options = {2 ** 31 : 100 })
412
+ ZstdDecompressor (options = {C_INT_MAX : 100 })
413
+ with self .assertRaises (ValueError ):
414
+ ZstdDecompressor (options = {C_INT_MIN : 100 })
415
+ with self .assertRaises (ValueError ):
416
+ ZstdDecompressor (options = {0 : C_INT_MAX })
417
+ with self .assertRaises (OverflowError ):
418
+ ZstdDecompressor (options = {2 ** 1000 : 100 })
419
+ with self .assertRaises (OverflowError ):
420
+ ZstdDecompressor (options = {- (2 ** 1000 ): 100 })
421
+ with self .assertRaises (OverflowError ):
422
+ ZstdDecompressor (options = {0 : - (2 ** 1000 )})
388
423
389
- with self .assertRaises (ZstdError ):
390
- ZstdDecompressor (options = {DecompressionParameter .window_log_max :100 })
391
- with self .assertRaises (ZstdError ):
392
- ZstdDecompressor (options = {3333 : 100 })
424
+ with self .assertRaises (ValueError ):
425
+ ZstdDecompressor (options = {DecompressionParameter .window_log_max : 100 })
426
+ with self .assertRaises (ValueError ):
427
+ ZstdDecompressor (options = {3333 : 100 })
393
428
394
429
empty = compress (b'' )
395
430
lzd = ZstdDecompressor ()
@@ -402,26 +437,52 @@ def test_decompress_parameters(self):
402
437
d = {DecompressionParameter .window_log_max : 15 }
403
438
ZstdDecompressor (options = d )
404
439
405
- # larger than signed int, ValueError
406
440
d1 = d .copy ()
407
- d1 [DecompressionParameter .window_log_max ] = 2 ** 31
408
- self .assertRaises (ValueError , ZstdDecompressor , None , d1 )
441
+ # larger than signed int
442
+ d1 [DecompressionParameter .window_log_max ] = 2 ** 1000
443
+ with self .assertRaises (OverflowError ):
444
+ ZstdDecompressor (None , d1 )
445
+ # smaller than signed int
446
+ d1 [DecompressionParameter .window_log_max ] = - (2 ** 1000 )
447
+ with self .assertRaises (OverflowError ):
448
+ ZstdDecompressor (None , d1 )
449
+
450
+ d1 [DecompressionParameter .window_log_max ] = C_INT_MAX
451
+ with self .assertRaises (ValueError ):
452
+ ZstdDecompressor (None , d1 )
453
+ d1 [DecompressionParameter .window_log_max ] = C_INT_MIN
454
+ with self .assertRaises (ValueError ):
455
+ ZstdDecompressor (None , d1 )
409
456
410
457
# out of bounds error msg
411
458
options = {DecompressionParameter .window_log_max :100 }
412
- with self .assertRaisesRegex (ZstdError ,
413
- (r'Error when setting zstd decompression parameter "window_log_max", '
414
- r'it should \d+ <= value <= \d+, provided value is 100\. '
415
- r'\((?:32|64)-bit build\)' )):
459
+ with self .assertRaisesRegex (
460
+ ValueError ,
461
+ "decompression parameter 'window_log_max' received an illegal value 100; "
462
+ r'the valid range is \[-?\d+, -?\d+\]' ,
463
+ ):
464
+ decompress (b'' , options = options )
465
+
466
+ # out of bounds deecompression parameter
467
+ options [DecompressionParameter .window_log_max ] = C_INT_MAX
468
+ with self .assertRaises (ValueError ):
469
+ decompress (b'' , options = options )
470
+ options [DecompressionParameter .window_log_max ] = C_INT_MIN
471
+ with self .assertRaises (ValueError ):
472
+ decompress (b'' , options = options )
473
+ options [DecompressionParameter .window_log_max ] = 2 ** 1000
474
+ with self .assertRaises (OverflowError ):
475
+ decompress (b'' , options = options )
476
+ options [DecompressionParameter .window_log_max ] = - (2 ** 1000 )
477
+ with self .assertRaises (OverflowError ):
416
478
decompress (b'' , options = options )
417
479
418
480
def test_unknown_decompression_parameter (self ):
419
481
KEY = 100001234
420
482
options = {DecompressionParameter .window_log_max : DecompressionParameter .window_log_max .bounds ()[1 ],
421
483
KEY : 200000000 }
422
- pattern = (r'Invalid zstd decompression parameter.*?'
423
- fr'"unknown parameter \(key { KEY } \)"' )
424
- with self .assertRaisesRegex (ZstdError , pattern ):
484
+ pattern = rf"invalid decompression parameter 'unknown parameter \(key { KEY } \)'"
485
+ with self .assertRaisesRegex (ValueError , pattern ):
425
486
ZstdDecompressor (options = options )
426
487
427
488
def test_decompress_epilogue_flags (self ):
@@ -1424,11 +1485,11 @@ def test_init_bad_mode(self):
1424
1485
ZstdFile (io .BytesIO (COMPRESSED_100_PLUS_32KB ), "rw" )
1425
1486
1426
1487
with self .assertRaisesRegex (TypeError ,
1427
- r"NOT be a CompressionParameter" ):
1488
+ r"not be a CompressionParameter" ):
1428
1489
ZstdFile (io .BytesIO (), 'rb' ,
1429
1490
options = {CompressionParameter .compression_level :5 })
1430
1491
with self .assertRaisesRegex (TypeError ,
1431
- r"NOT be a DecompressionParameter" ):
1492
+ r"not be a DecompressionParameter" ):
1432
1493
ZstdFile (io .BytesIO (), 'wb' ,
1433
1494
options = {DecompressionParameter .window_log_max :21 })
1434
1495
@@ -1439,19 +1500,19 @@ def test_init_bad_check(self):
1439
1500
with self .assertRaises (TypeError ):
1440
1501
ZstdFile (io .BytesIO (), "w" , level = 'asd' )
1441
1502
# CHECK_UNKNOWN and anything above CHECK_ID_MAX should be invalid.
1442
- with self .assertRaises (ZstdError ):
1503
+ with self .assertRaises (ValueError ):
1443
1504
ZstdFile (io .BytesIO (), "w" , options = {999 :9999 })
1444
- with self .assertRaises (ZstdError ):
1505
+ with self .assertRaises (ValueError ):
1445
1506
ZstdFile (io .BytesIO (), "w" , options = {CompressionParameter .window_log :99 })
1446
1507
1447
1508
with self .assertRaises (TypeError ):
1448
1509
ZstdFile (io .BytesIO (COMPRESSED_100_PLUS_32KB ), "r" , options = 33 )
1449
1510
1450
- with self .assertRaises (ValueError ):
1511
+ with self .assertRaises (OverflowError ):
1451
1512
ZstdFile (io .BytesIO (COMPRESSED_100_PLUS_32KB ),
1452
1513
options = {DecompressionParameter .window_log_max :2 ** 31 })
1453
1514
1454
- with self .assertRaises (ZstdError ):
1515
+ with self .assertRaises (ValueError ):
1455
1516
ZstdFile (io .BytesIO (COMPRESSED_100_PLUS_32KB ),
1456
1517
options = {444 :333 })
1457
1518
@@ -1467,7 +1528,7 @@ def test_init_close_fp(self):
1467
1528
tmp_f .write (DAT_130K_C )
1468
1529
filename = tmp_f .name
1469
1530
1470
- with self .assertRaises (ValueError ):
1531
+ with self .assertRaises (TypeError ):
1471
1532
ZstdFile (filename , options = {'a' :'b' })
1472
1533
1473
1534
# for PyPy
0 commit comments