diff --git a/Lib/zoneinfo/_common.py b/Lib/zoneinfo/_common.py index 4c24f01bd7b270..1e4f3e517784e6 100644 --- a/Lib/zoneinfo/_common.py +++ b/Lib/zoneinfo/_common.py @@ -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 @@ -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): diff --git a/Misc/NEWS.d/next/Library/2020-08-13-01-17-31.bpo-41530.WYqwKz.rst b/Misc/NEWS.d/next/Library/2020-08-13-01-17-31.bpo-41530.WYqwKz.rst new file mode 100644 index 00000000000000..5aa6f5d809cca3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-08-13-01-17-31.bpo-41530.WYqwKz.rst @@ -0,0 +1 @@ +zoneinfo ZoneInfo no longer raises IsADirectoryError or PermissionError when attempting to parse certain keys. \ No newline at end of file