Repository navigation
zoneinfo: ZoneInfo raises IsADirectoryError instead of ZoneInfoNotFoundError #85702
Description
Activity
Attempting to parse specific keys in zoneinfo.ZoneInfo with tzdata installed will raise unhandled exceptions
e.g. on windows
>>> import zoneinfo >>> zoneinfo.ZoneInfo('Pacific') PermissionError: [Errno 13] Permission denied: 'C:\\Program Files\\Python39\\lib\\site-packages\\tzdata\\zoneinfo\\Pacific' >>> import zoneinfo >>> zoneinfo.ZoneInfo('__init__.py') ValueError: Invalid TZif file: magic not found
This happens when non TZif files or directories in the tzdata.zoneinfo module are used as keys.
- added3.9 (EOL)end of lifeend of life3.10 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Aug 12, 2020 Hi. It seems like you are running Windows.
About the permission error, how did you install tzdata? Did you install it as an admin user and do you run Python as a different user?
tzdata was installed as an admin user, however this behaviour was
reproducible on a linux installation where for the ZoneInfo.('Pacific')
instance an IsADirectoryError would instead be raised.On Wed, Aug 12, 2020 at 9:50 PM STINNER Victor <report@bugs.python.org>
wrote:STINNER Victor <vstinner@python.org> added the comment:
Hi. It seems like you are running Windows.
About the permission error, how did you install tzdata? Did you install it
as an admin user and do you run Python as a different user?----------
nosy: +vstinner
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41530\>
Oh right, 'Pacific' is a directory, not a valid zone, and so ZoneInfoNotFoundError should be raised. I see.
Example:
$ ./python -m venv env $ env/bin/python -m pip install tzdata $ env/bin/python
# ZoneInfoNotFoundError expected, get IsADirectoryError >>> import zoneinfo; zoneinfo.ZoneInfo('Pacific') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/vstinner/python/master/Lib/zoneinfo/_common.py", line 12, in load_tzdata return importlib.resources.open_binary(package_name, resource_name) File "/home/vstinner/python/master/Lib/importlib/resources.py", line 40, in open_binary return reader.open_resource(resource) File "/home/vstinner/python/master/Lib/importlib/abc.py", line 419, in open_resource return self.files().joinpath(resource).open('rb') File "/home/vstinner/python/master/Lib/pathlib.py", line 1238, in open return io.open(self, mode, buffering, encoding, errors, newline, IsADirectoryError: [Errno 21] Is a directory: '/home/vstinner/python/master/env/lib/python3.10/site-packages/tzdata/zoneinfo/Pacific' # valid zone >>> import zoneinfo; zoneinfo.ZoneInfo('Pacific/Noumea') zoneinfo.ZoneInfo(key='Pacific/Noumea') # raise ZoneInfoNotFoundError as expected (from FileNotFoundError) >>> import zoneinfo; zoneinfo.ZoneInfo('xxx') Traceback (most recent call last): File "/home/vstinner/python/master/Lib/zoneinfo/_common.py", line 12, in load_tzdata return importlib.resources.open_binary(package_name, resource_name) File "/home/vstinner/python/master/Lib/importlib/resources.py", line 40, in open_binary return reader.open_resource(resource) File "/home/vstinner/python/master/Lib/importlib/abc.py", line 419, in open_resource return self.files().joinpath(resource).open('rb') File "/home/vstinner/python/master/Lib/pathlib.py", line 1238, in open return io.open(self, mode, buffering, encoding, errors, newline, File "/home/vstinner/python/master/Lib/pathlib.py", line 1106, in _opener return self._accessor.open(self, flags, mode) FileNotFoundError: [Errno 2] No such file or directory: '/home/vstinner/python/master/env/lib/python3.10/site-packages/tzdata/zoneinfo/xxx' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/vstinner/python/master/Lib/zoneinfo/_common.py", line 24, in load_tzdata raise ZoneInfoNotFoundError(f"No time zone found with key {key}") zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key xxx'
- changed the title
[-]Unhandled exceptions in zoneinfo.ZoneInfo constructor[/-][+]zoneinfo: ZoneInfo raises IsADirectoryError instead of ZoneInfoNotFoundError[/+]on Aug 12, 2020 - changed the title
[-]Unhandled exceptions in zoneinfo.ZoneInfo constructor[/-][+]zoneinfo: ZoneInfo raises IsADirectoryError instead of ZoneInfoNotFoundError[/+]on Aug 12, 2020 On Linux, converting IsADirectoryError to ZoneInfoNotFoundError is easy.
But on Windows, I don't think that converting any PermissionError into a ZoneInfoNotFoundError is a good idea. Maybe if PermissionError happens, we should check if the path is a directory.
Pseudo-code:
try:
<open and read file>
except OSError as exc:
if os.path.isdir(zone_path):
raise ZoneInfoNotFoundError
else:
raise23 remaining items
- added a commit that references this issue
on Jun 30, 2025 Windows pr and backports have been merged. I do not know of anything else to do here, if other os-specific cases pop up in the future we can always reopen.
Reacted by Victor Stinner- moved this from In Progress to Done in Date and time issues 🕰️
on Jul 7, 2025
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs