mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
Fixed socket_gethostname() on windows.
This commit is contained in:
parent
9696088b6d
commit
09fff7a8d1
1 changed files with 9 additions and 6 deletions
|
@ -3097,17 +3097,20 @@ socket_gethostname(PyObject *self, PyObject *unused)
|
||||||
/* Don't use winsock's gethostname, as this returns the ANSI
|
/* Don't use winsock's gethostname, as this returns the ANSI
|
||||||
version of the hostname, whereas we need a Unicode string.
|
version of the hostname, whereas we need a Unicode string.
|
||||||
Otherwise, gethostname apparently also returns the DNS name. */
|
Otherwise, gethostname apparently also returns the DNS name. */
|
||||||
wchar_t buf[MAX_COMPUTERNAME_LENGTH];
|
wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
|
||||||
DWORD size = sizeof(buf);
|
DWORD size = sizeof(buf) / sizeof(wchar_t);
|
||||||
|
PyObject *result;
|
||||||
if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) {
|
if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) {
|
||||||
if (GetLastError() == ERROR_MORE_DATA) {
|
if (GetLastError() == ERROR_MORE_DATA) {
|
||||||
/* MSDN says this may occur "because DNS allows longer names */
|
/* MSDN says this may occur "because DNS allows longer names */
|
||||||
PyObject *result = PyUnicode_FromUnicode(NULL, size);
|
if (size == 0) /* XXX: I'm not sure how to handle this */
|
||||||
|
return PyUnicode_FromUnicode(NULL, 0);
|
||||||
|
result = PyUnicode_FromUnicode(NULL, size - 1);
|
||||||
if (!result)
|
if (!result)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (GetComputerName(ComputerNamePhysicalDnsHostname,
|
if (GetComputerNameExW(ComputerNamePhysicalDnsHostname,
|
||||||
PyUnicode_AS_UNICODE(result),
|
PyUnicode_AS_UNICODE(result),
|
||||||
size+1))
|
&size))
|
||||||
return result;
|
return result;
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue