commit 95c2187563cb194afa4aa4ec2d479614da91e291
Author: Foxboron <mcfoxax(a)gmail.com>
Date: Fri Jan 2 23:57:28 2015 +0100
fixed python2.6 compatability
---
stem/_compat.py | 8 ++++----
stem/control.py | 12 ++++++------
stem/descriptor/__init__.py | 6 +++---
stem/descriptor/reader.py | 4 ++--
stem/exit_policy.py | 10 +++++-----
stem/response/events.py | 20 ++++++++++----------
stem/util/connection.py | 4 ++--
stem/util/enum.py | 4 ++--
stem/util/str_tools.py | 10 +++++-----
stem/util/system.py | 4 ++--
test/unit/connection/connect.py | 5 ++++-
test/unit/tutorial_examples.py | 2 +-
12 files changed, 46 insertions(+), 43 deletions(-)
diff --git a/stem/_compat.py b/stem/_compat.py
index 0fb3bfc..e0ba489 100644
--- a/stem/_compat.py
+++ b/stem/_compat.py
@@ -6,11 +6,11 @@ PY33 = sys.version_info >= (3, 3)
PY34 = sys.version_info >= (3, 4)
if PY3:
- unicode = str
+ str_type = str
else:
- unicode = unicode # NOQA
+ str_type = unicode # NOQA
if PY3:
- long = int
+ int_type = int
else:
- long = long # NOQA
+ int_type = long # NOQA
diff --git a/stem/control.py b/stem/control.py
index 8986c21..993d5c3 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -258,7 +258,7 @@ import stem.version
from stem import UNDEFINED, CircStatus, Signal
from stem.util import log
-from stem._compat import unicode
+from stem._compat import str_type
# state changes a control socket can have
@@ -992,7 +992,7 @@ class Controller(BaseController):
start_time = time.time()
reply = {}
- if isinstance(params, (bytes, unicode)):
+ if isinstance(params, (bytes, str_type)):
is_multiple = False
params = set([params])
else:
@@ -1887,7 +1887,7 @@ class Controller(BaseController):
start_time = time.time()
reply = {}
- if isinstance(params, (bytes, unicode)):
+ if isinstance(params, (bytes, str_type)):
params = [params]
# remove strings which contain only whitespace
@@ -2077,7 +2077,7 @@ class Controller(BaseController):
for param, value in params:
param = param.lower()
- if isinstance(value, (bytes, unicode)):
+ if isinstance(value, (bytes, str_type)):
value = [value]
to_cache[param] = value
@@ -2635,7 +2635,7 @@ class Controller(BaseController):
* :class:`stem.InvalidArguments` if features passed were invalid
"""
- if isinstance(features, (bytes, unicode)):
+ if isinstance(features, (bytes, str_type)):
features = [features]
response = self.msg('USEFEATURE %s' % ' '.join(features))
@@ -2780,7 +2780,7 @@ class Controller(BaseController):
args = [circuit_id]
- if isinstance(path, (bytes, unicode)):
+ if isinstance(path, (bytes, str_type)):
path = [path]
if path:
diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 25a880e..6f4653f 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -58,7 +58,7 @@ import stem.prereq
import stem.util.enum
import stem.util.str_tools
import stem.util.system
-from stem._compat import unicode
+from stem._compat import str_type
try:
# added in python 2.7
@@ -151,7 +151,7 @@ def parse_file(descriptor_file, descriptor_type = None, validate = True, documen
handler = None
- if isinstance(descriptor_file, (bytes, unicode)):
+ if isinstance(descriptor_file, (bytes, str_type)):
if stem.util.system.is_tarfile(descriptor_file):
handler = _parse_file_for_tar_path
else:
@@ -433,7 +433,7 @@ def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_fi
ending_keyword = None
- if isinstance(keywords, (bytes, unicode)):
+ if isinstance(keywords, (bytes, str_type)):
keywords = (keywords,)
if ignore_first:
diff --git a/stem/descriptor/reader.py b/stem/descriptor/reader.py
index bbd6c6c..0022d89 100644
--- a/stem/descriptor/reader.py
+++ b/stem/descriptor/reader.py
@@ -90,7 +90,7 @@ except ImportError:
import stem.descriptor
import stem.prereq
import stem.util.system
-from stem._compat import unicode
+from stem._compat import str_type
# flag to indicate when the reader thread is out of descriptor files to read
FINISHED = 'DONE'
@@ -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, **kwargs):
- if isinstance(target, (bytes, unicode)):
+ if isinstance(target, (bytes, str_type)):
self._targets = [target]
else:
self._targets = target
diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index 7ac3b8b..06b2b53 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -75,7 +75,7 @@ import stem.util.connection
import stem.util.enum
import stem.util.str_tools
-from stem._compat import unicode
+from stem._compat import str_type
try:
# added in python 3.2
@@ -121,7 +121,7 @@ def get_config_policy(rules, ip_address = None):
if ip_address and not (stem.util.connection.is_valid_ipv4_address(ip_address) or stem.util.connection.is_valid_ipv6_address(ip_address)):
raise ValueError("%s isn't a valid IP address" % ip_address)
- if isinstance(rules, (bytes, unicode)):
+ if isinstance(rules, (bytes, str_type)):
rules = rules.split(',')
result = []
@@ -238,7 +238,7 @@ class ExitPolicy(object):
# sanity check the types
for rule in rules:
- if not isinstance(rule, (bytes, unicode, ExitPolicyRule)):
+ if not isinstance(rule, (bytes, str_type, ExitPolicyRule)):
raise TypeError('Exit policy rules can only contain strings or ExitPolicyRules, got a %s (%s)' % (type(rule), rules))
# Unparsed representation of the rules we were constructed with. Our
@@ -249,7 +249,7 @@ class ExitPolicy(object):
is_all_str = True
for rule in rules:
- if not isinstance(rule, (bytes, unicode)):
+ if not isinstance(rule, (bytes, str_type)):
is_all_str = False
if rules and is_all_str:
@@ -458,7 +458,7 @@ class ExitPolicy(object):
if isinstance(rule, bytes):
rule = stem.util.str_tools._to_unicode(rule)
- if isinstance(rule, unicode):
+ if isinstance(rule, str_type):
rule = ExitPolicyRule(rule.strip())
if rule.is_accept:
diff --git a/stem/response/events.py b/stem/response/events.py
index 01a7b26..55c73e3 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -12,7 +12,7 @@ import stem.response
import stem.version
from stem.util import connection, log, str_tools, tor_tools
-from stem._compat import unicode, long
+from stem._compat import str_type, int_type
# Matches keyword=value arguments. This can't be a simple "(.*)=(.*)" pattern
# because some positional arguments, like circuit paths, can have an equal
@@ -142,7 +142,7 @@ class Event(stem.response.ControlMessage):
attr_values = getattr(self, attr)
if attr_values:
- if isinstance(attr_values, (bytes, unicode)):
+ if isinstance(attr_values, (bytes, str_type)):
attr_values = [attr_values]
for value in attr_values:
@@ -256,8 +256,8 @@ class BandwidthEvent(Event):
elif not self.read.isdigit() or not self.written.isdigit():
raise stem.ProtocolError("A BW event's bytes sent and received should be a positive numeric value, received: %s" % self)
- self.read = long(self.read)
- self.written = long(self.written)
+ self.read = int_type(self.read)
+ self.written = int_type(self.written)
class BuildTimeoutSetEvent(Event):
@@ -977,8 +977,8 @@ class StreamBwEvent(Event):
elif not self.read.isdigit() or not self.written.isdigit():
raise stem.ProtocolError("A STREAM_BW event's bytes sent and received should be a positive numeric value, received: %s" % self)
- self.read = long(self.read)
- self.written = long(self.written)
+ self.read = int_type(self.read)
+ self.written = int_type(self.written)
class TransportLaunchedEvent(Event):
@@ -1051,8 +1051,8 @@ class ConnectionBandwidthEvent(Event):
elif not tor_tools.is_valid_connection_id(self.id):
raise stem.ProtocolError("Connection IDs must be one to sixteen alphanumeric characters, got '%s': %s" % (self.id, self))
- self.read = long(self.read)
- self.written = long(self.written)
+ self.read = int_type(self.read)
+ self.written = int_type(self.written)
self._log_if_unrecognized('type', stem.ConnectionType)
@@ -1091,8 +1091,8 @@ class CircuitBandwidthEvent(Event):
elif not tor_tools.is_valid_circuit_id(self.id):
raise stem.ProtocolError("Circuit IDs must be one to sixteen alphanumeric characters, got '%s': %s" % (self.id, self))
- self.read = long(self.read)
- self.written = long(self.written)
+ self.read = int_type(self.read)
+ self.written = int_type(self.written)
class CellStatsEvent(Event):
diff --git a/stem/util/connection.py b/stem/util/connection.py
index 276f97c..ded09b3 100644
--- a/stem/util/connection.py
+++ b/stem/util/connection.py
@@ -51,7 +51,7 @@ import stem.util.proc
import stem.util.system
from stem.util import conf, enum, log
-from stem._compat import unicode
+from stem._compat import str_type
# Connection resolution is risky to log about since it's highly likely to
# contain sensitive information. That said, it's also difficult to get right in
@@ -337,7 +337,7 @@ def is_valid_ipv4_address(address):
:returns: **True** if input is a valid IPv4 address, **False** otherwise
"""
- if not isinstance(address, (bytes, unicode)):
+ if not isinstance(address, (bytes, str_type)):
return False
# checks if theres four period separated values
diff --git a/stem/util/enum.py b/stem/util/enum.py
index 7520bda..d641789 100644
--- a/stem/util/enum.py
+++ b/stem/util/enum.py
@@ -40,7 +40,7 @@ constructed as simple type listings...
+- __iter__ - iterator over our enum keys
"""
-from stem._compat import unicode
+from stem._compat import str_type
def UppercaseEnum(*args):
@@ -76,7 +76,7 @@ class Enum(object):
keys, values = [], []
for entry in args:
- if isinstance(entry, (bytes, unicode)):
+ if isinstance(entry, (bytes, str_type)):
key, val = entry, _to_camel_case(entry)
elif isinstance(entry, tuple) and len(entry) == 2:
key, val = entry
diff --git a/stem/util/str_tools.py b/stem/util/str_tools.py
index 3556a33..63b66b0 100644
--- a/stem/util/str_tools.py
+++ b/stem/util/str_tools.py
@@ -28,7 +28,7 @@ import sys
import stem.prereq
import stem.util.enum
-from stem._compat import unicode
+from stem._compat import str_type
# label conversion tuples of the form...
@@ -74,13 +74,13 @@ if stem.prereq.is_python_3():
return msg
else:
def _to_bytes_impl(msg):
- if msg is not None and isinstance(msg, unicode):
+ if msg is not None and isinstance(msg, str_type):
return codecs.latin_1_encode(msg, 'replace')[0]
else:
return msg
def _to_unicode_impl(msg):
- if msg is not None and not isinstance(msg, unicode):
+ if msg is not None and not isinstance(msg, str_type):
return msg.decode('utf-8', 'replace')
else:
return msg
@@ -455,7 +455,7 @@ def _parse_timestamp(entry):
:raises: **ValueError** if the timestamp is malformed
"""
- if not isinstance(entry, (str, unicode)):
+ if not isinstance(entry, (str, str_type)):
raise ValueError('parse_timestamp() input must be a str, got a %s' % type(entry))
try:
@@ -481,7 +481,7 @@ def _parse_iso_timestamp(entry):
:raises: **ValueError** if the timestamp is malformed
"""
- if not isinstance(entry, (str, unicode)):
+ if not isinstance(entry, (str, str_type)):
raise ValueError('parse_iso_timestamp() input must be a str, got a %s' % type(entry))
# based after suggestions from...
diff --git a/stem/util/system.py b/stem/util/system.py
index ec33c2b..315c39e 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -56,7 +56,7 @@ import stem.util.str_tools
from stem import UNDEFINED
from stem.util import log
-from stem._compat import unicode
+from stem._compat import str_type
# Mapping of commands to if they're available or not.
@@ -243,7 +243,7 @@ def is_running(command):
command_listing = call(secondary_resolver, None)
if command_listing:
- command_listing = list(map(unicode.strip, command_listing))
+ command_listing = list(map(str_type.strip, command_listing))
return command in command_listing
return None
diff --git a/test/unit/connection/connect.py b/test/unit/connection/connect.py
index b342eb7..37ded2f 100644
--- a/test/unit/connection/connect.py
+++ b/test/unit/connection/connect.py
@@ -9,7 +9,10 @@ try:
except ImportError:
from io import StringIO
-from mock import Mock, patch
+try:
+ from mock import Mock, patch
+except ImportError:
+ from unittest.mock import Mock, patch
import stem
import stem.connection
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
index b277192..618844d 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -280,7 +280,7 @@ class TestTutorialExamples(unittest.TestCase):
# Query all authority votes asynchronously.
downloader = remote.DescriptorDownloader(document_handler = DocumentHandler.DOCUMENT)
- queries = collections.OrderedDict() # needed so output's order matches what's expected
+ queries = {}
for name, authority in list(remote.get_authorities().items()):
if authority.v3ident is None: