[Python-checkins] r85934 - in python/branches/py3k: Misc/NEWS Modules/socketmodule.c

Benjamin Peterson benjamin at python.org
Fri Oct 29 20:36:04 CEST 2010


2010/10/29 martin.v.loewis <python-checkins at python.org>:
> Author: martin.v.loewis
> Date: Fri Oct 29 20:20:08 2010
> New Revision: 85934
>
> Log:
> Issue #9377: Use Unicode API for gethostname on Windows.
>
> Modified:
>   python/branches/py3k/Misc/NEWS
>   python/branches/py3k/Modules/socketmodule.c
>
> Modified: python/branches/py3k/Misc/NEWS
> ==============================================================================
> --- python/branches/py3k/Misc/NEWS      (original)
> +++ python/branches/py3k/Misc/NEWS      Fri Oct 29 20:20:08 2010
> @@ -160,6 +160,8 @@
>  Extensions
>  ----------
>
> +- Issue #9377: Use Unicode API for gethostname on Windows.
> +
>  - Issue #10143: Update "os.pathconf" values.
>
>  - Issue #6518: Support context manager protcol for ossaudiodev types.
>
> 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;

A reference leak (of result) occurs here if GetComputerName fails again.


-- 
Regards,
Benjamin


More information about the Python-checkins mailing list