Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d35a32e

Browse files
committed
Issue #9011: Remove buggy and unnecessary 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. (The peephole optimizer already takes care of optimizing this case, so there's no lost optimization opportunity here.)
1 parent a1b3740 commit d35a32e

3 files changed

Lines changed: 16 additions & 27 deletions

File tree

Lib/test/test_parser.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,18 @@ def test_compile_badunicode(self):
484484
st = parser.suite('a = "\\u1"')
485485
self.assertRaises(SyntaxError, parser.compilest, st)
486486

487+
def test_issue_9011(self):
488+
# Issue 9011: compilation of an unary minus expression changed
489+
# the meaning of the ST, so that a second compilation produced
490+
# incorrect results.
491+
st = parser.expr('-3')
492+
code1 = parser.compilest(st)
493+
self.assertEqual(eval(code1), -3)
494+
code2 = parser.compilest(st)
495+
self.assertEqual(eval(code2), -3)
496+
487497
class ParserStackLimitTestCase(unittest.TestCase):
488-
"""try to push the parser to/over it's limits.
498+
"""try to push the parser to/over its limits.
489499
see http://bugs.python.org/issue1881 for a discussion
490500
"""
491501
def _nested_expression(self, level):

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #9011: Remove buggy and unnecessary (in 3.x) ST->AST
16+
compilation code dealing with unary minus applied to a constant.
17+
The removed code was mutating the ST, causing a second compilation
18+
to fail.
19+
1520
- Issue #850997: mbcs encoding (Windows only) handles errors argument: strict
1621
mode raises unicode errors. The encoder only supports "strict" and "replace"
1722
error handlers, the decoder only supports "strict" and "ignore" error

Python/ast.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,34 +1678,8 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
16781678
static expr_ty
16791679
ast_for_factor(struct compiling *c, const node *n)
16801680
{
1681-
node *pfactor, *ppower, *patom, *pnum;
16821681
expr_ty expression;
16831682

1684-
/* If the unary - operator is applied to a constant, don't generate
1685-
a UNARY_NEGATIVE opcode. Just store the approriate value as a
1686-
constant. The peephole optimizer already does something like
1687-
this but it doesn't handle the case where the constant is
1688-
(sys.maxint - 1). In that case, we want a PyIntObject, not a
1689-
PyLongObject.
1690-
*/
1691-
if (TYPE(CHILD(n, 0)) == MINUS
1692-
&& NCH(n) == 2
1693-
&& TYPE((pfactor = CHILD(n, 1))) == factor
1694-
&& NCH(pfactor) == 1
1695-
&& TYPE((ppower = CHILD(pfactor, 0))) == power
1696-
&& NCH(ppower) == 1
1697-
&& TYPE((patom = CHILD(ppower, 0))) == atom
1698-
&& TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1699-
char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1700-
if (s == NULL)
1701-
return NULL;
1702-
s[0] = '-';
1703-
strcpy(s + 1, STR(pnum));
1704-
PyObject_FREE(STR(pnum));
1705-
STR(pnum) = s;
1706-
return ast_for_atom(c, patom);
1707-
}
1708-
17091683
expression = ast_for_expr(c, CHILD(n, 1));
17101684
if (!expression)
17111685
return NULL;

0 commit comments

Comments
 (0)