13
13
from random import randint , random
14
14
from unittest import skipUnless
15
15
16
- from test .support import TESTFN , run_unittest , findfile , unlink , requires_zlib , requires_bz2
16
+ from test .support import TESTFN , run_unittest , findfile , unlink , requires_zlib , requires_bz2 , requires_lzma
17
17
18
18
TESTFN2 = TESTFN + "2"
19
19
TESTFNDIR = TESTFN + "d"
@@ -361,6 +361,55 @@ def test_low_compression_bzip2(self):
361
361
self .assertEqual (openobj .read (1 ), b'1' )
362
362
self .assertEqual (openobj .read (1 ), b'2' )
363
363
364
+ @requires_lzma
365
+ def test_lzma (self ):
366
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
367
+ self .zip_test (f , zipfile .ZIP_LZMA )
368
+
369
+ @requires_lzma
370
+ def test_open_lzma (self ):
371
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
372
+ self .zip_open_test (f , zipfile .ZIP_LZMA )
373
+
374
+ @requires_lzma
375
+ def test_random_open_lzma (self ):
376
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
377
+ self .zip_random_open_test (f , zipfile .ZIP_LZMA )
378
+
379
+ @requires_lzma
380
+ def test_readline_read_lzma (self ):
381
+ # Issue #7610: calls to readline() interleaved with calls to read().
382
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
383
+ self .zip_readline_read_test (f , zipfile .ZIP_LZMA )
384
+
385
+ @requires_lzma
386
+ def test_readline_lzma (self ):
387
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
388
+ self .zip_readline_test (f , zipfile .ZIP_LZMA )
389
+
390
+ @requires_lzma
391
+ def test_readlines_lzma (self ):
392
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
393
+ self .zip_readlines_test (f , zipfile .ZIP_LZMA )
394
+
395
+ @requires_lzma
396
+ def test_iterlines_lzma (self ):
397
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
398
+ self .zip_iterlines_test (f , zipfile .ZIP_LZMA )
399
+
400
+ @requires_lzma
401
+ def test_low_compression_lzma (self ):
402
+ """Check for cases where compressed data is larger than original."""
403
+ # Create the ZIP archive
404
+ with zipfile .ZipFile (TESTFN2 , "w" , zipfile .ZIP_LZMA ) as zipfp :
405
+ zipfp .writestr ("strfile" , '12' )
406
+
407
+ # Get an open object for strfile
408
+ with zipfile .ZipFile (TESTFN2 , "r" , zipfile .ZIP_LZMA ) as zipfp :
409
+ with zipfp .open ("strfile" ) as openobj :
410
+ self .assertEqual (openobj .read (1 ), b'1' )
411
+ self .assertEqual (openobj .read (1 ), b'2' )
412
+
364
413
def test_absolute_arcnames (self ):
365
414
with zipfile .ZipFile (TESTFN2 , "w" , zipfile .ZIP_STORED ) as zipfp :
366
415
zipfp .write (TESTFN , "/absolute" )
@@ -508,6 +557,13 @@ def test_writestr_compression_bzip2(self):
508
557
info = zipfp .getinfo ('b.txt' )
509
558
self .assertEqual (info .compress_type , zipfile .ZIP_BZIP2 )
510
559
560
+ @requires_lzma
561
+ def test_writestr_compression_lzma (self ):
562
+ zipfp = zipfile .ZipFile (TESTFN2 , "w" )
563
+ zipfp .writestr ("b.txt" , "hello world" , compress_type = zipfile .ZIP_LZMA )
564
+ info = zipfp .getinfo ('b.txt' )
565
+ self .assertEqual (info .compress_type , zipfile .ZIP_LZMA )
566
+
511
567
def zip_test_writestr_permissions (self , f , compression ):
512
568
# Make sure that writestr creates files with mode 0600,
513
569
# when it is passed a name rather than a ZipInfo instance.
@@ -686,6 +742,11 @@ def test_bzip2(self):
686
742
for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
687
743
self .zip_test (f , zipfile .ZIP_BZIP2 )
688
744
745
+ @requires_lzma
746
+ def test_lzma (self ):
747
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
748
+ self .zip_test (f , zipfile .ZIP_LZMA )
749
+
689
750
def test_absolute_arcnames (self ):
690
751
with zipfile .ZipFile (TESTFN2 , "w" , zipfile .ZIP_STORED ,
691
752
allowZip64 = True ) as zipfp :
@@ -826,6 +887,16 @@ class OtherTests(unittest.TestCase):
826
887
b'\x00 \x80 \x80 \x81 \x00 \x00 \x00 \x00 afilePK'
827
888
b'\x05 \x06 \x00 \x00 \x00 \x00 \x01 \x00 \x01 \x00 3\x00 \x00 \x00 [\x00 '
828
889
b'\x00 \x00 \x00 \x00 ' ),
890
+ zipfile .ZIP_LZMA : (
891
+ b'PK\x03 \x04 \x14 \x03 \x00 \x00 \x0e \x00 nu\x0c =FA'
892
+ b'KE\x1b \x00 \x00 \x00 n\x00 \x00 \x00 \x05 \x00 \x00 \x00 af'
893
+ b'ile\t \x04 \x05 \x00 ]\x00 \x00 \x00 \x04 \x00 4\x19 I'
894
+ b'\xee \x8d \xe9 \x17 \x89 :3`\t q!.8\x00 PK'
895
+ b'\x01 \x02 \x14 \x03 \x14 \x03 \x00 \x00 \x0e \x00 nu\x0c =FA'
896
+ b'KE\x1b \x00 \x00 \x00 n\x00 \x00 \x00 \x05 \x00 \x00 \x00 \x00 \x00 '
897
+ b'\x00 \x00 \x00 \x00 \x80 \x80 \x81 \x00 \x00 \x00 \x00 afil'
898
+ b'ePK\x05 \x06 \x00 \x00 \x00 \x00 \x01 \x00 \x01 \x00 3\x00 \x00 '
899
+ b'\x00 >\x00 \x00 \x00 \x00 \x00 ' ),
829
900
}
830
901
831
902
def test_unsupported_version (self ):
@@ -1104,6 +1175,10 @@ def test_testzip_with_bad_crc_deflated(self):
1104
1175
def test_testzip_with_bad_crc_bzip2 (self ):
1105
1176
self .check_testzip_with_bad_crc (zipfile .ZIP_BZIP2 )
1106
1177
1178
+ @requires_lzma
1179
+ def test_testzip_with_bad_crc_lzma (self ):
1180
+ self .check_testzip_with_bad_crc (zipfile .ZIP_LZMA )
1181
+
1107
1182
def check_read_with_bad_crc (self , compression ):
1108
1183
"""Tests that files with bad CRCs raise a BadZipFile exception when read."""
1109
1184
zipdata = self .zips_with_bad_crc [compression ]
@@ -1136,6 +1211,10 @@ def test_read_with_bad_crc_deflated(self):
1136
1211
def test_read_with_bad_crc_bzip2 (self ):
1137
1212
self .check_read_with_bad_crc (zipfile .ZIP_BZIP2 )
1138
1213
1214
+ @requires_lzma
1215
+ def test_read_with_bad_crc_lzma (self ):
1216
+ self .check_read_with_bad_crc (zipfile .ZIP_LZMA )
1217
+
1139
1218
def check_read_return_size (self , compression ):
1140
1219
# Issue #9837: ZipExtFile.read() shouldn't return more bytes
1141
1220
# than requested.
@@ -1160,6 +1239,10 @@ def test_read_return_size_deflated(self):
1160
1239
def test_read_return_size_bzip2 (self ):
1161
1240
self .check_read_return_size (zipfile .ZIP_BZIP2 )
1162
1241
1242
+ @requires_lzma
1243
+ def test_read_return_size_lzma (self ):
1244
+ self .check_read_return_size (zipfile .ZIP_LZMA )
1245
+
1163
1246
def test_empty_zipfile (self ):
1164
1247
# Check that creating a file in 'w' or 'a' mode and closing without
1165
1248
# adding any files to the archives creates a valid empty ZIP file
@@ -1306,6 +1389,11 @@ def test_bzip2(self):
1306
1389
for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1307
1390
self .zip_test (f , zipfile .ZIP_BZIP2 )
1308
1391
1392
+ @requires_lzma
1393
+ def test_lzma (self ):
1394
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1395
+ self .zip_test (f , zipfile .ZIP_LZMA )
1396
+
1309
1397
def zip_open_test (self , f , compression ):
1310
1398
self .make_test_archive (f , compression )
1311
1399
@@ -1351,6 +1439,11 @@ def test_open_bzip2(self):
1351
1439
for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1352
1440
self .zip_open_test (f , zipfile .ZIP_BZIP2 )
1353
1441
1442
+ @requires_lzma
1443
+ def test_open_lzma (self ):
1444
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1445
+ self .zip_open_test (f , zipfile .ZIP_LZMA )
1446
+
1354
1447
def zip_random_open_test (self , f , compression ):
1355
1448
self .make_test_archive (f , compression )
1356
1449
@@ -1384,6 +1477,11 @@ def test_random_open_bzip2(self):
1384
1477
for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1385
1478
self .zip_random_open_test (f , zipfile .ZIP_BZIP2 )
1386
1479
1480
+ @requires_lzma
1481
+ def test_random_open_lzma (self ):
1482
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1483
+ self .zip_random_open_test (f , zipfile .ZIP_LZMA )
1484
+
1387
1485
1388
1486
@requires_zlib
1389
1487
class TestsWithMultipleOpens (unittest .TestCase ):
@@ -1628,6 +1726,31 @@ def test_iterlines_bzip2(self):
1628
1726
for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1629
1727
self .iterlines_test (f , zipfile .ZIP_BZIP2 )
1630
1728
1729
+ @requires_lzma
1730
+ def test_read_lzma (self ):
1731
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1732
+ self .read_test (f , zipfile .ZIP_LZMA )
1733
+
1734
+ @requires_lzma
1735
+ def test_readline_read_lzma (self ):
1736
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1737
+ self .readline_read_test (f , zipfile .ZIP_LZMA )
1738
+
1739
+ @requires_lzma
1740
+ def test_readline_lzma (self ):
1741
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1742
+ self .readline_test (f , zipfile .ZIP_LZMA )
1743
+
1744
+ @requires_lzma
1745
+ def test_readlines_lzma (self ):
1746
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1747
+ self .readlines_test (f , zipfile .ZIP_LZMA )
1748
+
1749
+ @requires_lzma
1750
+ def test_iterlines_lzma (self ):
1751
+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1752
+ self .iterlines_test (f , zipfile .ZIP_LZMA )
1753
+
1631
1754
def tearDown (self ):
1632
1755
for sep , fn in self .arcfiles .items ():
1633
1756
os .remove (fn )
0 commit comments