FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-39413: Use _wputenv(), not SetEnvironmentVariableW() by vstinner · Pull Request #18107 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 14 additions & 2 deletions Lib/os.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,27 @@ 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")

try:
_unsetenv = unsetenv
except NameError:
_unsetenv = lambda key: _putenv(key, "")
if name == 'nt':

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This looks too complicated and obscure. Many lines are duplicated with small differences. Would not it be easier to implement nt.unsetenv() on C?

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")
Expand Down
42 changes: 3 additions & 39 deletions Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 5 additions & 50 deletions Modules/posixmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

Back | FazBrowse Home | New Git URL