[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Drop StringIO fallback



commit 902ce4186be618f6597baf7e57aee2c74852df09
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Jan 3 16:23:14 2020 -0800

    Drop StringIO fallback
    
    Python 3.x moved StringIO into its io module.
---
 run_tests.py                     |  8 ++------
 stem/descriptor/export.py        |  8 ++------
 stem/interpreter/commands.py     |  8 ++------
 test/integ/connection/connect.py | 14 +++++---------
 test/unit/connection/connect.py  | 12 ++++--------
 test/unit/descriptor/export.py   |  8 ++------
 test/unit/tutorial.py            | 19 +++++++------------
 test/unit/tutorial_examples.py   | 18 +++++++-----------
 8 files changed, 31 insertions(+), 64 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index fc67af3c..d6ced384 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -7,6 +7,7 @@ Runs unit and integration tests. For usage information run this with '--help'.
 """
 
 import errno
+import io
 import importlib
 import logging
 import multiprocessing
@@ -18,11 +19,6 @@ import time
 import traceback
 import unittest
 
-try:
-  from StringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 import stem.prereq
 import stem.util.conf
 import stem.util.log
@@ -427,7 +423,7 @@ def _run_test(args, test_class, output_filters):
     traceback.print_exc(exc)
     return None
 
-  test_results = StringIO()
+  test_results = io.StringIO()
   run_result = stem.util.test_tools.TimedTestRunner(test_results, verbosity = 2).run(suite)
 
   if args.verbose:
diff --git a/stem/descriptor/export.py b/stem/descriptor/export.py
index fea681be..4b909c97 100644
--- a/stem/descriptor/export.py
+++ b/stem/descriptor/export.py
@@ -17,13 +17,9 @@ Toolkit for exporting descriptors to other formats.
    use this modle please `let me know <https://www.atagar.com/contact/>`_.
 """
 
+import io
 import csv
 
-try:
-  from cStringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 import stem.descriptor
 import stem.prereq
 
@@ -50,7 +46,7 @@ def export_csv(descriptors, included_fields = (), excluded_fields = (), header =
   :raises: **ValueError** if descriptors contain more than one descriptor type
   """
 
-  output_buffer = StringIO()
+  output_buffer = io.StringIO()
   export_csv_file(output_buffer, descriptors, included_fields, excluded_fields, header)
   return output_buffer.getvalue()
 
diff --git a/stem/interpreter/commands.py b/stem/interpreter/commands.py
index 0f8f333c..6e61fdda 100644
--- a/stem/interpreter/commands.py
+++ b/stem/interpreter/commands.py
@@ -7,6 +7,7 @@ Handles making requests and formatting the responses.
 
 import code
 import contextlib
+import io
 import socket
 import sys
 
@@ -21,11 +22,6 @@ import stem.util.tor_tools
 from stem.interpreter import STANDARD_OUTPUT, BOLD_OUTPUT, ERROR_OUTPUT, uses_settings, msg
 from stem.util.term import format
 
-try:
-  from cStringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 MAX_EVENTS = 100
 
 
@@ -359,7 +355,7 @@ class ControlInterpreter(code.InteractiveConsole):
         is_tor_command = cmd in config.get('help.usage', {}) and cmd.lower() != 'events'
 
         if self._run_python_commands and not is_tor_command:
-          console_output = StringIO()
+          console_output = io.StringIO()
 
           with redirect(console_output, console_output):
             self.is_multiline_context = code.InteractiveConsole.push(self, command)
diff --git a/test/integ/connection/connect.py b/test/integ/connection/connect.py
index bfeefb19..84b4a8fc 100644
--- a/test/integ/connection/connect.py
+++ b/test/integ/connection/connect.py
@@ -2,6 +2,7 @@
 Integration tests for the connect_* convenience functions.
 """
 
+import io
 import unittest
 
 import stem.connection
@@ -10,15 +11,10 @@ import test.runner
 
 from unittest.mock import patch
 
-try:
-  from StringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 
 class TestConnect(unittest.TestCase):
   @test.require.controller
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   def test_connect(self, stdout_mock):
     """
     Basic sanity checks for the connect function.
@@ -37,7 +33,7 @@ class TestConnect(unittest.TestCase):
     self.assertEqual('', stdout_mock.getvalue())
 
   @test.require.controller
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   def test_connect_port(self, stdout_mock):
     """
     Basic sanity checks for the connect_port function.
@@ -59,7 +55,7 @@ class TestConnect(unittest.TestCase):
       self.assertEqual(control_socket, None)
 
   @test.require.controller
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   def test_connect_socket_file(self, stdout_mock):
     """
     Basic sanity checks for the connect_socket_file function.
@@ -81,7 +77,7 @@ class TestConnect(unittest.TestCase):
       self.assertEqual(control_socket, None)
 
   @test.require.controller
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   def test_connect_to_socks_port(self, stdout_mock):
     """
     Common user gotcha is connecting to the SocksPort or ORPort rather than the
diff --git a/test/unit/connection/connect.py b/test/unit/connection/connect.py
index ec82f19f..175a1ebd 100644
--- a/test/unit/connection/connect.py
+++ b/test/unit/connection/connect.py
@@ -2,6 +2,7 @@
 Unit tests for the stem.connection.connect function.
 """
 
+import io
 import unittest
 
 import stem
@@ -10,14 +11,9 @@ import stem.socket
 
 from unittest.mock import Mock, patch
 
-try:
-  from StringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 
 class TestConnect(unittest.TestCase):
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.util.system.is_running')
   @patch('os.path.exists', Mock(return_value = True))
   @patch('stem.socket.ControlSocketFile', Mock(side_effect = stem.SocketError('failed')))
@@ -30,7 +26,7 @@ class TestConnect(unittest.TestCase):
     is_running_mock.return_value = True
     self._assert_connect_fails_with({}, stdout_mock, "Unable to connect to tor. Maybe it's running without a ControlPort?")
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('os.path.exists')
   @patch('stem.util.system.is_running', Mock(return_value = True))
   @patch('stem.socket.ControlSocketFile', Mock(side_effect = stem.SocketError('failed')))
@@ -118,7 +114,7 @@ class TestConnect(unittest.TestCase):
     authenticate_mock.assert_any_call(control_socket, None, None)
     authenticate_mock.assert_any_call(control_socket, 'my_password', None)
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.connection.authenticate')
   def test_auth_failure(self, authenticate_mock, stdout_mock):
     control_socket = stem.socket.ControlPort(connect = False)
diff --git a/test/unit/descriptor/export.py b/test/unit/descriptor/export.py
index bf7d054a..d27ed241 100644
--- a/test/unit/descriptor/export.py
+++ b/test/unit/descriptor/export.py
@@ -2,13 +2,9 @@
 Unit tests for stem.descriptor.export.
 """
 
+import io
 import unittest
 
-try:
-  from StringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 import stem.prereq
 
 from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor
@@ -59,7 +55,7 @@ class TestExport(unittest.TestCase):
     desc = RelayDescriptor.create()
     desc_csv = export_csv(desc)
 
-    csv_buffer = StringIO()
+    csv_buffer = io.StringIO()
     export_csv_file(csv_buffer, desc)
 
     self.assertEqual(desc_csv, csv_buffer.getvalue())
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 58abde9b..91a70a32 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -16,11 +16,6 @@ from stem.descriptor.server_descriptor import RelayDescriptor
 from stem.exit_policy import ExitPolicy
 from test.unit import exec_documentation_example
 
-try:
-  from StringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 
 OVER_THE_RIVER_OUTPUT = """\
  * Connecting to tor
@@ -43,7 +38,7 @@ class TestTutorial(unittest.TestCase):
 
     stem.descriptor.remote.SINGLETON_DOWNLOADER = None
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
   def test_the_little_relay_that_could(self, from_port_mock, stdout_mock):
     controller = from_port_mock().__enter__()
@@ -55,7 +50,7 @@ class TestTutorial(unittest.TestCase):
     exec_documentation_example('hello_world.py')
     self.assertEqual('My Tor relay has read 33406 bytes and written 29649.\n', stdout_mock.getvalue())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('shutil.rmtree')
   @patch('stem.control.Controller.from_port', spec = Controller)
   def test_over_the_river(self, from_port_mock, rmtree_mock, stdout_mock):
@@ -121,7 +116,7 @@ class TestTutorial(unittest.TestCase):
 
     self.assertEqual(OVER_THE_RIVER_OUTPUT, stdout_mock.getvalue())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.descriptor.remote.DescriptorDownloader')
   def test_mirror_mirror_on_the_wall_1(self, downloader_mock, stdout_mock):
     downloader_mock().get_consensus.return_value = [RouterStatusEntryV2.create({
@@ -131,7 +126,7 @@ class TestTutorial(unittest.TestCase):
     exec_documentation_example('current_descriptors.py')
     self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
   def test_mirror_mirror_on_the_wall_2(self, from_port_mock, stdout_mock):
     controller = from_port_mock().__enter__()
@@ -142,7 +137,7 @@ class TestTutorial(unittest.TestCase):
     exec_documentation_example('descriptor_from_tor_control_socket.py')
     self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('%s.open' % __name__, create = True)
   def test_mirror_mirror_on_the_wall_3(self, open_mock, stdout_mock):
     def tutorial_example():
@@ -160,7 +155,7 @@ class TestTutorial(unittest.TestCase):
     tutorial_example()
     self.assertEqual('found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n', stdout_mock.getvalue())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.descriptor.collector.get_server_descriptors')
   def test_mirror_mirror_on_the_wall_4(self, get_desc_mock, stdout_mock):
     get_desc_mock.return_value = iter([RelayDescriptor.create({
@@ -171,7 +166,7 @@ class TestTutorial(unittest.TestCase):
     exec_documentation_example('collector_reading.py')
     self.assertEqual('1 relays published an exiting policy today...\n\n  caerSidi (2C3C46625698B6D67DF32BC1918AD3EE1F9906B1)\n', stdout_mock.getvalue())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.descriptor.remote.DescriptorDownloader')
   @patch('stem.prereq.is_crypto_available', Mock(return_value = False))
   def test_mirror_mirror_on_the_wall_5(self, downloader_mock, stdout_mock):
diff --git a/test/unit/tutorial_examples.py b/test/unit/tutorial_examples.py
index c3359d1e..d96ff71b 100644
--- a/test/unit/tutorial_examples.py
+++ b/test/unit/tutorial_examples.py
@@ -2,15 +2,11 @@
 Tests for the examples given in stem's tutorial.
 """
 
+import io
 import itertools
 import os
 import unittest
 
-try:
-  from StringIO import StringIO
-except ImportError:
-  from io import StringIO
-
 import stem.response
 import stem.descriptor.remote
 import stem.prereq
@@ -125,7 +121,7 @@ def _get_router_status(address = None, port = None, nickname = None, fingerprint
 
 
 class TestTutorialExamples(unittest.TestCase):
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
   def test_list_circuits(self, from_port_mock, stdout_mock):
     path_1 = ('B1FA7D51B8B6F0CB585D944F450E7C06EDE7E44C', 'ByTORAndTheSnowDog')
@@ -156,7 +152,7 @@ class TestTutorialExamples(unittest.TestCase):
     exec_documentation_example('list_circuits.py')
     self.assertCountEqual(LIST_CIRCUITS_OUTPUT.splitlines(), stdout_mock.getvalue().splitlines())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.control.Controller.from_port', spec = Controller)
   def test_exit_used(self, from_port_mock, stdout_mock):
     def tutorial_example(mock_event):
@@ -207,7 +203,7 @@ class TestTutorialExamples(unittest.TestCase):
     tutorial_example(event)
     self.assertCountEqual(EXIT_USED_OUTPUT.splitlines(), stdout_mock.getvalue().splitlines())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.descriptor.remote.DescriptorDownloader')
   def test_outdated_relays(self, downloader_mock, stdout_mock):
     downloader_mock().get_server_descriptors.return_value = [
@@ -221,7 +217,7 @@ class TestTutorialExamples(unittest.TestCase):
 
     self.assertCountEqual(OUTDATED_RELAYS_OUTPUT.splitlines(), stdout_mock.getvalue().splitlines())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.descriptor.remote.Query')
   @patch('stem.directory.Authority.from_cache')
   def test_compare_flags(self, authorities_mock, query_mock, stdout_mock):
@@ -262,7 +258,7 @@ class TestTutorialExamples(unittest.TestCase):
 
     self.assertCountEqual(COMPARE_FLAGS_OUTPUT.splitlines(), stdout_mock.getvalue().splitlines())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.directory.Authority.from_cache')
   @patch('stem.descriptor.remote.DescriptorDownloader.query')
   def test_votes_by_bandwidth_authorities(self, query_mock, authorities_mock, stdout_mock):
@@ -295,7 +291,7 @@ class TestTutorialExamples(unittest.TestCase):
     exec_documentation_example('votes_by_bandwidth_authorities.py')
     self.assertCountEqual(VOTES_BY_BANDWIDTH_AUTHORITIES_OUTPUT.splitlines(), stdout_mock.getvalue().splitlines())
 
-  @patch('sys.stdout', new_callable = StringIO)
+  @patch('sys.stdout', new_callable = io.StringIO)
   @patch('stem.descriptor.parse_file')
   @patch('stem.descriptor.remote.Query')
   def test_persisting_a_consensus(self, query_mock, parse_file_mock, stdout_mock):



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits