8000 gh-106233: Fix stacklevel in `zoneinfo.InvalidTZPathWarning` by sobolevn · Pull Request #106234 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-106233: Fix stacklevel in zoneinfo.InvalidTZPathWarning #106234

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

Merged
merged 10 commits into from
Feb 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use explicit stacklevel argument
  • Loading branch information
sobolevn committed Feb 5, 2024
commit 4ce91237038839715e6764a4ebfd6754396a3418
21 changes: 15 additions & 6 deletions Lib/zoneinfo/_tzpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sysconfig


def reset_tzpath(to=None):
def _reset_tzpath(to=None, stacklevel=4):
global TZPATH

tzpaths = to
Expand All @@ -19,16 +19,25 @@ def reset_tzpath(to=None):
else:
env_var = os.environ.get("PYTHONTZPATH", None)
if env_var is not None:
base_tzpath = _parse_python_tzpath(env_var)
base_tzpath = _parse_python_tzpath(env_var, stacklevel)
else:
base_tzpath = _parse_python_tzpath(
sysconfig.get_config_var("TZPATH")
sysconfig.get_config_var("TZPATH"),
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 ()

Expand All @@ -45,7 +54,7 @@ def _parse_python_tzpath(env_var):
"Invalid paths specified in PYTHONTZPATH environment variable. "
+ msg,
InvalidTZPathWarning,
stacklevel=5,
stacklevel=stacklevel,
)

return new_tzpath
Expand Down Expand Up @@ -173,4 +182,4 @@ class InvalidTZPathWarning(RuntimeWarning):


TZPATH = ()
reset_tzpath()
_reset_tzpath(stacklevel=5)
0