Skip to content

zoneinfo: ZoneInfo raises IsADirectoryError instead of ZoneInfoNotFoundError #85702

Description

@bijij
mannequin
BPO 41530
Nosy @vstinner, @pganssle, @bijij
PRs
  • bpo-41530: Handle unhandled IsADirectoryError and PermissionError in zoneinfo.ZoneInfo #21839
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2020-08-12.11:42:16.788>
    labels = ['type-bug', 'library', '3.9', '3.10']
    title = 'zoneinfo: ZoneInfo raises IsADirectoryError instead of ZoneInfoNotFoundError'
    updated_at = <Date 2020-08-12.14:46:45.318>
    user = 'https://github.com/bijij'

    bugs.python.org fields:

    activity = <Date 2020-08-12.14:46:45.318>
    actor = 'josh.ja.butt'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-08-12.11:42:16.788>
    creator = 'josh.ja.butt'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41530
    keywords = ['patch']
    message_count = 10.0
    messages = ['375225', '375226', '375228', '375233', '375234', '375235', '375238', '375239', '375240', '375251']
    nosy_count = 4.0
    nosy_names = ['vstinner', 'python-dev', 'p-ganssle', 'josh.ja.butt']
    pr_nums = ['21839']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41530'
    versions = ['Python 3.9', 'Python 3.10']

    Linked PRs

    Activity

    1. bijij commented on Aug 12, 2020

      bijijmannequin
      MannequinAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Aug 12, 2020
    3. vstinner commented on Aug 12, 2020

      @vstinner
      Member

      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?

    4. bijij commented on Aug 12, 2020

      bijijmannequin
      MannequinAuthor

      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\>


    5. vstinner commented on Aug 12, 2020

      @vstinner
      Member

      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'
    6. changed the title [-]Unhandled exceptions in zoneinfo.ZoneInfo constructor[/-] [+]zoneinfo: ZoneInfo raises IsADirectoryError instead of ZoneInfoNotFoundError[/+] on Aug 12, 2020
    7. changed the title [-]Unhandled exceptions in zoneinfo.ZoneInfo constructor[/-] [+]zoneinfo: ZoneInfo raises IsADirectoryError instead of ZoneInfoNotFoundError[/+] on Aug 12, 2020
    8. vstinner commented on Aug 12, 2020

      @vstinner
      Member

      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:
      raise

    9. 23 remaining items

    10. added a commit that references this issue on Jun 30, 2025
    11. added 2 commits that reference this issue on Jun 30, 2025
    12. added 2 commits that reference this issue on Jun 30, 2025
    13. added a commit that references this issue on Jun 30, 2025
    14. added a commit that references this issue on Jun 30, 2025
    15. StanFromIreland commented on Jul 7, 2025

      @StanFromIreland
      Member

      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.

    16. added a commit that references this issue on Jul 11, 2025
    17. added a commit that references this issue on Jul 12, 2025
    18. added a commit that references this issue on Jul 13, 2025
    19. added a commit that references this issue on Aug 4, 2025
    20. added a commit that references this issue on Aug 19, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      OS-windowsstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions