summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>2010-11-05 17:24:13 (GMT)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>2010-11-05 17:24:13 (GMT)
commit09fff7a8d185c5b87a979eefb94a2aa462b8a035 (patch)
tree4eb286fd4b434d73e2a0c3dfaf163d3daa38b469
parent9696088b6d04efe179a825f05060ea3c96a625eb (diff)
downloadcpython-09fff7a8d185c5b87a979eefb94a2aa462b8a035.zip
cpython-09fff7a8d185c5b87a979eefb94a2aa462b8a035.tar.gz
cpython-09fff7a8d185c5b87a979eefb94a2aa462b8a035.tar.bz2
Fixed socket_gethostname() on windows.
-rw-r--r--Modules/socketmodule.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index a1a90f4..e24bf54 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3097,17 +3097,20 @@ socket_gethostname(PyObject *self, PyObject *unused)
/* 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);
+ wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
+ DWORD size = sizeof(buf) / sizeof(wchar_t);
+ PyObject *result;
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 (size == 0) /* XXX: I'm not sure how to handle this */
+ return PyUnicode_FromUnicode(NULL, 0);
+ result = PyUnicode_FromUnicode(NULL, size - 1);
if (!result)
return NULL;
- if (GetComputerName(ComputerNamePhysicalDnsHostname,
- PyUnicode_AS_UNICODE(result),
- size+1))
+ if (GetComputerNameExW(ComputerNamePhysicalDnsHostname,
+ PyUnicode_AS_UNICODE(result),
+ &size))
return result;
Py_DECREF(result);
}