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