This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Kelvin You, amaury.forgeotdarc, belopolsky, eryksun, josh.r, meador.inge, paul.moore, steve.dower, tim.golden, zach.ware
Date 2016-10-20.16:48:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1476982130.37.0.96244748857.issue28474@psf.upfronthosting.co.za>
In-reply-to
Content
Kelvin is calling WlanScan [1], which returns an error code that apparently can include HRESULT (signed) values cast as DWORD (unsigned) values, including 0x80342002 (ERROR_NDIS_DOT11_POWER_STATE_INVALID). 

ctypes.FormatError calls FormatMessage [2] with the flag FORMAT_MESSAGE_FROM_SYSTEM. About half of the system error codes defined in winerror.h consist of COM HRESULT codes. But, to clarify Kelvin's link [3], this does not include NTSTATUS values from kernel-mode system calls. NTSTATUS values require the ntdll.dll message table.

[1]: https://msdn.microsoft.com/en-us/library/ms706783
[2]: https://msdn.microsoft.com/en-us/library/ms679351
[3]: https://msdn.microsoft.com/en-us/library/cc231196

A workaround in this case is to use the default c_int restype to return the error as a signed integer:

    >>> ctypes.FormatError(0x80342002 - 2**32)
    "The wireless local area network interface is powered down and doesn't
    support the requested operation."

A similar workaround works for setting the exit code to an HRESULT or NTSTATUS error code:

    >>> hex(subprocess.call('python -c "import os; os._exit(0xC0000001)"'))
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    OverflowError: Python int too large to convert to C long
    '0x1'
    >>> hex(subprocess.call('python -c "import sys; sys.exit(0xC0000001)"'))
    '0xffffffff'

    >>> hex(subprocess.call('python -c "import os; os._exit(0xC0000001 - 2**32)"'))
    '0xc0000001'
    >>> hex(subprocess.call('python -c "import sys; sys.exit(0xC0000001 - 2**32)"'))
    '0xc0000001'
History
Date User Action Args
2016-10-20 16:48:50eryksunsetrecipients: + eryksun, paul.moore, amaury.forgeotdarc, belopolsky, tim.golden, meador.inge, zach.ware, steve.dower, josh.r, Kelvin You
2016-10-20 16:48:50eryksunsetmessageid: <1476982130.37.0.96244748857.issue28474@psf.upfronthosting.co.za>
2016-10-20 16:48:50eryksunlinkissue28474 messages
2016-10-20 16:48:50eryksuncreate