[tor-commits] [stem/master] Drop stem.util.str_type

atagar at torproject.org atagar at torproject.org
Mon May 14 00:44:57 UTC 2018


commit ec796c735b826dcce865ec2d1b109e0ce84fd365
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun May 13 17:28:24 2018 -0700

    Drop stem.util.str_type
    
    Our str_type constant was used for a couple things...
    
      * Throughout our library it was pretty much exclusively a cross-version 'is
        this a string?' check. Replacing these with an is_str() function instead.
    
      * In our tests it was used to create test data. Interestingly it seems we
        actually do just fine without this.
---
 stem/client/cell.py                               |  9 ++---
 stem/client/datatype.py                           |  9 ++---
 stem/control.py                                   | 13 +++----
 stem/descriptor/__init__.py                       |  7 ++--
 stem/descriptor/certificate.py                    |  5 ++-
 stem/descriptor/reader.py                         | 13 +++----
 stem/descriptor/server_descriptor.py              |  3 +-
 stem/exit_policy.py                               | 15 ++++-----
 stem/response/events.py                           |  5 +--
 stem/util/__init__.py                             | 39 ++++++++++++++++++---
 stem/util/connection.py                           |  7 ++--
 stem/util/enum.py                                 |  4 +--
 stem/util/str_tools.py                            | 11 +++---
 stem/util/system.py                               |  5 +--
 test/settings.cfg                                 |  8 +++--
 test/unit/descriptor/microdescriptor.py           |  5 ++-
 test/unit/descriptor/networkstatus/document_v3.py | 41 +++++++++++------------
 test/unit/descriptor/reader.py                    | 37 ++++++++++----------
 test/unit/descriptor/server_descriptor.py         |  7 ++--
 test/unit/tutorial_examples.py                    | 13 ++++---
 test/unit/util/system.py                          |  5 ++-
 21 files changed, 143 insertions(+), 118 deletions(-)

diff --git a/stem/client/cell.py b/stem/client/cell.py
index 7f801fd6..b40a737f 100644
--- a/stem/client/cell.py
+++ b/stem/client/cell.py
@@ -40,14 +40,15 @@ Messages communicated over a Tor relay's ORPort.
 import datetime
 import inspect
 import io
-import numbers
 import os
 import random
 import sys
 
+import stem.util
+
 from stem import UNDEFINED
 from stem.client.datatype import HASH_LEN, ZERO, Address, Certificate, CloseReason, RelayCommand, Size, split
-from stem.util import _hash_attr, datetime_to_unix, str_type, str_tools
+from stem.util import _hash_attr, datetime_to_unix, str_tools
 
 FIXED_PAYLOAD_LEN = 509
 AUTH_CHALLENGE_SIZE = 32
@@ -308,9 +309,9 @@ class RelayCell(CircuitCell):
       # isinstance() isn't such a great option.
 
       digest = Size.LONG.unpack(digest.digest()[:4])
-    elif isinstance(digest, (bytes, str_type)):
+    elif stem.util._is_str(digest):
       digest = Size.LONG.unpack(digest[:4])
-    elif isinstance(digest, numbers.Integral):
+    elif stem.util._is_int(digest):
       pass
     else:
       raise ValueError('RELAY cell digest must be a hash, string, or int but was a %s' % type(digest).__name__)
diff --git a/stem/client/datatype.py b/stem/client/datatype.py
index 9abea36b..038229d2 100644
--- a/stem/client/datatype.py
+++ b/stem/client/datatype.py
@@ -113,10 +113,10 @@ users.** See our :class:`~stem.client.Relay` the API you probably want.
 import collections
 import hashlib
 import io
-import numbers
 import struct
 
 import stem.prereq
+import stem.util
 import stem.util.connection
 import stem.util.enum
 
