(self)
| 430 | raise TestFailed('"%*d"%(maxsize, -127) should fail') |
| 431 | |
| 432 | def test_bytes_and_bytearray_format(self): |
| 433 | # %c will insert a single byte, either from an int in range(256), or |
| 434 | # from a bytes argument of length 1, not from a str. |
| 435 | testcommon(b"%c", 7, b"\x07") |
| 436 | testcommon(b"%c", b"Z", b"Z") |
| 437 | testcommon(b"%c", bytearray(b"Z"), b"Z") |
| 438 | testcommon(b"%5c", 65, b" A") |
| 439 | testcommon(b"%-5c", 65, b"A ") |
| 440 | # %b will insert a series of bytes, either from a type that supports |
| 441 | # the Py_buffer protocol, or something that has a __bytes__ method |
| 442 | class FakeBytes(object): |
| 443 | def __bytes__(self): |
| 444 | return b'123' |
| 445 | fb = FakeBytes() |
| 446 | testcommon(b"%b", b"abc", b"abc") |
| 447 | testcommon(b"%b", bytearray(b"def"), b"def") |
| 448 | testcommon(b"%b", fb, b"123") |
| 449 | testcommon(b"%b", memoryview(b"abc"), b"abc") |
| 450 | # # %s is an alias for %b -- should only be used for Py2/3 code |
| 451 | testcommon(b"%s", b"abc", b"abc") |
| 452 | testcommon(b"%s", bytearray(b"def"), b"def") |
| 453 | testcommon(b"%s", fb, b"123") |
| 454 | testcommon(b"%s", memoryview(b"abc"), b"abc") |
| 455 | # %a will give the equivalent of |
| 456 | # repr(some_obj).encode('ascii', 'backslashreplace') |
| 457 | testcommon(b"%a", 3.25, b"3.25") |
| 458 | testcommon(b"%a", b"ghi", b"b'ghi'") |
| 459 | testcommon(b"%a", "jkl", b"'jkl'") |
| 460 | testcommon(b"%a", "\u0544", b"'\\u0544'") |
| 461 | # %r is an alias for %a |
| 462 | testcommon(b"%r", 3.25, b"3.25") |
| 463 | testcommon(b"%r", b"ghi", b"b'ghi'") |
| 464 | testcommon(b"%r", "jkl", b"'jkl'") |
| 465 | testcommon(b"%r", "\u0544", b"'\\u0544'") |
| 466 | testcommon(b'%(x)r', {b'x': 1}, b'1') |
| 467 | |
| 468 | # Test exception for unknown format characters, etc. |
| 469 | if verbose: |
| 470 | print('Testing exceptions') |
| 471 | test_exc(b"abc %\nd", 1, ValueError, |
| 472 | "stray % at position 4 or unexpected format character with code 0x0a at position 5") |
| 473 | test_exc(b"abc %'d", 1, ValueError, |
| 474 | "stray % at position 4 or unexpected format character \"'\" at position 5") |
| 475 | test_exc(b"abc %\x1fd", 1, ValueError, |
| 476 | "stray % at position 4 or unexpected format character with code 0x1f at position 5") |
| 477 | test_exc(b"abc %\x7fd", 1, ValueError, |
| 478 | "stray % at position 4 or unexpected format character with code 0x7f at position 5") |
| 479 | test_exc(b"abc %\x80d", 1, ValueError, |
| 480 | "stray % at position 4 or unexpected format character with code 0x80 at position 5") |
| 481 | test_exc(b'no format', 7, TypeError, |
| 482 | "not all arguments converted during bytes formatting (required 0, got 1)") |
| 483 | test_exc(b'no format', b'1', TypeError, |
| 484 | "not all arguments converted during bytes formatting (required 0, got 1)") |
| 485 | test_exc(b'no format', bytearray(b'1'), TypeError, |
| 486 | "not all arguments converted during bytes formatting (required 0, got 1)") |
| 487 | test_exc(b'%r', (1, 2), TypeError, |
| 488 | "not all arguments converted during bytes formatting (required 1, got 2)") |
| 489 | test_exc(b'%(x)r %r', {b'x': 1}, ValueError, |
nothing calls this directly
no test coverage detected