1from test.support import verbose, TestFailed 2import locale 3import sys 4import test.support as support 5import unittest 6 7maxsize = support.MAX_Py_ssize_t 8 9# test string formatting operator (I am not sure if this is being tested 10# elsewhere but, surely, some of the given cases are *not* tested because 11# they crash python) 12# test on bytes object as well 13 14def testformat(formatstr, args, output=None, limit=None, overflowok=False): 15 if verbose: 16 if output: 17 print("{!a} % {!a} =? {!a} ...".format(formatstr, args, output), 18 end=' ') 19 else: 20 print("{!a} % {!a} works? ...".format(formatstr, args), end=' ') 21 try: 22 result = formatstr % args 23 except OverflowError: 24 if not overflowok: 25 raise 26 if verbose: 27 print('overflow (this is fine)') 28 else: 29 if output and limit is None and result != output: 30 if verbose: 31 print('no') 32 raise AssertionError("%r %% %r == %r != %r" % 33 (formatstr, args, result, output)) 34 # when 'limit' is specified, it determines how many characters 35 # must match exactly; lengths must always match. 36 # ex: limit=5, '12345678' matches '12345___' 37 # (mainly for floating point format tests for which an exact match 38 # can't be guaranteed due to rounding and representation errors) 39 elif output and limit is not None and ( 40 len(result)!=len(output) or result[:limit]!=output[:limit]): 41 if verbose: 42 print('no') 43 print("%s %% %s == %s != %s" % \ 44 (repr(formatstr), repr(args), repr(result), repr(output))) 45 else: 46 if verbose: 47 print('yes') 48 49def testcommon(formatstr, args, output=None, limit=None, overflowok=False): 50 # if formatstr is a str, test str, bytes, and bytearray; 51 # otherwise, test bytes and bytearry 52 if isinstance(formatstr, str): 53 testformat(formatstr, args, output, limit, overflowok) 54 b_format = formatstr.encode('ascii') 55 else: 56 b_format = formatstr 57 ba_format = bytearray(b_format) 58 b_args = [] 59 if not isinstance(args, tuple): 60 args = (args, ) 61 b_args = tuple(args) 62 if output is None: 63 b_output = ba_output = None 64 else: 65 if isinstance(output, str): 66 b_output = output.encode('ascii') 67 else: 68 b_output = output 69 ba_output = bytearray(b_output) 70 testformat(b_format, b_args, b_output, limit, overflowok) 71 testformat(ba_format, b_args, ba_output, limit, overflowok) 72 73 74class FormatTest(unittest.TestCase): 75 76 def test_common_format(self): 77 # test the format identifiers that work the same across 78 # str, bytes, and bytearrays (integer, float, oct, hex) 79 testcommon("%.1d", (1,), "1") 80 testcommon("%.*d", (sys.maxsize,1), overflowok=True) # expect overflow 81 testcommon("%.100d", (1,), '00000000000000000000000000000000000000' 82 '000000000000000000000000000000000000000000000000000000' 83 '00000001', overflowok=True) 84 testcommon("%#.117x", (1,), '0x00000000000000000000000000000000000' 85 '000000000000000000000000000000000000000000000000000000' 86 '0000000000000000000000000001', 87 overflowok=True) 88 testcommon("%#.118x", (1,), '0x00000000000000000000000000000000000' 89 '000000000000000000000000000000000000000000000000000000' 90 '00000000000000000000000000001', 91 overflowok=True) 92 93 testcommon("%f", (1.0,), "1.000000") 94 # these are trying to test the limits of the internal magic-number-length 95 # formatting buffer, if that number changes then these tests are less 96 # effective 97 testcommon("%#.*g", (109, -1.e+49/3.)) 98 testcommon("%#.*g", (110, -1.e+49/3.)) 99 testcommon("%#.*g", (110, -1.e+100/3.)) 100 # test some ridiculously large precision, expect overflow 101 testcommon('%12.*f', (123456, 1.0)) 102 103 # check for internal overflow validation on length of precision 104 # these tests should no longer cause overflow in Python 105 # 2.7/3.1 and later. 106 testcommon("%#.*g", (110, -1.e+100/3.)) 107 testcommon("%#.*G", (110, -1.e+100/3.)) 108 testcommon("%#.*f", (110, -1.e+100/3.)) 109 testcommon("%#.*F", (110, -1.e+100/3.)) 110 # Formatting of integers. Overflow is not ok 111 testcommon("%x", 10, "a") 112 testcommon("%x", 100000000000, "174876e800") 113 testcommon("%o", 10, "12") 114 testcommon("%o", 100000000000, "1351035564000") 115 testcommon("%d", 10, "10") 116 testcommon("%d", 100000000000, "100000000000") 117 118 big = 123456789012345678901234567890 119 testcommon("%d", big, "123456789012345678901234567890") 120 testcommon("%d", -big, "-123456789012345678901234567890") 121 testcommon("%5d", -big, "-123456789012345678901234567890") 122 testcommon("%31d", -big, "-123456789012345678901234567890") 123 testcommon("%32d", -big, " -123456789012345678901234567890") 124 testcommon("%-32d", -big, "-123456789012345678901234567890 ") 125 testcommon("%032d", -big, "-0123456789012345678901234567890") 126 testcommon("%-032d", -big, "-123456789012345678901234567890 ") 127 testcommon("%034d", -big, "-000123456789012345678901234567890") 128 testcommon("%034d", big, "0000123456789012345678901234567890") 129 testcommon("%0+34d", big, "+000123456789012345678901234567890") 130 testcommon("%+34d", big, " +123456789012345678901234567890") 131 testcommon("%34d", big, " 123456789012345678901234567890") 132 testcommon("%.2d", big, "123456789012345678901234567890") 133 testcommon("%.30d", big, "123456789012345678901234567890") 134 testcommon("%.31d", big, "0123456789012345678901234567890") 135 testcommon("%32.31d", big, " 0123456789012345678901234567890") 136 testcommon("%d", float(big), "123456________________________", 6) 137 138 big = 0x1234567890abcdef12345 # 21 hex digits 139 testcommon("%x", big, "1234567890abcdef12345") 140 testcommon("%x", -big, "-1234567890abcdef12345") 141 testcommon("%5x", -big, "-1234567890abcdef12345") 142 testcommon("%22x", -big, "-1234567890abcdef12345") 143 testcommon("%23x", -big, " -1234567890abcdef12345") 144 testcommon("%-23x", -big, "-1234567890abcdef12345 ") 145 testcommon("%023x", -big, "-01234567890abcdef12345") 146 testcommon("%-023x", -big, "-1234567890abcdef12345 ") 147 testcommon("%025x", -big, "-0001234567890abcdef12345") 148 testcommon("%025x", big, "00001234567890abcdef12345") 149 testcommon("%0+25x", big, "+0001234567890abcdef12345") 150 testcommon("%+25x", big, " +1234567890abcdef12345") 151 testcommon("%25x", big, " 1234567890abcdef12345") 152 testcommon("%.2x", big, "1234567890abcdef12345") 153 testcommon("%.21x", big, "1234567890abcdef12345") 154 testcommon("%.22x", big, "01234567890abcdef12345") 155 testcommon("%23.22x", big, " 01234567890abcdef12345") 156 testcommon("%-23.22x", big, "01234567890abcdef12345 ") 157 testcommon("%X", big, "1234567890ABCDEF12345") 158 testcommon("%#X", big, "0X1234567890ABCDEF12345") 159 testcommon("%#x", big, "0x1234567890abcdef12345") 160 testcommon("%#x", -big, "-0x1234567890abcdef12345") 161 testcommon("%#27x", big, " 0x1234567890abcdef12345") 162 testcommon("%#-27x", big, "0x1234567890abcdef12345 ") 163 testcommon("%#027x", big, "0x00001234567890abcdef12345") 164 testcommon("%#.23x", big, "0x001234567890abcdef12345") 165 testcommon("%#.23x", -big, "-0x001234567890abcdef12345") 166 testcommon("%#27.23x", big, " 0x001234567890abcdef12345") 167 testcommon("%#-27.23x", big, "0x001234567890abcdef12345 ") 168 testcommon("%#027.23x", big, "0x00001234567890abcdef12345") 169 testcommon("%#+.23x", big, "+0x001234567890abcdef12345") 170 testcommon("%# .23x", big, " 0x001234567890abcdef12345") 171 testcommon("%#+.23X", big, "+0X001234567890ABCDEF12345") 172 # next one gets two leading zeroes from precision, and another from the 173 # 0 flag and the width 174 testcommon("%#+027.23X", big, "+0X0001234567890ABCDEF12345") 175 testcommon("%# 027.23X", big, " 0X0001234567890ABCDEF12345") 176 # same, except no 0 flag 177 testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345") 178 testcommon("%#-+27.23x", big, "+0x001234567890abcdef12345 ") 179 testcommon("%#- 27.23x", big, " 0x001234567890abcdef12345 ") 180 181 big = 0o12345670123456701234567012345670 # 32 octal digits 182 testcommon("%o", big, "12345670123456701234567012345670") 183 testcommon("%o", -big, "-12345670123456701234567012345670") 184 testcommon("%5o", -big, "-12345670123456701234567012345670") 185 testcommon("%33o", -big, "-12345670123456701234567012345670") 186 testcommon("%34o", -big, " -12345670123456701234567012345670") 187 testcommon("%-34o", -big, "-12345670123456701234567012345670 ") 188 testcommon("%034o", -big, "-012345670123456701234567012345670") 189 testcommon("%-034o", -big, "-12345670123456701234567012345670 ") 190 testcommon("%036o", -big, "-00012345670123456701234567012345670") 191 testcommon("%036o", big, "000012345670123456701234567012345670") 192 testcommon("%0+36o", big, "+00012345670123456701234567012345670") 193 testcommon("%+36o", big, " +12345670123456701234567012345670") 194 testcommon("%36o", big, " 12345670123456701234567012345670") 195 testcommon("%.2o", big, "12345670123456701234567012345670") 196 testcommon("%.32o", big, "12345670123456701234567012345670") 197 testcommon("%.33o", big, "012345670123456701234567012345670") 198 testcommon("%34.33o", big, " 012345670123456701234567012345670") 199 testcommon("%-34.33o", big, "012345670123456701234567012345670 ") 200 testcommon("%o", big, "12345670123456701234567012345670") 201 testcommon("%#o", big, "0o12345670123456701234567012345670") 202 testcommon("%#o", -big, "-0o12345670123456701234567012345670") 203 testcommon("%#38o", big, " 0o12345670123456701234567012345670") 204 testcommon("%#-38o", big, "0o12345670123456701234567012345670 ") 205 testcommon("%#038o", big, "0o000012345670123456701234567012345670") 206 testcommon("%#.34o", big, "0o0012345670123456701234567012345670") 207 testcommon("%#.34o", -big, "-0o0012345670123456701234567012345670") 208 testcommon("%#38.34o", big, " 0o0012345670123456701234567012345670") 209 testcommon("%#-38.34o", big, "0o0012345670123456701234567012345670 ") 210 testcommon("%#038.34o", big, "0o000012345670123456701234567012345670") 211 testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670") 212 testcommon("%# .34o", big, " 0o0012345670123456701234567012345670") 213 testcommon("%#+38.34o", big, " +0o0012345670123456701234567012345670") 214 testcommon("%#-+38.34o", big, "+0o0012345670123456701234567012345670 ") 215 testcommon("%#- 38.34o", big, " 0o0012345670123456701234567012345670 ") 216 testcommon("%#+038.34o", big, "+0o00012345670123456701234567012345670") 217 testcommon("%# 038.34o", big, " 0o00012345670123456701234567012345670") 218 # next one gets one leading zero from precision 219 testcommon("%.33o", big, "012345670123456701234567012345670") 220 # base marker added in spite of leading zero (different to Python 2) 221 testcommon("%#.33o", big, "0o012345670123456701234567012345670") 222 # reduce precision, and base marker is always added 223 testcommon("%#.32o", big, "0o12345670123456701234567012345670") 224 # one leading zero from precision, plus two from "0" flag & width 225 testcommon("%035.33o", big, "00012345670123456701234567012345670") 226 # base marker shouldn't change the size 227 testcommon("%0#35.33o", big, "0o012345670123456701234567012345670") 228 229 # Some small ints, in both Python int and flavors). 230 testcommon("%d", 42, "42") 231 testcommon("%d", -42, "-42") 232 testcommon("%d", 42.0, "42") 233 testcommon("%#x", 1, "0x1") 234 testcommon("%#X", 1, "0X1") 235 testcommon("%#o", 1, "0o1") 236 testcommon("%#o", 0, "0o0") 237 testcommon("%o", 0, "0") 238 testcommon("%d", 0, "0") 239 testcommon("%#x", 0, "0x0") 240 testcommon("%#X", 0, "0X0") 241 testcommon("%x", 0x42, "42") 242 testcommon("%x", -0x42, "-42") 243 testcommon("%o", 0o42, "42") 244 testcommon("%o", -0o42, "-42") 245 # alternate float formatting 246 testcommon('%g', 1.1, '1.1') 247 testcommon('%#g', 1.1, '1.10000') 248 249 def test_str_format(self): 250 testformat("%r", "\u0378", "'\\u0378'") # non printable 251 testformat("%a", "\u0378", "'\\u0378'") # non printable 252 testformat("%r", "\u0374", "'\u0374'") # printable 253 testformat("%a", "\u0374", "'\\u0374'") # printable 254 255 # Test exception for unknown format characters, etc. 256 if verbose: 257 print('Testing exceptions') 258 def test_exc(formatstr, args, exception, excmsg): 259 try: 260 testformat(formatstr, args) 261 except exception as exc: 262 if str(exc) == excmsg: 263 if verbose: 264 print("yes") 265 else: 266 if verbose: print('no') 267 print('Unexpected ', exception, ':', repr(str(exc))) 268 except: 269 if verbose: print('no') 270 print('Unexpected exception') 271 raise 272 else: 273 raise TestFailed('did not get expected exception: %s' % excmsg) 274 test_exc('abc %b', 1, ValueError, 275 "unsupported format character 'b' (0x62) at index 5") 276 #test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError, 277 # "unsupported format character '?' (0x3000) at index 5") 278 test_exc('%d', '1', TypeError, "%d format: a number is required, not str") 279 test_exc('%x', '1', TypeError, "%x format: an integer is required, not str") 280 test_exc('%x', 3.14, TypeError, "%x format: an integer is required, not float") 281 test_exc('%g', '1', TypeError, "must be real number, not str") 282 test_exc('no format', '1', TypeError, 283 "not all arguments converted during string formatting") 284 test_exc('%c', -1, OverflowError, "%c arg not in range(0x110000)") 285 test_exc('%c', sys.maxunicode+1, OverflowError, 286 "%c arg not in range(0x110000)") 287 #test_exc('%c', 2**128, OverflowError, "%c arg not in range(0x110000)") 288 test_exc('%c', 3.14, TypeError, "%c requires int or char") 289 test_exc('%c', 'ab', TypeError, "%c requires int or char") 290 test_exc('%c', b'x', TypeError, "%c requires int or char") 291 292 if maxsize == 2**31-1: 293 # crashes 2.2.1 and earlier: 294 try: 295 "%*d"%(maxsize, -127) 296 except MemoryError: 297 pass 298 else: 299 raise TestFailed('"%*d"%(maxsize, -127) should fail') 300 301 def test_bytes_and_bytearray_format(self): 302 # %c will insert a single byte, either from an int in range(256), or 303 # from a bytes argument of length 1, not from a str. 304 testcommon(b"%c", 7, b"\x07") 305 testcommon(b"%c", b"Z", b"Z") 306 testcommon(b"%c", bytearray(b"Z"), b"Z") 307 testcommon(b"%5c", 65, b" A") 308 testcommon(b"%-5c", 65, b"A ") 309 # %b will insert a series of bytes, either from a type that supports 310 # the Py_buffer protocol, or something that has a __bytes__ method 311 class FakeBytes(object): 312 def __bytes__(self): 313 return b'123' 314 fb = FakeBytes() 315 testcommon(b"%b", b"abc", b"abc") 316 testcommon(b"%b", bytearray(b"def"), b"def") 317 testcommon(b"%b", fb, b"123") 318 # # %s is an alias for %b -- should only be used for Py2/3 code 319 testcommon(b"%s", b"abc", b"abc") 320 testcommon(b"%s", bytearray(b"def"), b"def") 321 testcommon(b"%s", fb, b"123") 322 # %a will give the equivalent of 323 # repr(some_obj).encode('ascii', 'backslashreplace') 324 testcommon(b"%a", 3.14, b"3.14") 325 testcommon(b"%a", b"ghi", b"b'ghi'") 326 testcommon(b"%a", "jkl", b"'jkl'") 327 testcommon(b"%a", "\u0544", b"'\\u0544'") 328 # %r is an alias for %a 329 testcommon(b"%r", 3.14, b"3.14") 330 testcommon(b"%r", b"ghi", b"b'ghi'") 331 testcommon(b"%r", "jkl", b"'jkl'") 332 testcommon(b"%r", "\u0544", b"'\\u0544'") 333 334 # Test exception for unknown format characters, etc. 335 if verbose: 336 print('Testing exceptions') 337 def test_exc(formatstr, args, exception, excmsg): 338 try: 339 testformat(formatstr, args) 340 except exception as exc: 341 if str(exc) == excmsg: 342 if verbose: 343 print("yes") 344 else: 345 if verbose: print('no') 346 print('Unexpected ', exception, ':', repr(str(exc))) 347 except: 348 if verbose: print('no') 349 print('Unexpected exception') 350 raise 351 else: 352 raise TestFailed('did not get expected exception: %s' % excmsg) 353 test_exc(b'%d', '1', TypeError, 354 "%d format: a number is required, not str") 355 test_exc(b'%d', b'1', TypeError, 356 "%d format: a number is required, not bytes") 357 test_exc(b'%x', 3.14, TypeError, 358 "%x format: an integer is required, not float") 359 test_exc(b'%g', '1', TypeError, "float argument required, not str") 360 test_exc(b'%g', b'1', TypeError, "float argument required, not bytes") 361 test_exc(b'no format', 7, TypeError, 362 "not all arguments converted during bytes formatting") 363 test_exc(b'no format', b'1', TypeError, 364 "not all arguments converted during bytes formatting") 365 test_exc(b'no format', bytearray(b'1'), TypeError, 366 "not all arguments converted during bytes formatting") 367 test_exc(b"%c", -1, OverflowError, 368 "%c arg not in range(256)") 369 test_exc(b"%c", 256, OverflowError, 370 "%c arg not in range(256)") 371 test_exc(b"%c", 2**128, OverflowError, 372 "%c arg not in range(256)") 373 test_exc(b"%c", b"Za", TypeError, 374 "%c requires an integer in range(256) or a single byte") 375 test_exc(b"%c", "Y", TypeError, 376 "%c requires an integer in range(256) or a single byte") 377 test_exc(b"%c", 3.14, TypeError, 378 "%c requires an integer in range(256) or a single byte") 379 test_exc(b"%b", "Xc", TypeError, 380 "%b requires bytes, or an object that implements __bytes__, not 'str'") 381 test_exc(b"%s", "Wd", TypeError, 382 "%b requires bytes, or an object that implements __bytes__, not 'str'") 383 384 if maxsize == 2**31-1: 385 # crashes 2.2.1 and earlier: 386 try: 387 "%*d"%(maxsize, -127) 388 except MemoryError: 389 pass 390 else: 391 raise TestFailed('"%*d"%(maxsize, -127) should fail') 392 393 def test_nul(self): 394 # test the null character 395 testcommon("a\0b", (), 'a\0b') 396 testcommon("a%cb", (0,), 'a\0b') 397 testformat("a%sb", ('c\0d',), 'ac\0db') 398 testcommon(b"a%sb", (b'c\0d',), b'ac\0db') 399 400 def test_non_ascii(self): 401 testformat("\u20ac=%f", (1.0,), "\u20ac=1.000000") 402 403 self.assertEqual(format("abc", "\u2007<5"), "abc\u2007\u2007") 404 self.assertEqual(format(123, "\u2007<5"), "123\u2007\u2007") 405 self.assertEqual(format(12.3, "\u2007<6"), "12.3\u2007\u2007") 406 self.assertEqual(format(0j, "\u2007<4"), "0j\u2007\u2007") 407 self.assertEqual(format(1+2j, "\u2007<8"), "(1+2j)\u2007\u2007") 408 409 self.assertEqual(format("abc", "\u2007>5"), "\u2007\u2007abc") 410 self.assertEqual(format(123, "\u2007>5"), "\u2007\u2007123") 411 self.assertEqual(format(12.3, "\u2007>6"), "\u2007\u200712.3") 412 self.assertEqual(format(1+2j, "\u2007>8"), "\u2007\u2007(1+2j)") 413 self.assertEqual(format(0j, "\u2007>4"), "\u2007\u20070j") 414 415 self.assertEqual(format("abc", "\u2007^5"), "\u2007abc\u2007") 416 self.assertEqual(format(123, "\u2007^5"), "\u2007123\u2007") 417 self.assertEqual(format(12.3, "\u2007^6"), "\u200712.3\u2007") 418 self.assertEqual(format(1+2j, "\u2007^8"), "\u2007(1+2j)\u2007") 419 self.assertEqual(format(0j, "\u2007^4"), "\u20070j\u2007") 420 421 def test_locale(self): 422 try: 423 oldloc = locale.setlocale(locale.LC_ALL) 424 locale.setlocale(locale.LC_ALL, '') 425 except locale.Error as err: 426 self.skipTest("Cannot set locale: {}".format(err)) 427 try: 428 localeconv = locale.localeconv() 429 sep = localeconv['thousands_sep'] 430 point = localeconv['decimal_point'] 431 432 text = format(123456789, "n") 433 self.assertIn(sep, text) 434 self.assertEqual(text.replace(sep, ''), '123456789') 435 436 text = format(1234.5, "n") 437 self.assertIn(sep, text) 438 self.assertIn(point, text) 439 self.assertEqual(text.replace(sep, ''), '1234' + point + '5') 440 finally: 441 locale.setlocale(locale.LC_ALL, oldloc) 442 443 @support.cpython_only 444 def test_optimisations(self): 445 text = "abcde" # 5 characters 446 447 self.assertIs("%s" % text, text) 448 self.assertIs("%.5s" % text, text) 449 self.assertIs("%.10s" % text, text) 450 self.assertIs("%1s" % text, text) 451 self.assertIs("%5s" % text, text) 452 453 self.assertIs("{0}".format(text), text) 454 self.assertIs("{0:s}".format(text), text) 455 self.assertIs("{0:.5s}".format(text), text) 456 self.assertIs("{0:.10s}".format(text), text) 457 self.assertIs("{0:1s}".format(text), text) 458 self.assertIs("{0:5s}".format(text), text) 459 460 self.assertIs(text % (), text) 461 self.assertIs(text.format(), text) 462 463 def test_precision(self): 464 f = 1.2 465 self.assertEqual(format(f, ".0f"), "1") 466 self.assertEqual(format(f, ".3f"), "1.200") 467 with self.assertRaises(ValueError) as cm: 468 format(f, ".%sf" % (sys.maxsize + 1)) 469 470 c = complex(f) 471 self.assertEqual(format(c, ".0f"), "1+0j") 472 self.assertEqual(format(c, ".3f"), "1.200+0.000j") 473 with self.assertRaises(ValueError) as cm: 474 format(c, ".%sf" % (sys.maxsize + 1)) 475 476 @support.cpython_only 477 def test_precision_c_limits(self): 478 from _testcapi import INT_MAX 479 480 f = 1.2 481 with self.assertRaises(ValueError) as cm: 482 format(f, ".%sf" % (INT_MAX + 1)) 483 484 c = complex(f) 485 with self.assertRaises(ValueError) as cm: 486 format(c, ".%sf" % (INT_MAX + 1)) 487 488 489if __name__ == "__main__": 490 unittest.main() 491