Skip to content
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
23 changes: 22 additions & 1 deletion src/backports/zoneinfo/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,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 @@ -25,6 +25,27 @@ 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)
import pathlib
try:
import importlib.util as importlib_util
except ImportError:
raise

tzdata_spec = importlib_util.find_spec("tzdata")
tzdata_path = pathlib.Path(tzdata_spec.origin).parent
resource_path = tzdata_path / "zoneinfo" / key

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


def load_data(fobj):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def test_bad_keys(self):
"America.Los_Angeles",
"🇨🇦", # Non-ascii
"America/New\ud800York", # Contains surrogate character
"America",
]

for bad_key in bad_keys:
Expand All @@ -239,6 +240,7 @@ def test_bad_keys_paths(self):
"../zoneinfo/America/Los_Angeles", # Traverses above TZPATH
"America/../America/Los_Angeles", # Not normalized
"America/./Los_Angeles",
"America/"
]

for bad_key in bad_keys:
Expand Down