Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reuse zone_from_tzstr with an encoding parameter in the non-ASCII test
  • Loading branch information
tonghuaroot committed Jun 26, 2026
commit bf26921d2f5a9ddeff94f1cefda7b5e27033d39c
19 changes: 4 additions & 15 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,14 +1009,14 @@ def populate_tzstr_header(cls):

cls._tzif_header = bytes(out)

def zone_from_tzstr(self, tzstr):
def zone_from_tzstr(self, tzstr, encoding="ascii"):
"""Creates a zoneinfo file following a POSIX rule."""
zonefile = io.BytesIO(self._tzif_header)
zonefile.seek(0, 2)

# Write the footer
zonefile.write(b"\x0A")
zonefile.write(tzstr.encode("ascii"))
zonefile.write(tzstr.encode(encoding))
zonefile.write(b"\x0A")

zonefile.seek(0)
Expand Down Expand Up @@ -1231,23 +1231,12 @@ def test_invalid_tzstr_non_ascii_abbr(self):
# It needs a separate test: it can't be ASCII-encoded for the shared
# invalid_tzstrs list, and the C error message holds the bytes repr.
tzstr = "ABÀC3"
footer = tzstr.encode("utf-8")

def from_footer():
zonefile = io.BytesIO(self._tzif_header)
zonefile.seek(0, 2)
zonefile.write(b"\x0A")
zonefile.write(footer)
zonefile.write(b"\x0A")
zonefile.seek(0)
return self.klass.from_file(zonefile, key=tzstr)

if self.module is py_zoneinfo:
expected = re.escape(tzstr)
else:
expected = re.escape(repr(footer))
expected = re.escape(repr(tzstr.encode("utf-8")))
with self.assertRaisesRegex(ValueError, expected):
from_footer()
self.zone_from_tzstr(tzstr, encoding="utf-8")

@classmethod
def _populate_test_cases(cls):
Expand Down
Loading