diff --git a/Lib/os.py b/Lib/os.py index ca418edbc57366..7b19073b043410 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -715,7 +715,9 @@ def setdefault(self, key, value): try: _putenv = putenv except NameError: - _putenv = lambda key, value: None + def _putenv(key, value): + # do nothing + return else: if "putenv" not in __all__: __all__.append("putenv") @@ -723,7 +725,17 @@ def setdefault(self, key, value): try: _unsetenv = unsetenv except NameError: - _unsetenv = lambda key: _putenv(key, "") + if name == 'nt': + def unsetenv(key): + """Delete an environment variable.""" + _putenv(key, "") + + if "unsetenv" not in __all__: + __all__.append("unsetenv") + _unsetenv = unsetenv + else: + def _unsetenv(key): + _putenv(key, "") else: if "unsetenv" not in __all__: __all__.append("unsetenv") diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h index 661d91afb7eb6f..aa4756a620aae5 100644 --- a/Modules/clinic/posixmodule.c.h +++ b/Modules/clinic/posixmodule.c.h @@ -6125,43 +6125,7 @@ os_putenv(PyObject *module, PyObject *const *args, Py_ssize_t nargs) #endif /* defined(HAVE_PUTENV) && !defined(MS_WINDOWS) */ -#if defined(MS_WINDOWS) - -PyDoc_STRVAR(os_unsetenv__doc__, -"unsetenv($module, name, /)\n" -"--\n" -"\n" -"Delete an environment variable."); - -#define OS_UNSETENV_METHODDEF \ - {"unsetenv", (PyCFunction)os_unsetenv, METH_O, os_unsetenv__doc__}, - -static PyObject * -os_unsetenv_impl(PyObject *module, PyObject *name); - -static PyObject * -os_unsetenv(PyObject *module, PyObject *arg) -{ - PyObject *return_value = NULL; - PyObject *name; - - if (!PyUnicode_Check(arg)) { - _PyArg_BadArgument("unsetenv", "argument", "str", arg); - goto exit; - } - if (PyUnicode_READY(arg) == -1) { - goto exit; - } - name = arg; - return_value = os_unsetenv_impl(module, name); - -exit: - return return_value; -} - -#endif /* defined(MS_WINDOWS) */ - -#if (defined(HAVE_UNSETENV) && !defined(MS_WINDOWS)) +#if defined(HAVE_UNSETENV) PyDoc_STRVAR(os_unsetenv__doc__, "unsetenv($module, name, /)\n" @@ -6193,7 +6157,7 @@ os_unsetenv(PyObject *module, PyObject *arg) return return_value; } -#endif /* (defined(HAVE_UNSETENV) && !defined(MS_WINDOWS)) */ +#endif /* defined(HAVE_UNSETENV) */ PyDoc_STRVAR(os_strerror__doc__, "strerror($module, code, /)\n" @@ -8809,4 +8773,4 @@ os__remove_dll_directory(PyObject *module, PyObject *const *args, Py_ssize_t nar #ifndef OS__REMOVE_DLL_DIRECTORY_METHODDEF #define OS__REMOVE_DLL_DIRECTORY_METHODDEF #endif /* !defined(OS__REMOVE_DLL_DIRECTORY_METHODDEF) */ -/*[clinic end generated code: output=6e739a2715712e88 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=51ba5b9536420cea input=a9049054013a1b77]*/ diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 5a0c8a311a790e..82becf67d3fc60 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -10180,51 +10180,7 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) #endif /* HAVE_PUTENV */ -#ifdef MS_WINDOWS -/*[clinic input] -os.unsetenv - name: unicode - / - -Delete an environment variable. -[clinic start generated code]*/ - -static PyObject * -os_unsetenv_impl(PyObject *module, PyObject *name) -/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/ -{ - /* PyUnicode_AsWideCharString() rejects embedded null characters */ - wchar_t *name_str = PyUnicode_AsWideCharString(name, NULL); - if (name_str == NULL) { - return NULL; - } - - BOOL ok = SetEnvironmentVariableW(name_str, NULL); - PyMem_Free(name_str); - - if (!ok) { - return PyErr_SetFromWindowsErr(0); - } - -#ifdef PY_PUTENV_DICT - /* Remove the key from putenv_dict; - * this will cause it to be collected. This has to - * happen after the real unsetenv() call because the - * old value was still accessible until then. - */ - if (PyDict_DelItem(_posixstate(module)->putenv_dict, name)) { - /* really not much we can do; just leak */ - if (!PyErr_ExceptionMatches(PyExc_KeyError)) { - return NULL; - } - PyErr_Clear(); - } -#endif - - Py_RETURN_NONE; -} -/* repeat !defined(MS_WINDOWS) to workaround an Argument Clinic issue */ -#elif defined(HAVE_UNSETENV) && !defined(MS_WINDOWS) +#ifdef HAVE_UNSETENV /*[clinic input] os.unsetenv name: FSConverter @@ -10237,14 +10193,13 @@ static PyObject * os_unsetenv_impl(PyObject *module, PyObject *name) /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ { -#ifndef HAVE_BROKEN_UNSETENV - int err; -#endif - + /* On Windows, this function is not implemented in C, but in os.py as + posix.putenv(name, ""). _wputenv() is preferred since it updates the + CRT, whereas SetEnvironmentVariableW(name, NULL) does not. */ #ifdef HAVE_BROKEN_UNSETENV unsetenv(PyBytes_AS_STRING(name)); #else - err = unsetenv(PyBytes_AS_STRING(name)); + int err = unsetenv(PyBytes_AS_STRING(name)); if (err) return posix_error(); #endif