Skip to content

Commit fc0275a

Browse files
committed
Issue #1027206: Support IDNA in gethostbyname, gethostbyname_ex and
getaddrinfo. Patch by David Watson.
1 parent d41a37a commit fc0275a

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

Lib/test/test_socket.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,11 @@ def testGetaddrinfo(self):
637637
flags=socket.AI_PASSIVE)
638638
self.assertEqual(a, b)
639639

640+
def test_idna(self):
641+
# these should all be successful
642+
socket.gethostbyname('испытание.python.org')
643+
socket.gethostbyname_ex('испытание.python.org')
644+
socket.getaddrinfo('испытание.python.org',0)
640645

641646
@unittest.skipUnless(thread, 'Threading required for this test.')
642647
class BasicTCPTest(SocketConnectedTest):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Core and Builtins
6666
Extensions
6767
----------
6868

69+
- Issue #1027206: Support IDNA in gethostbyname, gethostbyname_ex and getaddrinfo.
70+
6971
- Issue #9214: Set operations on a KeysView or ItemsView in collections
7072
now correctly return a set. (Patch by Eli Bendersky.)
7173

Modules/socketmodule.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,12 +3000,16 @@ socket_gethostbyname(PyObject *self, PyObject *args)
30003000
{
30013001
char *name;
30023002
sock_addr_t addrbuf;
3003+
PyObject *ret = NULL;
30033004

3004-
if (!PyArg_ParseTuple(args, "s:gethostbyname", &name))
3005+
if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
30053006
return NULL;
30063007
if (setipaddr(name, SAS2SA(&addrbuf), sizeof(addrbuf), AF_INET) < 0)
3007-
return NULL;
3008-
return makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
3008+
goto finally;
3009+
ret = makeipaddr(SAS2SA(&addrbuf), sizeof(struct sockaddr_in));
3010+
finally:
3011+
PyMem_Free(name);
3012+
return ret;
30093013
}
30103014

30113015
PyDoc_STRVAR(gethostbyname_doc,
@@ -3156,7 +3160,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
31563160
struct sockaddr_in addr;
31573161
#endif
31583162
struct sockaddr *sa;
3159-
PyObject *ret;
3163+
PyObject *ret = NULL;
31603164
#ifdef HAVE_GETHOSTBYNAME_R
31613165
struct hostent hp_allocated;
31623166
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
@@ -3171,10 +3175,10 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
31713175
#endif
31723176
#endif /* HAVE_GETHOSTBYNAME_R */
31733177

3174-
if (!PyArg_ParseTuple(args, "s:gethostbyname_ex", &name))
3178+
if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
31753179
return NULL;
31763180
if (setipaddr(name, (struct sockaddr *)&addr, sizeof(addr), AF_INET) < 0)
3177-
return NULL;
3181+
goto finally;
31783182
Py_BEGIN_ALLOW_THREADS
31793183
#ifdef HAVE_GETHOSTBYNAME_R
31803184
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
@@ -3204,6 +3208,8 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
32043208
#ifdef USE_GETHOSTBYNAME_LOCK
32053209
PyThread_release_lock(netdb_lock);
32063210
#endif
3211+
finally:
3212+
PyMem_Free(name);
32073213
return ret;
32083214
}
32093215

@@ -3228,7 +3234,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
32283234
struct sockaddr *sa = (struct sockaddr *)&addr;
32293235
char *ip_num;
32303236
struct hostent *h;
3231-
PyObject *ret;
3237+
PyObject *ret = NULL;
32323238
#ifdef HAVE_GETHOSTBYNAME_R
32333239
struct hostent hp_allocated;
32343240
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
@@ -3250,11 +3256,11 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
32503256
int al;
32513257
int af;
32523258

3253-
if (!PyArg_ParseTuple(args, "s:gethostbyaddr", &ip_num))
3259+
if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
32543260
return NULL;
32553261
af = AF_UNSPEC;
32563262
if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
3257-
return NULL;
3263+
goto finally;
32583264
af = sa->sa_family;
32593265
ap = NULL;
32603266
al = 0;
@@ -3271,7 +3277,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
32713277
#endif
32723278
default:
32733279
PyErr_SetString(socket_error, "unsupported address family");
3274-
return NULL;
3280+
goto finally;
32753281
}
32763282
Py_BEGIN_ALLOW_THREADS
32773283
#ifdef HAVE_GETHOSTBYNAME_R
@@ -3298,6 +3304,8 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
32983304
#ifdef USE_GETHOSTBYNAME_LOCK
32993305
PyThread_release_lock(netdb_lock);
33003306
#endif
3307+
finally:
3308+
PyMem_Free(ip_num);
33013309
return ret;
33023310
}
33033311

0 commit comments

Comments
 (0)