Skip to content

Commit 1d0f6fe

Browse files
committed
Avoid repeated lazy imports in address utilities
1 parent 6d82fce commit 1d0f6fe

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

multiaddr/codec.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,19 @@ def bytes_to_string(buf):
6060
return '/'.join(st)
6161

6262

63+
int_to_hex = None
64+
encode_big_endian_16 = None
65+
66+
6367
def address_string_to_bytes(proto, addr_string):
64-
from .util import int_to_hex
65-
from .util import encode_big_endian_16
68+
global int_to_hex
69+
if int_to_hex is None:
70+
from .util import int_to_hex
71+
72+
global encode_big_endian_16
73+
if encode_big_endian_16 is None:
74+
from .util import encode_big_endian_16
75+
6676
if proto.code == P_IP4: # ipv4
6777
try:
6878
ip = IPAddress(addr_string)
@@ -143,8 +153,13 @@ def address_string_to_bytes(proto, addr_string):
143153
raise ValueError("failed to parse %s addr: unknown" % proto.name)
144154

145155

156+
decode_big_endian_16 = None
157+
158+
146159
def address_bytes_to_string(proto, buf):
147-
from .util import decode_big_endian_16
160+
global decode_big_endian_16
161+
if decode_big_endian_16 is None:
162+
from .util import decode_big_endian_16
148163
if proto.code == P_IP4:
149164
return str(IPAddress(int(buf, 16), 4).ipv4())
150165
elif proto.code == P_IP6:

0 commit comments

Comments
 (0)