@@ -159,10 +159,7 @@ class _IntegerEnum(stem.util.enum.Enum):
     Privides the (enum, int_value) tuple for a given value.
     """
 
-    # TODO: when we drop python 2.x support all "isinstance(val,
-    # numbers.Integral)" checks should become "isinstance(val, int)"
-
-    if isinstance(val, numbers.Integral):
+    if stem.util._is_int(val):
       return self._int_to_enum.get(val, self.UNKNOWN), val
     elif val in self:
       return val, self._enum_to_int.get(val, val)
@@ -314,7 +311,7 @@ class Size(Field):
     raise NotImplementedError("Use our constant's unpack() and pop() instead")
 
   def pack(self, content):
-    if not isinstance(content, numbers.Integral):
+    if not stem.util._is_int(content):
       raise ValueError('Size.pack encodes an integer, but was a %s' % type(content).__name__)
 
     packed = struct.pack(self.format, content)
diff --git a/stem/control.py b/stem/control.py
index 244aed0b..d10989af 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -269,6 +269,7 @@ import stem.exit_policy
 import stem.response
 import stem.response.events
 import stem.socket
+import stem.util
 import stem.util.conf
 import stem.util.connection
 import stem.util.enum
@@ -278,7 +279,7 @@ import stem.util.tor_tools
 import stem.version
 
 from stem import UNDEFINED, CircStatus, Signal
-from stem.util import str_type, log
+from stem.util import log
 
 # When closing the controller we attempt to finish processing enqueued events,
 # but if it takes longer than this we terminate.
@@ -1141,7 +1142,7 @@ class Controller(BaseController):
     start_time = time.time()
     reply = {}
 
-    if isinstance(params, (bytes, str_type)):
+    if stem.util._is_str(params):
       is_multiple = False
       params = set([params])
     else:
@@ -2191,7 +2192,7 @@ class Controller(BaseController):
     start_time = time.time()
     reply = {}
 
-    if isinstance(params, (bytes, str_type)):
+    if stem.util._is_str(params):
       params = [params]
 
     # remove strings which contain only whitespace
@@ -2433,7 +2434,7 @@ class Controller(BaseController):
         for param, value in params:
           param = param.lower()
 
-          if isinstance(value, (bytes, str_type)):
+          if stem.util._is_str(value):
             value = [value]
 
           to_cache[param] = value
@@ -3331,7 +3332,7 @@ class Controller(BaseController):
       * :class:`stem.InvalidArguments` if features passed were invalid
     """
 
-    if isinstance(features, (bytes, str_type)):
+    if stem.util._is_str(features):
       features = [features]
 
     response = self.msg('USEFEATURE %s' % ' '.join(features))
@@ -3486,7 +3487,7 @@ class Controller(BaseController):
 
       args = [circuit_id]
 
-      if isinstance(path, (bytes, str_type)):
+      if stem.util._is_str(path):
         path = [path]
 
       if path:
diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index d56b38b8..6a54ef29 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -68,12 +68,11 @@ import string
 import tarfile
 
 import stem.prereq
+import stem.util
 import stem.util.enum
 import stem.util.str_tools
 import stem.util.system
 
-from stem.util import str_type
-
 try:
   # added in python 2.7
   from collections import OrderedDict
