Eryk Sun added the comment:

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'

----------
nosy: +eryksun

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28474>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to