Commit 3c8194e6 authored by Victor Stinner's avatar Victor Stinner

socket_gethostname() uses a wchar_t* with PyMem_Malloc() to avoid the

old Unicode API.
parent 2967d4ef
...@@ -3896,24 +3896,34 @@ socket_gethostname(PyObject *self, PyObject *unused) ...@@ -3896,24 +3896,34 @@ socket_gethostname(PyObject *self, PyObject *unused)
Otherwise, gethostname apparently also returns the DNS name. */ Otherwise, gethostname apparently also returns the DNS name. */
wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1]; wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = Py_ARRAY_LENGTH(buf); DWORD size = Py_ARRAY_LENGTH(buf);
wchar_t *name;
PyObject *result; PyObject *result;
if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) {
if (GetLastError() == ERROR_MORE_DATA) { if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
/* MSDN says this may occur "because DNS allows longer names */ return PyUnicode_FromUnicode(buf, size);
if (size == 0) /* XXX: I'm not sure how to handle this */
return PyUnicode_FromUnicode(NULL, 0); if (GetLastError() != ERROR_MORE_DATA)
result = PyUnicode_FromUnicode(NULL, size - 1); return PyErr_SetFromWindowsErr(0);
if (!result)
return NULL; if (size == 0)
if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, return PyUnicode_New(0, 0);
PyUnicode_AS_UNICODE(result),
/* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
names */
name = PyMem_Malloc(size * sizeof(wchar_t));
if (!name)
return NULL;
if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
name,
&size)) &size))
return result; {
Py_DECREF(result); PyMem_Free(name);
} return PyErr_SetFromWindowsErr(0);
return PyErr_SetExcFromWindowsErr(PyExc_WindowsError, GetLastError());
} }
return PyUnicode_FromUnicode(buf, size);
result = PyUnicode_FromWideChar(name, size);
PyMem_Free(name);
return result;
#else #else
char buf[1024]; char buf[1024];
int res; int res;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment