]> granicus.if.org Git - python/commitdiff
Merged revisions 86538 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Sat, 20 Nov 2010 01:48:38 +0000 (01:48 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 20 Nov 2010 01:48:38 +0000 (01:48 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r86538 | benjamin.peterson | 2010-11-19 19:38:49 -0600 (Fri, 19 Nov 2010) | 1 line

  use %R format code; fixes invalid dereferencing #10391
........

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

index 7ee16bf5bd601b951472108f14ea9a0c398e4236..01c4ba38ca034da1416d3ead274c3e55c41d15eb 100644 (file)
@@ -183,6 +183,16 @@ class AST_Tests(unittest.TestCase):
                     ast2 = mod.loads(mod.dumps(ast, protocol))
                     self.assertEquals(to_tuple(ast2), to_tuple(ast))
 
+    def test_invalid_sum(self):
+        pos = dict(lineno=2, col_offset=3)
+        m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
+        try:
+            compile(m, "<test>", "exec")
+        except TypeError as exc:
+            self.assertIn("but got <_ast.expr", str(exc))
+        else:
+            self.fail("needed TypeError")
+
 
 class ASTHelpers_Test(unittest.TestCase):
 
index a8edff13782fc4e7d35ce7c9e22a44a038ba5f8e..fc9f5a61cc75cb18f58434dc1d839a0ab7464d0a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.1.3?
 Core and Builtins
 -----------------
 
+- Issue #10391: Don't dereference invalid memory in error messages in the ast
+  module.
+
 Library
 -------
 
index c1e07d2200d65a049474ceb18f29f37e1937a2d1..087f95a2dab848b9bd184d5d017a5f74eb4842e9 100755 (executable)
@@ -366,19 +366,19 @@ class Obj2ModVisitor(PickleVisitor):
         self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
         self.emit("{", 0)
         self.emit("PyObject* tmp = NULL;", 1)
+        # Prevent compiler warnings about unused variable.
+        self.emit("tmp = tmp;", 1)
         self.emit("int isinstance;", 1)
         self.emit("", 0)
 
-    def sumTrailer(self, name):
+    def sumTrailer(self, name, add_label=False):
         self.emit("", 0)
-        self.emit("tmp = PyObject_Repr(obj);", 1)
         # there's really nothing more we can do if this fails ...
-        self.emit("if (tmp == NULL) goto failed;", 1)
-        error = "expected some sort of %s, but got %%.400s" % name
-        format = "PyErr_Format(PyExc_TypeError, \"%s\", PyBytes_AS_STRING(tmp));"
+        error = "expected some sort of %s, but got %%R" % name
+        format = "PyErr_Format(PyExc_TypeError, \"%s\", obj);"
         self.emit(format % error, 1, reflow=False)
-        self.emit("failed:", 0)
-        self.emit("Py_XDECREF(tmp);", 1)
+        if add_label:
+            self.emit("failed:", 1)
         self.emit("return 1;", 1)
         self.emit("}", 0)
         self.emit("", 0)
@@ -430,7 +430,7 @@ class Obj2ModVisitor(PickleVisitor):
             self.emit("if (*out == NULL) goto failed;", 2)
             self.emit("return 0;", 2)
             self.emit("}", 1)
-        self.sumTrailer(name)
+        self.sumTrailer(name, True)
 
     def visitAttributeDeclaration(self, a, name, sum=sum):
         ctype = get_c_type(a.type)
index ecc6f9e5892e15aa190187e3d24357539a4af74b..47b3e508f2552bc246ad2754af5dc21890cebda3 100644 (file)
@@ -3379,6 +3379,7 @@ int
 obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
 
@@ -3518,11 +3519,8 @@ obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
+        failed:
         return 1;
 }
 
@@ -3530,6 +3528,7 @@ int
 obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
         int lineno;
@@ -4717,11 +4716,8 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
+        failed:
         return 1;
 }
 
@@ -4729,6 +4725,7 @@ int
 obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
         int lineno;
@@ -5835,11 +5832,8 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
+        failed:
         return 1;
 }
 
@@ -5847,6 +5841,7 @@ int
 obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
         isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
@@ -5898,11 +5893,7 @@ obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
         return 1;
 }
 
@@ -5910,6 +5901,7 @@ int
 obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
 
@@ -6023,11 +6015,8 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
+        failed:
         return 1;
 }
 
@@ -6035,6 +6024,7 @@ int
 obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
         isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
@@ -6054,11 +6044,7 @@ obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
         return 1;
 }
 
@@ -6066,6 +6052,7 @@ int
 obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
         isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
@@ -6165,11 +6152,7 @@ obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
         return 1;
 }
 
@@ -6177,6 +6160,7 @@ int
 obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
         isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
@@ -6212,11 +6196,7 @@ obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
         return 1;
 }
 
@@ -6224,6 +6204,7 @@ int
 obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
         isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
@@ -6307,11 +6288,7 @@ obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
         return 1;
 }
 
@@ -6383,6 +6360,7 @@ int
 obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
 {
         PyObject* tmp = NULL;
+        tmp = tmp;
         int isinstance;
 
         int lineno;
@@ -6478,11 +6456,8 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
                 return 0;
         }
 
-        tmp = PyObject_Repr(obj);
-        if (tmp == NULL) goto failed;
-        PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyBytes_AS_STRING(tmp));
-failed:
-        Py_XDECREF(tmp);
+        PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
+        failed:
         return 1;
 }