[Python-checkins] bpo-46124: Update zoneinfo to rely on importlib.resources traversable API. (GH-30190)
miss-islington
webhook-mailer at python.org
Fri Jan 21 16:18:36 EST 2022
https://github.com/python/cpython/commit/00b2b578bd9e516d601063a086b03177f546bcdd
commit: 00b2b578bd9e516d601063a086b03177f546bcdd
branch: main
author: Jason R. Coombs <jaraco at jaraco.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-01-21T13:18:31-08:00
summary:
bpo-46124: Update zoneinfo to rely on importlib.resources traversable API. (GH-30190)
Automerge-Triggered-By: GH:jaraco
files:
A Misc/NEWS.d/next/Library/2021-12-18-18-41-30.bpo-46124.ESPrb7.rst
M Lib/zoneinfo/_common.py
M Lib/zoneinfo/_tzpath.py
diff --git a/Lib/zoneinfo/_common.py b/Lib/zoneinfo/_common.py
index 4c24f01bd7b27..98cdfe37ca6ca 100644
--- a/Lib/zoneinfo/_common.py
+++ b/Lib/zoneinfo/_common.py
@@ -2,14 +2,14 @@
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":
diff --git a/Lib/zoneinfo/_tzpath.py b/Lib/zoneinfo/_tzpath.py
index 672560b951442..4985dce2dc36d 100644
--- a/Lib/zoneinfo/_tzpath.py
+++ b/Lib/zoneinfo/_tzpath.py
@@ -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:
diff --git a/Misc/NEWS.d/next/Library/2021-12-18-18-41-30.bpo-46124.ESPrb7.rst b/Misc/NEWS.d/next/Library/2021-12-18-18-41-30.bpo-46124.ESPrb7.rst
new file mode 100644
index 0000000000000..26f9f81303a96
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-12-18-18-41-30.bpo-46124.ESPrb7.rst
@@ -0,0 +1 @@
+Update :mod:`zoneinfo` to rely on importlib.resources traversable API.
More information about the Python-checkins
mailing list