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

Skip to content

Enhance test.support.os_helper.EnvironmentVarGuard interface #131277

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

Closed
3 tasks done
picnixz opened this issue Mar 15, 2025 · 3 comments
Closed
3 tasks done

Enhance test.support.os_helper.EnvironmentVarGuard interface #131277

picnixz opened this issue Mar 15, 2025 · 3 comments
Assignees
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes stdlib Python modules in the Lib dir tests Tests in the Lib/test dir triaged The issue has been accepted as valid by a triager. type-feature A feature request or enhancement

Comments

@picnixz
Copy link
Member

picnixz commented Mar 15, 2025

Feature or enhancement

Currently, we have a lot of pattern of the form:

with EnvironmentVarGuard() as env:
   if no_c in env:
       env.unset(no_c)
   if no_d in env:
       env.unset(no_d)
   for ev in {no_a, no_b}:
       if ev in env:
           env.unset(ev)

I think it makes sense to have something like this:

with EnvironmentVarGuard() as env:
   env.unset(no_a, no_b, no_c, no_d)  # unset many vars at once

or to also provide a decorator-based approach when the values to set/unset are statically known.

Features

  • Allow to unset multiple environment variables: env.unset(k1, k2, ...)

Some optional features that I rejected after looking at the use cases:

  • Allow to specify new environment variables at construction time (it could save a few lines when we're only modifying one or two envvars) [abandoned, not enough readable use cases]
  • Add decorator-based environment guard for EnvironmentVarGuard() [abandoned for now]

I plan to create a PR today or tomorrow to illustrate the new interface.

EDIT: no need to the support for setting multiple variables at once because EnvironmentVarGuard is a mapping, so it has .update() which does the trick.

Linked PRs

See #131277 (comment) for the version labels rationale and #131280 (comment) for the backporte rationale.

@picnixz picnixz added stdlib Python modules in the Lib dir tests Tests in the Lib/test dir type-feature A feature request or enhancement labels Mar 15, 2025
@picnixz picnixz self-assigned this Mar 15, 2025
@picnixz
Copy link
Member Author

picnixz commented Mar 15, 2025

Ok, nvm actually __del__ checks for the presence of the variable. So we have useless checks :')

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Mar 15, 2025
@picnixz picnixz removed the pending The issue will be closed if no feedback is provided label Mar 15, 2025
picnixz added a commit that referenced this issue Mar 16, 2025
…ment variable at once (#131280)

Co-authored-by: Hugo van Kemenade <[email protected]>
@picnixz
Copy link
Member Author

picnixz commented Mar 16, 2025

Keeping the issue opened until we decide whether to backport this or not to ease future backports.

plashchynski pushed a commit to plashchynski/cpython that referenced this issue Mar 17, 2025
…nvironment variable at once (python#131280)

Co-authored-by: Hugo van Kemenade <[email protected]>
@picnixz picnixz added 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes labels Mar 18, 2025
@picnixz
Copy link
Member Author

picnixz commented Mar 18, 2025

Adding the version labels as I decided to backport this to ease future development. However, the API will not be publicly documented.

picnixz added a commit to picnixz/cpython that referenced this issue Mar 18, 2025
…nvironment variable at once (python#131280)

Co-authored-by: Hugo van Kemenade <[email protected]>
picnixz added a commit to picnixz/cpython that referenced this issue Mar 18, 2025
…nvironment variable at once (python#131280)

Co-authored-by: Hugo van Kemenade <[email protected]>
@picnixz picnixz added the triaged The issue has been accepted as valid by a triager. label Mar 18, 2025
picnixz added a commit that referenced this issue Mar 18, 2025
…vironment variable at once (GH-131280) (#131410)

(cherry picked from commit 3185e31)

---------

Co-authored-by: Hugo van Kemenade <[email protected]>
picnixz added a commit that referenced this issue Mar 18, 2025
…vironment variable at once (GH-131280) (#131409)

(cherry picked from commit 3185e31)

---------

Co-authored-by: Hugo van Kemenade <[email protected]>
@picnixz picnixz closed this as completed Mar 18, 2025
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
…nvironment variable at once (python#131280)

Co-authored-by: Hugo van Kemenade <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes stdlib Python modules in the Lib dir tests Tests in the Lib/test dir triaged The issue has been accepted as valid by a triager. type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant