Skip to content

Commit d82d5c8

Browse files
committed
Make sure that getaddrinfo() accepts bytes (as asyncio/socket version)
1 parent 193ee32 commit d82d5c8

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

tests/test_dns.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def _test_getnameinfo(self, *args, **kwargs):
4646
self.loop.getnameinfo(*args, **kwargs))
4747
except Exception as ex:
4848
if err is not None:
49-
self.assertEqual(ex.__class__, err.__class__)
49+
if ex.__class__ is not err.__class__:
50+
print(ex, err)
51+
self.assertIs(ex.__class__, err.__class__)
5052
self.assertEqual(ex.args, err.args)
5153
else:
5254
raise
@@ -69,6 +71,12 @@ def test_getaddrinfo_3(self):
6971
def test_getaddrinfo_4(self):
7072
self._test_getaddrinfo(_HOST, _PORT, family=-1)
7173

74+
def test_getaddrinfo_5(self):
75+
self._test_getaddrinfo(_HOST, str(_PORT))
76+
77+
def test_getaddrinfo_6(self):
78+
self._test_getaddrinfo(_HOST.encode(), str(_PORT).encode())
79+
7280
######
7381

7482
def test_getnameinfo_1(self):

uvloop/dns.pyx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ cdef __convert_pyaddr_to_sockaddr(int family, object addr,
4343
system.sockaddr* res):
4444
cdef:
4545
int err
46-
int port
4746
int addr_len
4847
int scope_id = 0
4948
int flowinfo = 0
@@ -58,8 +57,12 @@ cdef __convert_pyaddr_to_sockaddr(int family, object addr,
5857
host = host.encode()
5958
if not isinstance(host, (bytes, bytearray)):
6059
raise TypeError('host must be a string or bytes object')
60+
if isinstance(port, bytes):
61+
port = port.decode()
62+
if isinstance(port, str):
63+
port = int(port)
6164

62-
err = uv.uv_ip4_addr(host, port, <system.sockaddr_in*>res)
65+
err = uv.uv_ip4_addr(host, <int>port, <system.sockaddr_in*>res)
6366
if err < 0:
6467
raise convert_error(err)
6568

@@ -146,7 +149,7 @@ cdef class AddrInfoRequest(UVRequest):
146149
object callback
147150

148151
def __cinit__(self, Loop loop,
149-
str host, int port,
152+
bytes host, bytes port,
150153
int family, int type, int proto, int flags,
151154
object callback):
152155

@@ -170,8 +173,8 @@ cdef class AddrInfoRequest(UVRequest):
170173
err = uv.uv_getaddrinfo(loop.uvloop,
171174
<uv.uv_getaddrinfo_t*>self.request,
172175
__on_addrinfo_resolved,
173-
host.encode('utf-8'),
174-
str(port).encode('latin-1'),
176+
host,
177+
port,
175178
&self.hints)
176179

177180
if err < 0:

uvloop/loop.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ cdef class Loop:
136136
cdef inline _check_closed(self)
137137
cdef inline _check_thread(self)
138138

139-
cdef _getaddrinfo(self, str host, int port,
139+
cdef _getaddrinfo(self, object host, object port,
140140
int family, int type,
141141
int proto, int flags,
142142
int unpack)

uvloop/loop.pyx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,22 @@ cdef class Loop:
489489
self._polls_gc[fd] = poll
490490
return result
491491

492-
cdef _getaddrinfo(self, str host, int port,
492+
cdef _getaddrinfo(self, object host, object port,
493493
int family, int type,
494494
int proto, int flags,
495495
int unpack):
496496

497+
if isinstance(port, str):
498+
port = port.encode()
499+
elif isinstance(port, int):
500+
port = str(port).encode()
501+
if not isinstance(port, bytes):
502+
raise TypeError('port must be a str, bytes or int')
503+
if isinstance(host, str):
504+
host = host.encode()
505+
if not isinstance(host, bytes):
506+
raise TypeError('host must be a str or bytes')
507+
497508
fut = self._new_future()
498509

499510
def callback(result):
@@ -895,7 +906,7 @@ cdef class Loop:
895906

896907
return future.result()
897908

898-
def getaddrinfo(self, str host, int port, *,
909+
def getaddrinfo(self, object host, object port, *,
899910
int family=0, int type=0, int proto=0, int flags=0):
900911

901912
return self._getaddrinfo(host, port, family, type, proto, flags, 1)
@@ -915,14 +926,6 @@ cdef class Loop:
915926
if sl < 2 or sl > 4:
916927
raise ValueError('sockaddr must be a tuple of 2, 3 or 4 values')
917928

918-
host = sockaddr[0]
919-
if not isinstance(host, str):
920-
raise TypeError('host must be a string')
921-
922-
port = sockaddr[1]
923-
if not isinstance(port, int):
924-
raise TypeError('port must be an int')
925-
926929
if sl > 2:
927930
flowinfo = sockaddr[2]
928931
if flowinfo < 0 or flowinfo > 0xfffff:
@@ -940,7 +943,7 @@ cdef class Loop:
940943
scope_id = 0
941944

942945
ai_cnt = await self._getaddrinfo(
943-
host, port,
946+
sockaddr[0], sockaddr[1],
944947
uv.AF_UNSPEC, # family
945948
uv.SOCK_DGRAM, # type
946949
0, # proto

0 commit comments

Comments
 (0)