]> granicus.if.org Git - python/commitdiff
Issue #9130: Fix validation of relative imports in parser module.
authorMark Dickinson <dickinsm@gmail.com>
Sun, 4 Jul 2010 16:59:04 +0000 (16:59 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sun, 4 Jul 2010 16:59:04 +0000 (16:59 +0000)
Lib/test/test_parser.py
Misc/NEWS
Modules/parsermodule.c

index 85e39c79746c953e2ec72200cccacab26072e3bc..6acd1e7eed9d50664a9aebd72f34ad69bf7ec6dc 100644 (file)
@@ -197,6 +197,12 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
         self.check_suite("import sys as system, 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):
         self.check_suite("# -*- coding: iso-8859-1 -*-\n"
                          "pass\n")
@@ -494,6 +500,20 @@ class IllegalSyntaxTestCase(unittest.TestCase):
                 (0, ''))
         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):
 
index f605050c0705e7222a6dc376a0d755ee6fa41ae6..d1a5dbe37767b9c689678ab4161e5cf688514434 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,8 @@ C-API
 Library
 -------
 
+- Issue #9130: Fix validation of relative imports in parser module.
+
 - Issue #9128: Fix validation of class decorators in parser module.
 
 - Issue #7673: Fix security vulnerability (CVE-2010-2089) in the audioop
index 097c450d90298069d56063100cbf72b4330e4f64..e1b7a2bc9859ac65c7723ed1e2af905021dd37be 100644 (file)
@@ -1886,8 +1886,8 @@ count_from_dots(node *tree)
         return i-1;
 }
 
-/* 'from' ('.'* dotted_name | '.') 'import' ('*' | '(' import_as_names ')' |
- *     import_as_names
+/* import_from: ('from' ('.'* dotted_name | '.'+)
+ *               'import' ('*' | '(' import_as_names ')' | import_as_names))
  */
 static int
 validate_import_from(node *tree)
@@ -1897,7 +1897,8 @@ validate_import_from(node *tree)
         int havename = (TYPE(CHILD(tree, ndots + 1)) == dotted_name);
         int offset = ndots + havename;
         int res = validate_ntype(tree, import_from)
-                && (nch >= 4 + ndots)
+                && (offset >= 1)
+                && (nch >= 3 + offset)
                 && validate_name(CHILD(tree, 0), "from")
                 && (!havename || validate_dotted_name(CHILD(tree, ndots + 1)))
                 && validate_name(CHILD(tree, offset + 1), "import");