[tor-commits] [stem/master] Accept Config.set() to allow unicode

atagar at torproject.org atagar at torproject.org
Sun Dec 6 21:57:12 UTC 2015


commit 08be7a955f8c0c3de1d7409a810d6225943fcc9f
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Nov 29 12:40:53 2015 -0800

    Accept Config.set() to allow unicode
    
    Oops, type checking didn't include unicode strings...
    
      Traceback (most recent call last):
        File "scrap.py", line 4, in <module>
          manual.save('/home/atagar/Desktop/output')
        File "/home/atagar/Desktop/stem/stem/manual.py", line 314, in save
          conf.set('name', self.name)
        File "/home/atagar/Desktop/stem/stem/util/conf.py", line 641, in set
          raise ValueError("Config.set() only accepts str, list, or tuple. Provided value was a '%s'" % type(value))
      ValueError: Config.set() only accepts str, list, or tuple. Provided value was a '<type 'unicode'>'
    
    Obviously something we want to allow.
---
 stem/util/conf.py |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/stem/util/conf.py b/stem/util/conf.py
index 92be685..22cf1c7 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -161,6 +161,8 @@ import inspect
 import os
 import threading
 
+import stem.prereq
+
 from stem.util import log
 
 try:
@@ -621,7 +623,9 @@ class Config(object):
     """
 
     with self._contents_lock:
-      if isinstance(value, str):
+      unicode_type = str if stem.prereq.is_python_3() else unicode
+
+      if isinstance(value, bytes) or isinstance(value, unicode_type):
         if not overwrite and key in self._contents:
           self._contents[key].append(value)
         else:
@@ -638,7 +642,7 @@ class Config(object):
         for listener in self._listeners:
           listener(self, key)
       else:
-        raise ValueError("Config.set() only accepts str, list, or tuple. Provided value was a '%s'" % type(value))
+        raise ValueError("Config.set() only accepts str (bytes or unicode), list, or tuple. Provided value was a '%s'" % type(value))
 
   def get(self, key, default = None):
     """





More information about the tor-commits mailing list