Accept ASCII/surrogateescape strings in getnameinfo(). diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -361,6 +361,10 @@ class GeneralModuleTests(unittest.TestCa self.tryHostnameArgs(lambda host: socket.getaddrinfo(host, None), socket.gaierror) + def testGetnameinfoHostnames(self): + self.tryHostnameArgs(lambda host: socket.getnameinfo((host, 0), 0), + socket.gaierror) + def testSocketObjectHostnames(self): def f(host): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -4012,6 +4012,7 @@ socket_getnameinfo(PyObject *self, PyObj { PyObject *sa = (PyObject *)NULL; int flags; + PyObject *hostobj; char *hostp; int port, flowinfo, scope_id; char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; @@ -4027,9 +4028,10 @@ socket_getnameinfo(PyObject *self, PyObj "getnameinfo() argument 1 must be a tuple"); return NULL; } - if (!PyArg_ParseTuple(sa, "si|ii", - &hostp, &port, &flowinfo, &scope_id)) + if (!PyArg_ParseTuple(sa, "O&i|ii", hostname_converter, + &hostobj, &port, &flowinfo, &scope_id)) return NULL; + hostp = PyBytes_AS_STRING(hostobj); PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port); memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; @@ -4080,6 +4082,7 @@ socket_getnameinfo(PyObject *self, PyObj fail: if (res) freeaddrinfo(res); + Py_DECREF(hostobj); return ret; }