@@ -205,7 +204,7 @@ def parse_file(descriptor_file, descriptor_type = None, validate = False, docume
 
   handler = None
 
-  if isinstance(descriptor_file, (bytes, str_type)):
+  if stem.util._is_str(descriptor_file):
     if stem.util.system.is_tarfile(descriptor_file):
       handler = _parse_file_for_tar_path
     else:
@@ -896,7 +895,7 @@ def _read_until_keywords(keywords, descriptor_file, inclusive = False, ignore_fi
   content = None if skip else []
   ending_keyword = None
 
-  if isinstance(keywords, (bytes, str_type)):
+  if stem.util._is_str(keywords):
     keywords = (keywords,)
 
   if ignore_first:
diff --git a/stem/descriptor/certificate.py b/stem/descriptor/certificate.py
index 3cdf4163..0c4796af 100644
--- a/stem/descriptor/certificate.py
+++ b/stem/descriptor/certificate.py
@@ -162,7 +162,10 @@ class Ed25519CertificateV1(Ed25519Certificate):
       raise ValueError("BUG: Ed25519 certificate type is decoded from one byte. It shouldn't be possible to have a value of %i." % cert_type)
 
     # expiration time is in hours since epoch
-    self.expiration = datetime.datetime.utcfromtimestamp(stem.util.str_tools._to_int(decoded[2:6]) * 3600)
+    try:
+      self.expiration = datetime.datetime.utcfromtimestamp(stem.util.str_tools._to_int(decoded[2:6]) * 3600)
+    except ValueError as exc:
+      raise ValueError('Invalid expiration timestamp (%s): %s' % (exc, stem.util.str_tools._to_int(decoded[2:6]) * 3600))
 
     self.key_type = stem.util.str_tools._to_int(decoded[6:7])
     self.key = decoded[7:39]
diff --git a/stem/descriptor/reader.py b/stem/descriptor/reader.py
index 1c07cdae..0f8547d7 100644
--- a/stem/descriptor/reader.py
+++ b/stem/descriptor/reader.py
@@ -89,10 +89,10 @@ except ImportError:
 
 import stem.descriptor
 import stem.prereq
+import stem.util
+import stem.util.str_tools
 import stem.util.system
 
-from stem.util import str_type
-
 # flag to indicate when the reader thread is out of descriptor files to read
 FINISHED = 'DONE'
 
@@ -179,9 +179,9 @@ def load_processed_files(path):
 
   processed_files = {}
 
-  with open(path) as input_file:
+  with open(path, 'rb') as input_file:
     for line in input_file.readlines():
-      line = line.strip()
+      line = stem.util.str_tools._to_unicode(line.strip())
 
       if not line:
         continue  # skip blank lines
@@ -265,10 +265,7 @@ class DescriptorReader(object):
   """
 
   def __init__(self, target, validate = False, follow_links = False, buffer_size = 100, persistence_path = None, document_handler = stem.descriptor.DocumentHandler.ENTRIES, **kwargs):
-    if isinstance(target, (bytes, str_type)):
-      self._targets = [target]
-    else:
-      self._targets = target
+    self._targets = [target] if stem.util._is_str(target) else target
 
     # expand any relative paths we got
 
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 63a3d069..afbec8e6 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -64,7 +64,6 @@ import stem.util.tor_tools
 import stem.version
 
 from stem.descriptor.router_status_entry import RouterStatusEntryV3
-from stem.util import str_type
 
 from stem.descriptor import (
   PGP_BLOCK_END,
@@ -412,7 +411,7 @@ def _parse_history_line(keyword, history_end_attribute, history_interval_attribu
 
 def _parse_exit_policy(descriptor, entries):
   if hasattr(descriptor, '_unparsed_exit_policy'):
-    if descriptor._unparsed_exit_policy == [str_type('reject *:*')]:
+    if descriptor._unparsed_exit_policy and stem.util.str_tools._to_unicode(descriptor._unparsed_exit_policy[0]) == 'reject *:*':
       descriptor.exit_policy = REJECT_ALL_POLICY
     else:
       descriptor.exit_policy = stem.exit_policy.ExitPolicy(*descriptor._unparsed_exit_policy)
diff --git a/stem/exit_policy.py b/stem/exit_policy.py
index e921fb3c..27475858 100644
--- a/stem/exit_policy.py
+++ b/stem/exit_policy.py
@@ -72,12 +72,11 @@ import socket
 import zlib
 
 import stem.prereq
+import stem.util
 import stem.util.connection
 import stem.util.enum
 import stem.util.str_tools
 
-from stem.util import _hash_attr, str_type
-
 try:
   # added in python 3.2
   from functools import lru_cache
@@ -130,7 +129,7 @@ def get_config_policy(rules, ip_address = None):
   elif ip_address and stem.util.connection.is_valid_ipv6_address(ip_address, allow_brackets = True) and not (ip_address[0] == '[' and ip_address[-1] == ']'):
     ip_address = '[%s]' % ip_address  # ExitPolicy validation expects IPv6 addresses to be bracketed
 
-  if isinstance(rules, (bytes, str_type)):
+  if stem.util._is_str(rules):
     rules = rules.split(',')
 
   result = []
@@ -244,7 +243,7 @@ class ExitPolicy(object):
     # sanity check the types
 
     for rule in rules:
-      if not isinstance(rule, (bytes, str_type, ExitPolicyRule)):
+      if not stem.util._is_str(rule) and not isinstance(rule, 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
@@ -255,7 +254,7 @@ class ExitPolicy(object):
     is_all_str = True
 
     for rule in rules:
-      if not isinstance(rule, (bytes, str_type)):
+      if not stem.util._is_str(rule):
         is_all_str = False
 
     if rules and is_all_str:
@@ -467,7 +466,7 @@ class ExitPolicy(object):
         if isinstance(rule, bytes):
           rule = stem.util.str_tools._to_unicode(rule)
 
-        if isinstance(rule, str_type):
+        if stem.util._is_str(rule):
           if not rule.strip():
             continue
 
@@ -1028,7 +1027,7 @@ class ExitPolicyRule(object):
 
   def __hash__(self):
     if self._hash is None:
-      self._hash = _hash_attr(self, 'is_accept', 'address', 'min_port', 'max_port') * 1024 + hash(self.get_mask(False))
+      self._hash = stem.util._hash_attr(self, 'is_accept', 'address', 'min_port', 'max_port') * 1024 + hash(self.get_mask(False))
 
     return self._hash
 
@@ -1074,7 +1073,7 @@ class MicroExitPolicyRule(ExitPolicyRule):
 
   def __hash__(self):
     if self._hash is None:
-      self._hash = _hash_attr(self, 'is_accept', 'min_port', 'max_port')
+      self._hash = stem.util._hash_attr(self, 'is_accept', 'min_port', 'max_port')
 
     return self._hash
 
diff --git a/stem/response/events.py b/stem/response/events.py
index cadbb0dc..de52c08f 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -10,9 +10,10 @@ import stem.control
 import stem.descriptor.router_status_entry
 import stem.prereq
 import stem.response
+import stem.util
 import stem.version
 
-from stem.util import str_type, connection, log, str_tools, tor_tools
+from stem.util import connection, log, str_tools, tor_tools
 
 # Matches keyword=value arguments. This can't be a simple "(.*)=(.*)" pattern
 # because some positional arguments, like circuit paths, can have an equal
@@ -166,7 +167,7 @@ class Event(stem.response.ControlMessage):
     attr_values = getattr(self, attr)
 
     if attr_values:
-      if isinstance(attr_values, (bytes, str_type)):
+      if stem.util._is_str(attr_values):
         attr_values = [attr_values]
 
       for value in attr_values:
diff --git a/stem/util/__init__.py b/stem/util/__init__.py
index d022af85..c2b4999f 100644
--- a/stem/util/__init__.py
+++ b/stem/util/__init__.py
@@ -24,11 +24,6 @@ __all__ = [
   'datetime_to_unix',
 ]
 
-if stem.prereq.is_python_3():
-  str_type = str
-else:
-  str_type = unicode
-
 # Python hashes booleans to zero or one. Usually this would be fine, but since
 # we use hashes for equality checks we need them to be something less common.
 
@@ -36,6 +31,40 @@ TRUE_HASH_VALUE = 4813749
 FALSE_HASH_VALUE = 5826450
 
 
+def _is_str(val):
+  """
+  Check if a value is a string. This will be removed when we no longer provide
+  backward compatibility for the Python 2.x series.
+
+  :param object val: value to be checked
+
+  :returns: **True** if the value is some form of string (unicode or bytes),
+    and **False** otherwise
+  """
+
+  if stem.prereq.is_python_3():
+    return isinstance(val, (bytes, str))
+  else:
+    return isinstance(val, (bytes, unicode))
+
+
+def _is_int(val):
+  """
+  Check if a value is an integer. This will be removed when we no longer
+  provide backward compatibility for the Python 2.x series.
+
+  :param object val: value to be checked
+
+  :returns: **True** if the value is some form of integer (int or long),
+    and **False** otherwise
+  """
+
+  if stem.prereq.is_python_3():
+    return isinstance(val, int)
+  else:
+    return isinstance(val, (int, long))
+
+
 def datetime_to_unix(timestamp):
   """
   Converts a utc datetime object to a unix timestamp.
diff --git a/stem/util/connection.py b/stem/util/connection.py
index 74fbdde2..4ac8b2ed 100644
--- a/stem/util/connection.py
+++ b/stem/util/connection.py
@@ -61,10 +61,11 @@ import os
 import platform
 import re
 
+import stem.util
 import stem.util.proc
 import stem.util.system
 
-from stem.util import str_type, conf, enum, log, str_tools
+from stem.util import conf, enum, log, str_tools
 
 # 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
@@ -409,7 +410,7 @@ def is_valid_ipv4_address(address):
 
   if isinstance(address, bytes):
     address = str_tools._to_unicode(address)
-  elif not isinstance(address, str_type):
+  elif not stem.util._is_str(address):
     return False
 
   # checks if theres four period separated values
@@ -439,7 +440,7 @@ def is_valid_ipv6_address(address, allow_brackets = False):
 
   if isinstance(address, bytes):
     address = str_tools._to_unicode(address)
-  elif not isinstance(address, str_type):
+  elif not stem.util._is_str(address):
     return False
 
   if allow_brackets:
diff --git a/stem/util/enum.py b/stem/util/enum.py
index 7b628d68..00835ca5 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.util import str_type
+import stem.util
 
 
 def UppercaseEnum(*args):
@@ -76,7 +76,7 @@ class Enum(object):
     keys, values = [], []
 
     for entry in args:
-      if isinstance(entry, (bytes, str_type)):
+      if stem.util._is_str(entry):
         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 5066a5c8..0f31fb22 100644
--- a/stem/util/str_tools.py
+++ b/stem/util/str_tools.py
@@ -27,10 +27,9 @@ import re
 import sys
 
 import stem.prereq
+import stem.util
 import stem.util.enum
 
-from stem.util import str_type
-
 # label conversion tuples of the form...
 # (bits / bytes / seconds, short label, long label)
 
@@ -75,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, str_type):
+    if msg is not None and isinstance(msg, unicode):
       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, str_type):
