4
4
except ImportError : # pragma: no cover (PY2)
5
5
import collections
6
6
collections .abc = collections
7
- from copy import copy
8
7
9
8
import six
10
9
@@ -120,8 +119,10 @@ def __init__(self, addr):
120
119
self ._bytes = string_to_bytes (addr )
121
120
elif isinstance (addr , six .binary_type ):
122
121
self ._bytes = addr
122
+ elif isinstance (addr , Multiaddr ):
123
+ self ._bytes = addr .to_bytes ()
123
124
else :
124
- raise TypeError ("MultiAddr must be bytes or str " )
125
+ raise TypeError ("MultiAddr must be bytes, str or another MultiAddr instance " )
125
126
126
127
def __eq__ (self , other ):
127
128
"""Checks if two Multiaddr objects are exactly equal."""
@@ -179,7 +180,7 @@ def encapsulate(self, other):
179
180
/ip4/1.2.3.4 encapsulate /tcp/80 = /ip4/1.2.3.4/tcp/80
180
181
"""
181
182
mb = self .to_bytes ()
182
- ob = other .to_bytes ()
183
+ ob = Multiaddr ( other ) .to_bytes ()
183
184
return Multiaddr (b'' .join ([mb , ob ]))
184
185
185
186
def decapsulate (self , other ):
@@ -189,12 +190,12 @@ def decapsulate(self, other):
189
190
/ip4/1.2.3.4/tcp/80 decapsulate /ip4/1.2.3.4 = /tcp/80
190
191
"""
191
192
s1 = self .to_bytes ()
192
- s2 = other .to_bytes ()
193
+ s2 = Multiaddr ( other ) .to_bytes ()
193
194
try :
194
195
idx = s1 .rindex (s2 )
195
196
except ValueError :
196
197
# if multiaddr not contained, returns a copy
197
- return copy (self )
198
+ return Multiaddr (self )
198
199
return Multiaddr (s1 [:idx ])
199
200
200
201
def value_for_protocol (self , proto ):
0 commit comments