diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2024-02-06 13:08:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-06 13:08:56 (GMT) |
commit | d7334e2c2012defaf7aae920d6a56689464509d1 (patch) | |
tree | f0aa2f7425f7fefd043a636b46a85ef1e487c69e /Lib/zoneinfo | |
parent | 1a10437a14b13100bdf41cbdab819c33258deb65 (diff) | |
download | cpython-d7334e2c2012defaf7aae920d6a56689464509d1.zip cpython-d7334e2c2012defaf7aae920d6a56689464509d1.tar.gz cpython-d7334e2c2012defaf7aae920d6a56689464509d1.tar.bz2 |
gh-106233: Fix stacklevel in zoneinfo.InvalidTZPathWarning (GH-106234)
Diffstat (limited to 'Lib/zoneinfo')
-rw-r--r-- | Lib/zoneinfo/_tzpath.py | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/Lib/zoneinfo/_tzpath.py b/Lib/zoneinfo/_tzpath.py index 4985dce..5db17be 100644 --- a/Lib/zoneinfo/_tzpath.py +++ b/Lib/zoneinfo/_tzpath.py @@ -2,7 +2,7 @@ import os import sysconfig -def reset_tzpath(to=None): +def _reset_tzpath(to=None, stacklevel=4): global TZPATH tzpaths = to @@ -18,17 +18,22 @@ def reset_tzpath(to=None): base_tzpath = tzpaths else: env_var = os.environ.get("PYTHONTZPATH", None) - if env_var is not None: - base_tzpath = _parse_python_tzpath(env_var) - else: - base_tzpath = _parse_python_tzpath( - sysconfig.get_config_var("TZPATH") - ) + if env_var is None: + env_var = sysconfig.get_config_var("TZPATH") + base_tzpath = _parse_python_tzpath(env_var, stacklevel) TZPATH = tuple(base_tzpath) -def _parse_python_tzpath(env_var): +def reset_tzpath(to=None): + """Reset global TZPATH.""" + # We need `_reset_tzpath` helper function because it produces a warning, + # it is used as both a module-level call and a public API. + # This is how we equalize the stacklevel for both calls. + _reset_tzpath(to) + + +def _parse_python_tzpath(env_var, stacklevel): if not env_var: return () @@ -45,6 +50,7 @@ def _parse_python_tzpath(env_var): "Invalid paths specified in PYTHONTZPATH environment variable. " + msg, InvalidTZPathWarning, + stacklevel=stacklevel, ) return new_tzpath @@ -172,4 +178,4 @@ class InvalidTZPathWarning(RuntimeWarning): TZPATH = () -reset_tzpath() +_reset_tzpath(stacklevel=5) |