]> granicus.if.org Git - python/commitdiff
Issue #9011: Remove buggy and unnecessary ST->AST compilation code
authorMark Dickinson <dickinsm@gmail.com>
Thu, 17 Jun 2010 12:33:22 +0000 (12:33 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Thu, 17 Jun 2010 12:33:22 +0000 (12:33 +0000)
dealing with unary minus applied to a constant.  The removed code was
mutating the ST, causing a second compilation to fail.  (The peephole
optimizer already takes care of optimizing this case, so there's no
lost optimization opportunity here.)

Lib/test/test_parser.py
Misc/NEWS
Python/ast.c

index a36b544b86c6daa28cd42a7dc432400ed9b676c0..071d3622df567429f34bdcc8a7181ec278a63f4c 100644 (file)
@@ -484,8 +484,18 @@ class CompileTestCase(unittest.TestCase):
         st = parser.suite('a = "\\u1"')
         self.assertRaises(SyntaxError, parser.compilest, st)
 
+    def test_issue_9011(self):
+        # Issue 9011: compilation of an unary minus expression changed
+        # the meaning of the ST, so that a second compilation produced
+        # incorrect results.
+        st = parser.expr('-3')
+        code1 = parser.compilest(st)
+        self.assertEqual(eval(code1), -3)
+        code2 = parser.compilest(st)
+        self.assertEqual(eval(code2), -3)
+
 class ParserStackLimitTestCase(unittest.TestCase):
-    """try to push the parser to/over it's limits.
+    """try to push the parser to/over its limits.
     see http://bugs.python.org/issue1881 for a discussion
     """
     def _nested_expression(self, level):
index 0de858163bcd080559a0091dc44d7af6e0a9f96b..9c5ea77ff3717eeee8f81fb44f5191c029cf10e7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,11 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #9011: Remove buggy and unnecessary (in 3.x) ST->AST
+  compilation code dealing with unary minus applied to a constant.
+  The removed code was mutating the ST, causing a second compilation
+  to fail.
+
 - Issue #850997: mbcs encoding (Windows only) handles errors argument: strict
   mode raises unicode errors. The encoder only supports "strict" and "replace"
   error handlers, the decoder only supports "strict" and "ignore" error
index 0703abc9a74b4827d69e8e929626d0410701e1e6..34dc30f04de8fbd2e6abb558075154c64aaa298d 100644 (file)
@@ -1678,34 +1678,8 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
 static expr_ty
 ast_for_factor(struct compiling *c, const node *n)
 {
-    node *pfactor, *ppower, *patom, *pnum;
     expr_ty expression;
 
-    /* If the unary - operator is applied to a constant, don't generate
-       a UNARY_NEGATIVE opcode.  Just store the approriate value as a
-       constant.  The peephole optimizer already does something like
-       this but it doesn't handle the case where the constant is
-       (sys.maxint - 1).  In that case, we want a PyIntObject, not a
-       PyLongObject.
-    */
-    if (TYPE(CHILD(n, 0)) == MINUS
-        && NCH(n) == 2
-        && TYPE((pfactor = CHILD(n, 1))) == factor
-        && NCH(pfactor) == 1
-        && TYPE((ppower = CHILD(pfactor, 0))) == power
-        && NCH(ppower) == 1
-        && TYPE((patom = CHILD(ppower, 0))) == atom
-        && TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
-        char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
-        if (s == NULL)
-            return NULL;
-        s[0] = '-';
-        strcpy(s + 1, STR(pnum));
-        PyObject_FREE(STR(pnum));
-        STR(pnum) = s;
-        return ast_for_atom(c, patom);
-    }
-
     expression = ast_for_expr(c, CHILD(n, 1));
     if (!expression)
         return NULL;