commit e1047c8173f08504a90779f518919e387e29fb9e Author: Foxboron mcfoxax@gmail.com Date: Sat Jan 3 00:54:42 2015 +0100
compatability for 3.x --- stem/descriptor/server_descriptor.py | 3 ++- test/unit/descriptor/microdescriptor.py | 5 ++-- test/unit/descriptor/networkstatus/document_v3.py | 23 +++++++++--------- test/unit/descriptor/reader.py | 27 +++++++++++---------- test/unit/descriptor/server_descriptor.py | 7 +++--- test/unit/tutorial.py | 8 ++++-- test/unit/tutorial_examples.py | 16 ++++++------ test/unit/util/proc.py | 9 ++++--- test/unit/util/system.py | 4 ++- 9 files changed, 59 insertions(+), 43 deletions(-)
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py index 11820e3..7a46a16 100644 --- a/stem/descriptor/server_descriptor.py +++ b/stem/descriptor/server_descriptor.py @@ -46,6 +46,7 @@ import stem.util.tor_tools import stem.version
from stem.util import log +from stem._compat import str_type
from stem.descriptor import ( PGP_BLOCK_END, @@ -297,7 +298,7 @@ class ServerDescriptor(Descriptor):
entries, policy = _get_descriptor_components(raw_contents, validate, ('accept', 'reject'))
- if policy == [u'reject *:*']: + if policy == [str_type('reject *:*')]: self.exit_policy = REJECT_ALL_POLICY else: self.exit_policy = stem.exit_policy.ExitPolicy(*policy) diff --git a/test/unit/descriptor/microdescriptor.py b/test/unit/descriptor/microdescriptor.py index 5635d54..0873fbb 100644 --- a/test/unit/descriptor/microdescriptor.py +++ b/test/unit/descriptor/microdescriptor.py @@ -9,6 +9,7 @@ import stem.exit_policy import stem.descriptor
from stem.descriptor.microdescriptor import Microdescriptor +from stem._compat import str_type
from test.mocking import ( get_microdescriptor, @@ -64,7 +65,7 @@ class TestMicrodescriptor(unittest.TestCase):
router = next(descriptors) self.assertEqual(SECOND_ONION_KEY, router.onion_key) - self.assertEqual(u'r5572HzD+PMPBbXlZwBhsm6YEbxnYgis8vhZ1jmdI2k=', router.ntor_onion_key) + self.assertEqual(str_type('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) @@ -74,7 +75,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([(u'2001:6b0:7:125::242', 9001, True)], router.or_addresses) + self.assertEqual([(str_type('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 63a72f0..fafd455 100644 --- a/test/unit/descriptor/networkstatus/document_v3.py +++ b/test/unit/descriptor/networkstatus/document_v3.py @@ -10,6 +10,7 @@ import stem.descriptor import stem.version
from stem import Flag +from stem._compat import str_type
from stem.descriptor.networkstatus import ( HEADER_STATUS_DOCUMENT_FIELDS, @@ -751,9 +752,9 @@ DnN5aFtYKiTc19qIC7Nmo+afPdDEf0MlJvEOP5EWl3w=
test_values = ( ('', {}), - ('fast-speed=40960', {u'fast-speed': 40960}), # numeric value - ('guard-wfu=94.669%', {u'guard-wfu': 0.94669}), # percentage value - ('guard-wfu=94.669% guard-tk=691200', {u'guard-wfu': 0.94669, u'guard-tk': 691200}), # multiple 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 )
for test_value, expected_value in test_values: @@ -765,14 +766,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 = { - u'stable-uptime': 693369, - u'stable-mtbf': 153249, - u'fast-speed': 40960, - u'guard-wfu': 0.94669, - u'guard-tk': 691200, - u'guard-bw-inc-exits': 174080, - u'guard-bw-exc-exits': 184320, - u'enough-mtbf': 1, + 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, }
document = get_network_status_document_v3({'vote-status': 'vote', 'flag-thresholds': full_line}) diff --git a/test/unit/descriptor/reader.py b/test/unit/descriptor/reader.py index 31c694e..85c3118 100644 --- a/test/unit/descriptor/reader.py +++ b/test/unit/descriptor/reader.py @@ -18,6 +18,7 @@ import test.runner import test.unit.descriptor
from stem.util import system +from stem._compat import str_type
try: # added in python 3.3 @@ -90,13 +91,13 @@ class TestDescriptorReader(unittest.TestCase): """
test_lines = ( - u'/dir/ 0', - u'/dir/file 12345', - u'/dir/file with spaces 7138743', - u' /dir/with extra space 12345 ', - u' \t ', - u'', - u'/dir/after empty line 12345', + 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'), )
expected_value = { @@ -107,7 +108,7 @@ class TestDescriptorReader(unittest.TestCase): '/dir/after empty line': 12345, }
- open_mock.return_value = StringIO(u'\n'.join(test_lines)) + open_mock.return_value = StringIO(str_type('\n'.join(test_lines))) self.assertEqual(expected_value, stem.descriptor.reader.load_processed_files(''))
@patch('stem.descriptor.reader.open', create = True) @@ -116,7 +117,7 @@ class TestDescriptorReader(unittest.TestCase): Tests the load_processed_files() function with an empty file. """
- open_mock.return_value = StringIO(u'') + open_mock.return_value = StringIO(str_type('')) self.assertEqual({}, stem.descriptor.reader.load_processed_files(''))
@patch('stem.descriptor.reader.open', create = True) @@ -126,7 +127,7 @@ class TestDescriptorReader(unittest.TestCase): it is missing the file path. """
- open_mock.return_value = StringIO(u' 12345') + open_mock.return_value = StringIO(str_type(' 12345')) self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
@patch('stem.descriptor.reader.open', create = True) @@ -136,7 +137,7 @@ class TestDescriptorReader(unittest.TestCase): it is missing the timestamp. """
- open_mock.return_value = StringIO(u'/dir/file ') + open_mock.return_value = StringIO(str_type('/dir/file ')) self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
@patch('stem.descriptor.reader.open', create = True) @@ -146,7 +147,7 @@ class TestDescriptorReader(unittest.TestCase): it has an invalid file path. """
- open_mock.return_value = StringIO(u'not_an_absolute_file 12345') + open_mock.return_value = StringIO(str_type('not_an_absolute_file 12345')) self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
@patch('stem.descriptor.reader.open', create = True) @@ -156,7 +157,7 @@ class TestDescriptorReader(unittest.TestCase): it has a non-numeric timestamp. """
- open_mock.return_value = StringIO(u'/dir/file 123a') + open_mock.return_value = StringIO(str_type('/dir/file 123a')) self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, '')
def test_load_processed_files_from_data(self): diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py index c05dee1..a83dfba 100644 --- a/test/unit/descriptor/server_descriptor.py +++ b/test/unit/descriptor/server_descriptor.py @@ -14,6 +14,7 @@ import stem.version import stem.util.str_tools
from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor +from stem._compat import str_type
from test.mocking import ( get_relay_server_descriptor, @@ -30,9 +31,9 @@ except ImportError: from mock import Mock, patch
TARFILE_FINGERPRINTS = set([ - u'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209', - u'E0BD57A11F00041A9789577C53A1B784473669E4', - u'1F43EE37A0670301AD9CB555D94AFEC2C89FDE86', + str_type('B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'), + str_type('E0BD57A11F00041A9789577C53A1B784473669E4'), + str_type('1F43EE37A0670301AD9CB555D94AFEC2C89FDE86'), ])
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py index 883c4a7..98d2a1a 100644 --- a/test/unit/tutorial.py +++ b/test/unit/tutorial.py @@ -11,12 +11,16 @@ from stem.descriptor.server_descriptor import RelayDescriptor from test import mocking
try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +try: # added in python 3.3 from unittest.mock import Mock, patch - from io import StringIO except ImportError: from mock import Mock, patch - from StringIO import StringIO +
OVER_THE_RIVER_OUTPUT = """\ * Connecting to tor diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py index 618844d..99b4466 100644 --- a/test/unit/tutorial_examples.py +++ b/test/unit/tutorial_examples.py @@ -2,7 +2,6 @@ Tests for the examples given in stem's tutorial. """
-import collections import itertools import unittest
@@ -16,6 +15,8 @@ import stem.descriptor.remote
from stem.control import Controller from stem.descriptor.remote import DIRECTORY_AUTHORITIES +from stem._compat import str_type + from test import mocking from test.mocking import ( get_relay_server_descriptor, @@ -29,6 +30,7 @@ try: from unittest.mock import Mock, patch except ImportError: from mock import Mock, patch + import sys oldstdout = sys.stdout
@@ -38,7 +40,7 @@ PURPOSE=%s'
PATH_CONTENT = '$%s=%s,$%s=%s,$%s=%s'
-LIST_CIRCUITS_OUTPUT = u"""\ +LIST_CIRCUITS_OUTPUT = str_type("""\
Circuit 4 (GENERAL) |- B1FA7D51B8B6F0CB585D944F450E7C06EDE7E44C (ByTORAndTheSnowDog, 173.209.180.61) @@ -54,9 +56,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 = u"""\ +EXIT_USED_OUTPUT = str_type("""\ Tracking requests for tor exits. Press 'enter' to end.
Exit relay for our connection to 64.15.112.44:80 @@ -65,15 +67,15 @@ Exit relay for our connection to 64.15.112.44:80 nickname: chaoscomputerclub19 locale: unknown
-""" +""")
-OUTDATED_RELAYS_OUTPUT = u"""\ +OUTDATED_RELAYS_OUTPUT = str_type("""\ 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/proc.py b/test/unit/util/proc.py index a612895..d1fb4aa 100644 --- a/test/unit/util/proc.py +++ b/test/unit/util/proc.py @@ -8,12 +8,15 @@ from stem.util import proc from test import mocking
try: - # added in python 3.3 - from unittest.mock import Mock, patch + from StringIO import StringIO +except ImportError: from io import StringIO + +try: + from unittest.mock import Mock, patch except ImportError: from mock import Mock, patch - from StringIO import StringIO +
class TestProc(unittest.TestCase): @patch('stem.util.proc._get_line') diff --git a/test/unit/util/system.py b/test/unit/util/system.py index d2ae43f..f9fbb10 100644 --- a/test/unit/util/system.py +++ b/test/unit/util/system.py @@ -11,6 +11,7 @@ import posixpath import unittest
from stem.util import system +from stem._compat import str_type
try: # added in python 3.3 @@ -116,7 +117,8 @@ class TestSystem(unittest.TestCase): """
# mock response with a linux and bsd resolver - running_commands = [u'irssi', u'moc', u'tor', u'ps', u' firefox '] + running_commands = [str_type('irssi'), str_type('moc'), str_type('tor'), + str_type('ps'), str_type(' 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)
tor-commits@lists.torproject.org