summaryrefslogtreecommitdiffstats
path: root/Modules/socketmodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-11-17 00:11:36 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-11-17 00:11:36 (GMT)
commit74168975cc9489e5f726d804cf51f36fd7e0a510 (patch)
treeebf1305a0ba44aec5010ff2c0fdc1ab7939d8b8b /Modules/socketmodule.c
parentee587eaa3668c5903acd9e93d46f269a7511d1c9 (diff)
downloadcpython-74168975cc9489e5f726d804cf51f36fd7e0a510.zip
cpython-74168975cc9489e5f726d804cf51f36fd7e0a510.tar.gz
cpython-74168975cc9489e5f726d804cf51f36fd7e0a510.tar.bz2
socket_gethostname() uses a wchar_t* with PyMem_Malloc() to avoid the
old Unicode API.
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r--Modules/socketmodule.c42
1 files changed, 26 insertions, 16 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 240feb7..4742b44 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3896,24 +3896,34 @@ socket_gethostname(PyObject *self, PyObject *unused)
Otherwise, gethostname apparently also returns the DNS name. */
wchar_t buf[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = Py_ARRAY_LENGTH(buf);
+ wchar_t *name;
PyObject *result;
- if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size)) {
- if (GetLastError() == ERROR_MORE_DATA) {
- /* MSDN says this may occur "because DNS allows longer names */
- 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 (GetComputerNameExW(ComputerNamePhysicalDnsHostname,
- PyUnicode_AS_UNICODE(result),
- &size))
- return result;
- Py_DECREF(result);
- }
- return PyErr_SetExcFromWindowsErr(PyExc_WindowsError, GetLastError());
+
+ if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf, &size))
+ return PyUnicode_FromUnicode(buf, size);
+
+ if (GetLastError() != ERROR_MORE_DATA)
+ return PyErr_SetFromWindowsErr(0);
+
+ if (size == 0)
+ return PyUnicode_New(0, 0);
+
+ /* MSDN says ERROR_MORE_DATA may occur because DNS allows longer
+ names */
+ name = PyMem_Malloc(size * sizeof(wchar_t));
+ if (!name)
+ 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
char buf[1024];
int res;