]> granicus.if.org Git - python/commitdiff
Merged revisions 82552-82553 via svnmerge from
authorMark Dickinson <dickinsm@gmail.com>
Sun, 4 Jul 2010 18:16:43 +0000 (18:16 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sun, 4 Jul 2010 18:16:43 +0000 (18:16 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r82552 | mark.dickinson | 2010-07-04 19:11:51 +0100 (Sun, 04 Jul 2010) | 2 lines

  Issue #9130: Fix validation of relative imports in parser module.
........
  r82553 | mark.dickinson | 2010-07-04 19:15:26 +0100 (Sun, 04 Jul 2010) | 1 line

  Fix symbol numbers in test_parser test.
........

Lib/test/test_parser.py
Misc/NEWS
Modules/parsermodule.c

index 3a7bbe95e4e36e74d057843472efa9fca164783b..17111b07ae75300393f899499083273bd559a9d1 100644 (file)
@@ -190,6 +190,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")
@@ -459,6 +465,20 @@ class IllegalSyntaxTestCase(unittest.TestCase):
                 (0, ''))
         self.check_bad_tree(tree, "malformed global ast")
 
+    def test_missing_import_source(self):
+        # from import fred
+        tree = \
+            (257,
+             (268,
+              (269,
+               (270,
+                (282,
+                 (284, (1, 'from'), (1, 'import'),
+                  (287, (285, (1, 'fred')))))),
+               (4, ''))),
+             (4, ''), (0, ''))
+        self.check_bad_tree(tree, "from import fred")
+
 
 class CompileTestCase(unittest.TestCase):
 
index b2a316062c984dffae480481058fe838622f944f..87af41cdda923303a62347ff380147eacb7d9b26 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -75,6 +75,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 #5468: urlencode to handle bytes type and other encodings in its query
index b00037db53c5979c56ea68ba9d47f1c09c92bfbc..c9150d943167c18ee2d1fe132fc9ea4a8338f109 100644 (file)
@@ -1763,8 +1763,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)
@@ -1774,7 +1774,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");