How to use __mod__ method in Slash

Best Python code snippet using slash

test6.py

Source:test6.py Github

copy

Full Screen

...

Full Screen

Full Screen

builtins_str_test.py

Source:builtins_str_test.py Github

copy

Full Screen

...1343 "123".zfill(3.2)1344 self.assertEqual(str(context.exception), "integer argument expected, got float")1345class StrModTests(unittest.TestCase):1346 def test_empty_format_returns_empty_string(self):1347 self.assertEqual(str.__mod__("", ()), "")1348 def test_simple_string_returns_string(self):1349 self.assertEqual(str.__mod__("foo bar (}", ()), "foo bar (}")1350 def test_with_non_tuple_args_returns_string(self):1351 self.assertEqual(str.__mod__("%s", "foo"), "foo")1352 self.assertEqual(str.__mod__("%d", 42), "42")1353 self.assertEqual(str.__mod__("%s", {"foo": "bar"}), "{'foo': 'bar'}")1354 def test_with_named_args_returns_string(self):1355 self.assertEqual(1356 str.__mod__("%(foo)s %(bar)d", {"foo": "ho", "bar": 42}), "ho 42"1357 )1358 self.assertEqual(str.__mod__("%()x", {"": 123}), "7b")1359 self.assertEqual(str.__mod__(")%(((()) ()))s(", {"((()) ())": 99}), ")99(")1360 self.assertEqual(str.__mod__("%(%s)s", {"%s": -5}), "-5")1361 def test_with_custom_mapping_returns_string(self):1362 class C:1363 def __getitem__(self, key):1364 return "getitem called with " + key1365 self.assertEqual(str.__mod__("%(foo)s", C()), "getitem called with foo")1366 def test_with_custom_mapping_propagates_errors(self):1367 with self.assertRaises(KeyError) as context:1368 str.__mod__("%(foo)s", {})1369 self.assertEqual(str(context.exception), "'foo'")1370 class C:1371 def __getitem__(self, key):1372 raise UserWarning()1373 with self.assertRaises(UserWarning):1374 str.__mod__("%(foo)s", C())1375 def test_without_mapping_raises_type_error(self):1376 with self.assertRaises(TypeError) as context:1377 str.__mod__("%(foo)s", None)1378 self.assertEqual(str(context.exception), "format requires a mapping")1379 with self.assertRaises(TypeError) as context:1380 str.__mod__("%(foo)s", "foobar")1381 self.assertEqual(str(context.exception), "format requires a mapping")1382 with self.assertRaises(TypeError) as context:1383 str.__mod__("%(foo)s", ("foobar",))1384 self.assertEqual(str(context.exception), "format requires a mapping")1385 def test_with_mapping_does_not_raise_type_error(self):1386 # The following must not raise1387 # "not all arguments converted during string formatting".1388 self.assertEqual(str.__mod__("foo", {"bar": 42}), "foo")1389 def test_positional_after_named_arg_raises_type_error(self):1390 with self.assertRaises(TypeError) as context:1391 str.__mod__("%(foo)s %s", {"foo": "bar"})1392 self.assertEqual(1393 str(context.exception), "not enough arguments for format string"1394 )1395 def test_mix_named_and_tuple_args_returns_string(self):1396 self.assertEqual(str.__mod__("%s %(a)s", {"a": 77}), "{'a': 77} 77")1397 def test_mapping_in_tuple_returns_string(self):1398 self.assertEqual(str.__mod__("%s", ({"foo": "bar"},)), "{'foo': 'bar'}")1399 def test_c_format_returns_string(self):1400 self.assertEqual(str.__mod__("%c", ("x",)), "x")1401 self.assertEqual(str.__mod__("%c", ("\U0001f44d",)), "\U0001f44d")1402 self.assertEqual(str.__mod__("%c", (76,)), "L")1403 self.assertEqual(str.__mod__("%c", (0x1F40D,)), "\U0001f40d")1404 def test_c_format_with_non_int_returns_string(self):1405 class C:1406 def __index__(self):1407 return 421408 self.assertEqual(str.__mod__("%c", (C(),)), "*")1409 def test_c_format_raises_overflow_error(self):1410 import sys1411 maxunicode_range = str.__mod__("range(0x%x)", (sys.maxunicode + 1))1412 with self.assertRaises(OverflowError) as context:1413 str.__mod__("%c", (sys.maxunicode + 1,))1414 self.assertEqual(str(context.exception), f"%c arg not in {maxunicode_range}")1415 with self.assertRaises(OverflowError) as context:1416 str.__mod__("%c", (-1,))1417 self.assertEqual(str(context.exception), f"%c arg not in {maxunicode_range}")1418 def test_c_format_raises_type_error(self):1419 with self.assertRaises(TypeError) as context:1420 str.__mod__("%c", (None,))1421 self.assertEqual(str(context.exception), "%c requires int or char")1422 with self.assertRaises(TypeError) as context:1423 str.__mod__("%c", ("ab",))1424 self.assertEqual(str(context.exception), "%c requires int or char")1425 with self.assertRaises(TypeError) as context:1426 str.__mod__("%c", (123456789012345678901234567890,))1427 self.assertEqual(str(context.exception), "%c requires int or char")1428 class C:1429 def __index__(self):1430 raise UserWarning()1431 with self.assertRaises(TypeError) as context:1432 str.__mod__("%c", (C(),))1433 self.assertEqual(str(context.exception), "%c requires int or char")1434 def test_d_format_returns_string_of_integer_value(self):1435 self.assertEqual(str.__mod__("%d", (1,)), "1")1436 def test_d_format_returns_string_of_boolean_value(self):1437 self.assertEqual(str.__mod__("%d", (True,)), "1")1438 def test_d_format_returns_string_of_integer_subclass(self):1439 class C(int):1440 def __str__(self):1441 return "C"1442 def __repr__(self):1443 return "C"1444 self.assertEqual(str.__mod__("%d", (C(),)), "0")1445 def test_s_format_returns_string(self):1446 self.assertEqual(str.__mod__("%s", ("foo",)), "foo")1447 class C:1448 __repr__ = None1449 def __str__(self):1450 return "str called"1451 self.assertEqual(str.__mod__("%s", (C(),)), "str called")1452 def test_s_format_propagates_errors(self):1453 class C:1454 def __str__(self):1455 raise UserWarning()1456 with self.assertRaises(UserWarning):1457 str.__mod__("%s", (C(),))1458 def test_r_format_returns_string(self):1459 self.assertEqual(str.__mod__("%r", (42,)), "42")1460 self.assertEqual(str.__mod__("%r", ("foo",)), "'foo'")1461 self.assertEqual(1462 str.__mod__("%r", ({"foo": "\U0001d4eb\U0001d4ea\U0001d4fb"},)),1463 "{'foo': '\U0001d4eb\U0001d4ea\U0001d4fb'}",1464 )1465 class C:1466 def __repr__(self):1467 return "repr called"1468 __str__ = None1469 self.assertEqual(str.__mod__("%r", (C(),)), "repr called")1470 def test_r_format_propagates_errors(self):1471 class C:1472 def __repr__(self):1473 raise UserWarning()1474 with self.assertRaises(UserWarning):1475 str.__mod__("%r", (C(),))1476 def test_a_format_returns_string(self):1477 self.assertEqual(str.__mod__("%a", (42,)), "42")1478 self.assertEqual(str.__mod__("%a", ("foo",)), "'foo'")1479 class C:1480 def __repr__(self):1481 return "repr \xca\r\ucafe\U00015acb called"1482 __str__ = None1483 self.assertEqual(1484 str.__mod__("%a", (C(),)), "repr \\xca\r\\ucafe\\U00015acb called"1485 )1486 def test_a_format_propagates_errors(self):1487 class C:1488 def __repr__(self):1489 raise UserWarning()1490 with self.assertRaises(UserWarning):1491 str.__mod__("%a", (C(),))1492 def test_diu_format_returns_string(self):1493 self.assertEqual(str.__mod__("%d", (0,)), "0")1494 self.assertEqual(str.__mod__("%d", (-1,)), "-1")1495 self.assertEqual(str.__mod__("%d", (42,)), "42")1496 self.assertEqual(str.__mod__("%i", (0,)), "0")1497 self.assertEqual(str.__mod__("%i", (-1,)), "-1")1498 self.assertEqual(str.__mod__("%i", (42,)), "42")1499 self.assertEqual(str.__mod__("%u", (0,)), "0")1500 self.assertEqual(str.__mod__("%u", (-1,)), "-1")1501 self.assertEqual(str.__mod__("%u", (42,)), "42")1502 def test_diu_format_with_largeint_returns_string(self):1503 self.assertEqual(1504 str.__mod__("%d", (-123456789012345678901234567890,)),1505 "-123456789012345678901234567890",1506 )1507 self.assertEqual(1508 str.__mod__("%i", (-123456789012345678901234567890,)),1509 "-123456789012345678901234567890",1510 )1511 self.assertEqual(1512 str.__mod__("%u", (-123456789012345678901234567890,)),1513 "-123456789012345678901234567890",1514 )1515 def test_diu_format_with_non_int_returns_string(self):1516 class C:1517 def __int__(self):1518 return 421519 def __index__(self):1520 raise UserWarning()1521 self.assertEqual(str.__mod__("%d", (C(),)), "42")1522 self.assertEqual(str.__mod__("%i", (C(),)), "42")1523 self.assertEqual(str.__mod__("%u", (C(),)), "42")1524 def test_diu_format_raises_typeerrors(self):1525 with self.assertRaises(TypeError) as context:1526 str.__mod__("%d", (None,))1527 self.assertEqual(1528 str(context.exception), "%d format: a number is required, not NoneType"1529 )1530 with self.assertRaises(TypeError) as context:1531 str.__mod__("%i", (None,))1532 self.assertEqual(1533 str(context.exception), "%i format: a number is required, not NoneType"1534 )1535 with self.assertRaises(TypeError) as context:1536 str.__mod__("%u", (None,))1537 self.assertEqual(1538 str(context.exception), "%u format: a number is required, not NoneType"1539 )1540 def test_diu_format_propagates_errors(self):1541 class C:1542 def __int__(self):1543 raise UserWarning()1544 with self.assertRaises(UserWarning):1545 str.__mod__("%d", (C(),))1546 with self.assertRaises(UserWarning):1547 str.__mod__("%i", (C(),))1548 with self.assertRaises(UserWarning):1549 str.__mod__("%u", (C(),))1550 def test_diu_format_reraises_typerrors(self):1551 class C:1552 def __int__(self):1553 raise TypeError("foobar")1554 with self.assertRaises(TypeError) as context:1555 str.__mod__("%d", (C(),))1556 self.assertEqual(1557 str(context.exception), "%d format: a number is required, not C"1558 )1559 with self.assertRaises(TypeError) as context:1560 str.__mod__("%i", (C(),))1561 self.assertEqual(1562 str(context.exception), "%i format: a number is required, not C"1563 )1564 with self.assertRaises(TypeError) as context:1565 str.__mod__("%u", (C(),))1566 self.assertEqual(1567 str(context.exception), "%u format: a number is required, not C"1568 )1569 def test_xX_format_returns_string(self):1570 self.assertEqual(str.__mod__("%x", (0,)), "0")1571 self.assertEqual(str.__mod__("%x", (-123,)), "-7b")1572 self.assertEqual(str.__mod__("%x", (42,)), "2a")1573 self.assertEqual(str.__mod__("%x", (True,)), "1")1574 self.assertEqual(str.__mod__("%X", (0,)), "0")1575 self.assertEqual(str.__mod__("%X", (-123,)), "-7B")1576 self.assertEqual(str.__mod__("%X", (42,)), "2A")1577 def test_xX_format_with_int_subclass_returns_string(self):1578 class C(int):1579 def __str__(self):1580 return "C"1581 def __repr__(self):1582 return "C"1583 self.assertEqual(str.__mod__("%x", (C(),)), "0")1584 def test_xX_format_with_largeint_returns_string(self):1585 self.assertEqual(1586 str.__mod__("%x", (-123456789012345678901234567890,)),1587 "-18ee90ff6c373e0ee4e3f0ad2",1588 )1589 self.assertEqual(1590 str.__mod__("%X", (-123456789012345678901234567890,)),1591 "-18EE90FF6C373E0EE4E3F0AD2",1592 )1593 def test_xX_format_with_non_int_returns_string(self):1594 class C:1595 def __float__(self):1596 return 3.31597 def __index__(self):1598 return 771599 self.assertEqual(str.__mod__("%x", (C(),)), "4d")1600 self.assertEqual(str.__mod__("%X", (C(),)), "4D")1601 def test_xX_format_raises_typeerrors(self):1602 with self.assertRaises(TypeError) as context:1603 str.__mod__("%x", (None,))1604 self.assertEqual(1605 str(context.exception), "%x format: an integer is required, not NoneType"1606 )1607 with self.assertRaises(TypeError) as context:1608 str.__mod__("%X", (None,))1609 self.assertEqual(1610 str(context.exception), "%X format: an integer is required, not NoneType"1611 )1612 def test_xX_format_propagates_errors(self):1613 class C:1614 def __int__(self):1615 return 421616 def __index__(self):1617 raise UserWarning()1618 with self.assertRaises(UserWarning):1619 str.__mod__("%x", (C(),))1620 with self.assertRaises(UserWarning):1621 str.__mod__("%X", (C(),))1622 def test_xX_format_reraises_typerrors(self):1623 class C:1624 def __int__(self):1625 return 421626 def __index__(self):1627 raise TypeError("foobar")1628 with self.assertRaises(TypeError) as context:1629 str.__mod__("%x", (C(),))1630 self.assertEqual(1631 str(context.exception), "%x format: an integer is required, not C"1632 )1633 with self.assertRaises(TypeError) as context:1634 str.__mod__("%X", (C(),))1635 self.assertEqual(1636 str(context.exception), "%X format: an integer is required, not C"1637 )1638 def test_o_format_returns_string(self):1639 self.assertEqual(str.__mod__("%o", (0,)), "0")1640 self.assertEqual(str.__mod__("%o", (-123,)), "-173")1641 self.assertEqual(str.__mod__("%o", (42,)), "52")1642 def test_o_format_with_largeint_returns_string(self):1643 self.assertEqual(1644 str.__mod__("%o", (-123456789012345678901234567890)),1645 "-143564417755415637016711617605322",1646 )1647 def test_o_format_with_non_int_returns_string(self):1648 class C:1649 def __float__(self):1650 return 3.31651 def __index__(self):1652 return 771653 self.assertEqual(str.__mod__("%o", (C(),)), "115")1654 def test_o_format_raises_typeerrors(self):1655 with self.assertRaises(TypeError) as context:1656 str.__mod__("%o", (None,))1657 self.assertEqual(1658 str(context.exception), "%o format: an integer is required, not NoneType"1659 )1660 def test_o_format_propagates_errors(self):1661 class C:1662 def __int__(self):1663 return 421664 def __index__(self):1665 raise UserWarning()1666 with self.assertRaises(UserWarning):1667 str.__mod__("%o", (C(),))1668 def test_o_format_reraises_typerrors(self):1669 class C:1670 def __int__(self):1671 return 421672 def __index__(self):1673 raise TypeError("foobar")1674 with self.assertRaises(TypeError) as context:1675 str.__mod__("%o", (C(),))1676 self.assertEqual(1677 str(context.exception), "%o format: an integer is required, not C"1678 )1679 def test_f_format_returns_string(self):1680 self.assertEqual(str.__mod__("%f", (0.0,)), "0.000000")1681 self.assertEqual(str.__mod__("%f", (-0.0,)), "-0.000000")1682 self.assertEqual(str.__mod__("%f", (1.0,)), "1.000000")1683 self.assertEqual(str.__mod__("%f", (-1.0,)), "-1.000000")1684 self.assertEqual(str.__mod__("%f", (42.125,)), "42.125000")1685 self.assertEqual(str.__mod__("%f", (1e3,)), "1000.000000")1686 self.assertEqual(str.__mod__("%f", (1e6,)), "1000000.000000")1687 self.assertEqual(1688 str.__mod__("%f", (1e40,)),1689 "10000000000000000303786028427003666890752.000000",1690 )1691 def test_F_format_returns_string(self):1692 self.assertEqual(str.__mod__("%F", (42.125,)), "42.125000")1693 def test_e_format_returns_string(self):1694 self.assertEqual(str.__mod__("%e", (0.0,)), "0.000000e+00")1695 self.assertEqual(str.__mod__("%e", (-0.0,)), "-0.000000e+00")1696 self.assertEqual(str.__mod__("%e", (1.0,)), "1.000000e+00")1697 self.assertEqual(str.__mod__("%e", (-1.0,)), "-1.000000e+00")1698 self.assertEqual(str.__mod__("%e", (42.125,)), "4.212500e+01")1699 self.assertEqual(str.__mod__("%e", (1e3,)), "1.000000e+03")1700 self.assertEqual(str.__mod__("%e", (1e6,)), "1.000000e+06")1701 self.assertEqual(str.__mod__("%e", (1e40,)), "1.000000e+40")1702 def test_E_format_returns_string(self):1703 self.assertEqual(str.__mod__("%E", (1.0,)), "1.000000E+00")1704 def test_g_format_returns_string(self):1705 self.assertEqual(str.__mod__("%g", (0.0,)), "0")1706 self.assertEqual(str.__mod__("%g", (-1.0,)), "-1")1707 self.assertEqual(str.__mod__("%g", (0.125,)), "0.125")1708 self.assertEqual(str.__mod__("%g", (3.5,)), "3.5")1709 def test_eEfFgG_format_with_inf_returns_string(self):1710 self.assertEqual(str.__mod__("%e", (float("inf"),)), "inf")1711 self.assertEqual(str.__mod__("%E", (float("inf"),)), "INF")1712 self.assertEqual(str.__mod__("%f", (float("inf"),)), "inf")1713 self.assertEqual(str.__mod__("%F", (float("inf"),)), "INF")1714 self.assertEqual(str.__mod__("%g", (float("inf"),)), "inf")1715 self.assertEqual(str.__mod__("%G", (float("inf"),)), "INF")1716 self.assertEqual(str.__mod__("%e", (-float("inf"),)), "-inf")1717 self.assertEqual(str.__mod__("%E", (-float("inf"),)), "-INF")1718 self.assertEqual(str.__mod__("%f", (-float("inf"),)), "-inf")1719 self.assertEqual(str.__mod__("%F", (-float("inf"),)), "-INF")1720 self.assertEqual(str.__mod__("%g", (-float("inf"),)), "-inf")1721 self.assertEqual(str.__mod__("%G", (-float("inf"),)), "-INF")1722 def test_eEfFgG_format_with_nan_returns_string(self):1723 self.assertEqual(str.__mod__("%e", (float("nan"),)), "nan")1724 self.assertEqual(str.__mod__("%E", (float("nan"),)), "NAN")1725 self.assertEqual(str.__mod__("%f", (float("nan"),)), "nan")1726 self.assertEqual(str.__mod__("%F", (float("nan"),)), "NAN")1727 self.assertEqual(str.__mod__("%g", (float("nan"),)), "nan")1728 self.assertEqual(str.__mod__("%G", (float("nan"),)), "NAN")1729 self.assertEqual(str.__mod__("%e", (float("-nan"),)), "nan")1730 self.assertEqual(str.__mod__("%E", (float("-nan"),)), "NAN")1731 self.assertEqual(str.__mod__("%f", (float("-nan"),)), "nan")1732 self.assertEqual(str.__mod__("%F", (float("-nan"),)), "NAN")1733 self.assertEqual(str.__mod__("%g", (float("-nan"),)), "nan")1734 self.assertEqual(str.__mod__("%G", (float("-nan"),)), "NAN")1735 def test_f_format_with_precision_returns_string(self):1736 number = 1.234567891234567891737 self.assertEqual(str.__mod__("%.0f", number), "1")1738 self.assertEqual(str.__mod__("%.1f", number), "1.2")1739 self.assertEqual(str.__mod__("%.2f", number), "1.23")1740 self.assertEqual(str.__mod__("%.3f", number), "1.235")1741 self.assertEqual(str.__mod__("%.4f", number), "1.2346")1742 self.assertEqual(str.__mod__("%.5f", number), "1.23457")1743 self.assertEqual(str.__mod__("%.6f", number), "1.234568")1744 self.assertEqual(str.__mod__("%f", number), "1.234568")1745 self.assertEqual(str.__mod__("%.17f", number), "1.23456789123456789")1746 self.assertEqual(str.__mod__("%.25f", number), "1.2345678912345678934769921")1747 self.assertEqual(1748 str.__mod__("%.60f", number),1749 "1.234567891234567893476992139767389744520187377929687500000000",1750 )1751 def test_eEfFgG_format_with_precision_returns_string(self):1752 number = 1.234567891234567891753 self.assertEqual(str.__mod__("%.0e", number), "1e+00")1754 self.assertEqual(str.__mod__("%.0E", number), "1E+00")1755 self.assertEqual(str.__mod__("%.0f", number), "1")1756 self.assertEqual(str.__mod__("%.0F", number), "1")1757 self.assertEqual(str.__mod__("%.0g", number), "1")1758 self.assertEqual(str.__mod__("%.0G", number), "1")1759 self.assertEqual(str.__mod__("%.4e", number), "1.2346e+00")1760 self.assertEqual(str.__mod__("%.4E", number), "1.2346E+00")1761 self.assertEqual(str.__mod__("%.4f", number), "1.2346")1762 self.assertEqual(str.__mod__("%.4F", number), "1.2346")1763 self.assertEqual(str.__mod__("%.4g", number), "1.235")1764 self.assertEqual(str.__mod__("%.4G", number), "1.235")1765 self.assertEqual(str.__mod__("%e", number), "1.234568e+00")1766 self.assertEqual(str.__mod__("%E", number), "1.234568E+00")1767 self.assertEqual(str.__mod__("%f", number), "1.234568")1768 self.assertEqual(str.__mod__("%F", number), "1.234568")1769 self.assertEqual(str.__mod__("%g", number), "1.23457")1770 self.assertEqual(str.__mod__("%G", number), "1.23457")1771 def test_g_format_with_flags_and_width_returns_string(self):1772 self.assertEqual(str.__mod__("%5g", 7.0), " 7")1773 self.assertEqual(str.__mod__("%5g", 7.2), " 7.2")1774 self.assertEqual(str.__mod__("% 5g", 7.2), " 7.2")1775 self.assertEqual(str.__mod__("%+5g", 7.2), " +7.2")1776 self.assertEqual(str.__mod__("%5g", -7.2), " -7.2")1777 self.assertEqual(str.__mod__("% 5g", -7.2), " -7.2")1778 self.assertEqual(str.__mod__("%+5g", -7.2), " -7.2")1779 self.assertEqual(str.__mod__("%-5g", 7.0), "7 ")1780 self.assertEqual(str.__mod__("%-5g", 7.2), "7.2 ")1781 self.assertEqual(str.__mod__("%- 5g", 7.2), " 7.2 ")1782 self.assertEqual(str.__mod__("%-+5g", 7.2), "+7.2 ")1783 self.assertEqual(str.__mod__("%-5g", -7.2), "-7.2 ")1784 self.assertEqual(str.__mod__("%- 5g", -7.2), "-7.2 ")1785 self.assertEqual(str.__mod__("%-+5g", -7.2), "-7.2 ")1786 self.assertEqual(str.__mod__("%#g", 7.0), "7.00000")1787 self.assertEqual(str.__mod__("%#- 7.2g", float("-nan")), " nan ")1788 self.assertEqual(str.__mod__("%#- 7.2g", float("inf")), " inf ")1789 self.assertEqual(str.__mod__("%#- 7.2g", float("-inf")), "-inf ")1790 def test_eEfFgG_format_with_flags_and_width_returns_string(self):1791 number = 1.234567891234567891792 self.assertEqual(str.__mod__("% -#12.3e", number), " 1.235e+00 ")1793 self.assertEqual(str.__mod__("% -#12.3E", number), " 1.235E+00 ")1794 self.assertEqual(str.__mod__("% -#12.3f", number), " 1.235 ")1795 self.assertEqual(str.__mod__("% -#12.3F", number), " 1.235 ")1796 self.assertEqual(str.__mod__("% -#12.3g", number), " 1.23 ")1797 self.assertEqual(str.__mod__("% -#12.3G", number), " 1.23 ")1798 def test_ef_format_with_non_float_raises_type_error(self):1799 with self.assertRaises(TypeError) as context:1800 str.__mod__("%e", (None,))1801 self.assertEqual(str(context.exception), "must be real number, not NoneType")1802 class C:1803 def __float__(self):1804 return "not a float"1805 with self.assertRaises(TypeError) as context:1806 str.__mod__("%f", (C(),))1807 self.assertEqual(1808 str(context.exception), "C.__float__ returned non-float (type str)"1809 )1810 def test_g_format_propogates_errors(self):1811 class C:1812 def __float__(self):1813 raise UserWarning()1814 with self.assertRaises(UserWarning):1815 str.__mod__("%g", (C(),))1816 def test_efg_format_with_non_float_returns_string(self):1817 class A(float):1818 pass1819 self.assertEqual(str.__mod__("%e", (A(9.625),)), str.__mod__("%e", (9.625,)))1820 class C:1821 def __float__(self):1822 return 3.51823 self.assertEqual(str.__mod__("%f", (C(),)), str.__mod__("%f", (3.5,)))1824 class D:1825 def __float__(self):1826 return A(-12.75)1827 warnings.filterwarnings(1828 action="ignore",1829 category=DeprecationWarning,1830 message=".*__float__ returned non-float.*",1831 module=__name__,1832 )1833 self.assertEqual(str.__mod__("%g", (D(),)), str.__mod__("%g", (-12.75,)))1834 def test_percent_format_returns_percent(self):1835 self.assertEqual(str.__mod__("%%", ()), "%")1836 def test_escaped_percent_with_characters_between_raises_type_error(self):1837 with self.assertRaises(TypeError) as context:1838 str.__mod__("%0.0%", ())1839 self.assertEqual(1840 str(context.exception), "not enough arguments for format string"1841 )1842 with self.assertRaises(TypeError) as context:1843 str.__mod__("%*.%", (42,))1844 self.assertEqual(1845 str(context.exception), "not enough arguments for format string"1846 )1847 with self.assertRaises(TypeError) as context:1848 str.__mod__("%d %*.%", (42,))1849 self.assertEqual(1850 str(context.exception), "not enough arguments for format string"1851 )1852 with self.assertRaises(TypeError) as context:1853 str.__mod__("%.*%", (88,))1854 self.assertEqual(1855 str(context.exception), "not enough arguments for format string"1856 )1857 with self.assertRaises(TypeError) as context:1858 str.__mod__("%0#*.42%", (1234,))1859 self.assertEqual(1860 str(context.exception), "not enough arguments for format string"1861 )1862 def test_escaped_percent_with_characters_between_raises_value_error(self):1863 with self.assertRaises(ValueError) as context:1864 str.__mod__("%*.%", (42, 1))1865 self.assertEqual(1866 str(context.exception), "unsupported format character '%' (0x25) at index 3"1867 )1868 def test_flags_get_accepted(self):1869 self.assertEqual(str.__mod__("%-s", ""), "")1870 self.assertEqual(str.__mod__("%+s", ""), "")1871 self.assertEqual(str.__mod__("% s", ""), "")1872 self.assertEqual(str.__mod__("%#s", ""), "")1873 self.assertEqual(str.__mod__("%0s", ""), "")1874 self.assertEqual(str.__mod__("%#-#0+ -s", ""), "")1875 def test_string_format_with_width_returns_string(self):1876 self.assertEqual(str.__mod__("%5s", "oh"), " oh")1877 self.assertEqual(str.__mod__("%-5s", "ah"), "ah ")1878 self.assertEqual(str.__mod__("%05s", "uh"), " uh")1879 self.assertEqual(str.__mod__("%-# 5s", "eh"), "eh ")1880 self.assertEqual(str.__mod__("%0s", "foo"), "foo")1881 self.assertEqual(str.__mod__("%-0s", "foo"), "foo")1882 self.assertEqual(str.__mod__("%10s", "hello world"), "hello world")1883 self.assertEqual(str.__mod__("%-10s", "hello world"), "hello world")1884 def test_string_format_with_width_star_returns_string(self):1885 self.assertEqual(str.__mod__("%*s", (7, "foo")), " foo")1886 self.assertEqual(str.__mod__("%*s", (-7, "bar")), "bar ")1887 self.assertEqual(str.__mod__("%-*s", (7, "baz")), "baz ")1888 self.assertEqual(str.__mod__("%-*s", (-7, "bam")), "bam ")1889 def test_string_format_with_precision_returns_string(self):1890 self.assertEqual(str.__mod__("%.3s", "python"), "pyt")1891 self.assertEqual(str.__mod__("%.0s", "python"), "")1892 self.assertEqual(str.__mod__("%.10s", "python"), "python")1893 def test_string_format_with_precision_star_returns_string(self):1894 self.assertEqual(str.__mod__("%.*s", (3, "monty")), "mon")1895 self.assertEqual(str.__mod__("%.*s", (0, "monty")), "")1896 self.assertEqual(str.__mod__("%.*s", (-4, "monty")), "")1897 def test_string_format_with_width_and_precision_returns_string(self):1898 self.assertEqual(str.__mod__("%8.3s", ("foobar",)), " foo")1899 self.assertEqual(str.__mod__("%-8.3s", ("foobar",)), "foo ")1900 self.assertEqual(str.__mod__("%*.3s", (8, "foobar")), " foo")1901 self.assertEqual(str.__mod__("%*.3s", (-8, "foobar")), "foo ")1902 self.assertEqual(str.__mod__("%8.*s", (3, "foobar")), " foo")1903 self.assertEqual(str.__mod__("%-8.*s", (3, "foobar")), "foo ")1904 self.assertEqual(str.__mod__("%*.*s", (8, 3, "foobar")), " foo")1905 self.assertEqual(str.__mod__("%-*.*s", (8, 3, "foobar")), "foo ")1906 def test_s_r_a_c_formats_accept_flags_width_precision_return_strings(self):1907 self.assertEqual(str.__mod__("%-*.3s", (8, "foobar")), "foo ")1908 self.assertEqual(str.__mod__("%-*.3r", (8, "foobar")), "'fo ")1909 self.assertEqual(str.__mod__("%-*.3a", (8, "foobar")), "'fo ")1910 self.assertEqual(str.__mod__("%-*.3c", (8, 94)), "^ ")1911 def test_number_format_with_sign_flag_returns_string(self):1912 self.assertEqual(str.__mod__("%+d", (42,)), "+42")1913 self.assertEqual(str.__mod__("%+d", (-42,)), "-42")1914 self.assertEqual(str.__mod__("% d", (17,)), " 17")1915 self.assertEqual(str.__mod__("% d", (-17,)), "-17")1916 self.assertEqual(str.__mod__("%+ d", (42,)), "+42")1917 self.assertEqual(str.__mod__("%+ d", (-42,)), "-42")1918 self.assertEqual(str.__mod__("% +d", (17,)), "+17")1919 self.assertEqual(str.__mod__("% +d", (-17,)), "-17")1920 def test_number_format_alt_flag_returns_string(self):1921 self.assertEqual(str.__mod__("%#d", (23,)), "23")1922 self.assertEqual(str.__mod__("%#x", (23,)), "0x17")1923 self.assertEqual(str.__mod__("%#X", (23,)), "0X17")1924 self.assertEqual(str.__mod__("%#o", (23,)), "0o27")1925 def test_number_format_with_width_returns_string(self):1926 self.assertEqual(str.__mod__("%5d", (123,)), " 123")1927 self.assertEqual(str.__mod__("%5d", (-8,)), " -8")1928 self.assertEqual(str.__mod__("%-5d", (123,)), "123 ")1929 self.assertEqual(str.__mod__("%-5d", (-8,)), "-8 ")1930 self.assertEqual(str.__mod__("%05d", (123,)), "00123")1931 self.assertEqual(str.__mod__("%05d", (-8,)), "-0008")1932 self.assertEqual(str.__mod__("%-05d", (123,)), "123 ")1933 self.assertEqual(str.__mod__("%0-5d", (-8,)), "-8 ")1934 self.assertEqual(str.__mod__("%#7x", (42,)), " 0x2a")1935 self.assertEqual(str.__mod__("%#7x", (-42,)), " -0x2a")1936 self.assertEqual(str.__mod__("%5d", (123456,)), "123456")1937 self.assertEqual(str.__mod__("%-5d", (-123456,)), "-123456")1938 def test_number_format_with_precision_returns_string(self):1939 self.assertEqual(str.__mod__("%.5d", (123,)), "00123")1940 self.assertEqual(str.__mod__("%.5d", (-123,)), "-00123")1941 self.assertEqual(str.__mod__("%.5d", (1234567,)), "1234567")1942 self.assertEqual(str.__mod__("%#.5x", (99,)), "0x00063")1943 def test_number_format_with_width_precision_flags_returns_string(self):1944 self.assertEqual(str.__mod__("%8.3d", (12,)), " 012")1945 self.assertEqual(str.__mod__("%8.3d", (-7,)), " -007")1946 self.assertEqual(str.__mod__("%05.3d", (12,)), "00012")1947 self.assertEqual(str.__mod__("%+05.3d", (12,)), "+0012")1948 self.assertEqual(str.__mod__("% 05.3d", (12,)), " 0012")1949 self.assertEqual(str.__mod__("% 05.3x", (19,)), " 0013")1950 self.assertEqual(str.__mod__("%-8.3d", (12,)), "012 ")1951 self.assertEqual(str.__mod__("%-8.3d", (-7,)), "-007 ")1952 self.assertEqual(str.__mod__("%- 8.3d", (66,)), " 066 ")1953 def test_width_and_precision_star_raises_type_error(self):1954 with self.assertRaises(TypeError) as context:1955 str.__mod__("%*d", (42,))1956 self.assertEqual(1957 str(context.exception), "not enough arguments for format string"1958 )1959 with self.assertRaises(TypeError) as context:1960 str.__mod__("%.*d", (42,))1961 self.assertEqual(1962 str(context.exception), "not enough arguments for format string"1963 )1964 with self.assertRaises(TypeError) as context:1965 str.__mod__("%*.*d", (42,))1966 self.assertEqual(1967 str(context.exception), "not enough arguments for format string"1968 )1969 with self.assertRaises(TypeError) as context:1970 str.__mod__("%*.*d", (1, 2))1971 self.assertEqual(1972 str(context.exception), "not enough arguments for format string"1973 )1974 def test_negative_precision_raises_value_error(self):1975 with self.assertRaises(ValueError) as context:1976 str.__mod__("%.-2s", "foo")1977 self.assertEqual(1978 str(context.exception), "unsupported format character '-' (0x2d) at index 2"1979 )1980 def test_two_specifiers_returns_string(self):1981 self.assertEqual(str.__mod__("%s%s", ("foo", "bar")), "foobar")1982 self.assertEqual(str.__mod__(",%s%s", ("foo", "bar")), ",foobar")1983 self.assertEqual(str.__mod__("%s,%s", ("foo", "bar")), "foo,bar")1984 self.assertEqual(str.__mod__("%s%s,", ("foo", "bar")), "foobar,")1985 self.assertEqual(str.__mod__(",%s..%s---", ("foo", "bar")), ",foo..bar---")1986 self.assertEqual(str.__mod__(",%s...%s--", ("foo", "bar")), ",foo...bar--")1987 self.assertEqual(str.__mod__(",,%s.%s---", ("foo", "bar")), ",,foo.bar---")1988 self.assertEqual(str.__mod__(",,%s...%s-", ("foo", "bar")), ",,foo...bar-")1989 self.assertEqual(str.__mod__(",,,%s..%s-", ("foo", "bar")), ",,,foo..bar-")1990 self.assertEqual(str.__mod__(",,,%s.%s--", ("foo", "bar")), ",,,foo.bar--")1991 def test_mixed_specifiers_with_percents_returns_string(self):1992 self.assertEqual(str.__mod__("%%%s%%%s%%", ("foo", "bar")), "%foo%bar%")1993 def test_mixed_specifiers_returns_string(self):1994 self.assertEqual(1995 str.__mod__("a %d %g %s", (123, 3.14, "baz")), "a 123 3.14 baz"1996 )1997 def test_specifier_missing_format_raises_value_error(self):1998 with self.assertRaises(ValueError) as context:1999 str.__mod__("%", ())2000 self.assertEqual(str(context.exception), "incomplete format")2001 with self.assertRaises(ValueError) as context:2002 str.__mod__("%(foo)", {"foo": None})2003 self.assertEqual(str(context.exception), "incomplete format")2004 def test_unknown_specifier_raises_value_error(self):2005 with self.assertRaises(ValueError) as context:2006 str.__mod__("try %Y", (42,))2007 self.assertEqual(2008 str(context.exception), "unsupported format character 'Y' (0x59) at index 5"2009 )2010 def test_too_few_args_raises_type_error(self):2011 with self.assertRaises(TypeError) as context:2012 str.__mod__("%s%s", ("foo",))2013 self.assertEqual(2014 str(context.exception), "not enough arguments for format string"2015 )2016 def test_too_many_args_raises_type_error(self):2017 with self.assertRaises(TypeError) as context:2018 str.__mod__("hello", 42)2019 self.assertEqual(2020 str(context.exception),2021 "not all arguments converted during string formatting",2022 )2023 with self.assertRaises(TypeError) as context:2024 str.__mod__("%d%s", (1, "foo", 3))2025 self.assertEqual(2026 str(context.exception),2027 "not all arguments converted during string formatting",2028 )2029if __name__ == "__main__":...

Full Screen

Full Screen

builtins_bytes_test.py

Source:builtins_bytes_test.py Github

copy

Full Screen

...882 self.assertIsInstance(dst, bytes)883 self.assertEqual(dst, b"A1!B2@C3#D4$E5%F6^G7&H8*I9(J0)")884class BytesModTests(unittest.TestCase):885 def test_empty_format_returns_empty_bytes(self):886 self.assertEqual(bytes.__mod__(b"", ()), b"")887 def test_simple_string_returns_bytes(self):888 self.assertEqual(bytes.__mod__(b"foo bar (}", ()), b"foo bar (}")889 def test_with_non_tuple_args_returns_bytes(self):890 self.assertEqual(bytes.__mod__(b"%s", b"foo"), b"foo")891 self.assertEqual(bytes.__mod__(b"%d", 42), b"42")892 def test_with_named_args_returns_bytes(self):893 self.assertEqual(894 bytes.__mod__(b"%(foo)s %(bar)d", {b"foo": b"ho", b"bar": 42}), b"ho 42"895 )896 self.assertEqual(bytes.__mod__(b"%()x", {b"": 123}), b"7b")897 self.assertEqual(bytes.__mod__(b")%(((()) ()))d(", {b"((()) ())": 99}), b")99(")898 self.assertEqual(bytes.__mod__(b"%(%s)d", {b"%s": -5}), b"-5")899 def test_with_custom_mapping_returns_bytes(self):900 class C:901 def __getitem__(self, key):902 return b"getitem called with " + key903 self.assertEqual(bytes.__mod__(b"%(foo)s", C()), b"getitem called with foo")904 def test_without_mapping_raises_type_error(self):905 with self.assertRaises(TypeError) as context:906 bytes.__mod__(b"%(foo)s", None)907 self.assertEqual(str(context.exception), "format requires a mapping")908 with self.assertRaises(TypeError) as context:909 bytes.__mod__(b"%(foo)s", "foobar")910 self.assertEqual(str(context.exception), "format requires a mapping")911 with self.assertRaises(TypeError) as context:912 bytes.__mod__(b"%(foo)s", ("foobar",))913 self.assertEqual(str(context.exception), "format requires a mapping")914 def test_with_mapping_does_not_raise_type_error(self):915 # The following must not raise916 # "not all arguments converted during string formatting".917 self.assertEqual(bytes.__mod__(b"foo", {"bar": 42}), b"foo")918 def test_positional_after_named_arg_raises_type_error(self):919 with self.assertRaises(TypeError) as context:920 bytes.__mod__(b"%(foo)s %s", {b"foo": b"bar"})921 self.assertEqual(922 str(context.exception), "not enough arguments for format string"923 )924 def test_c_format_raises_type_error(self):925 with self.assertRaises(TypeError) as context:926 bytes.__mod__(b"%c", ("x",))927 self.assertEqual(928 str(context.exception),929 "%c requires an integer in range(256) or a single byte",930 )931 self.assertEqual(bytes.__mod__(b"%c", ("\U0001f44d",)), b"\U0001f44d")932 self.assertEqual(bytes.__mod__(b"%c", (76,)), b"L")933 self.assertEqual(bytes.__mod__(b"%c", (0x1F40D,)), b"\U0001f40d")934 def test_c_format_with_non_int_returns_bytes(self):935 class C:936 def __index__(self):937 return 42938 self.assertEqual(bytes.__mod__(b"%c", (C(),)), b"*")939 def test_c_format_raises_overflow_error(self):940 import sys941 with self.assertRaises(OverflowError) as context:942 bytes.__mod__(b"%c", (sys.maxunicode + 1,))943 self.assertEqual(str(context.exception), "%c arg not in range(256)")944 with self.assertRaises(OverflowError) as context:945 bytes.__mod__(b"%c", (-1,))946 self.assertEqual(str(context.exception), "%c arg not in range(256)")947 def test_c_format_raises_type_error(self):948 with self.assertRaises(TypeError) as context:949 bytes.__mod__(b"%c", (None,))950 self.assertEqual(951 str(context.exception),952 "%c requires an integer in range(256) or a single byte",953 )954 with self.assertRaises(TypeError) as context:955 bytes.__mod__(b"%c", ("ab",))956 self.assertEqual(957 str(context.exception),958 "%c requires an integer in range(256) or a single byte",959 )960 with self.assertRaises(OverflowError) as context:961 bytes.__mod__(b"%c", (123456789012345678901234567890,))962 self.assertEqual(str(context.exception), "%c arg not in range(256)")963 def test_s_format_returns_bytes(self):964 self.assertEqual(bytes.__mod__(b"%s", (b"foo",)), b"foo")965 class C:966 def __bytes__(self):967 return b"bytes called"968 r = bytes.__mod__(b"%s", (C(),))969 self.assertEqual(r, b"bytes called")970 def test_s_format_propagates_errors(self):971 class C:972 def __bytes__(self):973 raise UserWarning()974 with self.assertRaises(UserWarning):975 bytes.__mod__(b"%s", (C(),))976 def test_r_format_returns_bytes(self):977 self.assertEqual(bytes.__mod__(b"%r", (42,)), b"42")978 self.assertEqual(bytes.__mod__(b"%r", ("foo",)), b"'foo'")979 self.assertEqual(980 bytes.__mod__(b"%r", ({"foo": "\U0001d4eb\U0001d4ea\U0001d4fb"},)),981 b"{'foo': '\U0001d4eb\U0001d4ea\U0001d4fb'}",982 )983 class C:984 def __repr__(self):985 return "repr called"986 __str__ = None987 self.assertEqual(bytes.__mod__(b"%r", (C(),)), b"repr called")988 def test_r_format_propagates_errors(self):989 class C:990 def __repr__(self):991 raise UserWarning()992 with self.assertRaises(UserWarning):993 bytes.__mod__(b"%r", (C(),))994 def test_a_format_returns_bytes(self):995 self.assertEqual(bytes.__mod__(b"%a", (42,)), b"42")996 self.assertEqual(bytes.__mod__(b"%a", ("foo",)), b"'foo'")997 class C:998 def __repr__(self):999 return "repr \t\xc4~\ucafe\U0001f00d called"1000 __str__ = None1001 self.assertEqual(1002 bytes.__mod__(b"%a", (C(),)), b"repr \t\\xc4~\\ucafe\\U0001f00d called"1003 )1004 def test_a_format_propagates_errors(self):1005 class C:1006 def __repr__(self):1007 raise UserWarning()1008 with self.assertRaises(UserWarning):1009 bytes.__mod__(b"%a", (C(),))1010 def test_diu_format_returns_bytes(self):1011 self.assertEqual(bytes.__mod__(b"%d", (0,)), b"0")1012 self.assertEqual(bytes.__mod__(b"%d", (-1,)), b"-1")1013 self.assertEqual(bytes.__mod__(b"%d", (42,)), b"42")1014 self.assertEqual(bytes.__mod__(b"%i", (0,)), b"0")1015 self.assertEqual(bytes.__mod__(b"%i", (-1,)), b"-1")1016 self.assertEqual(bytes.__mod__(b"%i", (42,)), b"42")1017 self.assertEqual(bytes.__mod__(b"%u", (0,)), b"0")1018 self.assertEqual(bytes.__mod__(b"%u", (-1,)), b"-1")1019 self.assertEqual(bytes.__mod__(b"%u", (42,)), b"42")1020 def test_diu_format_with_largeint_returns_bytes(self):1021 self.assertEqual(1022 bytes.__mod__(b"%d", (-123456789012345678901234567890,)),1023 b"-123456789012345678901234567890",1024 )1025 self.assertEqual(1026 bytes.__mod__(b"%i", (-123456789012345678901234567890,)),1027 b"-123456789012345678901234567890",1028 )1029 self.assertEqual(1030 bytes.__mod__(b"%u", (-123456789012345678901234567890,)),1031 b"-123456789012345678901234567890",1032 )1033 def test_diu_format_with_non_int_returns_bytes(self):1034 class C:1035 def __int__(self):1036 return 421037 def __index__(self):1038 raise UserWarning()1039 self.assertEqual(bytes.__mod__(b"%d", (C(),)), b"42")1040 self.assertEqual(bytes.__mod__(b"%i", (C(),)), b"42")1041 self.assertEqual(bytes.__mod__(b"%u", (C(),)), b"42")1042 def test_diu_format_raises_typeerrors(self):1043 with self.assertRaises(TypeError) as context:1044 bytes.__mod__(b"%d", (None,))1045 self.assertEqual(1046 str(context.exception), "%d format: a number is required, not NoneType"1047 )1048 with self.assertRaises(TypeError) as context:1049 bytes.__mod__(b"%i", (None,))1050 self.assertEqual(1051 str(context.exception), "%d format: a number is required, not NoneType"1052 )1053 with self.assertRaises(TypeError) as context:1054 bytes.__mod__(b"%u", (None,))1055 self.assertEqual(1056 str(context.exception), "%u format: a number is required, not NoneType"1057 )1058 def test_diu_format_propagates_errors(self):1059 class C:1060 def __int__(self):1061 raise UserWarning()1062 with self.assertRaises(UserWarning):1063 bytes.__mod__(b"%d", (C(),))1064 with self.assertRaises(UserWarning):1065 bytes.__mod__(b"%i", (C(),))1066 with self.assertRaises(UserWarning):1067 bytes.__mod__(b"%u", (C(),))1068 def test_diu_format_reraises_typerrors(self):1069 class C:1070 def __int__(self):1071 raise TypeError("foobar")1072 with self.assertRaises(TypeError) as context:1073 bytes.__mod__(b"%d", (C(),))1074 self.assertEqual(1075 str(context.exception), "%d format: a number is required, not C"1076 )1077 with self.assertRaises(TypeError) as context:1078 bytes.__mod__(b"%i", (C(),))1079 self.assertEqual(1080 str(context.exception), "%d format: a number is required, not C"1081 )1082 with self.assertRaises(TypeError) as context:1083 bytes.__mod__(b"%u", (C(),))1084 self.assertEqual(1085 str(context.exception), "%u format: a number is required, not C"1086 )1087 def test_xX_format_returns_bytes(self):1088 self.assertEqual(bytes.__mod__(b"%x", (0,)), b"0")1089 self.assertEqual(bytes.__mod__(b"%x", (-123,)), b"-7b")1090 self.assertEqual(bytes.__mod__(b"%x", (42,)), b"2a")1091 self.assertEqual(bytes.__mod__(b"%X", (0,)), b"0")1092 self.assertEqual(bytes.__mod__(b"%X", (-123,)), b"-7B")1093 self.assertEqual(bytes.__mod__(b"%X", (42,)), b"2A")1094 def test_xX_format_with_largeint_returns_bytes(self):1095 self.assertEqual(1096 bytes.__mod__(b"%x", (-123456789012345678901234567890,)),1097 b"-18ee90ff6c373e0ee4e3f0ad2",1098 )1099 self.assertEqual(1100 bytes.__mod__(b"%X", (-123456789012345678901234567890,)),1101 b"-18EE90FF6C373E0EE4E3F0AD2",1102 )1103 def test_xX_format_with_non_int_returns_bytes(self):1104 class C:1105 def __float__(self):1106 return 3.31107 def __index__(self):1108 return 771109 self.assertEqual(bytes.__mod__(b"%x", (C(),)), b"4d")1110 self.assertEqual(bytes.__mod__(b"%X", (C(),)), b"4D")1111 def test_xX_format_raises_typeerrors(self):1112 with self.assertRaises(TypeError) as context:1113 bytes.__mod__(b"%x", (None,))1114 self.assertEqual(1115 str(context.exception), "%x format: an integer is required, not NoneType"1116 )1117 with self.assertRaises(TypeError) as context:1118 bytes.__mod__(b"%X", (None,))1119 self.assertEqual(1120 str(context.exception), "%X format: an integer is required, not NoneType"1121 )1122 def test_xX_format_propagates_errors(self):1123 class C:1124 def __int__(self):1125 return 421126 def __index__(self):1127 raise UserWarning()1128 with self.assertRaises(UserWarning):1129 bytes.__mod__(b"%x", (C(),))1130 with self.assertRaises(UserWarning):1131 bytes.__mod__(b"%X", (C(),))1132 def test_xX_format_reraises_typerrors(self):1133 class C:1134 def __int__(self):1135 return 421136 def __index__(self):1137 raise TypeError("foobar")1138 with self.assertRaises(TypeError) as context:1139 bytes.__mod__(b"%x", (C(),))1140 self.assertEqual(1141 str(context.exception), "%x format: an integer is required, not C"1142 )1143 with self.assertRaises(TypeError) as context:1144 bytes.__mod__(b"%X", (C(),))1145 self.assertEqual(1146 str(context.exception), "%X format: an integer is required, not C"1147 )1148 def test_o_format_returns_bytes(self):1149 self.assertEqual(bytes.__mod__(b"%o", (0,)), b"0")1150 self.assertEqual(bytes.__mod__(b"%o", (-123,)), b"-173")1151 self.assertEqual(bytes.__mod__(b"%o", (42,)), b"52")1152 def test_o_format_with_largeint_returns_bytes(self):1153 self.assertEqual(1154 bytes.__mod__(b"%o", (-123456789012345678901234567890)),1155 b"-143564417755415637016711617605322",1156 )1157 def test_o_format_with_non_int_returns_bytes(self):1158 class C:1159 def __float__(self):1160 return 3.31161 def __index__(self):1162 return 771163 self.assertEqual(bytes.__mod__(b"%o", (C(),)), b"115")1164 def test_o_format_raises_typeerrors(self):1165 with self.assertRaises(TypeError) as context:1166 bytes.__mod__(b"%o", (None,))1167 self.assertEqual(1168 str(context.exception), "%o format: an integer is required, not NoneType"1169 )1170 def test_o_format_propagates_errors(self):1171 class C:1172 def __int__(self):1173 return 421174 def __index__(self):1175 raise UserWarning()1176 with self.assertRaises(UserWarning):1177 bytes.__mod__(b"%o", (C(),))1178 def test_o_format_reraises_typerrors(self):1179 class C:1180 def __int__(self):1181 return 421182 def __index__(self):1183 raise TypeError("foobar")1184 with self.assertRaises(TypeError) as context:1185 bytes.__mod__(b"%o", (C(),))1186 self.assertEqual(1187 str(context.exception), "%o format: an integer is required, not C"1188 )1189 def test_f_format_returns_bytes(self):1190 self.assertEqual(bytes.__mod__(b"%f", (0.0,)), b"0.000000")1191 self.assertEqual(bytes.__mod__(b"%f", (-0.0,)), b"-0.000000")1192 self.assertEqual(bytes.__mod__(b"%f", (1.0,)), b"1.000000")1193 self.assertEqual(bytes.__mod__(b"%f", (-1.0,)), b"-1.000000")1194 self.assertEqual(bytes.__mod__(b"%f", (42.125,)), b"42.125000")1195 self.assertEqual(bytes.__mod__(b"%f", (1e3,)), b"1000.000000")1196 self.assertEqual(bytes.__mod__(b"%f", (1e6,)), b"1000000.000000")1197 self.assertEqual(1198 bytes.__mod__(b"%f", (1e40,)),1199 b"10000000000000000303786028427003666890752.000000",1200 )1201 def test_F_format_returns_bytes(self):1202 self.assertEqual(bytes.__mod__(b"%F", (42.125,)), b"42.125000")1203 def test_e_format_returns_bytes(self):1204 self.assertEqual(bytes.__mod__(b"%e", (0.0,)), b"0.000000e+00")1205 self.assertEqual(bytes.__mod__(b"%e", (-0.0,)), b"-0.000000e+00")1206 self.assertEqual(bytes.__mod__(b"%e", (1.0,)), b"1.000000e+00")1207 self.assertEqual(bytes.__mod__(b"%e", (-1.0,)), b"-1.000000e+00")1208 self.assertEqual(bytes.__mod__(b"%e", (42.125,)), b"4.212500e+01")1209 self.assertEqual(bytes.__mod__(b"%e", (1e3,)), b"1.000000e+03")1210 self.assertEqual(bytes.__mod__(b"%e", (1e6,)), b"1.000000e+06")1211 self.assertEqual(bytes.__mod__(b"%e", (1e40,)), b"1.000000e+40")1212 def test_E_format_returns_bytes(self):1213 self.assertEqual(bytes.__mod__(b"%E", (1.0,)), b"1.000000E+00")1214 def test_g_format_returns_bytes(self):1215 self.assertEqual(bytes.__mod__(b"%g", (0.0,)), b"0")1216 self.assertEqual(bytes.__mod__(b"%g", (-1.0,)), b"-1")1217 self.assertEqual(bytes.__mod__(b"%g", (0.125,)), b"0.125")1218 self.assertEqual(bytes.__mod__(b"%g", (3.5,)), b"3.5")1219 def test_eEfFgG_format_with_inf_returns_bytes(self):1220 self.assertEqual(bytes.__mod__(b"%e", (float("inf"),)), b"inf")1221 self.assertEqual(bytes.__mod__(b"%E", (float("inf"),)), b"INF")1222 self.assertEqual(bytes.__mod__(b"%f", (float("inf"),)), b"inf")1223 self.assertEqual(bytes.__mod__(b"%F", (float("inf"),)), b"INF")1224 self.assertEqual(bytes.__mod__(b"%g", (float("inf"),)), b"inf")1225 self.assertEqual(bytes.__mod__(b"%G", (float("inf"),)), b"INF")1226 self.assertEqual(bytes.__mod__(b"%e", (-float("inf"),)), b"-inf")1227 self.assertEqual(bytes.__mod__(b"%E", (-float("inf"),)), b"-INF")1228 self.assertEqual(bytes.__mod__(b"%f", (-float("inf"),)), b"-inf")1229 self.assertEqual(bytes.__mod__(b"%F", (-float("inf"),)), b"-INF")1230 self.assertEqual(bytes.__mod__(b"%g", (-float("inf"),)), b"-inf")1231 self.assertEqual(bytes.__mod__(b"%G", (-float("inf"),)), b"-INF")1232 def test_eEfFgG_format_with_nan_returns_bytes(self):1233 self.assertEqual(bytes.__mod__(b"%e", (float("nan"),)), b"nan")1234 self.assertEqual(bytes.__mod__(b"%E", (float("nan"),)), b"NAN")1235 self.assertEqual(bytes.__mod__(b"%f", (float("nan"),)), b"nan")1236 self.assertEqual(bytes.__mod__(b"%F", (float("nan"),)), b"NAN")1237 self.assertEqual(bytes.__mod__(b"%g", (float("nan"),)), b"nan")1238 self.assertEqual(bytes.__mod__(b"%G", (float("nan"),)), b"NAN")1239 self.assertEqual(bytes.__mod__(b"%e", (float("-nan"),)), b"nan")1240 self.assertEqual(bytes.__mod__(b"%E", (float("-nan"),)), b"NAN")1241 self.assertEqual(bytes.__mod__(b"%f", (float("-nan"),)), b"nan")1242 self.assertEqual(bytes.__mod__(b"%F", (float("-nan"),)), b"NAN")1243 self.assertEqual(bytes.__mod__(b"%g", (float("-nan"),)), b"nan")1244 self.assertEqual(bytes.__mod__(b"%G", (float("-nan"),)), b"NAN")1245 def test_f_format_with_precision_returns_bytes(self):1246 number = 1.234567891234567891247 self.assertEqual(bytes.__mod__(b"%.0f", number), b"1")1248 self.assertEqual(bytes.__mod__(b"%.1f", number), b"1.2")1249 self.assertEqual(bytes.__mod__(b"%.2f", number), b"1.23")1250 self.assertEqual(bytes.__mod__(b"%.3f", number), b"1.235")1251 self.assertEqual(bytes.__mod__(b"%.4f", number), b"1.2346")1252 self.assertEqual(bytes.__mod__(b"%.5f", number), b"1.23457")1253 self.assertEqual(bytes.__mod__(b"%.6f", number), b"1.234568")1254 self.assertEqual(bytes.__mod__(b"%f", number), b"1.234568")1255 self.assertEqual(bytes.__mod__(b"%.17f", number), b"1.23456789123456789")1256 self.assertEqual(1257 bytes.__mod__(b"%.25f", number), b"1.2345678912345678934769921"1258 )1259 self.assertEqual(1260 bytes.__mod__(b"%.60f", number),1261 b"1.234567891234567893476992139767389744520187377929687500000000",1262 )1263 def test_eEfFgG_format_with_precision_returns_bytes(self):1264 number = 1.234567891234567891265 self.assertEqual(bytes.__mod__(b"%.0e", number), b"1e+00")1266 self.assertEqual(bytes.__mod__(b"%.0E", number), b"1E+00")1267 self.assertEqual(bytes.__mod__(b"%.0f", number), b"1")1268 self.assertEqual(bytes.__mod__(b"%.0F", number), b"1")1269 self.assertEqual(bytes.__mod__(b"%.0g", number), b"1")1270 self.assertEqual(bytes.__mod__(b"%.0G", number), b"1")1271 self.assertEqual(bytes.__mod__(b"%.4e", number), b"1.2346e+00")1272 self.assertEqual(bytes.__mod__(b"%.4E", number), b"1.2346E+00")1273 self.assertEqual(bytes.__mod__(b"%.4f", number), b"1.2346")1274 self.assertEqual(bytes.__mod__(b"%.4F", number), b"1.2346")1275 self.assertEqual(bytes.__mod__(b"%.4g", number), b"1.235")1276 self.assertEqual(bytes.__mod__(b"%.4G", number), b"1.235")1277 self.assertEqual(bytes.__mod__(b"%e", number), b"1.234568e+00")1278 self.assertEqual(bytes.__mod__(b"%E", number), b"1.234568E+00")1279 self.assertEqual(bytes.__mod__(b"%f", number), b"1.234568")1280 self.assertEqual(bytes.__mod__(b"%F", number), b"1.234568")1281 self.assertEqual(bytes.__mod__(b"%g", number), b"1.23457")1282 self.assertEqual(bytes.__mod__(b"%G", number), b"1.23457")1283 def test_g_format_with_flags_and_width_returns_bytes(self):1284 self.assertEqual(bytes.__mod__(b"%5g", 7.0), b" 7")1285 self.assertEqual(bytes.__mod__(b"%5g", 7.2), b" 7.2")1286 self.assertEqual(bytes.__mod__(b"% 5g", 7.2), b" 7.2")1287 self.assertEqual(bytes.__mod__(b"%+5g", 7.2), b" +7.2")1288 self.assertEqual(bytes.__mod__(b"%5g", -7.2), b" -7.2")1289 self.assertEqual(bytes.__mod__(b"% 5g", -7.2), b" -7.2")1290 self.assertEqual(bytes.__mod__(b"%+5g", -7.2), b" -7.2")1291 self.assertEqual(bytes.__mod__(b"%-5g", 7.0), b"7 ")1292 self.assertEqual(bytes.__mod__(b"%-5g", 7.2), b"7.2 ")1293 self.assertEqual(bytes.__mod__(b"%- 5g", 7.2), b" 7.2 ")1294 self.assertEqual(bytes.__mod__(b"%-+5g", 7.2), b"+7.2 ")1295 self.assertEqual(bytes.__mod__(b"%-5g", -7.2), b"-7.2 ")1296 self.assertEqual(bytes.__mod__(b"%- 5g", -7.2), b"-7.2 ")1297 self.assertEqual(bytes.__mod__(b"%-+5g", -7.2), b"-7.2 ")1298 self.assertEqual(bytes.__mod__(b"%#g", 7.0), b"7.00000")1299 self.assertEqual(bytes.__mod__(b"%#- 7.2g", float("-nan")), b" nan ")1300 self.assertEqual(bytes.__mod__(b"%#- 7.2g", float("inf")), b" inf ")1301 self.assertEqual(bytes.__mod__(b"%#- 7.2g", float("-inf")), b"-inf ")1302 def test_eEfFgG_format_with_flags_and_width_returns_bytes(self):1303 number = 1.234567891234567891304 self.assertEqual(bytes.__mod__(b"% -#12.3e", number), b" 1.235e+00 ")1305 self.assertEqual(bytes.__mod__(b"% -#12.3E", number), b" 1.235E+00 ")1306 self.assertEqual(bytes.__mod__(b"% -#12.3f", number), b" 1.235 ")1307 self.assertEqual(bytes.__mod__(b"% -#12.3F", number), b" 1.235 ")1308 self.assertEqual(bytes.__mod__(b"% -#12.3g", number), b" 1.23 ")1309 self.assertEqual(bytes.__mod__(b"% -#12.3G", number), b" 1.23 ")1310 def test_ef_format_with_non_float_raises_type_error(self):1311 with self.assertRaises(TypeError) as context:1312 bytes.__mod__(b"%e", (None,))1313 self.assertEqual(1314 str(context.exception), "float argument required, not NoneType"1315 )1316 class C:1317 def __float__(self):1318 return "not a float"1319 with self.assertRaises(TypeError) as context:1320 bytes.__mod__(b"%f", (C(),))1321 self.assertEqual(str(context.exception), "float argument required, not C")1322 def test_efg_format_with_non_float_returns_bytes(self):1323 class A(float):1324 pass1325 self.assertEqual(1326 bytes.__mod__(b"%e", (A(9.625),)), bytes.__mod__(b"%e", (9.625,))1327 )1328 class C:1329 def __float__(self):1330 return 3.51331 self.assertEqual(bytes.__mod__(b"%f", (C(),)), bytes.__mod__(b"%f", (3.5,)))1332 class D:1333 def __float__(self):1334 return A(-12.75)1335 warnings.filterwarnings(1336 action="ignore",1337 category=DeprecationWarning,1338 message=".*__float__ returned non-float.*",1339 module=__name__,1340 )1341 self.assertEqual(bytes.__mod__(b"%g", (D(),)), bytes.__mod__(b"%g", (-12.75,)))1342 def test_percent_format_returns_percent(self):1343 self.assertEqual(bytes.__mod__(b"%%", ()), b"%")1344 def test_escaped_percent_with_characters_between_raises_type_error(self):1345 with self.assertRaises(TypeError) as context:1346 bytes.__mod__(b"%0.0%", ())1347 self.assertEqual(1348 str(context.exception), "not enough arguments for format string"1349 )1350 with self.assertRaises(TypeError) as context:1351 bytes.__mod__(b"%*.%", (42,))1352 self.assertEqual(1353 str(context.exception), "not enough arguments for format string"1354 )1355 with self.assertRaises(TypeError) as context:1356 bytes.__mod__(b"%d %*.%", (42,))1357 self.assertEqual(1358 str(context.exception), "not enough arguments for format string"1359 )1360 with self.assertRaises(TypeError) as context:1361 bytes.__mod__(b"%.*%", (88,))1362 self.assertEqual(1363 str(context.exception), "not enough arguments for format string"1364 )1365 with self.assertRaises(TypeError) as context:1366 bytes.__mod__(b"%0#*.42%", (1234,))1367 self.assertEqual(1368 str(context.exception), "not enough arguments for format string"1369 )1370 def test_escaped_percent_with_characters_between_raises_value_error(self):1371 with self.assertRaises(ValueError) as context:1372 bytes.__mod__(b"%*.%", (42, 1))1373 self.assertEqual(1374 str(context.exception), "unsupported format character '%' (0x25) at index 3"1375 )1376 def test_flags_get_accepted(self):1377 self.assertEqual(bytes.__mod__(b"%-s", b""), b"")1378 self.assertEqual(bytes.__mod__(b"%+s", b""), b"")1379 self.assertEqual(bytes.__mod__(b"% s", b""), b"")1380 self.assertEqual(bytes.__mod__(b"%#s", b""), b"")1381 self.assertEqual(bytes.__mod__(b"%0s", b""), b"")1382 self.assertEqual(bytes.__mod__(b"%#-#0+ -s", b""), b"")1383 def test_string_format_with_width_returns_bytes(self):1384 self.assertEqual(bytes.__mod__(b"%5s", b"oh"), b" oh")1385 self.assertEqual(bytes.__mod__(b"%-5s", b"ah"), b"ah ")1386 self.assertEqual(bytes.__mod__(b"%05s", b"uh"), b" uh")1387 self.assertEqual(bytes.__mod__(b"%-# 5s", b"eh"), b"eh ")1388 self.assertEqual(bytes.__mod__(b"%0s", b"foo"), b"foo")1389 self.assertEqual(bytes.__mod__(b"%-0s", b"foo"), b"foo")1390 self.assertEqual(bytes.__mod__(b"%10s", b"hello world"), b"hello world")1391 self.assertEqual(bytes.__mod__(b"%-10s", b"hello world"), b"hello world")1392 def test_string_format_with_width_star_returns_bytes(self):1393 self.assertEqual(bytes.__mod__(b"%*s", (7, b"foo")), b" foo")1394 self.assertEqual(bytes.__mod__(b"%*s", (-7, b"bar")), b"bar ")1395 self.assertEqual(bytes.__mod__(b"%-*s", (7, b"baz")), b"baz ")1396 self.assertEqual(bytes.__mod__(b"%-*s", (-7, b"bam")), b"bam ")1397 def test_string_format_with_precision_returns_bytes(self):1398 self.assertEqual(bytes.__mod__(b"%.3s", b"python"), b"pyt")1399 self.assertEqual(bytes.__mod__(b"%.0s", b"python"), b"")1400 self.assertEqual(bytes.__mod__(b"%.10s", b"python"), b"python")1401 def test_string_format_with_precision_star_returns_bytes(self):1402 self.assertEqual(bytes.__mod__(b"%.*s", (3, b"monty")), b"mon")1403 self.assertEqual(bytes.__mod__(b"%.*s", (0, b"monty")), b"")1404 self.assertEqual(bytes.__mod__(b"%.*s", (-4, b"monty")), b"")1405 def test_string_format_with_width_and_precision_returns_bytes(self):1406 self.assertEqual(bytes.__mod__(b"%8.3s", (b"foobar",)), b" foo")1407 self.assertEqual(bytes.__mod__(b"%-8.3s", (b"foobar",)), b"foo ")1408 self.assertEqual(bytes.__mod__(b"%*.3s", (8, b"foobar")), b" foo")1409 self.assertEqual(bytes.__mod__(b"%*.3s", (-8, b"foobar")), b"foo ")1410 self.assertEqual(bytes.__mod__(b"%8.*s", (3, b"foobar")), b" foo")1411 self.assertEqual(bytes.__mod__(b"%-8.*s", (3, b"foobar")), b"foo ")1412 self.assertEqual(bytes.__mod__(b"%*.*s", (8, 3, b"foobar")), b" foo")1413 self.assertEqual(bytes.__mod__(b"%-*.*s", (8, 3, b"foobar")), b"foo ")1414 def test_s_r_a_c_formats_accept_flags_width_precision_return_strings(self):1415 self.assertEqual(bytes.__mod__(b"%-*.3s", (8, b"foobar")), b"foo ")1416 self.assertEqual(bytes.__mod__(b"%-*.3r", (8, b"foobar")), b"b'f ")1417 self.assertEqual(bytes.__mod__(b"%-*.3a", (8, b"foobar")), b"b'f ")1418 self.assertEqual(bytes.__mod__(b"%-*.3c", (8, 94)), b"^ ")1419 def test_number_format_with_sign_flag_returns_bytes(self):1420 self.assertEqual(bytes.__mod__(b"%+d", (42,)), b"+42")1421 self.assertEqual(bytes.__mod__(b"%+d", (-42,)), b"-42")1422 self.assertEqual(bytes.__mod__(b"% d", (17,)), b" 17")1423 self.assertEqual(bytes.__mod__(b"% d", (-17,)), b"-17")1424 self.assertEqual(bytes.__mod__(b"%+ d", (42,)), b"+42")1425 self.assertEqual(bytes.__mod__(b"%+ d", (-42,)), b"-42")1426 self.assertEqual(bytes.__mod__(b"% +d", (17,)), b"+17")1427 self.assertEqual(bytes.__mod__(b"% +d", (-17,)), b"-17")1428 def test_number_format_alt_flag_returns_bytes(self):1429 self.assertEqual(bytes.__mod__(b"%#d", (23,)), b"23")1430 self.assertEqual(bytes.__mod__(b"%#x", (23,)), b"0x17")1431 self.assertEqual(bytes.__mod__(b"%#X", (23,)), b"0X17")1432 self.assertEqual(bytes.__mod__(b"%#o", (23,)), b"0o27")1433 def test_number_format_with_width_returns_bytes(self):1434 self.assertEqual(bytes.__mod__(b"%5d", (123,)), b" 123")1435 self.assertEqual(bytes.__mod__(b"%5d", (-8,)), b" -8")1436 self.assertEqual(bytes.__mod__(b"%-5d", (123,)), b"123 ")1437 self.assertEqual(bytes.__mod__(b"%-5d", (-8,)), b"-8 ")1438 self.assertEqual(bytes.__mod__(b"%05d", (123,)), b"00123")1439 self.assertEqual(bytes.__mod__(b"%05d", (-8,)), b"-0008")1440 self.assertEqual(bytes.__mod__(b"%-05d", (123,)), b"123 ")1441 self.assertEqual(bytes.__mod__(b"%0-5d", (-8,)), b"-8 ")1442 self.assertEqual(bytes.__mod__(b"%#7x", (42,)), b" 0x2a")1443 self.assertEqual(bytes.__mod__(b"%#7x", (-42,)), b" -0x2a")1444 self.assertEqual(bytes.__mod__(b"%5d", (123456,)), b"123456")1445 self.assertEqual(bytes.__mod__(b"%-5d", (-123456,)), b"-123456")1446 def test_number_format_with_precision_returns_bytes(self):1447 self.assertEqual(bytes.__mod__(b"%.5d", (123,)), b"00123")1448 self.assertEqual(bytes.__mod__(b"%.5d", (-123,)), b"-00123")1449 self.assertEqual(bytes.__mod__(b"%.5d", (1234567,)), b"1234567")1450 self.assertEqual(bytes.__mod__(b"%#.5x", (99,)), b"0x00063")1451 def test_number_format_with_width_precision_flags_returns_bytes(self):1452 self.assertEqual(bytes.__mod__(b"%8.3d", (12,)), b" 012")1453 self.assertEqual(bytes.__mod__(b"%8.3d", (-7,)), b" -007")1454 self.assertEqual(bytes.__mod__(b"%05.3d", (12,)), b"00012")1455 self.assertEqual(bytes.__mod__(b"%+05.3d", (12,)), b"+0012")1456 self.assertEqual(bytes.__mod__(b"% 05.3d", (12,)), b" 0012")1457 self.assertEqual(bytes.__mod__(b"% 05.3x", (19,)), b" 0013")1458 self.assertEqual(bytes.__mod__(b"%-8.3d", (12,)), b"012 ")1459 self.assertEqual(bytes.__mod__(b"%-8.3d", (-7,)), b"-007 ")1460 self.assertEqual(bytes.__mod__(b"%- 8.3d", (66,)), b" 066 ")1461 def test_width_and_precision_star_raises_type_error(self):1462 with self.assertRaises(TypeError) as context:1463 bytes.__mod__(b"%*d", (42,))1464 self.assertEqual(1465 str(context.exception), "not enough arguments for format string"1466 )1467 with self.assertRaises(TypeError) as context:1468 bytes.__mod__(b"%.*d", (42,))1469 self.assertEqual(1470 str(context.exception), "not enough arguments for format string"1471 )1472 with self.assertRaises(TypeError) as context:1473 bytes.__mod__(b"%*.*d", (42,))1474 self.assertEqual(1475 str(context.exception), "not enough arguments for format string"1476 )1477 with self.assertRaises(TypeError) as context:1478 bytes.__mod__(b"%*.*d", (1, 2))1479 self.assertEqual(1480 str(context.exception), "not enough arguments for format string"1481 )1482 def test_negative_precision_raises_value_error(self):1483 with self.assertRaises(ValueError) as context:1484 bytes.__mod__(b"%.-2s", "foo")1485 self.assertEqual(1486 str(context.exception), "unsupported format character '-' (0x2d) at index 2"1487 )1488 def test_two_specifiers_returns_bytes(self):1489 self.assertEqual(bytes.__mod__(b"%s%s", (b"foo", b"bar")), b"foobar")1490 self.assertEqual(bytes.__mod__(b",%s%s", (b"foo", b"bar")), b",foobar")1491 self.assertEqual(bytes.__mod__(b"%s,%s", (b"foo", b"bar")), b"foo,bar")1492 self.assertEqual(bytes.__mod__(b"%s%s,", (b"foo", b"bar")), b"foobar,")1493 self.assertEqual(1494 bytes.__mod__(b",%s..%s---", (b"foo", b"bar")), b",foo..bar---"1495 )1496 self.assertEqual(1497 bytes.__mod__(b",%s...%s--", (b"foo", b"bar")), b",foo...bar--"1498 )1499 self.assertEqual(1500 bytes.__mod__(b",,%s.%s---", (b"foo", b"bar")), b",,foo.bar---"1501 )1502 self.assertEqual(1503 bytes.__mod__(b",,%s...%s-", (b"foo", b"bar")), b",,foo...bar-"1504 )1505 self.assertEqual(1506 bytes.__mod__(b",,,%s..%s-", (b"foo", b"bar")), b",,,foo..bar-"1507 )1508 self.assertEqual(1509 bytes.__mod__(b",,,%s.%s--", (b"foo", b"bar")), b",,,foo.bar--"1510 )1511 def test_mixed_specifiers_with_percents_returns_bytes(self):1512 self.assertEqual(bytes.__mod__(b"%%%s%%%s%%", (b"foo", b"bar")), b"%foo%bar%")1513 def test_mixed_specifiers_returns_bytes(self):1514 self.assertEqual(1515 bytes.__mod__(b"a %d %g %s", (123, 3.14, b"baz")), b"a 123 3.14 baz"1516 )1517 def test_specifier_missing_format_raises_value_error(self):1518 with self.assertRaises(ValueError) as context:1519 bytes.__mod__(b"%", ())1520 self.assertEqual(str(context.exception), "incomplete format")1521 with self.assertRaises(ValueError) as context:1522 bytes.__mod__(b"%(foo)", {b"foo": None})1523 self.assertEqual(str(context.exception), "incomplete format")1524 def test_unknown_specifier_raises_value_error(self):1525 with self.assertRaises(ValueError) as context:1526 bytes.__mod__(b"try %Y", (42,))1527 self.assertEqual(1528 str(context.exception), "unsupported format character 'Y' (0x59) at index 5"1529 )1530 def test_too_few_args_raises_type_error(self):1531 with self.assertRaises(TypeError) as context:1532 bytes.__mod__(b"%s%s", (b"foo",))1533 self.assertEqual(1534 str(context.exception), "not enough arguments for format string"1535 )1536 def test_too_many_args_raises_type_error(self):1537 with self.assertRaises(TypeError) as context:1538 bytes.__mod__(b"hello", 42)1539 self.assertEqual(1540 str(context.exception),1541 "not all arguments converted during bytes formatting",1542 )1543 with self.assertRaises(TypeError) as context:1544 bytes.__mod__(b"%d%s", (1, b"foo", 3))1545 self.assertEqual(1546 str(context.exception),1547 "not all arguments converted during bytes formatting",1548 )1549if __name__ == "__main__":...

