Skip to content

Commit 6e998fa

Browse files
bpo-16396: Allow wintypes to be imported on non-Windows systems. (GH-21394)
Co-authored-by: Christian Heimes <[email protected]> (cherry picked from commit 5456e78) Co-authored-by: Jason R. Coombs <[email protected]>
1 parent dc785db commit 6e998fa

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Lib/ctypes/test/test_wintypes.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import sys
21
import unittest
32

3+
# also work on POSIX
4+
45
from ctypes import *
6+
from ctypes import wintypes
7+
58

6-
@unittest.skipUnless(sys.platform.startswith('win'), 'Windows-only test')
79
class WinTypesTest(unittest.TestCase):
810
def test_variant_bool(self):
9-
from ctypes import wintypes
1011
# reads 16-bits from memory, anything non-zero is True
1112
for true_value in (1, 32767, 32768, 65535, 65537):
1213
true = POINTER(c_int16)(c_int16(true_value))
@@ -37,5 +38,6 @@ def test_variant_bool(self):
3738
vb.value = []
3839
self.assertIs(vb.value, False)
3940

41+
4042
if __name__ == "__main__":
4143
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow ``ctypes.wintypes`` to be imported on non-Windows systems.

Modules/_ctypes/cfield.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,11 @@ i_get_sw(void *ptr, Py_ssize_t size)
695695
return PyLong_FromLong(val);
696696
}
697697

698-
#ifdef MS_WIN32
698+
#ifndef MS_WIN32
699+
/* http://msdn.microsoft.com/en-us/library/cc237864.aspx */
700+
#define VARIANT_FALSE 0x0000
701+
#define VARIANT_TRUE 0xFFFF
702+
#endif
699703
/* short BOOL - VARIANT_BOOL */
700704
static PyObject *
701705
vBOOL_set(void *ptr, PyObject *value, Py_ssize_t size)
@@ -717,7 +721,6 @@ vBOOL_get(void *ptr, Py_ssize_t size)
717721
{
718722
return PyBool_FromLong((long)*(short int *)ptr);
719723
}
720-
#endif
721724

722725
static PyObject *
723726
bool_set(void *ptr, PyObject *value, Py_ssize_t size)
@@ -1544,8 +1547,8 @@ static struct fielddesc formattable[] = {
15441547
#endif
15451548
#ifdef MS_WIN32
15461549
{ 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
1547-
{ 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
15481550
#endif
1551+
{ 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
15491552
#if SIZEOF__BOOL == 1
15501553
{ '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */
15511554
#elif SIZEOF__BOOL == SIZEOF_SHORT

0 commit comments

Comments
 (0)