Skip to content

bpo-41530: Handle unhandled IsADirectoryError and PermissionError in zoneinfo.ZoneInfo #21839

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

Closed
wants to merge 13 commits into from
Closed
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
15 changes: 14 additions & 1 deletion Lib/zoneinfo/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def load_tzdata(key):
try:
return importlib.resources.open_binary(package_name, resource_name)
except (ImportError, FileNotFoundError, UnicodeEncodeError):
# There are three types of exception that can be raised that all amount
# There are some types of exception that can be raised that all amount
# to "we cannot find this key":
#
# ImportError: If package_name doesn't exist (e.g. if tzdata is not
Expand All @@ -22,6 +22,19 @@ def load_tzdata(key):
# UnicodeEncodeError: If package_name or resource_name are not UTF-8,
# such as keys containing a surrogate character.
raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
except (IsADirectoryError, PermissionError):
# A few exceptions inherited from OSError can be raised in various scenarios:
#
# IsADirectoryError: If the resource_name links to a directory
# (e.g. Australia)
# PermissionError: If the resource_name links to a directory on Windows
# (e.g. Pacific)
resource_path = importlib.resources.files(package_name).joinpath(key)

if resource_path.is_dir():
raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
else:
raise


def load_data(fobj):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zoneinfo ZoneInfo no longer raises IsADirectoryError or PermissionError when attempting to parse certain keys.