+    if msg is not None and not isinstance(msg, unicode):
       return msg.decode('utf-8', 'replace')
     else:
       return msg
@@ -497,7 +496,7 @@ def _parse_timestamp(entry):
   :raises: **ValueError** if the timestamp is malformed
   """
 
-  if not isinstance(entry, (str, str_type)):
+  if not stem.util._is_str(entry):
     raise ValueError('parse_timestamp() input must be a str, got a %s' % type(entry))
 
   try:
@@ -523,7 +522,7 @@ def _parse_iso_timestamp(entry):
   :raises: **ValueError** if the timestamp is malformed
   """
 
-  if not isinstance(entry, (str, str_type)):
+  if not stem.util._is_str(entry):
     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 82891d7e..3eb2fd4d 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -80,12 +80,13 @@ import tarfile
 import threading
 import time
 
+import stem.util
 import stem.util.enum
 import stem.util.proc
 import stem.util.str_tools
 
 from stem import UNDEFINED
-from stem.util import str_type, log
+from stem.util import log
 
 State = stem.util.enum.UppercaseEnum(
   'PENDING',
@@ -446,7 +447,7 @@ def is_running(command):
     if command_listing:
       command_listing = [c.strip() for c in command_listing]
 
-      if isinstance(command, (bytes, str_type)):
+      if stem.util._is_str(command):
         command = [command]
 
       for cmd in command:
diff --git a/test/settings.cfg b/test/settings.cfg
index bd99a9d2..c3952567 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -147,9 +147,8 @@ pycodestyle.ignore test/unit/util/connection.py => W291: _tor     tor        158
 
 pyflakes.ignore run_tests.py => 'unittest' imported but unused
 pyflakes.ignore stem/client/datatype.py => redefinition of unused 'pop' from *
-pyflakes.ignore stem/util/__init__.py => undefined name 'long'
-pyflakes.ignore stem/util/__init__.py => undefined name 'unicode'
 pyflakes.ignore stem/control.py => undefined name 'controller'
+pyflakes.ignore stem/interpreter/__init__.py => undefined name 'raw_input'
 pyflakes.ignore stem/manual.py => undefined name 'unichr'
 pyflakes.ignore stem/prereq.py => 'int_to_bytes' imported but unused
 pyflakes.ignore stem/prereq.py => 'int_from_bytes' imported but unused
@@ -174,13 +173,16 @@ pyflakes.ignore stem/prereq.py => 'cryptography.hazmat.primitives.ciphers.algori
 pyflakes.ignore stem/prereq.py => 'lzma' imported but unused
 pyflakes.ignore stem/prereq.py => 'nacl.encoding' imported but unused
 pyflakes.ignore stem/prereq.py => 'nacl.signing' imported but unused
-pyflakes.ignore stem/interpreter/__init__.py => undefined name 'raw_input'
+pyflakes.ignore stem/response/events.py => undefined name 'long'
+pyflakes.ignore stem/util/__init__.py => undefined name 'long'
+pyflakes.ignore stem/util/__init__.py => undefined name 'unicode'
 pyflakes.ignore stem/util/conf.py => undefined name 'unicode'
 pyflakes.ignore stem/util/test_tools.py => 'pyflakes' imported but unused
 pyflakes.ignore stem/util/test_tools.py => 'pycodestyle' imported but unused
 pyflakes.ignore test/unit/descriptor/reader.py => 'bz2' imported but unused
 pyflakes.ignore test/unit/response/events.py => 'from stem import *' used; unable to detect undefined names
 pyflakes.ignore test/unit/response/events.py => *may be undefined, or defined from star imports: stem
+pyflakes.ignore stem/util/str_tools.py => undefined name 'unicode'
 pyflakes.ignore test/__init__.py => undefined name 'test'
 
 # Test modules we want to run. Modules are roughly ordered by the dependencies
diff --git a/test/unit/descriptor/microdescriptor.py b/test/unit/descriptor/microdescriptor.py
index aab9ab4c..4fd14f5b 100644
--- a/test/unit/descriptor/microdescriptor.py
+++ b/test/unit/descriptor/microdescriptor.py
@@ -8,7 +8,6 @@ import stem.descriptor
 import stem.exit_policy
 import test.require
 
-from stem.util import str_type
 from stem.descriptor.microdescriptor import Microdescriptor
 from test.unit.descriptor import get_resource
 
@@ -59,7 +58,7 @@ class TestMicrodescriptor(unittest.TestCase):
 
       router = next(descriptors)
       self.assertEqual(SECOND_ONION_KEY, router.onion_key)
-      self.assertEqual(str_type('r5572HzD+PMPBbXlZwBhsm6YEbxnYgis8vhZ1jmdI2k='), router.ntor_onion_key)
+      self.assertEqual('r5572HzD+PMPBbXlZwBhsm6YEbxnYgis8vhZ1jmdI2k=', router.ntor_onion_key)
       self.assertEqual([], router.or_addresses)
       self.assertEqual(['$6141629FA0D15A6AEAEF3A1BEB76E64C767B3174'], router.family)
       self.assertEqual(stem.exit_policy.MicroExitPolicy('reject 1-65535'), router.exit_policy)
@@ -69,7 +68,7 @@ class TestMicrodescriptor(unittest.TestCase):
       router = next(descriptors)
       self.assertEqual(THIRD_ONION_KEY, router.onion_key)
       self.assertEqual(None, router.ntor_onion_key)
-      self.assertEqual([(str_type('2001:6b0:7:125::242'), 9001, True)], router.or_addresses)
+      self.assertEqual([('2001:6b0:7:125::242', 9001, True)], router.or_addresses)
       self.assertEqual([], router.family)
       self.assertEqual(stem.exit_policy.MicroExitPolicy('accept 80,443'), router.exit_policy)
       self.assertEqual({b'@last-listed': b'2013-02-24 00:18:36'}, router.get_annotations())
diff --git a/test/unit/descriptor/networkstatus/document_v3.py b/test/unit/descriptor/networkstatus/document_v3.py
index a99d76be..20b38563 100644
--- a/test/unit/descriptor/networkstatus/document_v3.py
+++ b/test/unit/descriptor/networkstatus/document_v3.py
@@ -12,7 +12,6 @@ import stem.version
 import test.require
 
 from stem import Flag
-from stem.util import str_type
 
 from stem.descriptor import CRYPTO_BLOB
 
@@ -783,19 +782,19 @@ DnN5aFtYKiTc19qIC7Nmo+afPdDEf0MlJvEOP5EWl3w=
 
     test_values = (
       ('', {}),
-      ('fast-speed=40960', {str_type('fast-speed'): 40960}),    # numeric value
-      ('guard-wfu=94.669%', {str_type('guard-wfu'): 0.94669}),  # percentage value
-      ('guard-wfu=94.669% guard-tk=691200', {str_type('guard-wfu'): 0.94669, str_type('guard-tk'): 691200}),  # multiple values
+      ('fast-speed=40960', {'fast-speed': 40960}),    # numeric value
+      ('guard-wfu=94.669%', {'guard-wfu': 0.94669}),  # percentage value
+      ('guard-wfu=94.669% guard-tk=691200', {'guard-wfu': 0.94669, 'guard-tk': 691200}),  # multiple values
       ('stable-uptime=0 stable-mtbf=0 fast-speed=0 guard-wfu=0.000% guard-tk=0 guard-bw-inc-exits=0 guard-bw-exc-exits=0 enough-mtbf=1 ignoring-advertised-bws=0', {
-        str_type('stable-uptime'): 0,
-        str_type('stable-mtbf'): 0,
-        str_type('fast-speed'): 0,
-        str_type('guard-wfu'): 0.0,
-        str_type('guard-tk'): 0,
-        str_type('guard-bw-inc-exits'): 0,
-        str_type('guard-bw-exc-exits'): 0,
-        str_type('enough-mtbf'): 1,
-        str_type('ignoring-advertised-bws'): 0,
+        'stable-uptime': 0,
+        'stable-mtbf': 0,
+        'fast-speed': 0,
+        'guard-wfu': 0.0,
+        'guard-tk': 0,
+        'guard-bw-inc-exits': 0,
+        'guard-bw-exc-exits': 0,
+        'enough-mtbf': 1,
+        'ignoring-advertised-bws': 0,
       }),
     )
 
@@ -808,14 +807,14 @@ DnN5aFtYKiTc19qIC7Nmo+afPdDEf0MlJvEOP5EWl3w=
     full_line = 'stable-uptime=693369 stable-mtbf=153249 fast-speed=40960 guard-wfu=94.669% guard-tk=691200 guard-bw-inc-exits=174080 guard-bw-exc-exits=184320 enough-mtbf=1'
 
     expected_value = {
-      str_type('stable-uptime'): 693369,
-      str_type('stable-mtbf'): 153249,
-      str_type('fast-speed'): 40960,
-      str_type('guard-wfu'): 0.94669,
-      str_type('guard-tk'): 691200,
-      str_type('guard-bw-inc-exits'): 174080,
-      str_type('guard-bw-exc-exits'): 184320,
-      str_type('enough-mtbf'): 1,
+      'stable-uptime': 693369,
+      'stable-mtbf': 153249,
+      'fast-speed': 40960,
+      'guard-wfu': 0.94669,
+      'guard-tk': 691200,
+      'guard-bw-inc-exits': 174080,
+      'guard-bw-exc-exits': 184320,
+      'enough-mtbf': 1,
     }
 
     document = NetworkStatusDocumentV3.create({'vote-status': 'vote', 'flag-thresholds': full_line})
diff --git a/test/unit/descriptor/reader.py b/test/unit/descriptor/reader.py
index e450f7ca..76d1d8f3 100644
--- a/test/unit/descriptor/reader.py
+++ b/test/unit/descriptor/reader.py
@@ -14,9 +14,10 @@ import time
 import unittest
 
 import stem.descriptor.reader
-import test.unit.descriptor
+import stem.util.str_tools
+import stem.util.system
 
-from stem.util import str_type, system
+import test.unit.descriptor
 
 try:
   # added in python 3.3
@@ -89,13 +90,13 @@ class TestDescriptorReader(unittest.TestCase):
     """
 
     test_lines = (
-      str_type('/dir/ 0'),
-      str_type('/dir/file 12345'),
-      str_type('/dir/file with spaces 7138743'),
-      str_type('  /dir/with extra space 12345   '),
-      str_type('   \t   '),
-      str_type(''),
-      str_type('/dir/after empty line 12345'),
+      '/dir/ 0',
+      '/dir/file 12345',
+      '/dir/file with spaces 7138743',
+      '  /dir/with extra space 12345   ',
+      '   \t   ',
+      '',
+      '/dir/after empty line 12345',
     )
 
     expected_value = {
@@ -106,7 +107,7 @@ class TestDescriptorReader(unittest.TestCase):
       '/dir/after empty line': 12345,
     }
 
-    open_mock.return_value = io.StringIO(str_type('\n'.join(test_lines)))
+    open_mock.return_value = io.BytesIO(stem.util.str_tools._to_bytes('\n'.join(test_lines)))
     self.assertEqual(expected_value, stem.descriptor.reader.load_processed_files(''))
 
   @patch('stem.descriptor.reader.open', create = True)
@@ -115,7 +116,7 @@ class TestDescriptorReader(unittest.TestCase):
     Tests the load_processed_files() function with an empty file.
     """
 
-    open_mock.return_value = io.StringIO(str_type(''))
+    open_mock.return_value = io.BytesIO(stem.util.str_tools._to_bytes(''))
     self.assertEqual({}, stem.descriptor.reader.load_processed_files(''))
 
   @patch('stem.descriptor.reader.open', create = True)
@@ -125,7 +126,7 @@ class TestDescriptorReader(unittest.TestCase):
     it is missing the file path.
     """
 
-    open_mock.return_value = io.StringIO(str_type(' 12345'))
+    open_mock.return_value = io.BytesIO(stem.util.str_tools._to_bytes(' 12345'))
     self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
 
   @patch('stem.descriptor.reader.open', create = True)
@@ -135,7 +136,7 @@ class TestDescriptorReader(unittest.TestCase):
     it is missing the timestamp.
     """
 
-    open_mock.return_value = io.StringIO(str_type('/dir/file '))
+    open_mock.return_value = io.BytesIO(stem.util.str_tools._to_bytes('/dir/file '))
     self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
 
   @patch('stem.descriptor.reader.open', create = True)
@@ -145,7 +146,7 @@ class TestDescriptorReader(unittest.TestCase):
     it has an invalid file path.
     """
 
-    open_mock.return_value = io.StringIO(str_type('not_an_absolute_file 12345'))
+    open_mock.return_value = io.BytesIO(stem.util.str_tools._to_bytes('not_an_absolute_file 12345'))
     self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
 
   @patch('stem.descriptor.reader.open', create = True)
@@ -155,7 +156,7 @@ class TestDescriptorReader(unittest.TestCase):
     it has a non-numeric timestamp.
     """
 
-    open_mock.return_value = io.StringIO(str_type('/dir/file 123a'))
+    open_mock.return_value = io.BytesIO(stem.util.str_tools._to_bytes('/dir/file 123a'))
     self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
 
   def test_load_processed_files_from_data(self):
@@ -197,7 +198,7 @@ class TestDescriptorReader(unittest.TestCase):
     # read-only flag with os.chmod(). For more information see...
     # http://docs.python.org/library/os.html#os.chmod
 
-    if system.is_windows():
+    if stem.util.system.is_windows():
       self.skipTest('(chmod not functional)')
 
     test_listing_path = self._make_processed_files_listing(BASIC_LISTING)
@@ -418,7 +419,7 @@ class TestDescriptorReader(unittest.TestCase):
 
     # Skip on windows since SIGALRM is unavailable
 
-    if system.is_windows():
+    if stem.util.system.is_windows():
       self.skipTest('(SIGALRM unavailable)')
 
     is_test_running = True
@@ -558,7 +559,7 @@ class TestDescriptorReader(unittest.TestCase):
     if getpass.getuser() == 'root':
       self.skipTest('(running as root)')
       return
-    elif system.is_windows():
+    elif stem.util.system.is_windows():
       self.skipTest('(chmod not functional)')
       return
 
diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py
index 99d3c38e..ff1035c4 100644
--- a/test/unit/descriptor/server_descriptor.py
+++ b/test/unit/descriptor/server_descriptor.py
@@ -19,7 +19,6 @@ import stem.version
 import stem.util.str_tools
 import test.require
 
-from stem.util import str_type
 from stem.descriptor.certificate import CertType, ExtensionType
 from stem.descriptor.server_descriptor import BridgeDistribution, RelayDescriptor, BridgeDescriptor
 
@@ -42,9 +41,9 @@ except ImportError:
   from mock import Mock, patch
 
 TARFILE_FINGERPRINTS = set([
-  str_type('B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'),
-  str_type('E0BD57A11F00041A9789577C53A1B784473669E4'),
-  str_type('1F43EE37A0670301AD9CB555D94AFEC2C89FDE86'),
+  'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209',
+  'E0BD57A11F00041A9789577C53A1B784473669E4',
+  '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86',
 ])
 
 expect_invalid_attr = functools.partial(base_expect_invalid_attr, RelayDescriptor, 'nickname', 'Unnamed')
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
index 5ca5993e..c4b26ad0 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -21,7 +21,6 @@ from stem.descriptor.router_status_entry import RouterStatusEntryV3
 from stem.descriptor.server_descriptor import RelayDescriptor
 from stem.directory import DIRECTORY_AUTHORITIES
 from stem.response import ControlMessage
-from stem.util import str_type
 
 from test.unit import exec_documentation_example
 
@@ -39,7 +38,7 @@ PURPOSE=%s'
 
 PATH_CONTENT = '$%s=%s,$%s=%s,$%s=%s'
 
-LIST_CIRCUITS_OUTPUT = str_type("""\
+LIST_CIRCUITS_OUTPUT = """\
 
 Circuit 4 (GENERAL)
  |- B1FA7D51B8B6F0CB585D944F450E7C06EDE7E44C (ByTORAndTheSnowDog, 173.209.180.61)
@@ -55,9 +54,9 @@ Circuit 10 (GENERAL)
  |- B1FA7D51B8B6F0CB585D944F450E7C06EDE7E44C (ByTORAndTheSnowDog, 173.209.180.61)
  |- 00C2C2A16AEDB51D5E5FB7D6168FC66B343D822F (ph3x, 86.59.119.83)
  +- 65242C91BFF30F165DA4D132C81A9EBA94B71D62 (torexit16, 176.67.169.171)
-""")
+"""
 
-EXIT_USED_OUTPUT = str_type("""\
+EXIT_USED_OUTPUT = """\
 Tracking requests for tor exits. Press 'enter' to end.
 
 Exit relay for our connection to 64.15.112.44:80
@@ -66,15 +65,15 @@ Exit relay for our connection to 64.15.112.44:80
   nickname: chaoscomputerclub19
   locale: unknown
 
-""")
+"""
 
-OUTDATED_RELAYS_OUTPUT = str_type("""\
+OUTDATED_RELAYS_OUTPUT = """\
 Checking for outdated relays...
 
   0.1.0           Sambuddha Basu
 
 2 outdated relays found, 1 had contact information
-""")
+"""
 
 COMPARE_FLAGS_OUTPUT = """\
 maatuska has the Running flag but moria1 doesn't: E2BB13AA2F6960CD93ABE5257A825687F3973C62
diff --git a/test/unit/util/system.py b/test/unit/util/system.py
index fd8fe2d9..451e6e72 100644
--- a/test/unit/util/system.py
+++ b/test/unit/util/system.py
@@ -12,7 +12,7 @@ import posixpath
 import tempfile
 import unittest
 
-from stem.util import str_type, system
+from stem.util import system
 
 try:
   # added in python 3.3
@@ -128,8 +128,7 @@ class TestSystem(unittest.TestCase):
     """
 
     # mock response with a linux and bsd resolver
-    running_commands = [str_type('irssi'), str_type('moc'), str_type('tor'),
-                        str_type('ps'), str_type('  firefox  ')]
+    running_commands = ['irssi', 'moc', 'tor', 'ps', '  firefox  ']
 
     for ps_cmd in (system.IS_RUNNING_PS_LINUX, system.IS_RUNNING_PS_BSD):
       call_mock.side_effect = mock_call(ps_cmd, running_commands)





More information about the tor-commits mailing list