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