]> granicus.if.org Git - python/commitdiff
type check AST strings and identifiers
authorBenjamin Peterson <benjamin@python.org>
Fri, 22 Jul 2011 15:50:23 +0000 (10:50 -0500)
committerBenjamin Peterson <benjamin@python.org>
Fri, 22 Jul 2011 15:50:23 +0000 (10:50 -0500)
This is related to a21829180423 as well as #12609 and #12610.

Lib/test/test_ast.py
Misc/NEWS
Parser/asdl_c.py
Python/Python-ast.c

index 13ec2d0d7131ad23ef94b83065e0c5e5eb1edd13..7d1649c1155c78f12080b2ef4e7872fb33785368 100644 (file)
@@ -364,6 +364,20 @@ class AST_Tests(unittest.TestCase):
             compile(m, "<test>", "exec")
         self.assertIn("but got <_ast.expr", str(cm.exception))
 
+    def test_invalid_identitifer(self):
+        m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
+        ast.fix_missing_locations(m)
+        with self.assertRaises(TypeError) as cm:
+            compile(m, "<test>", "exec")
+        self.assertIn("identifier must be of type str", str(cm.exception))
+
+    def test_invalid_string(self):
+        m = ast.Module([ast.Expr(ast.Str(42))])
+        ast.fix_missing_locations(m)
+        with self.assertRaises(TypeError) as cm:
+            compile(m, "<test>", "exec")
+        self.assertIn("string must be of type str", str(cm.exception))
+
 
 class ASTHelpers_Test(unittest.TestCase):
 
index c1162ed640fdab21814bf5b564de4a3ed7c074d7..b3ecefd4f1548b3c516c58048defb3f85655df2c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.2?
 Core and Builtins
 -----------------
 
+- Verify the types of AST strings and identifiers provided by the user before
+  compiling them.
+
 - Issue #12579: str.format_map() now raises a ValueError if used on a
   format string that contains positional fields. Initial patch by
   Julian Berman.
index d6555d6d7252bd40c9bdba410d93c29a10c71c76..729ded8b6b73f4cee133d85cd16216d80cbfecdf 100755 (executable)
@@ -794,8 +794,25 @@ static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
     return 0;
 }
 
-#define obj2ast_identifier obj2ast_object
-#define obj2ast_string obj2ast_object
+static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena,
+    const char *name)
+{
+    if (!PyUnicode_CheckExact(name)) {
+        PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name);
+        return 1;
+    }
+    return obj2ast_object(obj, out, arena);
+}
+
+static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
+{
+    return obj2ast_stringlike(obj, out, arena, "identifier");
+}
+
+static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
+{
+    return obj2ast_stringlike(obj, out, arena, "string");
+}
 
 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
 {
index 2c09f96f0edd520f60b8e7f48d98415a55bc3561..43dcf6a508186472aa87db96382fc9a745b39a7e 100644 (file)
@@ -2,7 +2,7 @@
 
 
 /*
-   __version__ 82163.
+   __version__ .
 
    This module must be committed separately after each AST grammar change;
    The __version__ number is set to the revision number of the commit
@@ -600,8 +600,25 @@ static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
     return 0;
 }
 
-#define obj2ast_identifier obj2ast_object
-#define obj2ast_string obj2ast_object
+static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena,
+    const char *name)
+{
+    if (!PyUnicode_CheckExact(name)) {
+        PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name);
+        return 1;
+    }
+    return obj2ast_object(obj, out, arena);
+}
+
+static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
+{
+    return obj2ast_stringlike(obj, out, arena, "identifier");
+}
+
+static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
+{
+    return obj2ast_stringlike(obj, out, arena, "string");
+}
 
 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
 {
@@ -6739,7 +6756,7 @@ PyInit__ast(void)
             NULL;
         if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
                 return NULL;
-        if (PyModule_AddStringConstant(m, "__version__", "82163") < 0)
+        if (PyModule_AddStringConstant(m, "__version__", "") < 0)
                 return NULL;
         if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
             NULL;