Re: [Python-Dev] [Python-checkins] r85934 - in python/branches/py3k: Misc/NEWS Modules/socketmodule.c
On 2010/10/30 3:20, martin.v.loewis wrote:
Modified: python/branches/py3k/Modules/socketmodule.c ============================================================================== --- python/branches/py3k/Modules/socketmodule.c (original) +++ python/branches/py3k/Modules/socketmodule.c Fri Oct 29 20:20:08 2010 @@ -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, _______________________________________________ Python-checkins mailing list Python-checkins@python.org http://mail.python.org/mailman/listinfo/python-checkins
I think size should be in TCHARs, not in bytes. (MSDN says so) And GetComputerName's signature differs from MSDN. (Maybe should use GetComputerNameExW again?)
I think size should be in TCHARs, not in bytes. (MSDN says so) And GetComputerName's signature differs from MSDN. (Maybe should use GetComputerNameExW again?)
You are right. So how about this patch? Index: Modules/socketmodule.c =================================================================== --- Modules/socketmodule.c (Revision 85983) +++ Modules/socketmodule.c (Arbeitskopie) @@ -3098,16 +3098,16 @@ 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); + DWORD size = sizeof(buf)/sizeof(wchar_t); 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)) + if (GetComputerNameExW(ComputerNamePhysicalDnsHostname, + PyUnicode_AS_UNICODE(result), + size+1)) return result; Py_DECREF(result); } Regards, Martin
On 10/30/2010 4:08 PM, Martin v. Löwis wrote:
I think size should be in TCHARs, not in bytes. (MSDN says so) And GetComputerName's signature differs from MSDN. (Maybe should use GetComputerNameExW again?)
You are right. So how about this patch?
Still not quite right. The call to GetComputerNameExW after ERROR_MORE_DATA (which gives the number of *bytes* needed) still needs to pass "size/sizeof(wchar_t)" back into GetComputerNameExW since it wants the number TCHARs. I don't think the +1 is needed either (MSDN says it already included the null-terminator in the byte count. -- Scott Dial scott@scottdial.com scodial@cs.indiana.edu
participants (3)
-
"Martin v. Löwis" -
Hirokazu Yamamoto -
Scott Dial