[Python-checkins] cpython (3.2): type check AST strings and identifiers

benjamin.peterson python-checkins at python.org
Fri Jul 22 17:55:24 CEST 2011


http://hg.python.org/cpython/rev/3301689bd78d
changeset:   71454:3301689bd78d
branch:      3.2
parent:      71448:f0475f78d45c
user:        Benjamin Peterson <benjamin at python.org>
date:        Fri Jul 22 10:50:23 2011 -0500
summary:
  type check AST strings and identifiers

This is related to a21829180423 as well as #12609 and #12610.

files:
  Lib/test/test_ast.py |  14 ++++++++++++++
  Misc/NEWS            |   3 +++
  Parser/asdl_c.py     |  21 +++++++++++++++++++--
  Python/Python-ast.c  |  25 +++++++++++++++++++++----
  4 files changed, 57 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -364,6 +364,20 @@
             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):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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.
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -794,8 +794,25 @@
     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)
 {
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -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 @@
     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 @@
             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;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list