Skip to content

gh-96463: fix load_tzdata raising OSError, not `ZoneInfoNotFound… #99602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
except importlib.metadata.PackageNotFoundError:
HAS_TZDATA_PKG = False

if HAS_TZDATA_PKG:
raise ValueError('Fail the test')

ZONEINFO_DATA = None
ZONEINFO_DATA_V1 = None
TEMP_DIR = None
Expand Down Expand Up @@ -220,6 +223,7 @@ def test_bad_keys(self):
"America.Los_Angeles",
"🇨🇦", # Non-ascii
"America/New\ud800York", # Contains surrogate character
"a" * 256, # Too long
]

for bad_key in bad_keys:
Expand Down
4 changes: 3 additions & 1 deletion Lib/zoneinfo/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def load_tzdata(key):

try:
return resources.files(package_name).joinpath(resource_name).open("rb")
except (ImportError, FileNotFoundError, UnicodeEncodeError):
except (ImportError, FileNotFoundError, UnicodeEncodeError): # TODO
# There are three types of exception that can be raised that all amount
# to "we cannot find this key":
#
Expand All @@ -21,6 +21,8 @@ def load_tzdata(key):
# (e.g. Europe/Krasnoy)
# UnicodeEncodeError: If package_name or resource_name are not UTF-8,
# such as keys containing a surrogate character.
# OSError: If filename is wrong (too long, has invalid ascii symbols)
# or when there's a permissing error.
raise ZoneInfoNotFoundError(f"No time zone found with key {key}")


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`zoneinfo.ZoneInfo` raising :exc:`OSError` instead of
``ZoneInfoNotFoundError``.