commit c1dc2ec4998d7816161beaa53ff19a2ca8a2564b Author: Damian Johnson atagar@torproject.org Date: Tue Feb 6 10:35:46 2018 -0800
Defer address validity check to Address constructor
There's no need to do upfront validation of our address since the Address constructor already does this. --- stem/client/__init__.py | 12 ++---------- stem/client/cell.py | 2 -- stem/client/datatype.py | 2 +- test/unit/client/address.py | 2 +- 4 files changed, 4 insertions(+), 14 deletions(-)
diff --git a/stem/client/__init__.py b/stem/client/__init__.py index 3f8507e5..b743a0eb 100644 --- a/stem/client/__init__.py +++ b/stem/client/__init__.py @@ -34,7 +34,7 @@ import stem.client.cell import stem.socket import stem.util.connection
-from stem.client.datatype import ZERO, AddrType, Address, KDF, split +from stem.client.datatype import ZERO, Address, KDF, split
__all__ = [ 'cell', @@ -71,13 +71,6 @@ class Relay(object): * :class:`stem.SocketError` if we're unable to establish a connection """
- if stem.util.connection.is_valid_ipv4_address(address): - addr_type = AddrType.IPv4 - elif stem.util.connection.is_valid_ipv6_address(address): - addr_type = AddrType.IPv6 - else: - raise ValueError("'%s' isn't an IPv4 or IPv6 address" % address) - if not stem.util.connection.is_valid_port(port): raise ValueError("'%s' isn't a valid port" % port) elif not link_protocols: @@ -111,10 +104,9 @@ class Relay(object): raise stem.SocketError('Unable to find a common link protocol. We support %s but %s:%i supports %s.' % (', '.join(link_protocols), address, port, ', '.join(versions_reply.versions)))
# TODO: we should fill in our address, right? - # TODO: what happens if we skip the NETINFO?
link_protocol = max(common_protocols) - conn.send(stem.client.cell.NetinfoCell(Address(address, addr_type), []).pack(link_protocol)) + conn.send(stem.client.cell.NetinfoCell(Address(address), []).pack(link_protocol))
return Relay(conn, link_protocol)
diff --git a/stem/client/cell.py b/stem/client/cell.py index 041e1c39..4ac8d40e 100644 --- a/stem/client/cell.py +++ b/stem/client/cell.py @@ -346,8 +346,6 @@ class RelayCell(CircuitCell):
@classmethod def _unpack(cls, content, circ_id, link_protocol): - orig_content = content - command, content = Size.CHAR.pop(content) recognized, content = Size.SHORT.pop(content) # 'recognized' field stream_id, content = Size.SHORT.pop(content) diff --git a/stem/client/datatype.py b/stem/client/datatype.py index 8b4c8e64..352a643e 100644 --- a/stem/client/datatype.py +++ b/stem/client/datatype.py @@ -347,7 +347,7 @@ class Address(Field): elif stem.util.connection.is_valid_ipv6_address(value): addr_type = AddrType.IPv6 else: - raise ValueError('Address type is required unless an IPv4 or IPv6 address') + raise ValueError("'%s' isn't an IPv4 or IPv6 address" % value)
self.type, self.type_int = AddrType.get(addr_type)
diff --git a/test/unit/client/address.py b/test/unit/client/address.py index f3da4971..09fcee95 100644 --- a/test/unit/client/address.py +++ b/test/unit/client/address.py @@ -44,7 +44,7 @@ class TestAddress(unittest.TestCase):
self.assertRaisesRegexp(ValueError, re.escape("Packed IPv4 addresses should be four bytes, but was: '\x7f\x00'"), Address, '\x7f\x00', 4) self.assertRaisesRegexp(ValueError, re.escape("Packed IPv6 addresses should be sixteen bytes, but was: '\x7f\x00'"), Address, '\x7f\x00', 6) - self.assertRaisesRegexp(ValueError, re.escape('Address type is required unless an IPv4 or IPv6 address'), Address, 'nope') + self.assertRaisesRegexp(ValueError, re.escape("'nope' isn't an IPv4 or IPv6 address"), Address, 'nope')
def test_unknown_type(self): addr = Address('hello', 12)