Mercurial > cpython-lru_cache-weakref
diff Modules/socketmodule.c @ 65721:a4fd3dc74299
Issue #9377: Use Unicode API for gethostname on Windows.
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Fri, 29 Oct 2010 18:20:08 +0000 |
| parents | cff78ffb932a |
| children | 2c2eced635f6 |
line wrap: on
line diff
--- a/Modules/socketmodule.c Fri Oct 29 18:15:33 2010 +0000 +++ b/Modules/socketmodule.c Fri Oct 29 18:20:08 2010 +0000 @@ -3093,6 +3093,27 @@ static PyObject * socket_gethostname(PyObject *self, PyObject *unused) { +#ifdef MS_WINDOWS + /* Don't use winsock's gethostname, as this returns the ANSI + version of the hostname, whereas we need a Unicode string. + Otherwise, gethostname apparently also returns the DNS name. */ + wchar_t buf[MAX_COMPUTERNAME_LENGTH]; + DWORD size = sizeof(buf); + if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) { + if (GetLastError() == ERROR_MORE_DATA) { + /* MSDN says this may occur "because DNS allows longer names */ + PyObject *result = PyUnicode_FromUnicode(NULL, size); + if (!result) + return NULL; + if (GetComputerName(ComputerNamePhysicalDnsHostname, + PyUnicode_AS_UNICODE(result), + size+1)) + return result; + } + return PyErr_SetExcFromWindowsErr(PyExc_WindowsError, GetLastError()); + } + return PyUnicode_FromUnicode(buf, size); +#else char buf[1024]; int res; Py_BEGIN_ALLOW_THREADS @@ -3102,6 +3123,7 @@ return set_error(); buf[sizeof buf - 1] = '\0'; return PyUnicode_FromString(buf); +#endif } PyDoc_STRVAR(gethostname_doc,