Full Screen

Full Screen

error___mod__parameters.py

Source:error___mod__parameters.py Github

copy

Full Screen

...52: __doc__ = "__mod__ method is present, but is declared with a wrong number of parameters"63: 74: if __name__ == '__main__':85: class Sample:96: def __mod__(self, other, another):107: return 1 % other % another118: 129: 1310: # Type error1411: print Sample() % 11512: 1613: 1714: class OtherSample:1815: def __mod__(self):1916: return 12017: 2118: 2219: # Type error2320: print OtherSample() % 12421: 25"""26# Import the stypy library necessary elements27from stypy.type_inference_programs.type_inference_programs_imports import *28# Create the module type store29module_type_store = Context(None, __file__)30# ################# Begin of the type inference program ##################31# Assigning a Str to a Name (line 2):32str_1 = get_builtin_python_type_instance(stypy.reporting.localization.Localization(__file__, 2, 10), 'str', '__mod__ method is present, but is declared with a wrong number of parameters')33# Assigning a type to the variable '__doc__' (line 2)34module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 2, 0), '__doc__', str_1)35if (__name__ == '__main__'):36 # Declaration of the 'Sample' class37 class Sample:38 @norecursion39 def __mod__(type_of_self, localization, *varargs, **kwargs):40 global module_type_store41 # Assign values to the parameters with defaults42 defaults = []43 # Create a new context for function '__mod__'44 module_type_store = module_type_store.open_function_context('__mod__', 6, 8, False)45 # Assigning a type to the variable 'self' (line 7)46 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 7, 8), 'self', type_of_self)47 48 # Passed parameters checking function49 Sample.__mod__.__dict__.__setitem__('stypy_localization', localization)50 Sample.__mod__.__dict__.__setitem__('stypy_type_of_self', type_of_self)51 Sample.__mod__.__dict__.__setitem__('stypy_type_store', module_type_store)52 Sample.__mod__.__dict__.__setitem__('stypy_function_name', 'Sample.__mod__')53 Sample.__mod__.__dict__.__setitem__('stypy_param_names_list', ['other', 'another'])54 Sample.__mod__.__dict__.__setitem__('stypy_varargs_param_name', None)55 Sample.__mod__.__dict__.__setitem__('stypy_kwargs_param_name', None)56 Sample.__mod__.__dict__.__setitem__('stypy_call_defaults', defaults)57 Sample.__mod__.__dict__.__setitem__('stypy_call_varargs', varargs)58 Sample.__mod__.__dict__.__setitem__('stypy_call_kwargs', kwargs)59 Sample.__mod__.__dict__.__setitem__('stypy_declared_arg_number', 3)60 arguments = process_argument_values(localization, type_of_self, module_type_store, 'Sample.__mod__', ['other', 'another'], None, None, defaults, varargs, kwargs)61 if is_error_type(arguments):62 # Destroy the current context63 module_type_store = module_type_store.close_function_context()64 return arguments65 # Initialize method data66 init_call_information(module_type_store, '__mod__', localization, ['other', 'another'], arguments)67 68 # Default return type storage variable (SSA)69 # Assigning a type to the variable 'stypy_return_type'70 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 0, 0), 'stypy_return_type', None)71 72 73 # ################# Begin of '__mod__(...)' code ##################74 int_2 = get_builtin_python_type_instance(stypy.reporting.localization.Localization(__file__, 7, 19), 'int')75 # Getting the type of 'other' (line 7)76 other_3 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 7, 23), 'other')77 # Applying the binary operator '%' (line 7)78 result_mod_4 = python_operator(stypy.reporting.localization.Localization(__file__, 7, 19), '%', int_2, other_3)79 80 # Getting the type of 'another' (line 7)81 another_5 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 7, 31), 'another')82 # Applying the binary operator '%' (line 7)83 result_mod_6 = python_operator(stypy.reporting.localization.Localization(__file__, 7, 29), '%', result_mod_4, another_5)84 85 # Assigning a type to the variable 'stypy_return_type' (line 7)86 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 7, 12), 'stypy_return_type', result_mod_6)87 88 # ################# End of '__mod__(...)' code ##################89 # Teardown call information90 teardown_call_information(localization, arguments)91 92 # Storing the return type of function '__mod__' in the type store93 # Getting the type of 'stypy_return_type' (line 6)94 stypy_return_type_7 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 6, 8), 'stypy_return_type')95 module_type_store.store_return_type_of_current_context(stypy_return_type_7)96 97 # Destroy the current context98 module_type_store = module_type_store.close_function_context()99 100 # Return type of the function '__mod__'101 return stypy_return_type_7102 @norecursion103 def __init__(type_of_self, localization, *varargs, **kwargs):104 global module_type_store105 # Assign values to the parameters with defaults106 defaults = []107 # Create a new context for function '__init__'108 module_type_store = module_type_store.open_function_context('__init__', 5, 4, False)109 # Assigning a type to the variable 'self' (line 6)110 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 6, 4), 'self', type_of_self)111 112 # Passed parameters checking function113 arguments = process_argument_values(localization, type_of_self, module_type_store, 'Sample.__init__', [], None, None, defaults, varargs, kwargs)114 if is_error_type(arguments):115 # Destroy the current context116 module_type_store = module_type_store.close_function_context()117 return118 # Initialize method data119 init_call_information(module_type_store, '__init__', localization, [], arguments)120 121 # Default return type storage variable (SSA)122 # Assigning a type to the variable 'stypy_return_type'123 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 0, 0), 'stypy_return_type', None)124 125 126 # ################# Begin of '__init__(...)' code ##################127 pass128 129 # ################# End of '__init__(...)' code ##################130 # Teardown call information131 teardown_call_information(localization, arguments)132 # Destroy the current context133 module_type_store = module_type_store.close_function_context()134 135 # Assigning a type to the variable 'Sample' (line 5)136 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 5, 4), 'Sample', Sample)137 138 # Call to Sample(...): (line 11)139 # Processing the call keyword arguments (line 11)140 kwargs_9 = {}141 # Getting the type of 'Sample' (line 11)142 Sample_8 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 11, 10), 'Sample', False)143 # Calling Sample(args, kwargs) (line 11)144 Sample_call_result_10 = invoke(stypy.reporting.localization.Localization(__file__, 11, 10), Sample_8, *[], **kwargs_9)145 146 int_11 = get_builtin_python_type_instance(stypy.reporting.localization.Localization(__file__, 11, 21), 'int')147 # Applying the binary operator '%' (line 11)148 result_mod_12 = python_operator(stypy.reporting.localization.Localization(__file__, 11, 10), '%', Sample_call_result_10, int_11)149 150 # Declaration of the 'OtherSample' class151 class OtherSample:152 @norecursion153 def __mod__(type_of_self, localization, *varargs, **kwargs):154 global module_type_store155 # Assign values to the parameters with defaults156 defaults = []157 # Create a new context for function '__mod__'158 module_type_store = module_type_store.open_function_context('__mod__', 15, 8, False)159 # Assigning a type to the variable 'self' (line 16)160 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 16, 8), 'self', type_of_self)161 162 # Passed parameters checking function163 OtherSample.__mod__.__dict__.__setitem__('stypy_localization', localization)164 OtherSample.__mod__.__dict__.__setitem__('stypy_type_of_self', type_of_self)165 OtherSample.__mod__.__dict__.__setitem__('stypy_type_store', module_type_store)166 OtherSample.__mod__.__dict__.__setitem__('stypy_function_name', 'OtherSample.__mod__')167 OtherSample.__mod__.__dict__.__setitem__('stypy_param_names_list', [])168 OtherSample.__mod__.__dict__.__setitem__('stypy_varargs_param_name', None)169 OtherSample.__mod__.__dict__.__setitem__('stypy_kwargs_param_name', None)170 OtherSample.__mod__.__dict__.__setitem__('stypy_call_defaults', defaults)171 OtherSample.__mod__.__dict__.__setitem__('stypy_call_varargs', varargs)172 OtherSample.__mod__.__dict__.__setitem__('stypy_call_kwargs', kwargs)173 OtherSample.__mod__.__dict__.__setitem__('stypy_declared_arg_number', 1)174 arguments = process_argument_values(localization, type_of_self, module_type_store, 'OtherSample.__mod__', [], None, None, defaults, varargs, kwargs)175 if is_error_type(arguments):176 # Destroy the current context177 module_type_store = module_type_store.close_function_context()178 return arguments179 # Initialize method data180 init_call_information(module_type_store, '__mod__', localization, [], arguments)181 182 # Default return type storage variable (SSA)183 # Assigning a type to the variable 'stypy_return_type'184 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 0, 0), 'stypy_return_type', None)185 186 187 # ################# Begin of '__mod__(...)' code ##################188 int_13 = get_builtin_python_type_instance(stypy.reporting.localization.Localization(__file__, 16, 19), 'int')189 # Assigning a type to the variable 'stypy_return_type' (line 16)190 module_type_store.set_type_of(stypy.reporting.localization.Localization(__file__, 16, 12), 'stypy_return_type', int_13)191 192 # ################# End of '__mod__(...)' code ##################193 # Teardown call information194 teardown_call_information(localization, arguments)195 196 # Storing the return type of function '__mod__' in the type store197 # Getting the type of 'stypy_return_type' (line 15)198 stypy_return_type_14 = module_type_store.get_type_of(stypy.reporting.localization.Localization(__file__, 15, 8), 'stypy_return_type')199 module_type_store.store_return_type_of_current_context(stypy_return_type_14)200 201 # Destroy the current context202 module_type_store = module_type_store.close_function_context()203 204 # Return type of the function '__mod__'205 return stypy_return_type_14206 @norecursion...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful