Skip to content

Commit e7125e7

Browse files
committed
gh-51067: Replace ComparableZipInfo class with a function
The previous spelling overrode __new__ to return a dict, which works but reads as a class that isn't one. Rename to comparable_zinfo() per PEP 8.
1 parent b6937c5 commit e7125e7

1 file changed

Lines changed: 59 additions & 59 deletions

File tree

Lib/test/test_zipfile/test_core.py

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,19 +1436,19 @@ class ZstdWriterTests(AbstractWriterTests, unittest.TestCase):
14361436
compression = zipfile.ZIP_ZSTANDARD
14371437

14381438

1439-
class ComparableZipInfo:
1440-
keys = [i for i in zipfile.ZipInfo.__slots__ if not i.startswith('_')]
1439+
_ZINFO_PUBLIC_KEYS = [k for k in zipfile.ZipInfo.__slots__ if not k.startswith('_')]
14411440

1442-
def __new__(cls, zinfo):
1443-
attrs = {i: getattr(zinfo, i) for i in cls.keys}
1441+
def comparable_zinfo(zinfo):
1442+
"""Return a dict of public ZipInfo attributes for assertEqual comparison."""
1443+
attrs = {k: getattr(zinfo, k) for k in _ZINFO_PUBLIC_KEYS}
14441444

1445-
# Since patch gh-84353, the _MASK_UTF_FILENAME (0x800) bit may be
1446-
# changed when writing to the end record depending on whether filename
1447-
# can be encoded with ascii or cp437. Skip checking this bit by
1448-
# pretending it's always set.
1449-
attrs['flag_bits'] |= 0x800
1445+
# Since patch gh-84353, the _MASK_UTF_FILENAME (0x800) bit may be
1446+
# changed when writing to the end record depending on whether filename
1447+
# can be encoded with ascii or cp437. Skip checking this bit by
1448+
# pretending it's always set.
1449+
attrs['flag_bits'] |= 0x800
14501450

1451-
return attrs
1451+
return attrs
14521452

14531453
_struct_pack = struct.pack
14541454

@@ -1499,8 +1499,8 @@ def test_remove_by_name(self):
14991499

15001500
# check infolist
15011501
self.assertEqual(
1502-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1503-
[ComparableZipInfo(zi) for j, zi in enumerate(zinfos) if j != i],
1502+
[comparable_zinfo(zi) for zi in zh.infolist()],
1503+
[comparable_zinfo(zi) for j, zi in enumerate(zinfos) if j != i],
15041504
)
15051505

15061506
# check NameToInfo cache
@@ -1520,8 +1520,8 @@ def test_remove_by_zinfo(self):
15201520

15211521
# check infolist
15221522
self.assertEqual(
1523-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1524-
[ComparableZipInfo(zi) for j, zi in enumerate(zinfos) if j != i],
1523+
[comparable_zinfo(zi) for zi in zh.infolist()],
1524+
[comparable_zinfo(zi) for j, zi in enumerate(zinfos) if j != i],
15251525
)
15261526

15271527
# check NameToInfo cache
@@ -1561,14 +1561,14 @@ def test_remove_by_name_duplicated(self):
15611561

15621562
# check infolist
15631563
self.assertEqual(
1564-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1565-
[ComparableZipInfo(zi) for zi in [zinfos[0], zinfos[2]]],
1564+
[comparable_zinfo(zi) for zi in zh.infolist()],
1565+
[comparable_zinfo(zi) for zi in [zinfos[0], zinfos[2]]],
15661566
)
15671567

15681568
# check NameToInfo cache
15691569
self.assertEqual(
1570-
ComparableZipInfo(zh.getinfo('file.txt')),
1571-
ComparableZipInfo(zinfos[0]),
1570+
comparable_zinfo(zh.getinfo('file.txt')),
1571+
comparable_zinfo(zinfos[0]),
15721572
)
15731573

15741574
# make sure the zip file is still valid
@@ -1586,8 +1586,8 @@ def test_remove_by_name_duplicated(self):
15861586

15871587
# check infolist
15881588
self.assertEqual(
1589-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1590-
[ComparableZipInfo(zi) for zi in [zinfos[2]]],
1589+
[comparable_zinfo(zi) for zi in zh.infolist()],
1590+
[comparable_zinfo(zi) for zi in [zinfos[2]]],
15911591
)
15921592

15931593
# check NameToInfo cache
@@ -1615,14 +1615,14 @@ def test_remove_by_zinfo_duplicated(self):
16151615

