Skip to content

Commit eacad9a

Browse files
Change test + simplify check.
1 parent bf3f3a5 commit eacad9a

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed
-288 Bytes
Binary file not shown.
-59 Bytes
Binary file not shown.

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,35 @@ def test_empty_zone(self):
742742
self.klass.from_file(zf)
743743

744744
def test_invalid_transition_index(self):
745-
with open(DATA_DIR / "tzif_invalid_trans_idx", "rb") as f:
746-
with self.assertRaises(ValueError):
747-
self.klass.from_file(f)
745+
STD = ZoneOffset("STD", ZERO)
746+
DST = ZoneOffset("DST", ONE_H, ONE_H)
747+
748+
zf = self.construct_zone([
749+
ZoneTransition(datetime(2026, 3, 1, 2), STD, DST),
750+
ZoneTransition(datetime(2026, 11, 1, 2), DST, STD),
751+
], after="", version=1)
752+
753+
data = bytearray(zf.read())
754+
timecnt = struct.unpack_from(">l", data, 32)[0]
755+
idx_offset = 44 + timecnt * 4
756+
data[idx_offset + 1] = 2 # typecnt is 2, so index 2 is OOB
757+
f = io.BytesIO(bytes(data))
758+
759+
with self.assertRaises(ValueError):
760+
self.klass.from_file(f)
748761

749762
def test_transition_lookahead_out_of_bounds(self):
750-
with open(DATA_DIR / "tzif_invalid_lookahead", "rb") as f:
751-
zi = self.klass.from_file(f)
763+
STD = ZoneOffset("STD", ZERO)
764+
DST = ZoneOffset("DST", ONE_H, ONE_H)
765+
EXT = ZoneOffset("EXT", ONE_H)
766+
767+
zf = self.construct_zone([
768+
ZoneTransition(datetime(2026, 3, 1), STD, DST),
769+
ZoneTransition(datetime(2026, 6, 1), DST, EXT),
770+
ZoneTransition(datetime(2026, 9, 1), EXT, DST),
771+
], after="")
772+
773+
zi = self.klass.from_file(zf)
752774
self.assertIsNotNone(zi)
753775

754776
def test_zone_very_large_timestamp(self):

Lib/zoneinfo/_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ def load_data(fobj):
7171
trans_list_utc = ()
7272
trans_idx = ()
7373

74-
for idx in trans_idx:
75-
if idx >= typecnt:
76-
raise ValueError(f"Invalid transition index found while reading TZif: {idx}")
74+
if trans_idx and max(trans_idx) >= typecnt:
75+
raise ValueError("Invalid transition index found while reading TZif: "
76+
f"{max(trans_idx)}")
7777

7878
# Read the ttinfo struct, (utoff, isdst, abbrind)
7979
if typecnt:

0 commit comments

Comments
 (0)