commit 88c3b1a6ae4152f1724fcc7267b7a5b69aae5a74 Author: Damian Johnson atagar@torproject.org Date: Sat Mar 23 15:06:08 2013 -0700
Changing isinstance() checks to look for both bytes and unicode
Commit b12e9c0 broke a couple of our integration tests because it caused us to call controller methods with unicode rather than str arguments...
https://trac.torproject.org/8576
This should be fine - python 2.x can usually use bytes and unicode inputs interchangeably. The problem was that we routinely use isinstance() checks to see if an input is a string and, if it isn't, assume it's a collection. As a result we were calling...
GETINFO m d / n a m e / f o o
... rather than...
GETINFO md/name/foo
Tor in turn errored saying that those single character getinfo options didn't exist. To fix this I'm changing most of our isinstance() checks to look for both bytes and unicode. --- stem/control.py | 10 +++++----- stem/descriptor/__init__.py | 2 +- stem/descriptor/reader.py | 2 +- stem/exit_policy.py | 6 +++--- stem/response/events.py | 2 +- stem/util/connection.py | 4 ++-- stem/util/enum.py | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/stem/control.py b/stem/control.py index 4d91ecd..02072d0 100644 --- a/stem/control.py +++ b/stem/control.py @@ -737,7 +737,7 @@ class Controller(BaseController): start_time = time.time() reply = {}
- if isinstance(params, str): + if isinstance(params, (bytes, unicode)): is_multiple = False params = set([params]) else: @@ -1299,7 +1299,7 @@ class Controller(BaseController): start_time = time.time() reply = {}
- if isinstance(params, str): + if isinstance(params, (bytes, unicode)): params = [params]
# remove strings which contain only whitespace @@ -1483,7 +1483,7 @@ class Controller(BaseController): if value is None: if cache_key in self._request_cache: del self._request_cache[cache_key] - elif isinstance(value, str): + elif isinstance(value, (bytes, unicode)): self._request_cache[cache_key] = [value] else: self._request_cache[cache_key] = value @@ -1697,7 +1697,7 @@ class Controller(BaseController): * :class:`stem.InvalidArguments` if features passed were invalid """
- if isinstance(features, str): + if isinstance(features, (bytes, unicode)): features = [features]
response = self.msg("USEFEATURE %s" % " ".join(features)) @@ -1848,7 +1848,7 @@ class Controller(BaseController):
args = [circuit_id]
- if isinstance(path, str): + if isinstance(path, (bytes, unicode)): path = [path]
if path: diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py index 04ee7dc..501ac5d 100644 --- a/stem/descriptor/__init__.py +++ b/stem/descriptor/__init__.py @@ -365,7 +365,7 @@ def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_fi content = None if skip else [] ending_keyword = None
- if isinstance(keywords, str): + if isinstance(keywords, (bytes, unicode)): keywords = (keywords,)
if ignore_first: diff --git a/stem/descriptor/reader.py b/stem/descriptor/reader.py index 4fb6ff4..01a2a3b 100644 --- a/stem/descriptor/reader.py +++ b/stem/descriptor/reader.py @@ -263,7 +263,7 @@ class DescriptorReader(object): """
def __init__(self, target, validate = True, follow_links = False, buffer_size = 100, persistence_path = None, document_handler = stem.descriptor.DocumentHandler.ENTRIES): - if isinstance(target, str): + if isinstance(target, (bytes, unicode)): self._targets = [target] else: self._targets = target diff --git a/stem/exit_policy.py b/stem/exit_policy.py index 48dc28a..d187d62 100644 --- a/stem/exit_policy.py +++ b/stem/exit_policy.py @@ -108,7 +108,7 @@ def get_config_policy(rules): :raises: **ValueError** if input isn't a valid tor exit policy """
- if isinstance(rules, (str, unicode)): + if isinstance(rules, (bytes, unicode)): rules = rules.split(',')
result = [] @@ -147,7 +147,7 @@ class ExitPolicy(object): def __init__(self, *rules): # sanity check the types for rule in rules: - if not isinstance(rule, (str, unicode, ExitPolicyRule)): + if not isinstance(rule, (bytes, unicode, ExitPolicyRule)): raise TypeError("Exit policy rules can only contain strings or ExitPolicyRules, got a %s (%s)" % (type(rule), rules))
self._rules = None # lazily loaded series of ExitPolicyRule @@ -304,7 +304,7 @@ class ExitPolicy(object): is_all_accept, is_all_reject = True, True
for rule in self._input_rules: - if isinstance(rule, (str, unicode)): + if isinstance(rule, (bytes, unicode)): rule = ExitPolicyRule(rule.strip())
if rule.is_accept: diff --git a/stem/response/events.py b/stem/response/events.py index 4c44e48..7ae4854 100644 --- a/stem/response/events.py +++ b/stem/response/events.py @@ -142,7 +142,7 @@ class Event(stem.response.ControlMessage): attr_values = getattr(self, attr)
if attr_values: - if isinstance(attr_values, str): + if isinstance(attr_values, (bytes, unicode)): attr_values = [attr_values]
for value in attr_values: diff --git a/stem/util/connection.py b/stem/util/connection.py index b1a292d..fd0ac6e 100644 --- a/stem/util/connection.py +++ b/stem/util/connection.py @@ -37,7 +37,7 @@ def is_valid_ipv4_address(address): :returns: **True** if input is a valid IPv4 address, **False** otherwise """
- if not isinstance(address, (str, unicode)): + if not isinstance(address, (bytes, unicode)): return False
# checks if theres four period separated values @@ -105,7 +105,7 @@ def is_valid_port(entry, allow_zero = False): return False
return True - elif isinstance(entry, (str, unicode)): + elif isinstance(entry, (bytes, unicode)): if not entry.isdigit(): return False elif entry[0] == "0" and len(entry) > 1: diff --git a/stem/util/enum.py b/stem/util/enum.py index 504af48..b39f675 100644 --- a/stem/util/enum.py +++ b/stem/util/enum.py @@ -74,7 +74,7 @@ class Enum(object): keys, values = [], []
for entry in args: - if isinstance(entry, str): + if isinstance(entry, (bytes, unicode)): key, val = entry, stem.util.str_tools._to_camel_case(entry) elif isinstance(entry, tuple) and len(entry) == 2: key, val = entry
tor-commits@lists.torproject.org