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

bpo-39395: Clear env vars set by Python at exit by vstinner · Pull Request #18078 · 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) .rst  (1) All 2 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
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
@@ -0,0 +1,2 @@
Environment variables set by :data:`os.environ` and :func:`os.putenv` are now
cleared at exit. Python manages their memory which is released at exit.
26 changes: 26 additions & 0 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 @@ -2101,10 +2101,36 @@ statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject*)result;
}


#ifdef HAVE_UNSETENV
/* bpo-39395: Clear environment variables set by os.environ and os.putenv()
before clearing posix_putenv_garbage dictionary. Python manages the
variables memory which is cleared at exit. */
static void
posix_unset_envvars(PyObject *posix_putenv_garbage)
{
if (posix_putenv_garbage == NULL) {
return;
}
assert(PyDict_CheckExact(posix_putenv_garbage));

Py_ssize_t pos = 0;
PyObject *key, *value;
while (PyDict_Next(posix_putenv_garbage, &pos, &key, &value)) {
/* ignore unsetenv() error */
unsetenv(PyBytes_AS_STRING(key));
}
}
#endif


static int
_posix_clear(PyObject *module)
{
Py_CLEAR(_posixstate(module)->billion);
#ifdef HAVE_UNSETENV
posix_unset_envvars(_posixstate(module)->posix_putenv_garbage);
#endif
Py_CLEAR(_posixstate(module)->posix_putenv_garbage);

eduardo-elizondo Jan 20, 2020 •
edited
Loading

Copy link
Copy Markdown
Contributor

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

Given that you are already doing these fixes around this, posix_putenv_garbage should also be guarded by HAVE_PUTENV (check its initialization in the init function).

Py_CLEAR(_posixstate(module)->DirEntryType);
Py_CLEAR(_posixstate(module)->ScandirIteratorType);
Expand Down

Back | FazBrowse Home | New Git URL