bpo-16396: Allow wintypes to be imported on non-Windows systems. (GH-21394)

Co-authored-by: Christian Heimes <christian@python.org>
(cherry picked from commit 5456e78f45)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
This commit is contained in:
Miss Skeleton (bot) 2020-10-19 15:29:37 -07:00 committed by GitHub
parent dc785db64d
commit 6e998fad1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -1,12 +1,13 @@
import sys
import unittest import unittest
from ctypes import * # also work on POSIX
from ctypes import *
from ctypes import wintypes
@unittest.skipUnless(sys.platform.startswith('win'), 'Windows-only test')
class WinTypesTest(unittest.TestCase): class WinTypesTest(unittest.TestCase):
def test_variant_bool(self): def test_variant_bool(self):
from ctypes import wintypes
# reads 16-bits from memory, anything non-zero is True # reads 16-bits from memory, anything non-zero is True
for true_value in (1, 32767, 32768, 65535, 65537): for true_value in (1, 32767, 32768, 65535, 65537):
true = POINTER(c_int16)(c_int16(true_value)) true = POINTER(c_int16)(c_int16(true_value))
@ -37,5 +38,6 @@ class WinTypesTest(unittest.TestCase):
vb.value = [] vb.value = []
self.assertIs(vb.value, False) self.assertIs(vb.value, False)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -0,0 +1 @@
Allow ``ctypes.wintypes`` to be imported on non-Windows systems.

View File

@ -695,7 +695,11 @@ i_get_sw(void *ptr, Py_ssize_t size)
return PyLong_FromLong(val); return PyLong_FromLong(val);
} }
#ifdef MS_WIN32 #ifndef MS_WIN32
/* http://msdn.microsoft.com/en-us/library/cc237864.aspx */
#define VARIANT_FALSE 0x0000
#define VARIANT_TRUE 0xFFFF
#endif
/* short BOOL - VARIANT_BOOL */ /* short BOOL - VARIANT_BOOL */
static PyObject * static PyObject *
vBOOL_set(void *ptr, PyObject *value, Py_ssize_t size) vBOOL_set(void *ptr, PyObject *value, Py_ssize_t size)
@ -717,7 +721,6 @@ vBOOL_get(void *ptr, Py_ssize_t size)
{ {
return PyBool_FromLong((long)*(short int *)ptr); return PyBool_FromLong((long)*(short int *)ptr);
} }
#endif
static PyObject * static PyObject *
bool_set(void *ptr, PyObject *value, Py_ssize_t size) bool_set(void *ptr, PyObject *value, Py_ssize_t size)
@ -1544,8 +1547,8 @@ static struct fielddesc formattable[] = {
#endif #endif
#ifdef MS_WIN32 #ifdef MS_WIN32
{ 'X', BSTR_set, BSTR_get, &ffi_type_pointer}, { 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
{ 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
#endif #endif
{ 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
#if SIZEOF__BOOL == 1 #if SIZEOF__BOOL == 1
{ '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */ { '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */
#elif SIZEOF__BOOL == SIZEOF_SHORT #elif SIZEOF__BOOL == SIZEOF_SHORT