Kaydet (Commit) 2cc8a5e4 authored tarafından Mark Dickinson's avatar Mark Dickinson

Issue #9130: Fix validation of relative imports in parser module.

üst 7cb6f2fe
...@@ -189,6 +189,12 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase): ...@@ -189,6 +189,12 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
self.check_suite("import sys as system, math") self.check_suite("import sys as system, math")
self.check_suite("import sys, math as my_math") self.check_suite("import sys, math as my_math")
def test_relative_imports(self):
self.check_suite("from . import name")
self.check_suite("from .. import name")
self.check_suite("from .pkg import name")
self.check_suite("from ..pkg import name")
def test_pep263(self): def test_pep263(self):
self.check_suite("# -*- coding: iso-8859-1 -*-\n" self.check_suite("# -*- coding: iso-8859-1 -*-\n"
"pass\n") "pass\n")
...@@ -464,6 +470,20 @@ class IllegalSyntaxTestCase(unittest.TestCase): ...@@ -464,6 +470,20 @@ class IllegalSyntaxTestCase(unittest.TestCase):
(0, '')) (0, ''))
self.check_bad_tree(tree, "malformed global ast") self.check_bad_tree(tree, "malformed global ast")
def test_missing_import_source(self):
# from import a
tree = \
(257,
(267,
(268,
(269,
(281,
(283, (1, 'from'), (1, 'import'),
(286, (284, (1, 'fred')))))),
(4, ''))),
(4, ''), (0, ''))
self.check_bad_tree(tree, "from import a")
class CompileTestCase(unittest.TestCase): class CompileTestCase(unittest.TestCase):
......
...@@ -468,6 +468,8 @@ C-API ...@@ -468,6 +468,8 @@ C-API
Library Library
------- -------
- Issue #9130: Fix validation of relative imports in parser module.
- Issue #9128: Fix validation of class decorators in parser module. - Issue #9128: Fix validation of class decorators in parser module.
- Issue #9094: python -m pickletools will now disassemble pickle files - Issue #9094: python -m pickletools will now disassemble pickle files
......
...@@ -1767,8 +1767,8 @@ count_from_dots(node *tree) ...@@ -1767,8 +1767,8 @@ count_from_dots(node *tree)
return i-1; return i-1;
} }
/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' | /* import_from: ('from' ('.'* dotted_name | '.'+)
* import_as_names * 'import' ('*' | '(' import_as_names ')' | import_as_names))
*/ */
static int static int
validate_import_from(node *tree) validate_import_from(node *tree)
...@@ -1778,7 +1778,8 @@ validate_import_from(node *tree) ...@@ -1778,7 +1778,8 @@ validate_import_from(node *tree)
int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name); int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
int offset = ndots + havename; int offset = ndots + havename;
int res = validate_ntype(tree, import_from) int res = validate_ntype(tree, import_from)
&& (nch >= 4 + ndots) && (offset >= 1)
&& (nch >= 3 + offset)
&& validate_name(CHILD(tree, 0), "from") && validate_name(CHILD(tree, 0), "from")
&& (!havename || validate_dotted_name(CHILD(tree, ndots + 1))) && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
&& validate_name(CHILD(tree, offset + 1), "import"); && validate_name(CHILD(tree, offset + 1), "import");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment