Skip to content

Commit 66b1259

Browse files
committed
SF #660455 : patch by NNorwitz.
"Unsigned" (i.e., positive-looking, but really negative) hex/oct constants with a leading minus sign are once again properly negated. The micro-optimization for negated numeric constants did the wrong thing for such hex/oct constants. The patch avoids the optimization for all hex/oct constants. This needs to be backported to Python 2.2!
1 parent e71b9f8 commit 66b1259

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

Lib/test/test_compile.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,12 @@ def expect_same(test_source, expected):
133133
expect_same("000000000000008.", 8.)
134134
expect_same("000000000000009.", 9.)
135135

136-
## # Verify treatment of unary minus on negative numbers SF bug #660455
137-
## import warnings
138-
## warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning)
139-
## # XXX Of course the following test will have to be changed in Python 2.4
140-
## expect_same("0xffffffff", -1)
141-
## expect_same("-0xffffffff", 1)
136+
# Verify treatment of unary minus on negative numbers SF bug #660455
137+
import warnings
138+
warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning)
139+
# XXX Of course the following test will have to be changed in Python 2.4
140+
# This test is in a <string> so the filterwarnings() can affect it
141+
exec """
142+
expect_same("0xffffffff", -1)
143+
expect_same("-0xffffffff", 1)
144+
"""

Lib/test/test_grammar.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
except ImportError:
3838
maxint = 2147483647
3939
if maxint == 2147483647:
40-
if -2147483647-1 != -020000000000: raise TestFailed, 'max negative int'
40+
# The following test will start to fail in Python 2.4;
41+
# change the 020000000000 to -020000000000
42+
if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
4143
# XXX -2147483648
4244
if 037777777777 != -1: raise TestFailed, 'oct -1'
4345
if 0xffffffff != -1: raise TestFailed, 'hex -1'

Python/compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2069,7 +2069,8 @@ com_factor(struct compiling *c, node *n)
20692069
&& NCH(ppower) == 1
20702070
&& TYPE((patom = CHILD(ppower, 0))) == atom
20712071
&& TYPE((pnum = CHILD(patom, 0))) == NUMBER
2072-
&& !(childtype == MINUS && is_float_zero(STR(pnum)))) {
2072+
&& !(childtype == MINUS &&
2073+
(STR(pnum)[0] == '0' || is_float_zero(STR(pnum))))) {
20732074
if (childtype == TILDE) {
20742075
com_invert_constant(c, pnum);
20752076
return;

0 commit comments

Comments
 (0)