summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-09-28 14:32:11 (GMT)
committerGitHub <noreply@github.com>2017-09-28 14:32:11 (GMT)
commit7d6ddb96b34b94c1cbdf95baa94492c48426404e (patch)
treedfd55c6ab9a5d3e11c5d5fc9e9dae71501fa5870 /Lib
parentec3d34c5b2feb10cb4d7606a10d81c178c3afce3 (diff)
downloadcpython-7d6ddb96b34b94c1cbdf95baa94492c48426404e.zip
cpython-7d6ddb96b34b94c1cbdf95baa94492c48426404e.tar.gz
cpython-7d6ddb96b34b94c1cbdf95baa94492c48426404e.tar.bz2
bpo-28129: fix ctypes crashes (#386) (#3799)
* init commit, with initial tests for from_param and fields __set__ and __get__, and some additions to from_buffer and from_buffer_copy * added the rest of tests and patches. probably only a first draft. * removed trailing spaces * replace ctype with ctypes in error messages * change back from ctypes instance to ctype instance (cherry picked from commit 1bea762d9ec823544c530d567330a47f64d93d4f)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ctypes/test/test_frombuffer.py9
-rw-r--r--Lib/ctypes/test/test_funcptr.py5
-rw-r--r--Lib/ctypes/test/test_parameters.py10
-rw-r--r--Lib/ctypes/test/test_pointers.py5
-rw-r--r--Lib/ctypes/test/test_struct_fields.py24
5 files changed, 53 insertions, 0 deletions
diff --git a/Lib/ctypes/test/test_frombuffer.py b/Lib/ctypes/test/test_frombuffer.py
index 7ab38f1..55c2443 100644
--- a/Lib/ctypes/test/test_frombuffer.py
+++ b/Lib/ctypes/test/test_frombuffer.py
@@ -121,12 +121,21 @@ class Test(unittest.TestCase):
(c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int))
def test_abstract(self):
+ from ctypes import _Pointer, _SimpleCData, _CFuncPtr
+
self.assertRaises(TypeError, Array.from_buffer, bytearray(10))
self.assertRaises(TypeError, Structure.from_buffer, bytearray(10))
self.assertRaises(TypeError, Union.from_buffer, bytearray(10))
+ self.assertRaises(TypeError, _CFuncPtr.from_buffer, bytearray(10))
+ self.assertRaises(TypeError, _Pointer.from_buffer, bytearray(10))
+ self.assertRaises(TypeError, _SimpleCData.from_buffer, bytearray(10))
+
self.assertRaises(TypeError, Array.from_buffer_copy, b"123")
self.assertRaises(TypeError, Structure.from_buffer_copy, b"123")
self.assertRaises(TypeError, Union.from_buffer_copy, b"123")
+ self.assertRaises(TypeError, _CFuncPtr.from_buffer_copy, b"123")
+ self.assertRaises(TypeError, _Pointer.from_buffer_copy, b"123")
+ self.assertRaises(TypeError, _SimpleCData.from_buffer_copy, b"123")
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/ctypes/test/test_funcptr.py b/Lib/ctypes/test/test_funcptr.py
index f34734b..e0b9b54 100644
--- a/Lib/ctypes/test/test_funcptr.py
+++ b/Lib/ctypes/test/test_funcptr.py
@@ -123,5 +123,10 @@ class CFuncPtrTestCase(unittest.TestCase):
self.assertEqual(strtok(None, b"\n"), b"c")
self.assertEqual(strtok(None, b"\n"), None)
+ def test_abstract(self):
+ from ctypes import _CFuncPtr
+
+ self.assertRaises(TypeError, _CFuncPtr, 13, "name", 42, "iid")
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py
index a61f1c7..e4c25fd 100644
--- a/Lib/ctypes/test/test_parameters.py
+++ b/Lib/ctypes/test/test_parameters.py
@@ -170,6 +170,16 @@ class SimpleTypesTestCase(unittest.TestCase):
# ArgumentError: argument 1: ValueError: 99
self.assertRaises(ArgumentError, func, 99)
+ def test_abstract(self):
+ from ctypes import (Array, Structure, Union, _Pointer,
+ _SimpleCData, _CFuncPtr)
+
+ self.assertRaises(TypeError, Array.from_param, 42)
+ self.assertRaises(TypeError, Structure.from_param, 42)
+ self.assertRaises(TypeError, Union.from_param, 42)
+ self.assertRaises(TypeError, _CFuncPtr.from_param, 42)
+ self.assertRaises(TypeError, _Pointer.from_param, 42)
+ self.assertRaises(TypeError, _SimpleCData.from_param, 42)
@test.support.cpython_only
def test_issue31311(self):
diff --git a/Lib/ctypes/test/test_pointers.py b/Lib/ctypes/test/test_pointers.py
index 751f85f..e975158 100644
--- a/Lib/ctypes/test/test_pointers.py
+++ b/Lib/ctypes/test/test_pointers.py
@@ -213,6 +213,11 @@ class PointersTestCase(unittest.TestCase):
from ctypes import _pointer_type_cache
del _pointer_type_cache[id(P)]
+ def test_abstract(self):
+ from ctypes import _Pointer
+
+ self.assertRaises(TypeError, _Pointer.set_type, 42)
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/ctypes/test/test_struct_fields.py b/Lib/ctypes/test/test_struct_fields.py
index 22eb3b0..8045cc8 100644
--- a/Lib/ctypes/test/test_struct_fields.py
+++ b/Lib/ctypes/test/test_struct_fields.py
@@ -46,5 +46,29 @@ class StructFieldsTestCase(unittest.TestCase):
Y._fields_ = []
self.assertRaises(AttributeError, setattr, X, "_fields_", [])
+ # __set__ and __get__ should raise a TypeError in case their self
+ # argument is not a ctype instance.
+ def test___set__(self):
+ class MyCStruct(Structure):
+ _fields_ = (("field", c_int),)
+ self.assertRaises(TypeError,
+ MyCStruct.field.__set__, 'wrong type self', 42)
+
+ class MyCUnion(Union):
+ _fields_ = (("field", c_int),)
+ self.assertRaises(TypeError,
+ MyCUnion.field.__set__, 'wrong type self', 42)
+
+ def test___get__(self):
+ class MyCStruct(Structure):
+ _fields_ = (("field", c_int),)
+ self.assertRaises(TypeError,
+ MyCStruct.field.__get__, 'wrong type self', 42)
+
+ class MyCUnion(Union):
+ _fields_ = (("field", c_int),)
+ self.assertRaises(TypeError,
+ MyCUnion.field.__get__, 'wrong type self', 42)
+
if __name__ == "__main__":
unittest.main()