Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7334e2

Browse files
authored
gh-106233: Fix stacklevel in zoneinfo.InvalidTZPathWarning (GH-106234)
1 parent 1a10437 commit d7334e2

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from test.support import MISSING_C_DOCSTRINGS
2121
from test.test_zoneinfo import _support as test_support
2222
from test.test_zoneinfo._support import OS_ENV_LOCK, TZPATH_TEST_LOCK, ZoneInfoTestBase
23-
from test.support.import_helper import import_module
23+
from test.support.import_helper import import_module, CleanImport
2424

2525
lzma = import_module('lzma')
2626
py_zoneinfo, c_zoneinfo = test_support.get_modules()
@@ -1720,13 +1720,26 @@ def test_env_variable_relative_paths(self):
17201720
with self.subTest("warning", path_var=path_var):
17211721
# Note: Per PEP 615 the warning is implementation-defined
17221722
# behavior, other implementations need not warn.
1723-
with self.assertWarns(self.module.InvalidTZPathWarning):
1723+
with self.assertWarns(self.module.InvalidTZPathWarning) as w:
17241724
self.module.reset_tzpath()
1725+
self.assertEqual(w.warnings[0].filename, __file__)
17251726

17261727
tzpath = self.module.TZPATH
17271728
with self.subTest("filtered", path_var=path_var):
17281729
self.assertSequenceEqual(tzpath, expected_paths)
17291730

1731+
def test_env_variable_relative_paths_warning_location(self):
1732+
path_var = "path/to/somewhere"
1733+
1734+
with self.python_tzpath_context(path_var):
1735+
with CleanImport("zoneinfo", "zoneinfo._tzpath"):
1736+
with self.assertWarns(RuntimeWarning) as w:
1737+
import zoneinfo
1738+
InvalidTZPathWarning = zoneinfo.InvalidTZPathWarning
1739+
self.assertIsInstance(w.warnings[0].message, InvalidTZPathWarning)
1740+
# It should represent the current file:
1741+
self.assertEqual(w.warnings[0].filename, __file__)
1742+
17301743
def test_reset_tzpath_kwarg(self):
17311744
self.module.reset_tzpath(to=[f"{DRIVE}/a/b/c"])
17321745

Lib/zoneinfo/_tzpath.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sysconfig
33

44

5-
def reset_tzpath(to=None):
5+
def _reset_tzpath(to=None, stacklevel=4):
66
global TZPATH
77

88
tzpaths = to
@@ -18,17 +18,22 @@ def reset_tzpath(to=None):
1818
base_tzpath = tzpaths
1919
else:
2020
env_var = os.environ.get("PYTHONTZPATH", None)
21-
if env_var is not None:
22-
base_tzpath = _parse_python_tzpath(env_var)
23-
else:
24-
base_tzpath = _parse_python_tzpath(
25-
sysconfig.get_config_var("TZPATH")
26-
)
21+
if env_var is None:
22+
env_var = sysconfig.get_config_var("TZPATH")
23+
base_tzpath = _parse_python_tzpath(env_var, stacklevel)
2724

2825
TZPATH = tuple(base_tzpath)
2926

3027

31-
def _parse_python_tzpath(env_var):
28+
def reset_tzpath(to=None):
29+
"""Reset global TZPATH."""
30+
# We need `_reset_tzpath` helper function because it produces a warning,
31+
# it is used as both a module-level call and a public API.
32+
# This is how we equalize the stacklevel for both calls.
33+
_reset_tzpath(to)
34+
35+
36+
def _parse_python_tzpath(env_var, stacklevel):
3237
if not env_var:
3338
return ()
3439

@@ -45,6 +50,7 @@ def _parse_python_tzpath(env_var):
4550
"Invalid paths specified in PYTHONTZPATH environment variable. "
4651
+ msg,
4752
InvalidTZPathWarning,
53+
stacklevel=stacklevel,
4854
)
4955

5056
return new_tzpath
@@ -172,4 +178,4 @@ class InvalidTZPathWarning(RuntimeWarning):
172178

173179

174180
TZPATH = ()
175-
reset_tzpath()
181+
_reset_tzpath(stacklevel=5)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix stacklevel in ``InvalidTZPathWarning`` during :mod:`zoneinfo` module
2+
import.

0 commit comments

Comments
 (0)