| 1 |
import sys |
| 2 |
from test.test_support import verbose, have_unicode, TestFailed |
| 3 |
import test.test_support as test_support |
| 4 |
import unittest |
| 5 |
|
| 6 |
maxsize = test_support.MAX_Py_ssize_t |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
def testformat(formatstr, args, output=None, limit=None, overflowok=False): |
| 14 |
if verbose: |
| 15 |
if output: |
| 16 |
print "%s %% %s =? %s ..." %\ |
| 17 |
(repr(formatstr), repr(args), repr(output)), |
| 18 |
else: |
| 19 |
print "%s %% %s works? ..." % (repr(formatstr), repr(args)), |
| 20 |
try: |
| 21 |
result = formatstr % args |
| 22 |
except OverflowError: |
| 23 |
if not overflowok: |
| 24 |
raise |
| 25 |
if verbose: |
| 26 |
print 'overflow (this is fine)' |
| 27 |
else: |
| 28 |
if output and limit is None and result != output: |
| 29 |
if verbose: |
| 30 |
print 'no' |
| 31 |
raise AssertionError("%r %% %r == %r != %r" % |
| 32 |
(formatstr, args, result, output)) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
elif output and limit is not None and ( |
| 39 |
len(result)!=len(output) or result[:limit]!=output[:limit]): |
| 40 |
if verbose: |
| 41 |
print 'no' |
| 42 |
print "%s %% %s == %s != %s" % \ |
| 43 |
(repr(formatstr), repr(args), repr(result), repr(output)) |
| 44 |
else: |
| 45 |
if verbose: |
| 46 |
print 'yes' |
| 47 |
|
| 48 |
|
| 49 |
def testboth(formatstr, *args, **kwargs): |
| 50 |
testformat(formatstr, *args, **kwargs) |
| 51 |
if have_unicode: |
| 52 |
testformat(unicode(formatstr), *args, **kwargs) |
| 53 |
|
| 54 |
|
| 55 |
class FormatTest(unittest.TestCase): |
| 56 |
def test_format(self): |
| 57 |
testboth("%.1d", (1,), "1") |
| 58 |
testboth("%.*d", (sys.maxint,1), overflowok=True) |
| 59 |
testboth("%.100d", (1,), '00000000000000000000000000000000000000' |
| 60 |
'000000000000000000000000000000000000000000000000000000' |
| 61 |
'00000001', overflowok=True) |
| 62 |
testboth("%#.117x", (1,), '0x00000000000000000000000000000000000' |
| 63 |
'000000000000000000000000000000000000000000000000000000' |
| 64 |
'0000000000000000000000000001', |
| 65 |
overflowok=True) |
| 66 |
testboth("%#.118x", (1,), '0x00000000000000000000000000000000000' |
| 67 |
'000000000000000000000000000000000000000000000000000000' |
| 68 |
'00000000000000000000000000001', |
| 69 |
overflowok=True) |
| 70 |
|
| 71 |
testboth("%f", (1.0,), "1.000000") |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
testboth("%#.*g", (109, -1.e+49/3.)) |
| 76 |
testboth("%#.*g", (110, -1.e+49/3.)) |
| 77 |
testboth("%#.*g", (110, -1.e+100/3.)) |
| 78 |
|
| 79 |
|
| 80 |
testboth('%12.*f', (123456, 1.0)) |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
testboth("%#.*g", (110, -1.e+100/3.)) |
| 86 |
testboth("%#.*G", (110, -1.e+100/3.)) |
| 87 |
testboth("%#.*f", (110, -1.e+100/3.)) |
| 88 |
testboth("%#.*F", (110, -1.e+100/3.)) |
| 89 |
|
| 90 |
|
| 91 |
testboth("%x", 10L, "a") |
| 92 |
testboth("%x", 100000000000L, "174876e800") |
| 93 |
testboth("%o", 10L, "12") |
| 94 |
testboth("%o", 100000000000L, "1351035564000") |
| 95 |
testboth("%d", 10L, "10") |
| 96 |
testboth("%d", 100000000000L, "100000000000") |
| 97 |
|
| 98 |
big = 123456789012345678901234567890L |
| 99 |
testboth("%d", big, "123456789012345678901234567890") |
| 100 |
testboth("%d", -big, "-123456789012345678901234567890") |
| 101 |
testboth("%5d", -big, "-123456789012345678901234567890") |
| 102 |
testboth("%31d", -big, "-123456789012345678901234567890") |
| 103 |
testboth("%32d", -big, " -123456789012345678901234567890") |
| 104 |
testboth("%-32d", -big, "-123456789012345678901234567890 ") |
| 105 |
testboth("%032d", -big, "-0123456789012345678901234567890") |
| 106 |
testboth("%-032d", -big, "-123456789012345678901234567890 ") |
| 107 |
testboth("%034d", -big, "-000123456789012345678901234567890") |
| 108 |
testboth("%034d", big, "0000123456789012345678901234567890") |
| 109 |
testboth("%0+34d", big, "+000123456789012345678901234567890") |
| 110 |
testboth("%+34d", big, " +123456789012345678901234567890") |
| 111 |
testboth("%34d", big, " 123456789012345678901234567890") |
| 112 |
testboth("%.2d", big, "123456789012345678901234567890") |
| 113 |
testboth("%.30d", big, "123456789012345678901234567890") |
| 114 |
testboth("%.31d", big, "0123456789012345678901234567890") |
| 115 |
testboth("%32.31d", big, " 0123456789012345678901234567890") |
| 116 |
testboth("%d", float(big), "123456________________________", 6) |
| 117 |
|
| 118 |
big = 0x1234567890abcdef12345L |
| 119 |
testboth("%x", big, "1234567890abcdef12345") |
| 120 |
testboth("%x", -big, "-1234567890abcdef12345") |
| 121 |
testboth("%5x", -big, "-1234567890abcdef12345") |
| 122 |
testboth("%22x", -big, "-1234567890abcdef12345") |
| 123 |
testboth("%23x", -big, " -1234567890abcdef12345") |
| 124 |
testboth("%-23x", -big, "-1234567890abcdef12345 ") |
| 125 |
testboth("%023x", -big, "-01234567890abcdef12345") |
| 126 |
testboth("%-023x", -big, "-1234567890abcdef12345 ") |
| 127 |
testboth("%025x", -big, "-0001234567890abcdef12345") |
| 128 |
testboth("%025x", big, "00001234567890abcdef12345") |
| 129 |
testboth("%0+25x", big, "+0001234567890abcdef12345") |
| 130 |
testboth("%+25x", big, " +1234567890abcdef12345") |
| 131 |
testboth("%25x", big, " 1234567890abcdef12345") |
| 132 |
testboth("%.2x", big, "1234567890abcdef12345") |
| 133 |
testboth("%.21x", big, "1234567890abcdef12345") |
| 134 |
testboth("%.22x", big, "01234567890abcdef12345") |
| 135 |
testboth("%23.22x", big, " 01234567890abcdef12345") |
| 136 |
testboth("%-23.22x", big, "01234567890abcdef12345 ") |
| 137 |
testboth("%X", big, "1234567890ABCDEF12345") |
| 138 |
testboth("%#X", big, "0X1234567890ABCDEF12345") |
| 139 |
testboth("%#x", big, "0x1234567890abcdef12345") |
| 140 |
testboth("%#x", -big, "-0x1234567890abcdef12345") |
| 141 |
testboth("%#.23x", -big, "-0x001234567890abcdef12345") |
| 142 |
testboth("%#+.23x", big, "+0x001234567890abcdef12345") |
| 143 |
testboth("%# .23x", big, " 0x001234567890abcdef12345") |
| 144 |
testboth("%#+.23X", big, "+0X001234567890ABCDEF12345") |
| 145 |
testboth("%#-+.23X", big, "+0X001234567890ABCDEF12345") |
| 146 |
testboth("%#-+26.23X", big, "+0X001234567890ABCDEF12345") |
| 147 |
testboth("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ") |
| 148 |
testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
| 149 |
|
| 150 |
|
| 151 |
testboth("%#+027.23X", big, "+0X0001234567890ABCDEF12345") |
| 152 |
|
| 153 |
testboth("%#+27.23X", big, " +0X001234567890ABCDEF12345") |
| 154 |
testboth("%x", float(big), "123456_______________", 6) |
| 155 |
|
| 156 |
big = 012345670123456701234567012345670L |
| 157 |
testboth("%o", big, "12345670123456701234567012345670") |
| 158 |
testboth("%o", -big, "-12345670123456701234567012345670") |
| 159 |
testboth("%5o", -big, "-12345670123456701234567012345670") |
| 160 |
testboth("%33o", -big, "-12345670123456701234567012345670") |
| 161 |
testboth("%34o", -big, " -12345670123456701234567012345670") |
| 162 |
testboth("%-34o", -big, "-12345670123456701234567012345670 ") |
| 163 |
testboth("%034o", -big, "-012345670123456701234567012345670") |
| 164 |
testboth("%-034o", -big, "-12345670123456701234567012345670 ") |
| 165 |
testboth("%036o", -big, "-00012345670123456701234567012345670") |
| 166 |
testboth("%036o", big, "000012345670123456701234567012345670") |
| 167 |
testboth("%0+36o", big, "+00012345670123456701234567012345670") |
| 168 |
testboth("%+36o", big, " +12345670123456701234567012345670") |
| 169 |
testboth("%36o", big, " 12345670123456701234567012345670") |
| 170 |
testboth("%.2o", big, "12345670123456701234567012345670") |
| 171 |
testboth("%.32o", big, "12345670123456701234567012345670") |
| 172 |
testboth("%.33o", big, "012345670123456701234567012345670") |
| 173 |
testboth("%34.33o", big, " 012345670123456701234567012345670") |
| 174 |
testboth("%-34.33o", big, "012345670123456701234567012345670 ") |
| 175 |
testboth("%o", big, "12345670123456701234567012345670") |
| 176 |
testboth("%#o", big, "012345670123456701234567012345670") |
| 177 |
testboth("%#o", -big, "-012345670123456701234567012345670") |
| 178 |
testboth("%#.34o", -big, "-0012345670123456701234567012345670") |
| 179 |
testboth("%#+.34o", big, "+0012345670123456701234567012345670") |
| 180 |
testboth("%# .34o", big, " 0012345670123456701234567012345670") |
| 181 |
testboth("%#+.34o", big, "+0012345670123456701234567012345670") |
| 182 |
testboth("%#-+.34o", big, "+0012345670123456701234567012345670") |
| 183 |
testboth("%#-+37.34o", big, "+0012345670123456701234567012345670 ") |
| 184 |
testboth("%#+37.34o", big, " +0012345670123456701234567012345670") |
| 185 |
|
| 186 |
testboth("%.33o", big, "012345670123456701234567012345670") |
| 187 |
|
| 188 |
testboth("%#.33o", big, "012345670123456701234567012345670") |
| 189 |
|
| 190 |
testboth("%#.32o", big, "012345670123456701234567012345670") |
| 191 |
|
| 192 |
testboth("%034.33o", big, "0012345670123456701234567012345670") |
| 193 |
|
| 194 |
testboth("%0#34.33o", big, "0012345670123456701234567012345670") |
| 195 |
testboth("%o", float(big), "123456__________________________", 6) |
| 196 |
|
| 197 |
|
| 198 |
testboth("%d", 42, "42") |
| 199 |
testboth("%d", -42, "-42") |
| 200 |
testboth("%d", 42L, "42") |
| 201 |
testboth("%d", -42L, "-42") |
| 202 |
testboth("%d", 42.0, "42") |
| 203 |
testboth("%#x", 1, "0x1") |
| 204 |
testboth("%#x", 1L, "0x1") |
| 205 |
testboth("%#X", 1, "0X1") |
| 206 |
testboth("%#X", 1L, "0X1") |
| 207 |
testboth("%#x", 1.0, "0x1") |
| 208 |
testboth("%#o", 1, "01") |
| 209 |
testboth("%#o", 1L, "01") |
| 210 |
testboth("%#o", 0, "0") |
| 211 |
testboth("%#o", 0L, "0") |
| 212 |
testboth("%o", 0, "0") |
| 213 |
testboth("%o", 0L, "0") |
| 214 |
testboth("%d", 0, "0") |
| 215 |
testboth("%d", 0L, "0") |
| 216 |
testboth("%#x", 0, "0x0") |
| 217 |
testboth("%#x", 0L, "0x0") |
| 218 |
testboth("%#X", 0, "0X0") |
| 219 |
testboth("%#X", 0L, "0X0") |
| 220 |
|
| 221 |
testboth("%x", 0x42, "42") |
| 222 |
testboth("%x", -0x42, "-42") |
| 223 |
testboth("%x", 0x42L, "42") |
| 224 |
testboth("%x", -0x42L, "-42") |
| 225 |
testboth("%x", float(0x42), "42") |
| 226 |
|
| 227 |
testboth("%o", 042, "42") |
| 228 |
testboth("%o", -042, "-42") |
| 229 |
testboth("%o", 042L, "42") |
| 230 |
testboth("%o", -042L, "-42") |
| 231 |
testboth("%o", float(042), "42") |
| 232 |
|
| 233 |
|
| 234 |
testformat('%g', 1.1, '1.1') |
| 235 |
testformat('%#g', 1.1, '1.10000') |
| 236 |
|
| 237 |
|
| 238 |
class IntFails(object): |
| 239 |
def __int__(self): |
| 240 |
raise TestFailed |
| 241 |
def __long__(self): |
| 242 |
return 0 |
| 243 |
|
| 244 |
fst = IntFails() |
| 245 |
testformat("%x", fst, '0') |
| 246 |
testformat(u"%x", fst, '0') |
| 247 |
|
| 248 |
|
| 249 |
if verbose: |
| 250 |
print 'Testing exceptions' |
| 251 |
|
| 252 |
def test_exc(formatstr, args, exception, excmsg): |
| 253 |
try: |
| 254 |
testformat(formatstr, args) |
| 255 |
except exception, exc: |
| 256 |
if str(exc) == excmsg: |
| 257 |
if verbose: |
| 258 |
print "yes" |
| 259 |
else: |
| 260 |
if verbose: print 'no' |
| 261 |
print 'Unexpected ', exception, ':', repr(str(exc)) |
| 262 |
except: |
| 263 |
if verbose: print 'no' |
| 264 |
print 'Unexpected exception' |
| 265 |
raise |
| 266 |
else: |
| 267 |
raise TestFailed, 'did not get expected exception: %s' % excmsg |
| 268 |
|
| 269 |
test_exc('abc %a', 1, ValueError, |
| 270 |
"unsupported format character 'a' (0x61) at index 5") |
| 271 |
if have_unicode: |
| 272 |
test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError, |
| 273 |
"unsupported format character '?' (0x3000) at index 5") |
| 274 |
|
| 275 |
test_exc('%d', '1', TypeError, "%d format: a number is required, not str") |
| 276 |
test_exc('%g', '1', TypeError, "float argument required, not str") |
| 277 |
test_exc('no format', '1', TypeError, |
| 278 |
"not all arguments converted during string formatting") |
| 279 |
test_exc('no format', u'1', TypeError, |
| 280 |
"not all arguments converted during string formatting") |
| 281 |
test_exc(u'no format', '1', TypeError, |
| 282 |
"not all arguments converted during string formatting") |
| 283 |
test_exc(u'no format', u'1', TypeError, |
| 284 |
"not all arguments converted during string formatting") |
| 285 |
|
| 286 |
class Foobar(long): |
| 287 |
def __oct__(self): |
| 288 |
|
| 289 |
return self + 1 |
| 290 |
|
| 291 |
test_exc('%o', Foobar(), TypeError, |
| 292 |
"expected string or Unicode object, long found") |
| 293 |
|
| 294 |
if maxsize == 2**31-1: |
| 295 |
|
| 296 |
try: |
| 297 |
"%*d"%(maxsize, -127) |
| 298 |
except MemoryError: |
| 299 |
pass |
| 300 |
else: |
| 301 |
raise TestFailed, '"%*d"%(maxsize, -127) should fail' |
| 302 |
|
| 303 |
def test_invalid_special_methods(self): |
| 304 |
tests = [] |
| 305 |
for f in 'sriduoxXfge': |
| 306 |
tests.append(('%' + f, 1, TypeError)) |
| 307 |
tests.append(('%#' + f, 1, TypeError)) |
| 308 |
for r in ['', '-', 'L', '-L']: |
| 309 |
for f in 'iduoxX': |
| 310 |
tests.append(('%' + f, r, ValueError)) |
| 311 |
tests.append(('%#' + f, r, ValueError)) |
| 312 |
tests.append(('%o', 'abc', ValueError)) |
| 313 |
for r in ('abc', '0abc', '0x', '0xL'): |
| 314 |
for f in 'xX': |
| 315 |
tests.append(('%' + f, r, ValueError)) |
| 316 |
for r in ('0x', '0xL'): |
| 317 |
for f in 'xX': |
| 318 |
tests.append(('%#' + f, r, ValueError)) |
| 319 |
|
| 320 |
class X(long): |
| 321 |
def __repr__(self): |
| 322 |
return result |
| 323 |
def __str__(self): |
| 324 |
return result |
| 325 |
def __oct__(self): |
| 326 |
return result |
| 327 |
def __hex__(self): |
| 328 |
return result |
| 329 |
def __float__(self): |
| 330 |
return result |
| 331 |
for fmt, result, exc in tests: |
| 332 |
try: |
| 333 |
fmt % X() |
| 334 |
except exc: |
| 335 |
pass |
| 336 |
else: |
| 337 |
self.fail('%s not raised for %r format of %r' % |
| 338 |
(exc.__name__, fmt, result)) |
| 339 |
|
| 340 |
|
| 341 |
def test_main(): |
| 342 |
test_support.run_unittest(FormatTest) |
| 343 |
|
| 344 |
def test_precision(self): |
| 345 |
f = 1.2 |
| 346 |
self.assertEqual(format(f, ".0f"), "1") |
| 347 |
self.assertEqual(format(f, ".3f"), "1.200") |
| 348 |
with self.assertRaises(ValueError) as cm: |
| 349 |
format(f, ".%sf" % (sys.maxsize + 1)) |
| 350 |
self.assertEqual(str(cm.exception), "precision too big") |
| 351 |
|
| 352 |
c = complex(f) |
| 353 |
self.assertEqual(format(c, ".0f"), "1+0j") |
| 354 |
self.assertEqual(format(c, ".3f"), "1.200+0.000j") |
| 355 |
with self.assertRaises(ValueError) as cm: |
| 356 |
format(c, ".%sf" % (sys.maxsize + 1)) |
| 357 |
self.assertEqual(str(cm.exception), "precision too big") |
| 358 |
|
| 359 |
@test_support.cpython_only |
| 360 |
def test_precision_c_limits(self): |
| 361 |
from _testcapi import INT_MAX |
| 362 |
|
| 363 |
f = 1.2 |
| 364 |
with self.assertRaises(ValueError) as cm: |
| 365 |
format(f, ".%sf" % (INT_MAX + 1)) |
| 366 |
|
| 367 |
c = complex(f) |
| 368 |
with self.assertRaises(ValueError) as cm: |
| 369 |
format(c, ".%sf" % (INT_MAX + 1)) |
| 370 |
|
| 371 |
|
| 372 |
if __name__ == "__main__": |
| 373 |
unittest.main() |