From bc9a0fb8a9f70dc85ee41375022c7d15e862a85b Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Wed, 22 Oct 2025 00:47:06 +0800 Subject: [PATCH 1/2] Improve error message --- Lib/zoneinfo/_common.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/zoneinfo/_common.py b/Lib/zoneinfo/_common.py index 03cc42149f9b74..2865beb1ee6a2a 100644 --- a/Lib/zoneinfo/_common.py +++ b/Lib/zoneinfo/_common.py @@ -14,17 +14,19 @@ def load_tzdata(key): if path.is_dir(): raise IsADirectoryError return path.open("rb") + except UnicodeEncodeError: + # This error occurs because of unsupported filesystem encoding which + # depends on the locale, such as keys containing a surrogate character. + ZoneInfoNotFoundError(f"key {key} contains non-UTF8 character(s)") except (ImportError, FileNotFoundError, UnicodeEncodeError, IsADirectoryError): - # There are four types of exception that can be raised that all amount - # to "we cannot find this key": + # There are three other 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 # 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, - # such as keys containing a surrogate character. # IsADirectoryError: If package_name without a resource_name specified. raise ZoneInfoNotFoundError(f"No time zone found with key {key}") From 8df24714c5c3c9c5347d654a5206f13b043b0f08 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 25 Oct 2025 16:04:39 +0800 Subject: [PATCH 2/2] Use !r --- Lib/zoneinfo/_common.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Lib/zoneinfo/_common.py b/Lib/zoneinfo/_common.py index 2865beb1ee6a2a..0db31e46d9ac43 100644 --- a/Lib/zoneinfo/_common.py +++ b/Lib/zoneinfo/_common.py @@ -14,21 +14,19 @@ def load_tzdata(key): if path.is_dir(): raise IsADirectoryError return path.open("rb") - except UnicodeEncodeError: - # This error occurs because of unsupported filesystem encoding which - # depends on the locale, such as keys containing a surrogate character. - ZoneInfoNotFoundError(f"key {key} contains non-UTF8 character(s)") except (ImportError, FileNotFoundError, UnicodeEncodeError, IsADirectoryError): - # There are three other types of exception that can be raised that - # all amount to "we cannot find this key": + # There are four 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 # 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, + # such as keys containing a surrogate character. # IsADirectoryError: If package_name without a resource_name specified. - raise ZoneInfoNotFoundError(f"No time zone found with key {key}") + raise ZoneInfoNotFoundError(f"No time zone found with key {key!r}") def load_data(fobj):