mirror of
https://github.com/python/cpython.git
synced 2025-01-21 07:55:16 +08:00
bpo-46124: Update zoneinfo to rely on importlib.resources traversable API. (GH-30190)
Automerge-Triggered-By: GH:jaraco
This commit is contained in:
parent
f1e559b754
commit
00b2b578bd
@ -2,14 +2,14 @@ import struct
|
||||
|
||||
|
||||
def load_tzdata(key):
|
||||
import importlib.resources
|
||||
from importlib import resources
|
||||
|
||||
components = key.split("/")
|
||||
package_name = ".".join(["tzdata.zoneinfo"] + components[:-1])
|
||||
resource_name = components[-1]
|
||||
|
||||
try:
|
||||
return importlib.resources.open_binary(package_name, resource_name)
|
||||
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":
|
||||
|
@ -118,7 +118,7 @@ def available_timezones():
|
||||
# Start with loading from the tzdata package if it exists: this has a
|
||||
# pre-assembled list of zones that only requires opening one file.
|
||||
try:
|
||||
with resources.open_text("tzdata", "zones") as f:
|
||||
with resources.files("tzdata").joinpath("zones").open("r") as f:
|
||||
for zone in f:
|
||||
zone = zone.strip()
|
||||
if zone:
|
||||
|
@ -0,0 +1 @@
|
||||
Update :mod:`zoneinfo` to rely on importlib.resources traversable API.
|
Loading…
Reference in New Issue
Block a user