Skip to content

Commit b73dd02

Browse files
authored
Revert "bpo-39413: Implement os.unsetenv() on Windows (GH-18104)" (GH-18124)
This reverts commit 56cd371.
1 parent beea26b commit b73dd02

File tree

6 files changed

+12
-94
lines changed

6 files changed

+12
-94
lines changed

Doc/library/os.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,6 @@ process and user.
645645

646646
.. availability:: most flavors of Unix, Windows.
647647

648-
.. versionchanged:: 3.9
649-
The function is now also available on Windows.
650-
651648

652649
.. _os-newstreams:
653650

Doc/whatsnew/3.9.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,6 @@ Exposed the Linux-specific :func:`os.pidfd_open` (:issue:`38692`) and
216216
:data:`os.P_PIDFD` (:issue:`38713`) for process management with file
217217
descriptors.
218218

219-
The :func:`os.unsetenv` function is now also available on Windows.
220-
(Contributed by Victor Stinner in :issue:`39413`.)
221-
222219
poplib
223220
------
224221

Lib/test/test_os.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,14 @@ def test_environb(self):
956956
# On OS X < 10.6, unsetenv() doesn't return a value (bpo-13415).
957957
@support.requires_mac_ver(10, 6)
958958
def test_unset_error(self):
959-
# "=" is not allowed in a variable name
960-
key = 'key='
961-
self.assertRaises(OSError, os.environ.__delitem__, key)
959+
if sys.platform == "win32":
960+
# an environment variable is limited to 32,767 characters
961+
key = 'x' * 50000
962+
self.assertRaises(ValueError, os.environ.__delitem__, key)
963+
else:
964+
# "=" is not allowed in a variable name
965+
key = 'key='
966+
self.assertRaises(OSError, os.environ.__delitem__, key)
962967

963968
def test_key_type(self):
964969
missing = 'missingkey'

Misc/NEWS.d/next/Library/2020-01-21-15-48-35.bpo-39413.7XYDM8.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

Modules/clinic/posixmodule.c.h

Lines changed: 3 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10180,51 +10180,7 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
1018010180
#endif /* HAVE_PUTENV */
1018110181

1018210182

10183-
#ifdef MS_WINDOWS
10184-
/*[clinic input]
10185-
os.unsetenv
10186-
name: unicode
10187-
/
10188-
10189-
Delete an environment variable.
10190-
[clinic start generated code]*/
10191-
10192-
static PyObject *
10193-
os_unsetenv_impl(PyObject *module, PyObject *name)
10194-
/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
10195-
{
10196-
/* PyUnicode_AsWideCharString() rejects embedded null characters */
10197-
wchar_t *name_str = PyUnicode_AsWideCharString(name, NULL);
10198-
if (name_str == NULL) {
10199-
return NULL;
10200-
}
10201-
10202-
BOOL ok = SetEnvironmentVariableW(name_str, NULL);
10203-
PyMem_Free(name_str);
10204-
10205-
if (!ok) {
10206-
return PyErr_SetFromWindowsErr(0);
10207-
}
10208-
10209-
#ifdef PY_PUTENV_DICT
10210-
/* Remove the key from putenv_dict;
10211-
* this will cause it to be collected. This has to
10212-
* happen after the real unsetenv() call because the
10213-
* old value was still accessible until then.
10214-
*/
10215-
if (PyDict_DelItem(_posixstate(module)->putenv_dict, name)) {
10216-
/* really not much we can do; just leak */
10217-
if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10218-
return NULL;
10219-
}
10220-
PyErr_Clear();
10221-
}
10222-
#endif
10223-
10224-
Py_RETURN_NONE;
10225-
}
10226-
/* repeat !defined(MS_WINDOWS) to workaround an Argument Clinic issue */
10227-
#elif defined(HAVE_UNSETENV) && !defined(MS_WINDOWS)
10183+
#ifdef HAVE_UNSETENV
1022810184
/*[clinic input]
1022910185
os.unsetenv
1023010186
name: FSConverter

0 commit comments

Comments
 (0)