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