Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8d2fb4

Browse files
committed
Merge 3.5: Issue #26227
2 parents 68df686 + 7240030 commit c8d2fb4

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ Core and Builtins
159159
Library
160160
-------
161161

162+
- Issue #26227: On Windows, getnameinfo(), gethostbyaddr() and
163+
gethostbyname_ex() functions of the socket module now decode the hostname
164+
from the ANSI code page rather than UTF-8.
165+
162166
- Issue #26099: The site module now writes an error into stderr if
163167
sitecustomize module can be imported but executing the module raise an
164168
ImportError. Same change for usercustomize.

Modules/socketmodule.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4519,6 +4519,19 @@ PyDoc_STRVAR(gethostbyname_doc,
45194519
Return the IP address (a string of the form '255.255.255.255') for a host.");
45204520

45214521

4522+
static PyObject*
4523+
sock_decode_hostname(const char *name)
4524+
{
4525+
#ifdef MS_WINDOWS
4526+
/* Issue #26227: gethostbyaddr() returns a string encoded
4527+
* to the ANSI code page */
4528+
return PyUnicode_DecodeFSDefault(name);
4529+
#else
4530+
/* Decode from UTF-8 */
4531+
return PyUnicode_FromString(name);
4532+
#endif
4533+
}
4534+
45224535
/* Convenience function common to gethostbyname_ex and gethostbyaddr */
45234536

45244537
static PyObject *
@@ -4529,6 +4542,7 @@ gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
45294542
PyObject *name_list = (PyObject *)NULL;
45304543
PyObject *addr_list = (PyObject *)NULL;
45314544
PyObject *tmp;
4545+
PyObject *name;
45324546

45334547
if (h == NULL) {
45344548
/* Let's get real error message to return */
@@ -4637,7 +4651,10 @@ gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
46374651
goto err;
46384652
}
46394653

4640-
rtn_tuple = Py_BuildValue("sOO", h->h_name, name_list, addr_list);
4654+
name = sock_decode_hostname(h->h_name);
4655+
if (name == NULL)
4656+
goto err;
4657+
rtn_tuple = Py_BuildValue("NOO", name, name_list, addr_list);
46414658

46424659
err:
46434660
Py_XDECREF(name_list);
@@ -5619,6 +5636,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
56195636
struct addrinfo hints, *res = NULL;
56205637
int error;
56215638
PyObject *ret = (PyObject *)NULL;
5639+
PyObject *name;
56225640

56235641
flags = flowinfo = scope_id = 0;
56245642
if (!PyArg_ParseTuple(args, "Oi:getnameinfo", &sa, &flags))
@@ -5682,7 +5700,11 @@ socket_getnameinfo(PyObject *self, PyObject *args)
56825700
set_gaierror(error);
56835701
goto fail;
56845702
}
5685-
ret = Py_BuildValue("ss", hbuf, pbuf);
5703+
5704+
name = sock_decode_hostname(hbuf);
5705+
if (name == NULL)
5706+
goto fail;
5707+
ret = Py_BuildValue("Ns", name, pbuf);
56865708

56875709
fail:
56885710
if (res)

0 commit comments

Comments
 (0)