diff -r 0fb96a43d381 Modules/socketmodule.c --- a/Modules/socketmodule.c Thu Jan 28 09:13:32 2016 +0200 +++ b/Modules/socketmodule.c Thu Jan 28 09:49:20 2016 +0100 @@ -4519,6 +4519,19 @@ PyDoc_STRVAR(gethostbyname_doc, Return the IP address (a string of the form '255.255.255.255') for a host."); +static PyObject* +sock_decode_hostname(const char *name) +{ +#ifdef MS_WINDOWS + /* Issue #26227: gethostbyaddr() returns a string encoded + * to the ANSI code page */ + return PyUnicode_DecodeFSDefault(h->h_name); +#else + /* Decode from UTF-8 */ + return PyUnicode_FromString(h->h_name); +#endif +} + /* Convenience function common to gethostbyname_ex and gethostbyaddr */ static PyObject * @@ -4529,6 +4542,7 @@ gethost_common(struct hostent *h, struct PyObject *name_list = (PyObject *)NULL; PyObject *addr_list = (PyObject *)NULL; PyObject *tmp; + PyObject *name; if (h == NULL) { /* Let's get real error message to return */ @@ -4637,7 +4651,11 @@ gethost_common(struct hostent *h, struct goto err; } - rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); + name = sock_decode_hostname(); + if (name == NULL) + goto err; + rtn_tuple = Py_BuildValue("OOO", name, name_list, addr_list); + Py_DECREF(name); err: Py_XDECREF(name_list); @@ -5619,6 +5637,7 @@ socket_getnameinfo(PyObject *self, PyObj struct addrinfo hints, *res = NULL; int error; PyObject *ret = (PyObject *)NULL; + PyObject *name; flags = flowinfo = scope_id = 0; if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags)) @@ -5682,7 +5701,11 @@ socket_getnameinfo(PyObject *self, PyObj set_gaierror(error); goto fail; } - ret = Py_BuildValue("ss", hbuf, pbuf); + + name = sock_decode_hostname(hbuf); + if (name == NULL) + goto fail; + ret = Py_BuildValue("Ns", name, pbuf); fail: if (res)