diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-08-26 07:13:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-26 07:13:19 (GMT) |
commit | c3ea41e9bf100a5396b851488c3efe208e5e2179 (patch) | |
tree | 5e30febe4e132ed33968e96ca81aeb043e33387a /Lib/test/test_ast.py | |
parent | 44cd86bbdddb1f7b05deba2c1986a1e98f992429 (diff) | |
download | cpython-c3ea41e9bf100a5396b851488c3efe208e5e2179.zip cpython-c3ea41e9bf100a5396b851488c3efe208e5e2179.tar.gz cpython-c3ea41e9bf100a5396b851488c3efe208e5e2179.tar.bz2 |
bpo-36917: Add default implementation of ast.NodeVisitor.visit_Constant(). (GH-15490)
It emits a deprecation warning and calls corresponding method
visit_Num(), visit_Str(), etc.
Diffstat (limited to 'Lib/test/test_ast.py')
-rw-r--r-- | Lib/test/test_ast.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index f35d9e6..3d12397 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -3,6 +3,7 @@ import dis import os import sys import unittest +import warnings import weakref from textwrap import dedent @@ -1662,6 +1663,56 @@ class EndPositionTests(unittest.TestCase): self.assertEqual(ast.get_source_segment(s, cdef.body[0], padded=True), s_method) +class NodeVisitorTests(unittest.TestCase): + def test_old_constant_nodes(self): + class Visitor(ast.NodeVisitor): + def visit_Num(self, node): + log.append((node.lineno, 'Num', node.n)) + def visit_Str(self, node): + log.append((node.lineno, 'Str', node.s)) + def visit_Bytes(self, node): + log.append((node.lineno, 'Bytes', node.s)) + def visit_NameConstant(self, node): + log.append((node.lineno, 'NameConstant', node.value)) + def visit_Ellipsis(self, node): + log.append((node.lineno, 'Ellipsis', ...)) + mod = ast.parse(dedent('''\ + i = 42 + f = 4.25 + c = 4.25j + s = 'string' + b = b'bytes' + t = True + n = None + e = ... + ''')) + visitor = Visitor() + log = [] + with warnings.catch_warnings(record=True) as wlog: + warnings.filterwarnings('always', '', DeprecationWarning) + visitor.visit(mod) + self.assertEqual(log, [ + (1, 'Num', 42), + (2, 'Num', 4.25), + (3, 'Num', 4.25j), + (4, 'Str', 'string'), + (5, 'Bytes', b'bytes'), + (6, 'NameConstant', True), + (7, 'NameConstant', None), + (8, 'Ellipsis', ...), + ]) + self.assertEqual([str(w.message) for w in wlog], [ + 'visit_Num is deprecated; add visit_Constant', + 'visit_Num is deprecated; add visit_Constant', + 'visit_Num is deprecated; add visit_Constant', + 'visit_Str is deprecated; add visit_Constant', + 'visit_Bytes is deprecated; add visit_Constant', + 'visit_NameConstant is deprecated; add visit_Constant', + 'visit_NameConstant is deprecated; add visit_Constant', + 'visit_Ellipsis is deprecated; add visit_Constant', + ]) + + def main(): if __name__ != '__main__': return |