Commit 9a0a5437 authored by Damian Johnson's avatar Damian Johnson
Browse files

Drop support for legacy tuple endpoints

parent 27cf44b8
Loading
Loading
Loading
Loading
+5 −12
Original line number Diff line number Diff line
@@ -346,11 +346,6 @@ class Query(object):
     this was called httplib.HTTPMessage, whereas in python3 the class was
     renamed to http.client.HTTPMessage.

  .. versionchanged:: 1.7.0
     Endpoints are now expected to be :class:`~stem.DirPort` or
     :class:`~stem.ORPort` instances. Usage of tuples for this
     argument is deprecated and will be removed in the future.

  .. versionchanged:: 1.7.0
     Avoid downloading from tor26. This directory authority throttles its
     DirPort to such an extent that requests either time out or take on the
@@ -459,12 +454,10 @@ class Query(object):

    if endpoints:
      for endpoint in endpoints:
        if isinstance(endpoint, tuple) and len(endpoint) == 2:
          self.endpoints.append(stem.DirPort(endpoint[0], endpoint[1]))  # TODO: remove this in stem 2.0
        elif isinstance(endpoint, (stem.ORPort, stem.DirPort)):
        if isinstance(endpoint, (stem.ORPort, stem.DirPort)):
          self.endpoints.append(endpoint)
        else:
          raise ValueError("Endpoints must be an stem.ORPort, stem.DirPort, or two value tuple. '%s' is a %s." % (endpoint, type(endpoint).__name__))
          raise ValueError("Endpoints must be an stem.ORPort or stem.DirPort. '%s' is a %s." % (endpoint, type(endpoint).__name__))

    self.resource = resource
    self.compression = new_compression
@@ -669,13 +662,13 @@ class DescriptorDownloader(object):
    """

    directories = [auth for auth in stem.directory.Authority.from_cache().values() if auth.nickname not in DIR_PORT_BLACKLIST]
    new_endpoints = set([(directory.address, directory.dir_port) for directory in directories])
    new_endpoints = set([stem.DirPort(directory.address, directory.dir_port) for directory in directories])

    consensus = list(self.get_consensus(document_handler = stem.descriptor.DocumentHandler.DOCUMENT).run())[0]

    for desc in consensus.routers.values():
      if stem.Flag.V2DIR in desc.flags and desc.dir_port:
        new_endpoints.add((desc.address, desc.dir_port))
        new_endpoints.add(stem.DirPort(desc.address, desc.dir_port))

    # we need our endpoints to be a list rather than set for random.choice()

@@ -858,7 +851,7 @@ class DescriptorDownloader(object):
    resource = '/tor/status-vote/current/authority'

    if 'endpoint' not in query_args:
      query_args['endpoints'] = [(authority.address, authority.dir_port)]
      query_args['endpoints'] = [stem.DirPort(authority.address, authority.dir_port)]

    return self.query(resource, **query_args)

+6 −6
Original line number Diff line number Diff line
@@ -301,7 +301,7 @@ class TestDescriptorDownloader(unittest.TestCase):
    query = stem.descriptor.remote.Query(
      TEST_RESOURCE,
      'server-descriptor 1.0',
      endpoints = [('128.31.0.39', 9131)],
      endpoints = [stem.DirPort('128.31.0.39', 9131)],
      compression = Compression.PLAINTEXT,
      validate = True,
    )
@@ -326,7 +326,7 @@ class TestDescriptorDownloader(unittest.TestCase):
    query = stem.descriptor.remote.Query(
      TEST_RESOURCE,
      'server-descriptor 1.0',
      endpoints = [('128.31.0.39', 9131)],
      endpoints = [stem.DirPort('128.31.0.39', 9131)],
      compression = Compression.PLAINTEXT,
      validate = True,
    )
@@ -353,7 +353,7 @@ class TestDescriptorDownloader(unittest.TestCase):
    query = stem.descriptor.remote.Query(
      TEST_RESOURCE,
      'server-descriptor 1.0',
      endpoints = [('128.31.0.39', 9131)],
      endpoints = [stem.DirPort('128.31.0.39', 9131)],
      fall_back_to_authority = False,
      timeout = 0.1,
      validate = True,
@@ -369,12 +369,12 @@ class TestDescriptorDownloader(unittest.TestCase):
    invalid_endpoints = {
      'hello': "'h' is a str.",
      ('hello',): "'hello' is a str.",
      (('hello',),): "'('hello',)' is a tuple.",
      (15,): "'15' is a int.",
      (('12.34.56.78', 15, 'third arg'),): "'('12.34.56.78', 15, 'third arg')' is a tuple.",
    }

    for endpoints, error_suffix in invalid_endpoints.items():
      expected_error = 'Endpoints must be an stem.ORPort, stem.DirPort, or two value tuple. ' + error_suffix
      expected_error = 'Endpoints must be an stem.ORPort or stem.DirPort. ' + error_suffix
      self.assertRaisesWith(ValueError, expected_error, stem.descriptor.remote.Query, TEST_RESOURCE, 'server-descriptor 1.0', endpoints = endpoints)

  @patch('urllib.request.urlopen', _dirport_mock(TEST_DESCRIPTOR))
@@ -382,7 +382,7 @@ class TestDescriptorDownloader(unittest.TestCase):
    query = stem.descriptor.remote.Query(
      TEST_RESOURCE,
      'server-descriptor 1.0',
      endpoints = [('128.31.0.39', 9131)],
      endpoints = [stem.DirPort('128.31.0.39', 9131)],
      compression = Compression.PLAINTEXT,
      validate = True,
    )