changeset: 100092:26f6d8cc2749 parent: 100090:df3290046e62 parent: 100091:0681f0a1fe6e user: Victor Stinner date: Thu Jan 28 15:44:10 2016 +0100 files: Misc/NEWS Modules/socketmodule.c description: Merge 3.5: Issue #26227 diff -r df3290046e62 -r 26f6d8cc2749 Misc/NEWS --- a/Misc/NEWS Thu Jan 28 12:42:45 2016 +0200 +++ b/Misc/NEWS Thu Jan 28 15:44:10 2016 +0100 @@ -159,6 +159,10 @@ Library ------- +- Issue #26227: On Windows, getnameinfo(), gethostbyaddr() and + gethostbyname_ex() functions of the socket module now decode the hostname + from the ANSI code page rather than UTF-8. + - Issue #26099: The site module now writes an error into stderr if sitecustomize module can be imported but executing the module raise an ImportError. Same change for usercustomize. diff -r df3290046e62 -r 26f6d8cc2749 Modules/socketmodule.c --- a/Modules/socketmodule.c Thu Jan 28 12:42:45 2016 +0200 +++ b/Modules/socketmodule.c Thu Jan 28 15:44:10 2016 +0100 @@ -4519,6 +4519,19 @@ 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(name); +#else + /* Decode from UTF-8 */ + return PyUnicode_FromString(name); +#endif +} + /* Convenience function common to gethostbyname_ex and gethostbyaddr */ static PyObject * @@ -4529,6 +4542,7 @@ 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,10 @@ goto err; } - rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list); + name = sock_decode_hostname(h->h_name); + if (name == NULL) + goto err; + rtn_tuple = Py_BuildValue("NOO", name, name_list, addr_list); err: Py_XDECREF(name_list); @@ -5619,6 +5636,7 @@ 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 +5700,11 @@ 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)