[Python-Dev] [Python-checkins] r85934 - in python/branches/py3k: Misc/NEWS Modules/socketmodule.c (original) (raw)
Hirokazu Yamamoto ocean-city at m2.ccsnet.ne.jp
Sat Oct 30 16:48:20 CEST 2010
- Previous message: [Python-Dev] r85960 - python/branches/py3k/Lib/test/test_mailbox.py
- Next message: [Python-Dev] [Python-checkins] r85934 - in python/branches/py3k: Misc/NEWS Modules/socketmodule.c
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 * socketgethostname(PyObject *self, PyObject *unused) { +#ifdef MSWINDOWS + /* 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. */ + wchart buf[MAXCOMPUTERNAMELENGTH]; + DWORD size = sizeof(buf); + if (!GetComputerNameExW(ComputerNamePhysicalDnsHostname, buf,&size)) { + if (GetLastError() == ERRORMOREDATA) { + /* MSDN says this may occur "because DNS allows longer names */ + PyObject *result = PyUnicodeFromUnicode(NULL, size); + if (!result) + return NULL; + if (GetComputerName(ComputerNamePhysicalDnsHostname, + PyUnicodeASUNICODE(result), + size+1)) + return result; + } + return PyErrSetExcFromWindowsErr(PyExcWindowsError, GetLastError()); + } + return PyUnicodeFromUnicode(buf, size); +#else char buf[1024]; int res; PyBEGINALLOWTHREADS @@ -3102,6 +3123,7 @@ return seterror(); buf[sizeof buf - 1] = '\0'; return PyUnicodeFromString(buf); +#endif }
PyDocSTRVAR(gethostnamedoc,
Python-checkins mailing list Python-checkins at 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?)
- Previous message: [Python-Dev] r85960 - python/branches/py3k/Lib/test/test_mailbox.py
- Next message: [Python-Dev] [Python-checkins] r85934 - in python/branches/py3k: Misc/NEWS Modules/socketmodule.c
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]