diff options
author | Miss Skeleton (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-10-19 22:32:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-19 22:32:36 (GMT) |
commit | 05d52a0ad69cbadd4b048f2a97991e4e58d4a922 (patch) | |
tree | 51120e341591152f98279a09fdccb2c2fc576cba | |
parent | b62ecc21b451d2b8e041f5367c7b563ac01bbdd8 (diff) | |
download | cpython-05d52a0ad69cbadd4b048f2a97991e4e58d4a922.zip cpython-05d52a0ad69cbadd4b048f2a97991e4e58d4a922.tar.gz cpython-05d52a0ad69cbadd4b048f2a97991e4e58d4a922.tar.bz2 |
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 5456e78f4593edc277ab72fb9a9db1ebae7d4c2d)
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
-rw-r--r-- | Lib/ctypes/test/test_wintypes.py | 8 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-07-08-09-45-00.bpo-16936.z8o8Pn.rst | 1 | ||||
-rw-r--r-- | Modules/_ctypes/cfield.c | 9 |
3 files changed, 12 insertions, 6 deletions
diff --git a/Lib/ctypes/test/test_wintypes.py b/Lib/ctypes/test/test_wintypes.py index 71442df..243d596 100644 --- a/Lib/ctypes/test/test_wintypes.py +++ b/Lib/ctypes/test/test_wintypes.py @@ -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)) @@ -37,5 +38,6 @@ class WinTypesTest(unittest.TestCase): vb.value = [] self.assertIs(vb.value, False) + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Library/2020-07-08-09-45-00.bpo-16936.z8o8Pn.rst b/Misc/NEWS.d/next/Library/2020-07-08-09-45-00.bpo-16936.z8o8Pn.rst new file mode 100644 index 0000000..c76db4e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-08-09-45-00.bpo-16936.z8o8Pn.rst @@ -0,0 +1 @@ +Allow ``ctypes.wintypes`` to be imported on non-Windows systems. diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index a72682d..2261a59 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -684,7 +684,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) @@ -706,7 +710,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) @@ -1538,8 +1541,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 |