summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2010-10-29 18:20:08 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2010-10-29 18:20:08 (GMT)
commit72f48422e21ce7b11d92744d52478edeb7fa4b0d (patch)
tree42676a53b6465451108ff6eb9e95494f6a20b7ab /Modules
parent83432babfd244f88a6fd7f336cfdb038fde5c4e8 (diff)
downloadcpython-72f48422e21ce7b11d92744d52478edeb7fa4b0d.zip
cpython-72f48422e21ce7b11d92744d52478edeb7fa4b0d.tar.gz
cpython-72f48422e21ce7b11d92744d52478edeb7fa4b0d.tar.bz2
Issue #9377: Use Unicode API for gethostname on Windows.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/socketmodule.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index fdbf7ee..e852287 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3093,6 +3093,27 @@ static PyTypeObject sock_type = {
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 @@ socket_gethostname(PyObject *self, PyObject *unused)
return set_error();
buf[sizeof buf - 1] = '\0';
return PyUnicode_FromString(buf);
+#endif
}
PyDoc_STRVAR(gethostname_doc,