Skip to content

Commit e9f6876

Browse files
committed
Return None on vlaue_for_protocol that does not have any value
1 parent 6f0acad commit e9f6876

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

multiaddr/multiaddr.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,21 @@ def decapsulate(self, other):
110110
return Multiaddr(s1[:idx])
111111

112112
def value_for_protocol(self, proto):
113-
"""Return the value (if any) following the specified protocol."""
113+
"""Return the value (if any) following the specified protocol
114+
115+
Returns
116+
-------
117+
union[object, NoneType]
118+
The parsed protocol value for the given protocol code or ``None``
119+
if the given protocol does not require any value
120+
121+
Raises
122+
------
123+
~multiaddr.exceptions.BinaryParseError
124+
The stored MultiAddr binary representation is invalid
125+
~multiaddr.exceptions.ProtocolLookupError
126+
MultiAddr does not contain any instance of this protocol
127+
"""
114128
if not isinstance(proto, protocols.Protocol):
115129
if isinstance(proto, int):
116130
proto = protocols.protocol_with_code(proto)
@@ -129,6 +143,6 @@ def value_for_protocol(self, proto):
129143
six.raise_from(exceptions.BinaryParseError(str(exc), self.to_bytes(), proto2.name, exc), exc)
130144
else:
131145
# We were given something like '/utp', which doesn't have
132-
# an address, so return ''
133-
return ''
146+
# an address, so return None
147+
return None
134148
raise exceptions.ProtocolLookupError(proto, str(self))

tests/test_multiaddr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,17 @@ def test_get_value():
190190
"p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
191191

192192
assert_value_for_proto(ma, P_IP4, "127.0.0.1")
193-
assert_value_for_proto(ma, P_UTP, "")
193+
assert_value_for_proto(ma, P_UTP, None)
194194
assert_value_for_proto(ma, P_TCP, "5555")
195195
assert_value_for_proto(ma, P_UDP, "1234")
196196
assert_value_for_proto(
197197
ma, P_P2P, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
198198
assert_value_for_proto(ma, "ip4", "127.0.0.1")
199-
assert_value_for_proto(ma, "utp", "")
199+
assert_value_for_proto(ma, "utp", None)
200200
assert_value_for_proto(ma, "tcp", "5555")
201201
assert_value_for_proto(ma, "udp", "1234")
202202
assert_value_for_proto(ma, protocol_with_name("ip4"), "127.0.0.1")
203-
assert_value_for_proto(ma, protocol_with_name("utp"), "")
203+
assert_value_for_proto(ma, protocol_with_name("utp"), None)
204204
assert_value_for_proto(ma, protocol_with_name("tcp"), "5555")
205205
assert_value_for_proto(ma, protocol_with_name("udp"), "1234")
206206

@@ -224,7 +224,7 @@ def test_get_value():
224224
a = Multiaddr("/ip4/0.0.0.0/udp/12345/utp") # ending in a no-value one.
225225
assert_value_for_proto(a, P_IP4, "0.0.0.0")
226226
assert_value_for_proto(a, P_UDP, "12345")
227-
assert_value_for_proto(a, P_UTP, "")
227+
assert_value_for_proto(a, P_UTP, None)
228228

229229
a = Multiaddr("/ip4/0.0.0.0/unix/a/b/c/d") # ending in a path one.
230230
assert_value_for_proto(a, P_IP4, "0.0.0.0")

0 commit comments

Comments
 (0)