bpo-39413: Implement os.unsetenv() on Windows#18163
bpo-39413: Implement os.unsetenv() on Windows#18163vstinner merged 3 commits intopython:masterfrom vstinner:win_unsetenv4
Conversation
The os.unsetenv() function is now also available on Windows.
|
@eryksun @serhiy-storchaka: Here is a simpler implementation of os.unsetenv() for Windows.
In practice,os.unsetenv() rejects "=" anywhere in the name on all platforms: it's now validated by unit tests. I chose to not change raised exceptions (ValueError vs OSError) for invalid variable name in this PR. I plan to propose a separated PR to unify the raised exception on all platforms. I also chose to not remove the Windows maximum variable length limit in this PR, again to make it easier to review. I also plan to propose a separated PR to limit this outdated limit. |
|
|
Consider calling static PyObject *
win32_putenv(wchar_t *name, wchar_t *value)
{
size_t name_length = wcslen(name);
if (!name_length || wcschr(name, L'=')) {
PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
return NULL;
}
if (name_length >= _MAX_ENV) {
PyErr_Format(PyExc_ValueError, "the environment variable name "
"is longer than %u characters", _MAX_ENV - 1);
return NULL;
}
if (wcslen(value) >= _MAX_ENV) {
PyErr_Format(PyExc_ValueError, "the environment variable value "
"is longer than %u characters", _MAX_ENV - 1);
return NULL;
}
/* Both _wputenv_s() and SetEnvironmentVariableW() update the environment
in the Process Environment Block (PEB), but _wputenv_s() also updates
the CRT environ and _wenviron variables. Use _wputenv_s() in order to
be compatible with C libraries that use the CRT variables and the CRT
functions that use these variables, such as getenv(). */
errno_t err;
_Py_BEGIN_SUPPRESS_IPH
err = _wputenv_s(name, value);
_Py_END_SUPPRESS_IPH
if (err) {
posix_error();
return NULL;
}
Py_RETURN_NONE;
} |
The os.unsetenv() function is now also available on Windows.
The os.unsetenv() function is now also available on Windows.
https://bugs.python.org/issue39413