Skip to content
Open
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
27 changes: 21 additions & 6 deletions Lib/zoneinfo/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@ def load_tzdata(key):
if path.is_dir():
raise IsADirectoryError
return path.open("rb")
except (ImportError, FileNotFoundError, UnicodeEncodeError, IsADirectoryError):
# There are four types of exception that can be raised that all amount
# to "we cannot find this key":
except ImportError:
# If package_name doesn't exist, it means tzdata is not installed
# or there's an error in the folder name like Amrica/New_York
exc = ZoneInfoNotFoundError(f"No time zone found with key {key}")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also delete the tab spaces to pass the CI lint test here and line 34.

# Check if tzdata is actually missing
try:
import tzdata
except ImportError:
# tzdata is not installed, provide installation instructions
exc.add_note("This error may occur if timezone data is not available.")
exc.add_note("Try:")
exc.add_note(" - Installing the tzdata package: python -m pip install tzdata")
exc.add_note(" - Verifying the timezone key is correct (for example, 'America/New_York')")
exc.add_note("")
exc.add_note("For more information, see:")
exc.add_note("https://docs.python.org/3/library/zoneinfo.html")
Comment on lines +27 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just add one note with a multiline string?


raise exc
except (FileNotFoundError, UnicodeEncodeError, IsADirectoryError):
# Other errors that amount to "we cannot find this key":
#
# ImportError: If package_name doesn't exist (e.g. if tzdata is not
# installed, or if there's an error in the folder name like
# Amrica/New_York)
# FileNotFoundError: If resource_name doesn't exist in the package
# (e.g. Europe/Krasnoy)
# UnicodeEncodeError: If package_name or resource_name are not UTF-8,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Improve error message in :mod:`zoneinfo` when timezone data is not found due
to missing :pypi:`tzdata` package. The new error message provides clear
instructions on how to resolve the issue, including installing the package
and links to documentation. Patch by daram62.
Loading