diff -r fd9c9579050d Lib/test/test_parser.py --- a/Lib/test/test_parser.py Sun Nov 25 13:25:57 2012 +0000 +++ b/Lib/test/test_parser.py Sun Nov 25 16:00:28 2012 +0000 @@ -567,6 +567,17 @@ st = parser.suite('a = u"\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. see http://bugs.python.org/issue1881 for a discussion diff -r fd9c9579050d Python/ast.c --- a/Python/ast.c Sun Nov 25 13:25:57 2012 +0000 +++ b/Python/ast.c Sun Nov 25 16:00:28 2012 +0000 @@ -1744,14 +1744,19 @@ NCH(ppower) == 1 && TYPE((patom = CHILD(ppower, 0))) == atom && TYPE((pnum = CHILD(patom, 0))) == NUMBER) { + PyObject *pynum; 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); + pynum = parsenumber(c, s); + PyObject_FREE(s); + if (!pynum) + return NULL; + + PyArena_AddPyObject(c->c_arena, pynum); + return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); } expression = ast_for_expr(c, CHILD(n, 1));