Skip to content

It is possible for python_tzpath_context to fail in test_zoneinfo #102537

Closed
@pganssle

Description

@pganssle

Bug report

This code started failing the pylint check on backports.zoneinfo recently:

@staticmethod
@contextlib.contextmanager
def python_tzpath_context(value):
    path_var = "PYTHONTZPATH"
    try:
        with OS_ENV_LOCK:
           old_env = os.environ.get(path_var, None)
           os.environ[path_var] = value
           yield
    finally:
        if old_env is None:
            del os.environ[path_var]
        else:
            os.environ[path_var] = old_env  # pragma: nocover

It's kind of a non-issue, but it is true that if an error is raised while acquiring OS_ENV_LOCK or when calling os.environ.get, old_env won't be set, which will raise a NameError. This is a kind of rare situation anyway, and it would probably only come up during a non-recoverable error, but we may as well fix it — if only so that people who use CPython as an example of "good code" will have an example of the right way to handle this sort of situation.

Probably this is trivial enough that we can skip an issue and news, but I'll just create one anyway 😛

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions