socket_gethostname() uses a wchar_t* with PyMem_Malloc() to avoid the
old Unicode API.
This commit is contained in:
parent
ee587eaa36
commit
74168975cc
|
@ -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),
|
|
||||||
&size))
|
/* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
|
||||||
return result;
|
names */
|
||||||
Py_DECREF(result);
|
name = PyMem_Malloc(size * sizeof(wchar_t));
|
||||||
}
|
if (!name)
|
||||||
return PyErr_SetExcFromWindowsErr(PyExc_WindowsError, GetLastError());
|
return NULL;
|
||||||
|
if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname,
|
||||||
|
name,
|
||||||
|
&size))
|
||||||
|
{
|
||||||
|
PyMem_Free(name);
|
||||||
|
return PyErr_SetFromWindowsErr(0);
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
|
Loading…
Reference in New Issue