diff options
author | Matthew Suozzo <msuozzo@google.com> | 2021-04-10 20:56:28 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-10 20:56:28 (GMT) |
commit | 75a06f067bd0a2687312e5f8e78f9075be76ad3a (patch) | |
tree | a0bb72532b0579b5949a00b620c83049ae803f8c /Lib/test | |
parent | e05a703848473b0365886dcc593cbddc46609f29 (diff) | |
download | cpython-75a06f067bd0a2687312e5f8e78f9075be76ad3a.zip cpython-75a06f067bd0a2687312e5f8e78f9075be76ad3a.tar.gz cpython-75a06f067bd0a2687312e5f8e78f9075be76ad3a.tar.bz2 |
bpo-43798: Add source location attributes to alias (GH-25324)
* Add source location attributes to alias.
* Move alias star construction to pegen helper.
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_asdl_parser.py | 4 | ||||
-rw-r--r-- | Lib/test/test_ast.py | 28 | ||||
-rw-r--r-- | Lib/test/test_peg_generator/test_c_parser.py | 2 |
3 files changed, 29 insertions, 5 deletions
diff --git a/Lib/test/test_asdl_parser.py b/Lib/test/test_asdl_parser.py index d2c2b51..2c198a6 100644 --- a/Lib/test/test_asdl_parser.py +++ b/Lib/test/test_asdl_parser.py @@ -62,7 +62,9 @@ class TestAsdlParser(unittest.TestCase): alias = self.types['alias'] self.assertEqual( str(alias), - 'Product([Field(identifier, name), Field(identifier, asname, opt=True)])') + 'Product([Field(identifier, name), Field(identifier, asname, opt=True)], ' + '[Field(int, lineno), Field(int, col_offset), ' + 'Field(int, end_lineno, opt=True), Field(int, end_col_offset, opt=True)])') def test_attributes(self): stmt = self.types['stmt'] diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index bddb3de..6824958 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -335,6 +335,26 @@ class AST_Tests(unittest.TestCase): mod.body[0].module = " __future__ ".strip() compile(mod, "<test>", "exec") + def test_alias(self): + im = ast.parse("from bar import y").body[0] + self.assertEqual(len(im.names), 1) + alias = im.names[0] + self.assertEqual(alias.name, 'y') + self.assertIsNone(alias.asname) + self.assertEqual(alias.lineno, 1) + self.assertEqual(alias.end_lineno, 1) + self.assertEqual(alias.col_offset, 16) + self.assertEqual(alias.end_col_offset, 17) + + im = ast.parse("from bar import *").body[0] + alias = im.names[0] + self.assertEqual(alias.name, '*') + self.assertIsNone(alias.asname) + self.assertEqual(alias.lineno, 1) + self.assertEqual(alias.end_lineno, 1) + self.assertEqual(alias.col_offset, 16) + self.assertEqual(alias.end_col_offset, 17) + def test_base_classes(self): self.assertTrue(issubclass(ast.For, ast.stmt)) self.assertTrue(issubclass(ast.Name, ast.expr)) @@ -1037,7 +1057,8 @@ Module( def test_level_as_none(self): body = [ast.ImportFrom(module='time', - names=[ast.alias(name='sleep')], + names=[ast.alias(name='sleep', + lineno=0, col_offset=0)], level=None, lineno=0, col_offset=0)] mod = ast.Module(body, []) @@ -1735,6 +1756,7 @@ class EndPositionTests(unittest.TestCase): ''').strip() imp = ast.parse(s).body[0] self._check_end_pos(imp, 3, 1) + self._check_end_pos(imp.names[2], 2, 16) def test_slices(self): s1 = 'f()[1, 2] [0]' @@ -2095,8 +2117,8 @@ exec_results = [ ('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [('ExceptHandler', (3, 0, 4, 6), ('Name', (3, 7, 3, 16), 'Exception', ('Load',)), None, [('Pass', (4, 2, 4, 6))])], [], [])], []), ('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [], [], [('Pass', (4, 2, 4, 6))])], []), ('Module', [('Assert', (1, 0, 1, 8), ('Name', (1, 7, 1, 8), 'v', ('Load',)), None)], []), -('Module', [('Import', (1, 0, 1, 10), [('alias', 'sys', None)])], []), -('Module', [('ImportFrom', (1, 0, 1, 17), 'sys', [('alias', 'v', None)], 0)], []), +('Module', [('Import', (1, 0, 1, 10), [('alias', (1, 7, 1, 10), 'sys', None)])], []), +('Module', [('ImportFrom', (1, 0, 1, 17), 'sys', [('alias', (1, 16, 1, 17), 'v', None)], 0)], []), ('Module', [('Global', (1, 0, 1, 8), ['v'])], []), ('Module', [('Expr', (1, 0, 1, 1), ('Constant', (1, 0, 1, 1), 1, None))], []), ('Module', [('Pass', (1, 0, 1, 4))], []), diff --git a/Lib/test/test_peg_generator/test_c_parser.py b/Lib/test/test_peg_generator/test_c_parser.py index 5d8f543..b7e80f0 100644 --- a/Lib/test/test_peg_generator/test_c_parser.py +++ b/Lib/test/test_peg_generator/test_c_parser.py @@ -314,7 +314,7 @@ class TestCParser(TempdirManager, unittest.TestCase): ) simple_name[expr_ty]: NAME import_as_names_from[asdl_alias_seq*]: a[asdl_alias_seq*]=','.import_as_name_from+ { a } - import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _PyAST_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, p->arena) } + import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _PyAST_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, EXTRA) } """ test_source = """ for stmt in ("from a import b as c", "from . import a as b"): |