0001 import unittest 0002 from test import test_support 0003 import sys 0004 0005 import warnings, re 0006 warnings.filterwarnings("ignore", 0007 category=DeprecationWarning, 0008 message=".*integer argument expected, got float", 0009 module=__name__) 0010 warnings.filterwarnings("ignore", 0011 category=DeprecationWarning, 0012 message=".*integer argument expected, got float", 0013 module="unittest") 0014 0015 """ 0016 > How about the following counterproposal. This also changes some of 0017 > the other format codes to be a little more regular. 0018 > 0019 > Code C type Range check 0020 > 0021 > b unsigned char 0..UCHAR_MAX 0022 > h signed short SHRT_MIN..SHRT_MAX 0023 > B unsigned char none ** 0024 > H unsigned short none ** 0025 > k * unsigned long none 0026 > I * unsigned int 0..UINT_MAX 0027 0028 0029 > i int INT_MIN..INT_MAX 0030 > l long LONG_MIN..LONG_MAX 0031 0032 > K * unsigned long long none 0033 > L long long LLONG_MIN..LLONG_MAX 0034 0035 > Notes: 0036 > 0037 > * New format codes. 0038 > 0039 > ** Changed from previous "range-and-a-half" to "none"; the 0040 > range-and-a-half checking wasn't particularly useful. 0041 0042 Plus a C API or two, e.g. PyInt_AsLongMask() -> 0043 unsigned long and PyInt_AsLongLongMask() -> unsigned 0044 long long (if that exists). 0045 """ 0046 0047 LARGE = 0x7FFFFFFF 0048 VERY_LARGE = 0xFF0000121212121212121242L 0049 0050 from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \ 0051 INT_MIN, LONG_MIN, LONG_MAX 0052 0053 # fake, they are not defined in Python's header files 0054 LLONG_MAX = 2**63-1 0055 LLONG_MIN = -2**63 0056 ULLONG_MAX = 2**64-1 0057 0058 class Long: 0059 def __int__(self): 0060 return 99L 0061 0062 class Int: 0063 def __int__(self): 0064 return 99 0065 0066 class Unsigned_TestCase(unittest.TestCase): 0067 def test_b(self): 0068 from _testcapi import getargs_b 0069 # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) 0070 self.failUnlessEqual(3, getargs_b(3.14)) 0071 self.failUnlessEqual(99, getargs_b(Long())) 0072 self.failUnlessEqual(99, getargs_b(Int())) 0073 0074 self.assertRaises(OverflowError, getargs_b, -1) 0075 self.failUnlessEqual(0, getargs_b(0)) 0076 self.failUnlessEqual(UCHAR_MAX, getargs_b(UCHAR_MAX)) 0077 self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1) 0078 0079 self.failUnlessEqual(42, getargs_b(42)) 0080 self.failUnlessEqual(42, getargs_b(42L)) 0081 self.assertRaises(OverflowError, getargs_b, VERY_LARGE) 0082 0083 def test_B(self): 0084 from _testcapi import getargs_B 0085 # B returns 'unsigned char', no range checking 0086 self.failUnlessEqual(3, getargs_B(3.14)) 0087 self.failUnlessEqual(99, getargs_B(Long())) 0088 self.failUnlessEqual(99, getargs_B(Int())) 0089 0090 self.failUnlessEqual(UCHAR_MAX, getargs_B(-1)) 0091 self.failUnlessEqual(UCHAR_MAX, getargs_B(-1L)) 0092 self.failUnlessEqual(0, getargs_B(0)) 0093 self.failUnlessEqual(UCHAR_MAX, getargs_B(UCHAR_MAX)) 0094 self.failUnlessEqual(0, getargs_B(UCHAR_MAX+1)) 0095 0096 self.failUnlessEqual(42, getargs_B(42)) 0097 self.failUnlessEqual(42, getargs_B(42L)) 0098 self.failUnlessEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE)) 0099 0100 def test_H(self): 0101 from _testcapi import getargs_H 0102 # H returns 'unsigned short', no range checking 0103 self.failUnlessEqual(3, getargs_H(3.14)) 0104 self.failUnlessEqual(99, getargs_H(Long())) 0105 self.failUnlessEqual(99, getargs_H(Int())) 0106 0107 self.failUnlessEqual(USHRT_MAX, getargs_H(-1)) 0108 self.failUnlessEqual(0, getargs_H(0)) 0109 self.failUnlessEqual(USHRT_MAX, getargs_H(USHRT_MAX)) 0110 self.failUnlessEqual(0, getargs_H(USHRT_MAX+1)) 0111 0112 self.failUnlessEqual(42, getargs_H(42)) 0113 self.failUnlessEqual(42, getargs_H(42L)) 0114 0115 self.failUnlessEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE)) 0116 0117 def test_I(self): 0118 from _testcapi import getargs_I 0119 # I returns 'unsigned int', no range checking 0120 self.failUnlessEqual(3, getargs_I(3.14)) 0121 self.failUnlessEqual(99, getargs_I(Long())) 0122 self.failUnlessEqual(99, getargs_I(Int())) 0123 0124 self.failUnlessEqual(UINT_MAX, getargs_I(-1)) 0125 self.failUnlessEqual(0, getargs_I(0)) 0126 self.failUnlessEqual(UINT_MAX, getargs_I(UINT_MAX)) 0127 self.failUnlessEqual(0, getargs_I(UINT_MAX+1)) 0128 0129 self.failUnlessEqual(42, getargs_I(42)) 0130 self.failUnlessEqual(42, getargs_I(42L)) 0131 0132 self.failUnlessEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE)) 0133 0134 def test_k(self): 0135 from _testcapi import getargs_k 0136 # k returns 'unsigned long', no range checking 0137 # it does not accept float, or instances with __int__ 0138 self.assertRaises(TypeError, getargs_k, 3.14) 0139 self.assertRaises(TypeError, getargs_k, Long()) 0140 self.assertRaises(TypeError, getargs_k, Int()) 0141 0142 self.failUnlessEqual(ULONG_MAX, getargs_k(-1)) 0143 self.failUnlessEqual(0, getargs_k(0)) 0144 self.failUnlessEqual(ULONG_MAX, getargs_k(ULONG_MAX)) 0145 self.failUnlessEqual(0, getargs_k(ULONG_MAX+1)) 0146 0147 self.failUnlessEqual(42, getargs_k(42)) 0148 self.failUnlessEqual(42, getargs_k(42L)) 0149 0150 self.failUnlessEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) 0151 0152 class Signed_TestCase(unittest.TestCase): 0153 def test_i(self): 0154 from _testcapi import getargs_i 0155 # i returns 'int', and does range checking (INT_MIN ... INT_MAX) 0156 self.failUnlessEqual(3, getargs_i(3.14)) 0157 self.failUnlessEqual(99, getargs_i(Long())) 0158 self.failUnlessEqual(99, getargs_i(Int())) 0159 0160 self.assertRaises(OverflowError, getargs_i, INT_MIN-1) 0161 self.failUnlessEqual(INT_MIN, getargs_i(INT_MIN)) 0162 self.failUnlessEqual(INT_MAX, getargs_i(INT_MAX)) 0163 self.assertRaises(OverflowError, getargs_i, INT_MAX+1) 0164 0165 self.failUnlessEqual(42, getargs_i(42)) 0166 self.failUnlessEqual(42, getargs_i(42L)) 0167 self.assertRaises(OverflowError, getargs_i, VERY_LARGE) 0168 0169 def test_l(self): 0170 from _testcapi import getargs_l 0171 # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) 0172 self.failUnlessEqual(3, getargs_l(3.14)) 0173 self.failUnlessEqual(99, getargs_l(Long())) 0174 self.failUnlessEqual(99, getargs_l(Int())) 0175 0176 self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) 0177 self.failUnlessEqual(LONG_MIN, getargs_l(LONG_MIN)) 0178 self.failUnlessEqual(LONG_MAX, getargs_l(LONG_MAX)) 0179 self.assertRaises(OverflowError, getargs_l, LONG_MAX+1) 0180 0181 self.failUnlessEqual(42, getargs_l(42)) 0182 self.failUnlessEqual(42, getargs_l(42L)) 0183 self.assertRaises(OverflowError, getargs_l, VERY_LARGE) 0184 0185 0186 class LongLong_TestCase(unittest.TestCase): 0187 def test_L(self): 0188 from _testcapi import getargs_L 0189 # L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX) 0190 0191 # XXX There's a bug in getargs.c, format code "L": 0192 # If you pass something else than a Python long, you 0193 # get "Bad argument to internal function". 0194 0195 # So these three tests are commented out: 0196 0197 ## self.failUnlessEqual(3, getargs_L(3.14)) 0198 ## self.failUnlessEqual(99, getargs_L(Long())) 0199 ## self.failUnlessEqual(99, getargs_L(Int())) 0200 0201 self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) 0202 self.failUnlessEqual(LLONG_MIN, getargs_L(LLONG_MIN)) 0203 self.failUnlessEqual(LLONG_MAX, getargs_L(LLONG_MAX)) 0204 self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1) 0205 0206 self.failUnlessEqual(42, getargs_L(42)) 0207 self.failUnlessEqual(42, getargs_L(42L)) 0208 self.assertRaises(OverflowError, getargs_L, VERY_LARGE) 0209 0210 def test_K(self): 0211 from _testcapi import getargs_K 0212 # K return 'unsigned long long', no range checking 0213 self.assertRaises(TypeError, getargs_K, 3.14) 0214 self.assertRaises(TypeError, getargs_K, Long()) 0215 self.assertRaises(TypeError, getargs_K, Int()) 0216 self.failUnlessEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) 0217 self.failUnlessEqual(0, getargs_K(0)) 0218 self.failUnlessEqual(0, getargs_K(ULLONG_MAX+1)) 0219 0220 self.failUnlessEqual(42, getargs_K(42)) 0221 self.failUnlessEqual(42, getargs_K(42L)) 0222 0223 self.failUnlessEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE)) 0224 0225 def test_main(): 0226 tests = [Signed_TestCase, Unsigned_TestCase] 0227 try: 0228 from _testcapi import getargs_L, getargs_K 0229 except ImportError: 0230 pass # PY_LONG_LONG not available 0231 else: 0232 tests.append(LongLong_TestCase) 0233 test_support.run_unittest(*tests) 0234 0235 if __name__ == "__main__": 0236 test_main() 0237
Generated by PyXR 0.9.4