summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ctypes/__init__.py3
-rw-r--r--Lib/ctypes/test/test_numbers.py31
-rw-r--r--Lib/ctypes/test/test_repr.py2
3 files changed, 33 insertions, 3 deletions
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index ef8e892..e2a0573 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -233,6 +233,9 @@ class c_void_p(_SimpleCData):
c_voidp = c_void_p # backwards compatibility (to a bug)
_check_size(c_void_p)
+class c_bool(_SimpleCData):
+ _type_ = "t"
+
# This cache maps types to pointers to them.
_pointer_type_cache = {}
diff --git a/Lib/ctypes/test/test_numbers.py b/Lib/ctypes/test/test_numbers.py
index c22688d..5cd3c94 100644
--- a/Lib/ctypes/test/test_numbers.py
+++ b/Lib/ctypes/test/test_numbers.py
@@ -24,6 +24,8 @@ ArgType = type(byref(c_int(0)))
unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong]
signed_types = [c_byte, c_short, c_int, c_long, c_longlong]
+bool_types = []
+
float_types = [c_double, c_float]
try:
@@ -35,8 +37,16 @@ else:
unsigned_types.append(c_ulonglong)
signed_types.append(c_longlong)
+try:
+ c_bool
+except NameError:
+ pass
+else:
+ bool_types.append(c_bool)
+
unsigned_ranges = valid_ranges(*unsigned_types)
signed_ranges = valid_ranges(*signed_types)
+bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]]
################################################################
@@ -59,6 +69,11 @@ class NumberTestCase(unittest.TestCase):
for t, (l, h) in zip(signed_types, signed_ranges):
self.failUnlessEqual(t(l).value, l)
self.failUnlessEqual(t(h).value, h)
+
+ def test_bool_values(self):
+ from operator import truth
+ for t, v in zip(bool_types, bool_values):
+ self.failUnlessEqual(t(v).value, truth(v))
def test_typeerror(self):
# Only numbers are allowed in the contructor,
@@ -82,7 +97,7 @@ class NumberTestCase(unittest.TestCase):
def test_byref(self):
# calling byref returns also a PyCArgObject instance
- for t in signed_types + unsigned_types + float_types:
+ for t in signed_types + unsigned_types + float_types + bool_types:
parm = byref(t())
self.failUnlessEqual(ArgType, type(parm))
@@ -101,7 +116,7 @@ class NumberTestCase(unittest.TestCase):
self.assertRaises(TypeError, t, 3.14)
def test_sizes(self):
- for t in signed_types + unsigned_types + float_types:
+ for t in signed_types + unsigned_types + float_types + bool_types:
size = struct.calcsize(t._type_)
# sizeof of the type...
self.failUnlessEqual(sizeof(t), size)
@@ -163,6 +178,18 @@ class NumberTestCase(unittest.TestCase):
a[0] = '?'
self.failUnlessEqual(v.value, a[0])
+
+ # array does not support c_bool / 't'
+ # def test_bool_from_address(self):
+ # from ctypes import c_bool
+ # from array import array
+ # a = array(c_bool._type_, [True])
+ # v = t.from_address(a.buffer_info()[0])
+ # self.failUnlessEqual(v.value, a[0])
+ # self.failUnlessEqual(type(v) is t)
+ # a[0] = False
+ # self.failUnlessEqual(v.value, a[0])
+ # self.failUnlessEqual(type(v) is t)
def test_init(self):
# c_int() can be initialized from Python's int, and c_int.
diff --git a/Lib/ctypes/test/test_repr.py b/Lib/ctypes/test/test_repr.py
index 1044f67..f6f9366 100644
--- a/Lib/ctypes/test/test_repr.py
+++ b/Lib/ctypes/test/test_repr.py
@@ -4,7 +4,7 @@ import unittest
subclasses = []
for base in [c_byte, c_short, c_int, c_long, c_longlong,
c_ubyte, c_ushort, c_uint, c_ulong, c_ulonglong,
- c_float, c_double]:
+ c_float, c_double, c_bool]:
class X(base):
pass
subclasses.append(X)