16161616
# check infolist
16171617
self.assertEqual(
1618-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1619-
[ComparableZipInfo(zi) for zi in [zinfos[1], zinfos[2]]],
1618+
[comparable_zinfo(zi) for zi in zh.infolist()],
1619+
[comparable_zinfo(zi) for zi in [zinfos[1], zinfos[2]]],
16201620
)
16211621

16221622
# check NameToInfo cache
16231623
self.assertEqual(
1624-
ComparableZipInfo(zh.getinfo('file.txt')),
1625-
ComparableZipInfo(zinfos[1]),
1624+
comparable_zinfo(zh.getinfo('file.txt')),
1625+
comparable_zinfo(zinfos[1]),
16261626
)
16271627

16281628
# make sure the zip file is still valid
@@ -1639,14 +1639,14 @@ def test_remove_by_zinfo_duplicated(self):
16391639

16401640
# check infolist
16411641
self.assertEqual(
1642-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1643-
[ComparableZipInfo(zi) for zi in [zinfos[0], zinfos[2]]],
1642+
[comparable_zinfo(zi) for zi in zh.infolist()],
1643+
[comparable_zinfo(zi) for zi in [zinfos[0], zinfos[2]]],
16441644
)
16451645

16461646
# check NameToInfo cache
16471647
self.assertEqual(
1648-
ComparableZipInfo(zh.getinfo('file.txt')),
1649-
ComparableZipInfo(zinfos[0]),
1648+
comparable_zinfo(zh.getinfo('file.txt')),
1649+
comparable_zinfo(zinfos[0]),
16501650
)
16511651

16521652
# make sure the zip file is still valid
@@ -1665,8 +1665,8 @@ def test_remove_by_zinfo_duplicated(self):
16651665

16661666
# check infolist
16671667
self.assertEqual(
1668-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1669-
[ComparableZipInfo(zi) for zi in [zinfos[2]]],
1668+
[comparable_zinfo(zi) for zi in zh.infolist()],
1669+
[comparable_zinfo(zi) for zi in [zinfos[2]]],
16701670
)
16711671

16721672
# check NameToInfo cache
@@ -1686,8 +1686,8 @@ def test_remove_zip64(self):
16861686

16871687
# check infolist
16881688
self.assertEqual(
1689-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1690-
[ComparableZipInfo(zi) for j, zi in enumerate(zinfos) if j != i],
1689+
[comparable_zinfo(zi) for zi in zh.infolist()],
1690+
[comparable_zinfo(zi) for j, zi in enumerate(zinfos) if j != i],
16911691
)
16921692

16931693
# check NameToInfo cache
@@ -1728,8 +1728,8 @@ def test_remove_mode_w(self):
17281728

17291729
# check infolist
17301730
self.assertEqual(
1731-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1732-
[ComparableZipInfo(zi) for zi in [zinfos[1], zinfos[2]]],
1731+
[comparable_zinfo(zi) for zi in zh.infolist()],
1732+
[comparable_zinfo(zi) for zi in [zinfos[1], zinfos[2]]],
17331733
)
17341734

17351735
# check NameToInfo cache
@@ -1750,8 +1750,8 @@ def test_remove_mode_x(self):
17501750

17511751
# check infolist
17521752
self.assertEqual(
1753-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1754-
[ComparableZipInfo(zi) for zi in [zinfos[1], zinfos[2]]],
1753+
[comparable_zinfo(zi) for zi in zh.infolist()],
1754+
[comparable_zinfo(zi) for zi in [zinfos[1], zinfos[2]]],
17551755
)
17561756

17571757
# check NameToInfo cache
@@ -1809,8 +1809,8 @@ def test_repack_basic(self):
18091809

18101810
# check infolist
18111811
self.assertEqual(
1812-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1813-
[ComparableZipInfo(zi) for zi in expected_zinfos],
1812+
[comparable_zinfo(zi) for zi in zh.infolist()],
1813+
[comparable_zinfo(zi) for zi in expected_zinfos],
18141814
)
18151815

18161816
# check file size
@@ -1861,8 +1861,8 @@ def test_repack_bytes_before_first_file(self):
18611861

18621862
# check infolist
18631863
self.assertEqual(
1864-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1865-
[ComparableZipInfo(zi) for zi in expected_zinfos],
1864+
[comparable_zinfo(zi) for zi in zh.infolist()],
1865+
[comparable_zinfo(zi) for zi in expected_zinfos],
18661866
)
18671867

18681868
# check file size
@@ -1895,8 +1895,8 @@ def test_repack_magic_before_first_file(self):
18951895

18961896
# check infolist
18971897
self.assertEqual(
1898-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1899-
[ComparableZipInfo(zi) for zi in expected_zinfos],
1898+
[comparable_zinfo(zi) for zi in zh.infolist()],
1899+
[comparable_zinfo(zi) for zi in expected_zinfos],
19001900
)
19011901

19021902
# check file size
@@ -1941,8 +1941,8 @@ def test_repack_file_entry_before_first_file(self):
19411941

19421942
# check infolist
19431943
self.assertEqual(
1944-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1945-
[ComparableZipInfo(zi) for zi in expected_zinfos],
1944+
[comparable_zinfo(zi) for zi in zh.infolist()],
1945+
[comparable_zinfo(zi) for zi in expected_zinfos],
19461946
)
19471947

19481948
# check file size
@@ -1985,8 +1985,8 @@ def test_repack_bytes_before_removed_files(self):
19851985

19861986
# check infolist
19871987
self.assertEqual(
1988-
[ComparableZipInfo(zi) for zi in zh.infolist()],
1989-
[ComparableZipInfo(zi) for zi in expected_zinfos],
1988+
[comparable_zinfo(zi) for zi in zh.infolist()],
1989+
[comparable_zinfo(zi) for zi in expected_zinfos],
19901990
)
19911991

19921992
# check file size
@@ -2028,8 +2028,8 @@ def test_repack_bytes_after_removed_files(self):
20282028

20292029
# check infolist
20302030
self.assertEqual(
2031-
[ComparableZipInfo(zi) for zi in zh.infolist()],
2032-
[ComparableZipInfo(zi) for zi in expected_zinfos],
2031+
[comparable_zinfo(zi) for zi in zh.infolist()],
2032+
[comparable_zinfo(zi) for zi in expected_zinfos],
20332033
)
20342034

20352035
# check file size
@@ -2068,8 +2068,8 @@ def test_repack_bytes_between_removed_files(self):
20682068

20692069
# check infolist
20702070
self.assertEqual(
2071-
[ComparableZipInfo(zi) for zi in zh.infolist()],
2072-
[ComparableZipInfo(zi) for zi in expected_zinfos],
2071+
[comparable_zinfo(zi) for zi in zh.infolist()],
2072+
[comparable_zinfo(zi) for zi in expected_zinfos],
20732073
)
20742074

20752075
# check file size
@@ -2108,8 +2108,8 @@ def test_repack_prepended_bytes(self):
21082108

21092109
# check infolist
21102110
self.assertEqual(
2111-
[ComparableZipInfo(zi) for zi in zh.infolist()],
2112-
[ComparableZipInfo(zi) for zi in expected_zinfos],
2111+
[comparable_zinfo(zi) for zi in zh.infolist()],
2112+
[comparable_zinfo(zi) for zi in expected_zinfos],
21132113
)
21142114

21152115
# check file size
@@ -2199,8 +2199,8 @@ def test_repack_removed_basic(self):
21992199

22002200
# check infolist
22012201
self.assertEqual(
2202-
[ComparableZipInfo(zi) for zi in zh.infolist()],
2203-
[ComparableZipInfo(zi) for zi in expected_zinfos],
2202+
[comparable_zinfo(zi) for zi in zh.infolist()],
2203+
[comparable_zinfo(zi) for zi in expected_zinfos],
22042204
)
22052205

22062206
# check file size
@@ -2232,7 +2232,7 @@ def test_repack_removed_partial(self):
22322232

22332233
# check infolist
22342234
self.assertEqual(
2235-
[ComparableZipInfo(zi) for zi in zh.infolist()],
2235+
[comparable_zinfo(zi) for zi in zh.infolist()],
22362236
[],
22372237
)
22382238

@@ -2272,8 +2272,8 @@ def test_repack_removed_bytes_between_files(self):
22722272

22732273
# check infolist
22742274
self.assertEqual(
2275-
[ComparableZipInfo(zi) for zi in zh.infolist()],
2276-
[ComparableZipInfo(zi) for zi in expected_zinfos],
2275+
[comparable_zinfo(zi) for zi in zh.infolist()],
2276+
[comparable_zinfo(zi) for zi in expected_zinfos],
22772277
)
22782278

22792279
# check file size
@@ -2345,8 +2345,8 @@ def test_repack_removed_prepended_bytes(self):
23452345

23462346
# check infolist
23472347
self.assertEqual(
2348-
[ComparableZipInfo(zi) for zi in zh.infolist()],
2349-
[ComparableZipInfo(zi) for zi in expected_zinfos],
2348+
[comparable_zinfo(zi) for zi in zh.infolist()],
2349+
[comparable_zinfo(zi) for zi in expected_zinfos],
23502350
)
23512351

23522352
# check file size

0 commit comments

Comments
 (0)