Skip to content

ZoneInfo object raises OSError instead of ZoneInfoNotFoundError for timezone input larger than 255 characters #96463

Open
@lleontop

Description

@lleontop

Bug report

According to documentation all calls to ZoneInfo should raise ValueError or ZoneInfoNotFoundError.
Creating a ZoneInfo object with an invalid very long timezone (length > 255) an OSError is raised instead (OSError: [Errno 36] File name too long).

I believe that

try:
return resources.files(package_name).joinpath(resource_name).open("rb")
except (ImportError, FileNotFoundError, UnicodeEncodeError):
# There are three 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.
raise ZoneInfoNotFoundError(f"No time zone found with key {key}")
should also handle the OSError exception raised by open and reraise it to ZoneInfoNotFoundError.

Sample code for reproduction:

from zoneinfo import ZoneInfo
ZoneInfo("a"*256)

Your environment

  • CPython versions tested on: Python 3.10.4
  • Operating system and architecture: Debian GNU/Linux 11 (bullseye)

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions