[tor-commits] [bridgedb/master] Replace usage of StringIO

phw at torproject.org phw at torproject.org
Wed Feb 19 18:26:37 UTC 2020


commit 5c4c8112eec99bd253dd934c167244bf13c99cec
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jan 10 13:19:17 2020 -0800

    Replace usage of StringIO
    
    Python 2.x had three copies of StringIO:
    
      * StringIO.StringIO   => Original iirc, and long ago deprecated.
      * cStringIO.StringIO  => More efficient, but also deprecated.
      * io.StringIO         => Latest copy and the only present in python 3.x.
    
    This fixes a few stacktraces like the following...
    
      Traceback (most recent call last):
        File "/usr/local/lib/python3.5/dist-packages/twisted/trial/runner.py", line 823, in loadByName
          return self.suiteFactory([self.findByName(name, recurse=recurse)])
        ...
        File "./bridgedb/test/test_metrics.py", line 18, in <module>
          import StringIO
      builtins.ImportError: No module named 'StringIO'
    
    This changes the test outcome as follows...
    
      before: FAILED (skips=1, failures=6, errors=54, successes=246)
      after:  FAILED (skips=1, failures=8, errors=53, successes=250)
---
 bridgedb/Bridges.py                         | 6 ------
 bridgedb/qrcodes.py                         | 4 ++--
 bridgedb/test/test_metrics.py               | 4 ++--
 bridgedb/test/test_persistent.py            | 4 ++--
 bridgedb/test/test_persistentSaveAndLoad.py | 4 ++--
 5 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/bridgedb/Bridges.py b/bridgedb/Bridges.py
index f80f4cd..7ec60e6 100644
--- a/bridgedb/Bridges.py
+++ b/bridgedb/Bridges.py
@@ -28,12 +28,6 @@ from bridgedb.parse.fingerprint import isValidFingerprint
 from bridgedb.parse.fingerprint import toHex
 from bridgedb.safelog import logSafely
 
-try:
-    from cStringIO import StringIO
-except ImportError:
-    from io import StringIO
-
-
 ID_LEN = 20  # XXX Only used in commented out line in Storage.py
 DIGEST_LEN = 20
 PORTSPEC_LEN = 16
diff --git a/bridgedb/qrcodes.py b/bridgedb/qrcodes.py
index b3c4546..80e82c8 100644
--- a/bridgedb/qrcodes.py
+++ b/bridgedb/qrcodes.py
@@ -13,7 +13,7 @@
 """Utilities for working with QRCodes."""
 
 
-import cStringIO
+import io
 import logging
 
 try:
@@ -60,7 +60,7 @@ def generateQR(bridgelines, imageFormat=u'JPEG', bridgeSchema=False):
         qr = qrcode.QRCode()
         qr.add_data(bridgelines)
 
-        buf = cStringIO.StringIO()
+        buf = io.StringIO()
         img = qr.make_image().resize([350, 350])
         img.save(buf, imageFormat)
         buf.seek(0)
diff --git a/bridgedb/test/test_metrics.py b/bridgedb/test/test_metrics.py
index ce0f63d..fcc9e4a 100644
--- a/bridgedb/test/test_metrics.py
+++ b/bridgedb/test/test_metrics.py
@@ -15,7 +15,7 @@ These tests are meant to ensure that the :mod:`bridgedb.metrics` module is
 functioning as expected.
 """
 
-import StringIO
+import io
 import json
 import os
 
@@ -104,7 +104,7 @@ class StateTest(unittest.TestCase):
         self.metrix.inc(self.key)
 
         self.metrix.coldMetrics = self.metrix.hotMetrics
-        pseudo_fh = StringIO.StringIO()
+        pseudo_fh = io.StringIO()
         metrics.export(pseudo_fh, 0)
 
         self.assertTrue(len(pseudo_fh.getvalue()) > 0)
diff --git a/bridgedb/test/test_persistent.py b/bridgedb/test/test_persistent.py
index 6547475..3ff6d87 100644
--- a/bridgedb/test/test_persistent.py
+++ b/bridgedb/test/test_persistent.py
@@ -17,10 +17,10 @@ functioning as expected.
 
 from __future__ import print_function
 
+import io
 import os.path
 
 from copy import deepcopy
-from io   import StringIO
 
 from bridgedb import persistent
 from bridgedb.parse.options import MainOptions
@@ -33,7 +33,7 @@ from sure import the
 from sure import expect
 
 
-TEST_CONFIG_FILE = StringIO(unicode("""\
+TEST_CONFIG_FILE = io.StringIO(unicode("""\
 BRIDGE_FILES = ['bridge-descriptors', 'bridge-descriptors.new']
 LOGFILE = 'bridgedb.log'"""))
 
diff --git a/bridgedb/test/test_persistentSaveAndLoad.py b/bridgedb/test/test_persistentSaveAndLoad.py
index 18f601e..7678948 100644
--- a/bridgedb/test/test_persistentSaveAndLoad.py
+++ b/bridgedb/test/test_persistentSaveAndLoad.py
@@ -18,17 +18,17 @@ are all functioning as expected.
 This module should not import :mod:`sure`.
 """
 
+import io
 import os
 
 from copy import deepcopy
-from io    import StringIO
 
 from twisted.trial import unittest
 
 from bridgedb import persistent
 
 
-TEST_CONFIG_FILE = StringIO(unicode("""\
+TEST_CONFIG_FILE = io.StringIO(unicode("""\
 BRIDGE_FILES = ['bridge-descriptors', 'bridge-descriptors.new']
 LOGFILE = 'bridgedb.log'"""))
 





More information about the tor-commits mailing list