[Python-checkins] r82552 - in python/branches/py3k: Lib/test/test_parser.py Misc/NEWS Modules/parsermodule.c

mark.dickinson python-checkins at python.org
Sun Jul 4 20:11:51 CEST 2010


Author: mark.dickinson
Date: Sun Jul  4 20:11:51 2010
New Revision: 82552

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


Modified:
   python/branches/py3k/Lib/test/test_parser.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/parsermodule.c

Modified: python/branches/py3k/Lib/test/test_parser.py
==============================================================================
--- python/branches/py3k/Lib/test/test_parser.py	(original)
+++ python/branches/py3k/Lib/test/test_parser.py	Sun Jul  4 20:11:51 2010
@@ -189,6 +189,12 @@
         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")
@@ -464,6 +470,20 @@
                 (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):
 

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sun Jul  4 20:11:51 2010
@@ -468,6 +468,8 @@
 Library
 -------
 
+- Issue #9130: Fix validation of relative imports in parser module.
+
 - Issue #9128: Fix validation of class decorators in parser module.
 
 - Issue #9094: python -m pickletools will now disassemble pickle files

Modified: python/branches/py3k/Modules/parsermodule.c
==============================================================================
--- python/branches/py3k/Modules/parsermodule.c	(original)
+++ python/branches/py3k/Modules/parsermodule.c	Sun Jul  4 20:11:51 2010
@@ -1767,8 +1767,8 @@
         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)
@@ -1778,7 +1778,8 @@
         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");


More information about the Python-checkins mailing list