[tor-commits] [stem/master] Python3 unit test regressions with new Descriptor.from_str() tests

atagar at torproject.org atagar at torproject.org
Wed Nov 21 17:31:55 UTC 2018


commit 2ea93474b863b4c63bff2652cc6c918c657211e7
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Nov 21 09:29:16 2018 -0800

    Python3 unit test regressions with new Descriptor.from_str() tests
    
    Oops, couple unicode-vs-bytes mistakes...
    
      https://trac.torproject.org/projects/tor/ticket/28550
    
      ======================================================================
      ERROR: test_from_str_multiple
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/unit/descriptor/descriptor.py", line 45, in test_from_str_multiple
          RelayDescriptor.content({'router': 'relay2 71.35.133.197 9001 0 0'}),
      TypeError: sequence item 1: expected str instance, bytes found
    
      ======================================================================
      ERROR: test_from_str_type_handling
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/unit/descriptor/descriptor.py", line 33, in test_from_str_type_handling
          desc = Descriptor.from_str('@type server-descriptor 1.0\n' + desc_text)
      TypeError: Can't convert 'bytes' object to str implicitly
    
      ----------------------------------------------------------------------
    
    I'm also making from_str() normalize unicode to bytes so the method isn't a
    misnomer for python3.
---
 stem/descriptor/__init__.py        | 4 ++--
 test/unit/descriptor/descriptor.py | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py
index 17bea678..1b38783c 100644
--- a/stem/descriptor/__init__.py
+++ b/stem/descriptor/__init__.py
@@ -722,7 +722,7 @@ class Descriptor(object):
 
     .. versionadded:: 1.8.0
 
-    :param bytes content: string to construct the descriptor from
+    :param str,bytes content: string to construct the descriptor from
     :param bool multiple: if provided with **True** this provides a list of
       descriptors rather than a single one
     :param dict kwargs: additional arguments for :func:`~stem.descriptor.__init__.parse_file`
@@ -741,7 +741,7 @@ class Descriptor(object):
       kwargs['descriptor_type'] = str(TypeAnnotation(cls.TYPE_ANNOTATION_NAME, 1, 0))[6:]
 
     is_multiple = kwargs.pop('multiple', False)
-    results = list(parse_file(io.BytesIO(content), **kwargs))
+    results = list(parse_file(io.BytesIO(stem.util.str_tools._to_bytes(content)), **kwargs))
 
     if is_multiple:
       return results
diff --git a/test/unit/descriptor/descriptor.py b/test/unit/descriptor/descriptor.py
index cedb3832..9de70206 100644
--- a/test/unit/descriptor/descriptor.py
+++ b/test/unit/descriptor/descriptor.py
@@ -30,7 +30,7 @@ class TestDescriptor(unittest.TestCase):
     desc = Descriptor.from_str(desc_text, descriptor_type = 'server-descriptor 1.0')
     self.assertEqual('caerSidi', desc.nickname)
 
-    desc = Descriptor.from_str('@type server-descriptor 1.0\n' + desc_text)
+    desc = Descriptor.from_str(b'@type server-descriptor 1.0\n' + desc_text)
     self.assertEqual('caerSidi', desc.nickname)
 
     desc = RelayDescriptor.from_str(desc_text)
@@ -39,8 +39,8 @@ class TestDescriptor(unittest.TestCase):
     self.assertRaisesWith(TypeError, "Unable to determine the descriptor's type. filename: '<undefined>', first line: 'router caerSidi 71.35.133.197 9001 0 0'", Descriptor.from_str, desc_text)
 
   def test_from_str_multiple(self):
-    desc_text = '\n'.join((
-      '@type server-descriptor 1.0',
+    desc_text = b'\n'.join((
+      b'@type server-descriptor 1.0',
       RelayDescriptor.content({'router': 'relay1 71.35.133.197 9001 0 0'}),
       RelayDescriptor.content({'router': 'relay2 71.35.133.197 9001 0 0'}),
     ))



More information about the tor-commits mailing list