From 95dcf3ed0b5f89161a513fdaf4642fb6e9c4360a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 19 Nov 2022 21:56:16 +0300 Subject: [PATCH 1/2] gh-96463: fix `load_tzdata` raising `OSError`, not `ZoneInfoNotFoundError` --- Lib/test/test_zoneinfo/test_zoneinfo.py | 1 + Lib/zoneinfo/_common.py | 4 +++- .../Library/2022-11-19-21-55-06.gh-issue-96463.N6Woyx.rst | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-11-19-21-55-06.gh-issue-96463.N6Woyx.rst diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index fd0e3bc032ec0c..50e6042b0b293c 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -220,6 +220,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: diff --git a/Lib/zoneinfo/_common.py b/Lib/zoneinfo/_common.py index 98cdfe37ca6cae..768f0d5a897f30 100644 --- a/Lib/zoneinfo/_common.py +++ b/Lib/zoneinfo/_common.py @@ -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": # @@ -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}") diff --git a/Misc/NEWS.d/next/Library/2022-11-19-21-55-06.gh-issue-96463.N6Woyx.rst b/Misc/NEWS.d/next/Library/2022-11-19-21-55-06.gh-issue-96463.N6Woyx.rst new file mode 100644 index 00000000000000..476e302efa024c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-11-19-21-55-06.gh-issue-96463.N6Woyx.rst @@ -0,0 +1,2 @@ +Fix :class:`zoneinfo.ZoneInfo` raising :exc:`OSError` instead of +``ZoneInfoNotFoundError``. From 22be3495e76fdcfd6fa5b775b177e50986ceebbf Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sun, 20 Nov 2022 12:19:46 +0300 Subject: [PATCH 2/2] Testing --- Lib/test/test_zoneinfo/test_zoneinfo.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_zoneinfo/test_zoneinfo.py b/Lib/test/test_zoneinfo/test_zoneinfo.py index 50e6042b0b293c..c6081219dc7a8b 100644 --- a/Lib/test/test_zoneinfo/test_zoneinfo.py +++ b/Lib/test/test_zoneinfo/test_zoneinfo.py @@ -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