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

Skip to content

Commit ce5b6c4

Browse files
committed
Revert r82044, since it changed the semantics of negated imaginary literals.
Before r82044, '-7j' became complex(0.0, -7.0); afterwards it was complex(-0.0, -7.0). See issue 9011.
1 parent 881c1b4 commit ce5b6c4

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

Lib/test/test_parser.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -479,16 +479,6 @@ def test_compile_badunicode(self):
479479
st = parser.suite('a = "\\u1"')
480480
self.assertRaises(SyntaxError, parser.compilest, st)
481481

482-
def test_issue_9011(self):
483-
# Issue 9011: compilation of an unary minus expression changed
484-
# the meaning of the ST, so that a second compilation produced
485-
# incorrect results.
486-
st = parser.expr('-3')
487-
code1 = parser.compilest(st)
488-
self.assertEqual(eval(code1), -3)
489-
code2 = parser.compilest(st)
490-
self.assertEqual(eval(code2), -3)
491-
492482
class ParserStackLimitTestCase(unittest.TestCase):
493483
"""try to push the parser to/over its limits.
494484
see http://bugs.python.org/issue1881 for a discussion

Misc/NEWS

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ Core and Builtins
1616
Fix the encoding of the modules filename. Patch written by Amaury Forgeot
1717
d'Arc.
1818

19-
- Issue #9011: Remove buggy and unnecessary (in 3.x) ST->AST
20-
compilation code dealing with unary minus applied to a constant.
21-
The removed code was mutating the ST, causing a second compilation
22-
to fail.
23-
2419
- Issue #9058: Remove assertions about INT_MAX in UnicodeDecodeError.
2520

2621
- Issue #8941: decoding big endian UTF-32 data in UCS-2 builds could crash

Python/ast.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,8 +1664,34 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
16641664
static expr_ty
16651665
ast_for_factor(struct compiling *c, const node *n)
16661666
{
1667+
node *pfactor, *ppower, *patom, *pnum;
16671668
expr_ty expression;
16681669

1670+
/* If the unary - operator is applied to a constant, don't generate
1671+
a UNARY_NEGATIVE opcode. Just store the approriate value as a
1672+
constant. The peephole optimizer already does something like
1673+
this but it doesn't handle the case where the constant is
1674+
(sys.maxint - 1). In that case, we want a PyIntObject, not a
1675+
PyLongObject.
1676+
*/
1677+
if (TYPE(CHILD(n, 0)) == MINUS
1678+
&& NCH(n) == 2
1679+
&& TYPE((pfactor = CHILD(n, 1))) == factor
1680+
&& NCH(pfactor) == 1
1681+
&& TYPE((ppower = CHILD(pfactor, 0))) == power
1682+
&& NCH(ppower) == 1
1683+
&& TYPE((patom = CHILD(ppower, 0))) == atom
1684+
&& TYPE((pnum = CHILD(patom, 0))) == NUMBER) {
1685+
char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
1686+
if (s == NULL)
1687+
return NULL;
1688+
s[0] = '-';
1689+
strcpy(s + 1, STR(pnum));
1690+
PyObject_FREE(STR(pnum));
1691+
STR(pnum) = s;
1692+
return ast_for_atom(c, patom);
1693+
}
1694+
16691695
expression = ast_for_expr(c, CHILD(n, 1));
16701696
if (!expression)
16711697
return NULL;

0 commit comments

Comments
 (0)