Debian Codegraph @

cpython27/Lib/ctypes/test/test_arrays.py

raw symbols SCIP
1 import unittest
2 from test.support import precisionbigmemtest, _2G
3 import sys
4 from ctypes import *
5
6 from ctypes.test import need_symbol
7
8 formats = "bBhHiIlLqQfd"
9
10 formats = c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, \
11 c_long, c_ulonglong, c_float, c_double, c_longdouble
12
13 class ArrayTestCase(unittest.TestCase):
14 def test_simple(self):
15 # create classes holding simple numeric types, and check
16 # various properties.
17
18 init = range(15, 25)
19
20 for fmt in formats:
21 alen = len(init)
22 int_array = ARRAY(fmt, alen)
23
24 ia = int_array(*init)
25 # length of instance ok?
26 self.assertEqual(len(ia), alen)
27
28 # slot values ok?
29 values = [ia[i] for i in range(alen)]
30 self.assertEqual(values, init)
31
32 # out-of-bounds accesses should be caught
33 with self.assertRaises(IndexError): ia[alen]
34 with self.assertRaises(IndexError): ia[-alen-1]
35
36 # change the items
37 from operator import setitem
38 new_values = range(42, 42+alen)
39 [setitem(ia, n, new_values[n]) for n in range(alen)]
40 values = [ia[i] for i in range(alen)]
41 self.assertEqual(values, new_values)
42
43 # are the items initialized to 0?
44 ia = int_array()
45 values = [ia[i] for i in range(alen)]
46 self.assertEqual(values, [0] * alen)
47
48 # Too many initializers should be caught
49 self.assertRaises(IndexError, int_array, *range(alen*2))
50
51 CharArray = ARRAY(c_char, 3)
52
53 ca = CharArray("a", "b", "c")
54
55 # Should this work? It doesn't:
56 # CharArray("abc")
57 self.assertRaises(TypeError, CharArray, "abc")
58
59 self.assertEqual(ca[0], "a")
60 self.assertEqual(ca[1], "b")
61 self.assertEqual(ca[2], "c")
62 self.assertEqual(ca[-3], "a")
63 self.assertEqual(ca[-2], "b")
64 self.assertEqual(ca[-1], "c")
65
66 self.assertEqual(len(ca), 3)
67
68 # slicing is now supported, but not extended slicing (3-argument)!
69 from operator import getslice, delitem
70 self.assertRaises(TypeError, getslice, ca, 0, 1, -1)
71
72 # cannot delete items
73 self.assertRaises(TypeError, delitem, ca, 0)
74
75 def test_numeric_arrays(self):
76
77 alen = 5
78
79 numarray = ARRAY(c_int, alen)
80
81 na = numarray()
82 values = [na[i] for i in range(alen)]
83 self.assertEqual(values, [0] * alen)
84
85 na = numarray(*[c_int()] * alen)
86 values = [na[i] for i in range(alen)]
87 self.assertEqual(values, [0]*alen)
88
89 na = numarray(1, 2, 3, 4, 5)
90 values = [i for i in na]
91 self.assertEqual(values, [1, 2, 3, 4, 5])
92
93 na = numarray(*map(c_int, (1, 2, 3, 4, 5)))
94 values = [i for i in na]
95 self.assertEqual(values, [1, 2, 3, 4, 5])
96
97 def test_classcache(self):
98 self.assertIsNot(ARRAY(c_int, 3), ARRAY(c_int, 4))
99 self.assertIs(ARRAY(c_int, 3), ARRAY(c_int, 3))
100
101 def test_from_address(self):
102 # Failed with 0.9.8, reported by JUrner
103 p = create_string_buffer("foo")
104 sz = (c_char * 3).from_address(addressof(p))
105 self.assertEqual(sz[:], "foo")
106 self.assertEqual(sz[::], "foo")
107 self.assertEqual(sz[::-1], "oof")
108 self.assertEqual(sz[::3], "f")
109 self.assertEqual(sz[1:4:2], "o")
110 self.assertEqual(sz.value, "foo")
111
112 @need_symbol('create_unicode_buffer')
113 def test_from_addressW(self):
114 p = create_unicode_buffer("foo")
115 sz = (c_wchar * 3).from_address(addressof(p))
116 self.assertEqual(sz[:], "foo")
117 self.assertEqual(sz[::], "foo")
118 self.assertEqual(sz[::-1], "oof")
119 self.assertEqual(sz[::3], "f")
120 self.assertEqual(sz[1:4:2], "o")
121 self.assertEqual(sz.value, "foo")
122
123 def test_cache(self):
124 # Array types are cached internally in the _ctypes extension,
125 # in a WeakValueDictionary. Make sure the array type is
126 # removed from the cache when the itemtype goes away. This
127 # test will not fail, but will show a leak in the testsuite.
128
129 # Create a new type:
130 class my_int(c_int):
131 pass
132 # Create a new array type based on it:
133 t1 = my_int * 1
134 t2 = my_int * 1
135 self.assertIs(t1, t2)
136
137 def test_empty_element_struct(self):
138 class EmptyStruct(Structure):
139 _fields_ = []
140
141 obj = (EmptyStruct * 2)() # bpo37188: Floating point exception
142 self.assertEqual(sizeof(obj), 0)
143
144 def test_empty_element_array(self):
145 class EmptyArray(Array):
146 _type_ = c_int
147 _length_ = 0
148
149 obj = (EmptyArray * 2)() # bpo37188: Floating point exception
150 self.assertEqual(sizeof(obj), 0)
151
152 def test_bpo36504_signed_int_overflow(self):
153 # The overflow check in PyCArrayType_new() could cause signed integer
154 # overflow.
155 with self.assertRaises(OverflowError):
156 c_char * sys.maxsize * 2
157
158 @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
159 @precisionbigmemtest(size=_2G, memuse=1, dry_run=False)
160 def test_large_array(self, size):
161 a = c_char * size
162
163 if __name__ == '__main__':
164 unittest.main()