1 | n/a | import unittest |
---|
2 | n/a | from test import support |
---|
3 | n/a | |
---|
4 | n/a | # Skip this test if the _testcapi module isn't available. |
---|
5 | n/a | support.import_module('_testcapi') |
---|
6 | n/a | from _testcapi import _test_structmembersType, \ |
---|
7 | n/a | CHAR_MAX, CHAR_MIN, UCHAR_MAX, \ |
---|
8 | n/a | SHRT_MAX, SHRT_MIN, USHRT_MAX, \ |
---|
9 | n/a | INT_MAX, INT_MIN, UINT_MAX, \ |
---|
10 | n/a | LONG_MAX, LONG_MIN, ULONG_MAX, \ |
---|
11 | n/a | LLONG_MAX, LLONG_MIN, ULLONG_MAX, \ |
---|
12 | n/a | PY_SSIZE_T_MAX, PY_SSIZE_T_MIN |
---|
13 | n/a | |
---|
14 | n/a | ts=_test_structmembersType(False, # T_BOOL |
---|
15 | n/a | 1, # T_BYTE |
---|
16 | n/a | 2, # T_UBYTE |
---|
17 | n/a | 3, # T_SHORT |
---|
18 | n/a | 4, # T_USHORT |
---|
19 | n/a | 5, # T_INT |
---|
20 | n/a | 6, # T_UINT |
---|
21 | n/a | 7, # T_LONG |
---|
22 | n/a | 8, # T_ULONG |
---|
23 | n/a | 23, # T_PYSSIZET |
---|
24 | n/a | 9.99999,# T_FLOAT |
---|
25 | n/a | 10.1010101010, # T_DOUBLE |
---|
26 | n/a | "hi" # T_STRING_INPLACE |
---|
27 | n/a | ) |
---|
28 | n/a | |
---|
29 | n/a | class ReadWriteTests(unittest.TestCase): |
---|
30 | n/a | |
---|
31 | n/a | def test_bool(self): |
---|
32 | n/a | ts.T_BOOL = True |
---|
33 | n/a | self.assertEqual(ts.T_BOOL, True) |
---|
34 | n/a | ts.T_BOOL = False |
---|
35 | n/a | self.assertEqual(ts.T_BOOL, False) |
---|
36 | n/a | self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1) |
---|
37 | n/a | |
---|
38 | n/a | def test_byte(self): |
---|
39 | n/a | ts.T_BYTE = CHAR_MAX |
---|
40 | n/a | self.assertEqual(ts.T_BYTE, CHAR_MAX) |
---|
41 | n/a | ts.T_BYTE = CHAR_MIN |
---|
42 | n/a | self.assertEqual(ts.T_BYTE, CHAR_MIN) |
---|
43 | n/a | ts.T_UBYTE = UCHAR_MAX |
---|
44 | n/a | self.assertEqual(ts.T_UBYTE, UCHAR_MAX) |
---|
45 | n/a | |
---|
46 | n/a | def test_short(self): |
---|
47 | n/a | ts.T_SHORT = SHRT_MAX |
---|
48 | n/a | self.assertEqual(ts.T_SHORT, SHRT_MAX) |
---|
49 | n/a | ts.T_SHORT = SHRT_MIN |
---|
50 | n/a | self.assertEqual(ts.T_SHORT, SHRT_MIN) |
---|
51 | n/a | ts.T_USHORT = USHRT_MAX |
---|
52 | n/a | self.assertEqual(ts.T_USHORT, USHRT_MAX) |
---|
53 | n/a | |
---|
54 | n/a | def test_int(self): |
---|
55 | n/a | ts.T_INT = INT_MAX |
---|
56 | n/a | self.assertEqual(ts.T_INT, INT_MAX) |
---|
57 | n/a | ts.T_INT = INT_MIN |
---|
58 | n/a | self.assertEqual(ts.T_INT, INT_MIN) |
---|
59 | n/a | ts.T_UINT = UINT_MAX |
---|
60 | n/a | self.assertEqual(ts.T_UINT, UINT_MAX) |
---|
61 | n/a | |
---|
62 | n/a | def test_long(self): |
---|
63 | n/a | ts.T_LONG = LONG_MAX |
---|
64 | n/a | self.assertEqual(ts.T_LONG, LONG_MAX) |
---|
65 | n/a | ts.T_LONG = LONG_MIN |
---|
66 | n/a | self.assertEqual(ts.T_LONG, LONG_MIN) |
---|
67 | n/a | ts.T_ULONG = ULONG_MAX |
---|
68 | n/a | self.assertEqual(ts.T_ULONG, ULONG_MAX) |
---|
69 | n/a | |
---|
70 | n/a | def test_py_ssize_t(self): |
---|
71 | n/a | ts.T_PYSSIZET = PY_SSIZE_T_MAX |
---|
72 | n/a | self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MAX) |
---|
73 | n/a | ts.T_PYSSIZET = PY_SSIZE_T_MIN |
---|
74 | n/a | self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MIN) |
---|
75 | n/a | |
---|
76 | n/a | @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present") |
---|
77 | n/a | def test_longlong(self): |
---|
78 | n/a | ts.T_LONGLONG = LLONG_MAX |
---|
79 | n/a | self.assertEqual(ts.T_LONGLONG, LLONG_MAX) |
---|
80 | n/a | ts.T_LONGLONG = LLONG_MIN |
---|
81 | n/a | self.assertEqual(ts.T_LONGLONG, LLONG_MIN) |
---|
82 | n/a | |
---|
83 | n/a | ts.T_ULONGLONG = ULLONG_MAX |
---|
84 | n/a | self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX) |
---|
85 | n/a | |
---|
86 | n/a | ## make sure these will accept a plain int as well as a long |
---|
87 | n/a | ts.T_LONGLONG = 3 |
---|
88 | n/a | self.assertEqual(ts.T_LONGLONG, 3) |
---|
89 | n/a | ts.T_ULONGLONG = 4 |
---|
90 | n/a | self.assertEqual(ts.T_ULONGLONG, 4) |
---|
91 | n/a | |
---|
92 | n/a | def test_bad_assignments(self): |
---|
93 | n/a | integer_attributes = [ |
---|
94 | n/a | 'T_BOOL', |
---|
95 | n/a | 'T_BYTE', 'T_UBYTE', |
---|
96 | n/a | 'T_SHORT', 'T_USHORT', |
---|
97 | n/a | 'T_INT', 'T_UINT', |
---|
98 | n/a | 'T_LONG', 'T_ULONG', |
---|
99 | n/a | 'T_PYSSIZET' |
---|
100 | n/a | ] |
---|
101 | n/a | if hasattr(ts, 'T_LONGLONG'): |
---|
102 | n/a | integer_attributes.extend(['T_LONGLONG', 'T_ULONGLONG']) |
---|
103 | n/a | |
---|
104 | n/a | # issue8014: this produced 'bad argument to internal function' |
---|
105 | n/a | # internal error |
---|
106 | n/a | for nonint in None, 3.2j, "full of eels", {}, []: |
---|
107 | n/a | for attr in integer_attributes: |
---|
108 | n/a | self.assertRaises(TypeError, setattr, ts, attr, nonint) |
---|
109 | n/a | |
---|
110 | n/a | def test_inplace_string(self): |
---|
111 | n/a | self.assertEqual(ts.T_STRING_INPLACE, "hi") |
---|
112 | n/a | self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s") |
---|
113 | n/a | self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE") |
---|
114 | n/a | |
---|
115 | n/a | |
---|
116 | n/a | class TestWarnings(unittest.TestCase): |
---|
117 | n/a | |
---|
118 | n/a | def test_byte_max(self): |
---|
119 | n/a | with support.check_warnings(('', RuntimeWarning)): |
---|
120 | n/a | ts.T_BYTE = CHAR_MAX+1 |
---|
121 | n/a | |
---|
122 | n/a | def test_byte_min(self): |
---|
123 | n/a | with support.check_warnings(('', RuntimeWarning)): |
---|
124 | n/a | ts.T_BYTE = CHAR_MIN-1 |
---|
125 | n/a | |
---|
126 | n/a | def test_ubyte_max(self): |
---|
127 | n/a | with support.check_warnings(('', RuntimeWarning)): |
---|
128 | n/a | ts.T_UBYTE = UCHAR_MAX+1 |
---|
129 | n/a | |
---|
130 | n/a | def test_short_max(self): |
---|
131 | n/a | with support.check_warnings(('', RuntimeWarning)): |
---|
132 | n/a | ts.T_SHORT = SHRT_MAX+1 |
---|
133 | n/a | |
---|
134 | n/a | def test_short_min(self): |
---|
135 | n/a | with support.check_warnings(('', RuntimeWarning)): |
---|
136 | n/a | ts.T_SHORT = SHRT_MIN-1 |
---|
137 | n/a | |
---|
138 | n/a | def test_ushort_max(self): |
---|
139 | n/a | with support.check_warnings(('', RuntimeWarning)): |
---|
140 | n/a | ts.T_USHORT = USHRT_MAX+1 |
---|
141 | n/a | |
---|
142 | n/a | |
---|
143 | n/a | if __name__ == "__main__": |
---|
144 | n/a | unittest.main() |
---|