|
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | import unittest |
| 6 | +import warnings |
6 | 7 | import weakref |
7 | 8 | from textwrap import dedent |
8 | 9 |
|
@@ -1662,6 +1663,56 @@ class C: |
1662 | 1663 | self.assertEqual(ast.get_source_segment(s, cdef.body[0], padded=True), s_method) |
1663 | 1664 |
|
1664 | 1665 |
|
| 1666 | +class NodeVisitorTests(unittest.TestCase): |
| 1667 | + def test_old_constant_nodes(self): |
| 1668 | + class Visitor(ast.NodeVisitor): |
| 1669 | + def visit_Num(self, node): |
| 1670 | + log.append((node.lineno, 'Num', node.n)) |
| 1671 | + def visit_Str(self, node): |
| 1672 | + log.append((node.lineno, 'Str', node.s)) |
| 1673 | + def visit_Bytes(self, node): |
| 1674 | + log.append((node.lineno, 'Bytes', node.s)) |
| 1675 | + def visit_NameConstant(self, node): |
| 1676 | + log.append((node.lineno, 'NameConstant', node.value)) |
| 1677 | + def visit_Ellipsis(self, node): |
| 1678 | + log.append((node.lineno, 'Ellipsis', ...)) |
| 1679 | + mod = ast.parse(dedent('''\ |
| 1680 | + i = 42 |
| 1681 | + f = 4.25 |
| 1682 | + c = 4.25j |
| 1683 | + s = 'string' |
| 1684 | + b = b'bytes' |
| 1685 | + t = True |
| 1686 | + n = None |
| 1687 | + e = ... |
| 1688 | + ''')) |
| 1689 | + visitor = Visitor() |
| 1690 | + log = [] |
| 1691 | + with warnings.catch_warnings(record=True) as wlog: |
| 1692 | + warnings.filterwarnings('always', '', PendingDeprecationWarning) |
| 1693 | + visitor.visit(mod) |
| 1694 | + self.assertEqual(log, [ |
| 1695 | + (1, 'Num', 42), |
| 1696 | + (2, 'Num', 4.25), |
| 1697 | + (3, 'Num', 4.25j), |
| 1698 | + (4, 'Str', 'string'), |
| 1699 | + (5, 'Bytes', b'bytes'), |
| 1700 | + (6, 'NameConstant', True), |
| 1701 | + (7, 'NameConstant', None), |
| 1702 | + (8, 'Ellipsis', ...), |
| 1703 | + ]) |
| 1704 | + self.assertEqual([str(w.message) for w in wlog], [ |
| 1705 | + 'visit_Num is deprecated; add visit_Constant', |
| 1706 | + 'visit_Num is deprecated; add visit_Constant', |
| 1707 | + 'visit_Num is deprecated; add visit_Constant', |
| 1708 | + 'visit_Str is deprecated; add visit_Constant', |
| 1709 | + 'visit_Bytes is deprecated; add visit_Constant', |
| 1710 | + 'visit_NameConstant is deprecated; add visit_Constant', |
| 1711 | + 'visit_NameConstant is deprecated; add visit_Constant', |
| 1712 | + 'visit_Ellipsis is deprecated; add visit_Constant', |
| 1713 | + ]) |
| 1714 | + |
| 1715 | + |
1665 | 1716 | def main(): |
1666 | 1717 | if __name__ != '__main__': |
1667 | 1718 | return |
|
0 commit comments