Skip to content

Commit 1a75eff

Browse files
committed
Introduce _handle_bad_field to make subclassing easier
Many extra fields (e.g., 0x000a and 0x000c) have sub-tags using the same (id, len, *fields) format. Introduce a `_handle_bad_field` class method to allow a subclass to parse with same logic while providing a customized error message.
1 parent 7b691b1 commit 1a75eff

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

Lib/zipfile/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ class LargeZipFile(Exception):
198198
class _Extra:
199199
FIELD_STRUCT = struct.Struct('<HH')
200200

201+
@classmethod
202+
def _handle_bad_field(cls, xid, xlen):
203+
raise BadZipFile("Corrupt extra field %04x (size=%d)" % (xid, xlen))
204+
201205
@classmethod
202206
def iter(cls, data, validate=False):
203207
"""Iter through and yield each (field, id)."""
@@ -213,8 +217,7 @@ def iter(cls, data, validate=False):
213217
xid, xlen = None, 0
214218
else:
215219
if validate and pos + 4 + xlen > data_len:
216-
raise BadZipFile(
217-
"Corrupt extra field %04x (size=%d)" % (xid, xlen))
220+
cls._handle_bad_field(xid, xlen)
218221
yield data[pos:pos + 4 + xlen], xid
219222
pos += 4 + xlen
220223

0 commit comments

Comments
 (0)