[tor-commits] [stem/master] Accounting for ascii/unicode for network status documents

atagar at torproject.org atagar at torproject.org
Sat Feb 2 18:20:50 UTC 2013


commit a047a74abef0dc9fedf463c093c14defe958d81b
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Feb 1 07:12:04 2013 -0800

    Accounting for ascii/unicode for network status documents
    
    Woohoo! Last descriptor type. Unlike the other descriptor types callers are
    encouraged to sometimes use our NetworkStatusDocument classes directly so
    swapping the input to unicode if we get ascii.
    
    With this all of the descriptor integ tests now pass with python 3!
---
 stem/descriptor/networkstatus.py       |    3 +++
 test/integ/descriptor/networkstatus.py |   16 ++++------------
 2 files changed, 7 insertions(+), 12 deletions(-)

diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py
index 69f1a44..0d60de1 100644
--- a/stem/descriptor/networkstatus.py
+++ b/stem/descriptor/networkstatus.py
@@ -73,6 +73,7 @@ import StringIO
 
 import stem.descriptor
 import stem.descriptor.router_status_entry
+import stem.util.str_tools
 import stem.util.tor_tools
 import stem.version
 
@@ -274,6 +275,7 @@ class NetworkStatusDocumentV2(NetworkStatusDocument):
   """
 
   def __init__(self, raw_content, validate = True):
+    raw_content = stem.util.str_tools.to_unicode(raw_content)
     super(NetworkStatusDocumentV2, self).__init__(raw_content)
 
     self.version = None
@@ -471,6 +473,7 @@ class NetworkStatusDocumentV3(NetworkStatusDocument):
     :raises: **ValueError** if the document is invalid
     """
 
+    raw_content = stem.util.str_tools.to_unicode(raw_content)
     super(NetworkStatusDocumentV3, self).__init__(raw_content)
     document_file = StringIO.StringIO(raw_content)
 
diff --git a/test/integ/descriptor/networkstatus.py b/test/integ/descriptor/networkstatus.py
index df676b4..3f758b4 100644
--- a/test/integ/descriptor/networkstatus.py
+++ b/test/integ/descriptor/networkstatus.py
@@ -43,9 +43,7 @@ class TestNetworkStatus(unittest.TestCase):
 
     count = 0
     with open(consensus_path, 'rb') as descriptor_file:
-      document_type = stem.descriptor.networkstatus.NetworkStatusDocumentV3
-
-      for router in stem.descriptor.networkstatus._parse_file(descriptor_file, document_type):
+      for router in stem.descriptor.parse_file(descriptor_file, "network-status-consensus-3 1.0", path = consensus_path):
         count += 1
 
         # We should have constant memory usage. Fail if we're using over 200 MB.
@@ -90,9 +88,7 @@ class TestNetworkStatus(unittest.TestCase):
 
     count = 0
     with open(consensus_path, 'rb') as descriptor_file:
-      document_type = stem.descriptor.networkstatus.NetworkStatusDocumentV3
-
-      for router in stem.descriptor.networkstatus._parse_file(descriptor_file, document_type, is_microdescriptor = True):
+      for router in stem.descriptor.parse_file(descriptor_file, "network-status-microdesc-consensus-3 1.0", path = consensus_path):
         count += 1
 
         if resource.getrusage(resource.RUSAGE_SELF).ru_maxrss > 200000:
@@ -143,9 +139,7 @@ class TestNetworkStatus(unittest.TestCase):
     consensus_path = get_resource("bridge_network_status")
 
     with open(consensus_path, 'rb') as descriptor_file:
-      descriptors = stem.descriptor.parse_file(descriptor_file, path = consensus_path)
-
-      router = next(descriptors)
+      router = next(stem.descriptor.parse_file(descriptor_file, path = consensus_path))
       self.assertEquals("Unnamed", router.nickname)
       self.assertEquals("0014A2055278DB3EB0E59EA701741416AF185558", router.fingerprint)
       self.assertEquals("FI74aFuNJZZQrgln0f+OaocMd0M", router.digest)
@@ -191,9 +185,7 @@ GM9hAsAMRX9Ogqhq5UjDNqEsvDKuyVeyh7unSZEOip9Zr6K/+7VsVPNb8vfBRBjo
     cert_path = get_resource("metrics_cert")
 
     with open(cert_path) as cert_file:
-      certs = stem.descriptor.parse_file(cert_file, path = cert_path)
-
-      cert = next(certs)
+      cert = next(stem.descriptor.parse_file(cert_file, path = cert_path))
       self.assertEquals(3, cert.version)
       self.assertEquals(None, cert.address)
       self.assertEquals(None, cert.dir_port)





More information about the tor-commits mailing list