classification
Title: Implement os.unsetenv() on Windows
Type: Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, miss-islington, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords: patch

Created on 2020-01-21 14:39 by vstinner, last changed 2020-01-28 02:08 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18104 merged vstinner, 2020-01-21 14:53
PR 18107 closed vstinner, 2020-01-21 18:30
PR 18108 merged vstinner, 2020-01-21 18:32
PR 18114 merged miss-islington, 2020-01-22 12:51
PR 18115 closed vstinner, 2020-01-22 13:07
PR 18124 merged vstinner, 2020-01-22 19:50
PR 18163 merged vstinner, 2020-01-24 10:08
Messages (10)
msg360402 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-21 14:39
os.unsetenv() is documented to be available on Windows, but it's not.

In Python 3.8, "del os.environ[key]" is implemented as:

   os.putenv(key.upper(), "")

Attached PR implements it using SetEnvironmentVariableW(name, NULL).
msg360403 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-21 15:13
New changeset 56cd3710a1ea3ba872d345ea1bebc86ed08bc8b8 by Victor Stinner in branch 'master':
bpo-39413: Implement os.unsetenv() on Windows (GH-18104)
https://github.com/python/cpython/commit/56cd3710a1ea3ba872d345ea1bebc86ed08bc8b8
msg360404 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2020-01-21 15:27
This bypasses the CRT's copy of the environment. If any C code calls [_w]getenv, it will still see the variable defined. The only way it won't is if setting the value also bypassed the CRT environment by directly calling SetEnvironmentVariableW.
msg360418 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-21 18:04
> This bypasses the CRT's copy of the environment. If any C code calls [_w]getenv, it will still see the variable defined. The only way it won't is if setting the value also bypassed the CRT environment by directly calling SetEnvironmentVariableW.

Oh. That's a valid concern. I reopen the issue.
msg360420 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-21 18:16
I checked the doc: _putenv("VAR=") removes the environment variable "VAR". It's well documented:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-wputenv?view=vs-2019

"You can remove a variable from the environment by specifying an empty value_string, or in other words, by specifying only varname=."

So Python 3.8 os._unsetenv() is correct, but this function was not exposed, and so this issue remains relevant: Python 3.8 documents that os.unsetenv() is available on Windows, but it's not.
msg360423 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-21 18:31
PR 18107 still fix this issue (add os.unsetenv() to Python 3.9 on Windows), but fix the implementation (use _wputenv() rather than SetEnvironmentVariableW(), to update the CRT).
msg360462 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-22 12:50
New changeset f84f65be5602e561fef04b66bb487fbc4e560db5 by Victor Stinner in branch '3.8':
bpo-39413: os.unsetenv() is not available on Windows (GH-18108)
https://github.com/python/cpython/commit/f84f65be5602e561fef04b66bb487fbc4e560db5
msg360463 - (view) Author: miss-islington (miss-islington) Date: 2020-01-22 12:56
New changeset 570724667d37fcb160cdef2b34190dc36e28155c by Miss Islington (bot) in branch '3.7':
bpo-39413: os.unsetenv() is not available on Windows (GH-18108)
https://github.com/python/cpython/commit/570724667d37fcb160cdef2b34190dc36e28155c
msg360503 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-22 20:11
New changeset b73dd02ea744288831f71363a7467552c09875ea by Victor Stinner in branch 'master':
Revert "bpo-39413: Implement os.unsetenv() on Windows (GH-18104)" (GH-18124)
https://github.com/python/cpython/commit/b73dd02ea744288831f71363a7467552c09875ea
msg360609 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-24 10:53
New changeset 161e7b36b1ea871a1352ccfc1d4f4c1eda76830f by Victor Stinner in branch 'master':
bpo-39413: Implement os.unsetenv() on Windows (GH-18163)
https://github.com/python/cpython/commit/161e7b36b1ea871a1352ccfc1d4f4c1eda76830f
History
Date User Action Args
2020-01-28 02:08:26vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-01-24 10:53:48vstinnersetmessages: + msg360609
2020-01-24 10:08:03vstinnersetpull_requests: + pull_request17550
2020-01-22 20:11:20vstinnersetmessages: + msg360503
2020-01-22 19:50:21vstinnersetpull_requests: + pull_request17511
2020-01-22 13:07:13vstinnersetpull_requests: + pull_request17502
2020-01-22 12:56:26miss-islingtonsetnosy: + miss-islington
messages: + msg360463
2020-01-22 12:51:13miss-islingtonsetpull_requests: + pull_request17501
2020-01-22 12:50:46vstinnersetmessages: + msg360462
2020-01-21 18:32:37vstinnersetpull_requests: + pull_request17497
2020-01-21 18:31:10vstinnersetmessages: + msg360423
2020-01-21 18:30:04vstinnersetstage: resolved -> patch review
pull_requests: + pull_request17496
2020-01-21 18:16:30vstinnersetmessages: + msg360420
2020-01-21 18:04:18vstinnersetstatus: closed -> open
resolution: fixed -> (no value)
messages: + msg360418
2020-01-21 15:27:01eryksunsetnosy: + eryksun
messages: + msg360404
2020-01-21 15:16:51vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-01-21 15:13:18vstinnersetmessages: + msg360403
2020-01-21 14:53:09vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request17492
2020-01-21 14:39:20vstinnercreate