0001 from test.test_support import verbose, have_unicode, TestFailed 0002 import sys 0003 0004 # test string formatting operator (I am not sure if this is being tested 0005 # elsewhere but, surely, some of the given cases are *not* tested because 0006 # they crash python) 0007 # test on unicode strings as well 0008 0009 overflowok = 1 0010 0011 def testformat(formatstr, args, output=None): 0012 if verbose: 0013 if output: 0014 print "%s %% %s =? %s ..." %\ 0015 (repr(formatstr), repr(args), repr(output)), 0016 else: 0017 print "%s %% %s works? ..." % (repr(formatstr), repr(args)), 0018 try: 0019 result = formatstr % args 0020 except OverflowError: 0021 if not overflowok: 0022 raise 0023 if verbose: 0024 print 'overflow (this is fine)' 0025 else: 0026 if output and result != output: 0027 if verbose: 0028 print 'no' 0029 print "%s %% %s == %s != %s" %\ 0030 (repr(formatstr), repr(args), repr(result), repr(output)) 0031 else: 0032 if verbose: 0033 print 'yes' 0034 0035 def testboth(formatstr, *args): 0036 testformat(formatstr, *args) 0037 if have_unicode: 0038 testformat(unicode(formatstr), *args) 0039 0040 0041 testboth("%.1d", (1,), "1") 0042 testboth("%.*d", (sys.maxint,1)) # expect overflow 0043 testboth("%.100d", (1,), '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') 0044 testboth("%#.117x", (1,), '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') 0045 testboth("%#.118x", (1,), '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001') 0046 0047 testboth("%f", (1.0,), "1.000000") 0048 # these are trying to test the limits of the internal magic-number-length 0049 # formatting buffer, if that number changes then these tests are less 0050 # effective 0051 testboth("%#.*g", (109, -1.e+49/3.)) 0052 testboth("%#.*g", (110, -1.e+49/3.)) 0053 testboth("%#.*g", (110, -1.e+100/3.)) 0054 0055 # test some ridiculously large precision, expect overflow 0056 testboth('%12.*f', (123456, 1.0)) 0057 0058 # Formatting of long integers. Overflow is not ok 0059 overflowok = 0 0060 testboth("%x", 10L, "a") 0061 testboth("%x", 100000000000L, "174876e800") 0062 testboth("%o", 10L, "12") 0063 testboth("%o", 100000000000L, "1351035564000") 0064 testboth("%d", 10L, "10") 0065 testboth("%d", 100000000000L, "100000000000") 0066 0067 big = 123456789012345678901234567890L 0068 testboth("%d", big, "123456789012345678901234567890") 0069 testboth("%d", -big, "-123456789012345678901234567890") 0070 testboth("%5d", -big, "-123456789012345678901234567890") 0071 testboth("%31d", -big, "-123456789012345678901234567890") 0072 testboth("%32d", -big, " -123456789012345678901234567890") 0073 testboth("%-32d", -big, "-123456789012345678901234567890 ") 0074 testboth("%032d", -big, "-0123456789012345678901234567890") 0075 testboth("%-032d", -big, "-123456789012345678901234567890 ") 0076 testboth("%034d", -big, "-000123456789012345678901234567890") 0077 testboth("%034d", big, "0000123456789012345678901234567890") 0078 testboth("%0+34d", big, "+000123456789012345678901234567890") 0079 testboth("%+34d", big, " +123456789012345678901234567890") 0080 testboth("%34d", big, " 123456789012345678901234567890") 0081 testboth("%.2d", big, "123456789012345678901234567890") 0082 testboth("%.30d", big, "123456789012345678901234567890") 0083 testboth("%.31d", big, "0123456789012345678901234567890") 0084 testboth("%32.31d", big, " 0123456789012345678901234567890") 0085 0086 big = 0x1234567890abcdef12345L # 21 hex digits 0087 testboth("%x", big, "1234567890abcdef12345") 0088 testboth("%x", -big, "-1234567890abcdef12345") 0089 testboth("%5x", -big, "-1234567890abcdef12345") 0090 testboth("%22x", -big, "-1234567890abcdef12345") 0091 testboth("%23x", -big, " -1234567890abcdef12345") 0092 testboth("%-23x", -big, "-1234567890abcdef12345 ") 0093 testboth("%023x", -big, "-01234567890abcdef12345") 0094 testboth("%-023x", -big, "-1234567890abcdef12345 ") 0095 testboth("%025x", -big, "-0001234567890abcdef12345") 0096 testboth("%025x", big, "00001234567890abcdef12345") 0097 testboth("%0+25x", big, "+0001234567890abcdef12345") 0098 testboth("%+25x", big, " +1234567890abcdef12345") 0099 testboth("%25x", big, " 1234567890abcdef12345") 0100 testboth("%.2x", big, "1234567890abcdef12345") 0101 testboth("%.21x", big, "1234567890abcdef12345") 0102 testboth("%.22x", big, "01234567890abcdef12345") 0103 testboth("%23.22x", big, " 01234567890abcdef12345") 0104 testboth("%-23.22x", big, "01234567890abcdef12345 ") 0105 testboth("%X", big, "1234567890ABCDEF12345") 0106 testboth("%#X", big, "0X1234567890ABCDEF12345") 0107 testboth("%#x", big, "0x1234567890abcdef12345") 0108 testboth("%#x", -big, "-0x1234567890abcdef12345") 0109 testboth("%#.23x", -big, "-0x001234567890abcdef12345") 0110 testboth("%#+.23x", big, "+0x001234567890abcdef12345") 0111 testboth("%# .23x", big, " 0x001234567890abcdef12345") 0112 testboth("%#+.23X", big, "+0X001234567890ABCDEF12345") 0113 testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345") 0114 testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345") 0115 testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ") 0116 testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") 0117 # next one gets two leading zeroes from precision, and another from the 0118 # 0 flag and the width 0119 testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345") 0120 # same, except no 0 flag 0121 testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") 0122 0123 big = 012345670123456701234567012345670L # 32 octal digits 0124 testboth("%o", big, "12345670123456701234567012345670") 0125 testboth("%o", -big, "-12345670123456701234567012345670") 0126 testboth("%5o", -big, "-12345670123456701234567012345670") 0127 testboth("%33o", -big, "-12345670123456701234567012345670") 0128 testboth("%34o", -big, " -12345670123456701234567012345670") 0129 testboth("%-34o", -big, "-12345670123456701234567012345670 ") 0130 testboth("%034o", -big, "-012345670123456701234567012345670") 0131 testboth("%-034o", -big, "-12345670123456701234567012345670 ") 0132 testboth("%036o", -big, "-00012345670123456701234567012345670") 0133 testboth("%036o", big, "000012345670123456701234567012345670") 0134 testboth("%0+36o", big, "+00012345670123456701234567012345670") 0135 testboth("%+36o", big, " +12345670123456701234567012345670") 0136 testboth("%36o", big, " 12345670123456701234567012345670") 0137 testboth("%.2o", big, "12345670123456701234567012345670") 0138 testboth("%.32o", big, "12345670123456701234567012345670") 0139 testboth("%.33o", big, "012345670123456701234567012345670") 0140 testboth("%34.33o", big, " 012345670123456701234567012345670") 0141 testboth("%-34.33o", big, "012345670123456701234567012345670 ") 0142 testboth("%o", big, "12345670123456701234567012345670") 0143 testboth("%#o", big, "012345670123456701234567012345670") 0144 testboth("%#o", -big, "-012345670123456701234567012345670") 0145 testboth("%#.34o", -big, "-0012345670123456701234567012345670") 0146 testboth("%#+.34o", big, "+0012345670123456701234567012345670") 0147 testboth("%# .34o", big, " 0012345670123456701234567012345670") 0148 testboth("%#+.34o", big, "+0012345670123456701234567012345670") 0149 testboth("%#-+.34o", big, "+0012345670123456701234567012345670") 0150 testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ") 0151 testboth("%#+37.34o", big, " +0012345670123456701234567012345670") 0152 # next one gets one leading zero from precision 0153 testboth("%.33o", big, "012345670123456701234567012345670") 0154 # base marker shouldn't change that, since "0" is redundant 0155 testboth("%#.33o", big, "012345670123456701234567012345670") 0156 # but reduce precision, and base marker should add a zero 0157 testboth("%#.32o", big, "012345670123456701234567012345670") 0158 # one leading zero from precision, and another from "0" flag & width 0159 testboth("%034.33o", big, "0012345670123456701234567012345670") 0160 # base marker shouldn't change that 0161 testboth("%0#34.33o", big, "0012345670123456701234567012345670") 0162 0163 # Some small ints, in both Python int and long flavors). 0164 testboth("%d", 42, "42") 0165 testboth("%d", -42, "-42") 0166 testboth("%d", 42L, "42") 0167 testboth("%d", -42L, "-42") 0168 testboth("%#x", 1, "0x1") 0169 testboth("%#x", 1L, "0x1") 0170 testboth("%#X", 1, "0X1") 0171 testboth("%#X", 1L, "0X1") 0172 testboth("%#o", 1, "01") 0173 testboth("%#o", 1L, "01") 0174 testboth("%#o", 0, "0") 0175 testboth("%#o", 0L, "0") 0176 testboth("%o", 0, "0") 0177 testboth("%o", 0L, "0") 0178 testboth("%d", 0, "0") 0179 testboth("%d", 0L, "0") 0180 testboth("%#x", 0, "0x0") 0181 testboth("%#x", 0L, "0x0") 0182 testboth("%#X", 0, "0X0") 0183 testboth("%#X", 0L, "0X0") 0184 0185 testboth("%x", 0x42, "42") 0186 testboth("%x", -0x42, "-42") 0187 testboth("%x", 0x42L, "42") 0188 testboth("%x", -0x42L, "-42") 0189 0190 testboth("%o", 042, "42") 0191 testboth("%o", -042, "-42") 0192 testboth("%o", 042L, "42") 0193 testboth("%o", -042L, "-42") 0194 0195 # Test exception for unknown format characters 0196 if verbose: 0197 print 'Testing exceptions' 0198 0199 def test_exc(formatstr, args, exception, excmsg): 0200 try: 0201 testformat(formatstr, args) 0202 except exception, exc: 0203 if str(exc) == excmsg: 0204 if verbose: 0205 print "yes" 0206 else: 0207 if verbose: print 'no' 0208 print 'Unexpected ', exception, ':', repr(str(exc)) 0209 except: 0210 if verbose: print 'no' 0211 print 'Unexpected exception' 0212 raise 0213 else: 0214 raise TestFailed, 'did not get expected exception: %s' % excmsg 0215 0216 test_exc('abc %a', 1, ValueError, 0217 "unsupported format character 'a' (0x61) at index 5") 0218 if have_unicode: 0219 test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError, 0220 "unsupported format character '?' (0x3000) at index 5") 0221 0222 test_exc('%d', '1', TypeError, "int argument required") 0223 test_exc('%g', '1', TypeError, "float argument required") 0224 test_exc('no format', '1', TypeError, 0225 "not all arguments converted during string formatting") 0226 test_exc('no format', u'1', TypeError, 0227 "not all arguments converted during string formatting") 0228 test_exc(u'no format', '1', TypeError, 0229 "not all arguments converted during string formatting") 0230 test_exc(u'no format', u'1', TypeError, 0231 "not all arguments converted during string formatting") 0232 0233 if sys.maxint == 2**31-1: 0234 # crashes 2.2.1 and earlier: 0235 try: 0236 "%*d"%(sys.maxint, -127) 0237 except MemoryError: 0238 pass 0239 else: 0240 raise TestFailed, '"%*d"%(sys.maxint, -127) should fail' 0241
Generated by PyXR 0.9.4