-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecation warning in zoneinfo module #90282
Comments
zoneinfo module currently emits deprecation warnings due to API changes in importlib.resources ./python -Wonce -m test test_zoneinfo == Tests result: SUCCESS == 1 test OK. Total duration: 376 ms A fix would be to adapt the _legacy module changes inline like below patch though normalize_path is not public API and need to be inlined too. diff --git a/Lib/zoneinfo/_common.py b/Lib/zoneinfo/_common.py
index 4c24f01bd7..bfe3fe4c3c 100644
--- a/Lib/zoneinfo/_common.py
+++ b/Lib/zoneinfo/_common.py
@@ -2,14 +2,15 @@
def load_tzdata(key):
- import importlib.resources
+ from importlib.resources import files
+ from importlib._common import normalize_path
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 (files(package_name) / normalize_path(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 672560b951..b1efe5d99e 100644
--- a/Lib/zoneinfo/_tzpath.py
+++ b/Lib/zoneinfo/_tzpath.py
@@ -111,14 +111,15 @@ def available_timezones():
determine if a given file on the time zone search path is to open it
and check for the "magic string" at the beginning.
"""
- from importlib import resources
+ from importlib.resources import files
+ from importlib._common import normalize_path
valid_zones = set()
# 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 (files("tzdata") / normalize_path("zones")).open('r') as f:
for zone in f:
zone = zone.strip()
if zone: |
I would hope that normalize_path would not be needed. It might be for drop-in compatibility. If it's needed for zoneinfo, I'd like to consider why and what implications that has for the deprecation of the legacy API. |
I just confirmed, and |
I filed bpo-46125 to track that issue separately. |
Jason's patch looks good to me, but I don't understand why Karthikeyan originally suggested using |
I just copied the implementation and normalize_path function was part of it. Looking into the implementation of normalize_path it validates the given argument to be a filename without any separator. I will leave it to Jason for a better understanding of the API and compatibility here. |
Yes. Previously, resources.* would perform some validation on the path to ensure that it didn't contain path separators (to avoid users attempting to get resources in subdirectories or perhaps manipulating the path in other ways). So no, they're not equivalent. If If it is worth exploring, I would recommend not to use normalize_path, but instead to implement the validation in zoneinfo._common. That is, wrap key in |
Normalize_path from legacy implementation: https://github.com/python/importlib_resources/blob/3beb2fd5831e65f7b45033e1ec276c4a6b4ca973/importlib_resources/_legacy.py#L30-L40 |
Closing, presumed fixed. Please re-open if not. |
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:
The text was updated successfully, but these errors were encountered: