From 4a21268a749ee7461a4b744ddaa1fbcb0f22e7d5 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Thu, 20 Sep 2007 17:57:59 +0000 Subject: Patch #1181: add os.environ.clear() method. --- Doc/library/os.rst | 8 ++++++-- Lib/os.py | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 7f03a75..24830d2 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -115,9 +115,13 @@ process and user. passed to the appropriate process-creation functions to cause child processes to use a modified environment. - If the platform supports the :func:`unsetenv` function, you can delete items in + If the platform supports the :func:`unsetenv` function, you can delete items in this mapping to unset environment variables. :func:`unsetenv` will be called - automatically when an item is deleted from ``os.environ``. + automatically when an item is deleted from ``os.environ``, and when + :meth:`os.environ.clear` is called. + + .. versionchanged:: 2.6 + Also unset environment variables when calling :meth:`os.environ.clear`. .. function:: chdir(path) diff --git a/Lib/os.py b/Lib/os.py index 206aa37..7d21763 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -446,6 +446,10 @@ else: def __delitem__(self, key): unsetenv(key) del self.data[key.upper()] + def clear(self): + for key in self.data.keys(): + unsetenv(key) + del self.data[key] def has_key(self, key): return key.upper() in self.data def __contains__(self, key): @@ -503,6 +507,10 @@ else: def __delitem__(self, key): unsetenv(key) del self.data[key] + def clear(self): + for key in self.data.keys(): + unsetenv(key) + del self.data[key] def copy(self): return dict(self) -- cgit v0.12