Skip to content

Commit

Permalink
bpo-16396: Allow wintypes to be imported on non-Windows systems. (pyt…
Browse files Browse the repository at this point in the history
…honGH-21394)

Co-authored-by: Christian Heimes <[email protected]>
  • Loading branch information
jaraco and tiran authored Oct 19, 2020
1 parent 33242a9 commit 5456e78
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Lib/ctypes/test/test_wintypes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import sys
import unittest

# also work on POSIX

from ctypes import *
from ctypes import wintypes


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


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow ``ctypes.wintypes`` to be imported on non-Windows systems.
9 changes: 6 additions & 3 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,11 @@ i_get_sw(void *ptr, Py_ssize_t size)
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 */
static PyObject *
vBOOL_set(void *ptr, PyObject *value, Py_ssize_t size)
Expand All @@ -680,7 +684,6 @@ vBOOL_get(void *ptr, Py_ssize_t size)
{
return PyBool_FromLong((long)*(short int *)ptr);
}
#endif

static PyObject *
bool_set(void *ptr, PyObject *value, Py_ssize_t size)
Expand Down Expand Up @@ -1511,8 +1514,8 @@ static struct fielddesc formattable[] = {
#endif
#ifdef MS_WIN32
{ 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
{ 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
#endif
{ 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
#if SIZEOF__BOOL == 1
{ '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */
#elif SIZEOF__BOOL == SIZEOF_SHORT
Expand Down

0 comments on commit 5456e78

Please sign in to comment.