[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Remove config values when Config.set() is provided with a None value



commit 1248546bcdd6c1d9e6aa0e79dd1c29d89c8d6060
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Jan 17 15:05:56 2016 -0800

    Remove config values when Config.set() is provided with a None value
    
    Ran into a situation where I wanted to unset a config value. Intuitive way of
    doing this is to provide None to our set() method.
---
 docs/change_log.rst |    1 +
 stem/util/conf.py   |   10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 0640aef..1420ddf 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -74,6 +74,7 @@ The following are only available within Stem's `git repository
   * Added :func:`~stem.util.__init__.datetime_to_unix`
   * Basic IPv6 support in :func:`~stem.util.connection.get_connections`
   * The 'ss' connection resolver didn't work on Gentoo (:trac:`18079`)
+  * Allow :func:`stem.util.conf.Config.set` to remove values when provided with a **None** value
 
  * **Interpreter**
 
diff --git a/stem/util/conf.py b/stem/util/conf.py
index 8e6597f..18226d8 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -620,6 +620,9 @@ class Config(object):
     Appends the given key/value configuration mapping, behaving the same as if
     we'd loaded this from a configuration file.
 
+    .. versionchanged:: 1.5.0
+       Allow removal of values by overwriting with a **None** value.
+
     :param str key: key for the configuration mapping
     :param str,list value: value we're setting the mapping to
     :param bool overwrite: replaces the previous value if **True**, otherwise
@@ -629,7 +632,12 @@ class Config(object):
     with self._contents_lock:
       unicode_type = str if stem.prereq.is_python_3() else unicode
 
-      if isinstance(value, bytes) or isinstance(value, unicode_type):
+      if value is None:
+        if overwrite and key in self._contents:
+          del self._contents[key]
+        else:
+          pass  # no value so this is a no-op
+      elif isinstance(value, (bytes, unicode_type)):
         if not overwrite and key in self._contents:
           self._contents[key].append(value)
         else:



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits