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

Skip to content

Commit 6c9e130

Browse files
committed
- Removed FutureWarnings related to hex/oct literals and conversions
and left shifts. (Thanks to Kalle Svensson for SF patch 849227.) This addresses most of the remaining semantic changes promised by PEP 237, except for repr() of a long, which still shows the trailing 'L'. The PEP appears to promise warnings for operations that changed semantics compared to Python 2.3, but this is not implemented; we've suffered through enough warnings related to hex/oct literals and I think it's best to be silent now.
1 parent 37e1363 commit 6c9e130

11 files changed

Lines changed: 120 additions & 148 deletions

File tree

Doc/ref/ref2.tex

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -580,25 +580,23 @@ \subsection{Integer and long integer literals\label{integers}}
580580
use \character{L}, since the letter \character{l} looks too much like the
581581
digit \character{1}.
582582

583-
Plain integer decimal literals that are above the largest representable
584-
plain integer (e.g., 2147483647 when using 32-bit arithmetic) are accepted
585-
as if they were long integers instead. Octal and hexadecimal literals
586-
behave similarly, but when in the range just above the largest representable
587-
plain integer but below the largest unsigned 32-bit number (on a machine
588-
using 32-bit arithmetic), 4294967296, they are taken as the negative plain
589-
integer obtained by subtracting 4294967296 from their unsigned value. There
590-
is no limit for long integer literals apart from what can be stored in
591-
available memory. For example, 0xdeadbeef is taken, on a 32-bit machine,
592-
as the value -559038737, while 0xdeadbeeffeed is taken as the value
593-
244837814107885L.
583+
Plain integer literals that are above the largest representable plain
584+
integer (e.g., 2147483647 when using 32-bit arithmetic) are accepted
585+
as if they were long integers instead.\footnote{In versions of Python
586+
prior to 2.4, octal and hexadecimal literals in the range just above
587+
the largest representable plain integer but below the largest unsigned
588+
32-bit number (on a machine using 32-bit arithmetic), 4294967296, were
589+
taken as the negative plain integer obtained by subtracting 4294967296
590+
from their unsigned value.} There is no limit for long integer
591+
literals apart from what can be stored in available memory.
594592

595593
Some examples of plain integer literals (first row) and long integer
596594
literals (second and third rows):
597595

598596
\begin{verbatim}
599-
7 2147483647 0177 0x80000000
597+
7 2147483647 0177
600598
3L 79228162514264337593543950336L 0377L 0x100000000L
601-
79228162514264337593543950336 0xdeadbeeffeed
599+
79228162514264337593543950336 0xdeadbeef
602600
\end{verbatim}
603601

604602

Lib/test/test_builtin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ def f(): pass
437437
def test_hex(self):
438438
self.assertEqual(hex(16), '0x10')
439439
self.assertEqual(hex(16L), '0x10L')
440-
self.assertEqual(len(hex(-1)), len(hex(sys.maxint)))
441-
self.assert_(hex(-16) in ('0xfffffff0', '0xfffffffffffffff0'))
440+
self.assertEqual(hex(-16), '-0x10')
442441
self.assertEqual(hex(-16L), '-0x10L')
443442
self.assertRaises(TypeError, hex, {})
444443

@@ -757,7 +756,7 @@ def __cmp__(self, other):
757756
def test_oct(self):
758757
self.assertEqual(oct(100), '0144')
759758
self.assertEqual(oct(100L), '0144L')
760-
self.assert_(oct(-100) in ('037777777634', '01777777777777777777634'))
759+
self.assertEqual(oct(-100), '-0144')
761760
self.assertEqual(oct(-100L), '-0144L')
762761
self.assertRaises(TypeError, oct, ())
763762

Lib/test/test_compile.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,18 @@ def test_literals_with_leading_zeroes(self):
119119

120120
def test_unary_minus(self):
121121
# Verify treatment of unary minus on negative numbers SF bug #660455
122-
warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning)
123-
warnings.filterwarnings("ignore", "hex.* of negative int", FutureWarning)
124-
# XXX Of course the following test will have to be changed in Python 2.4
125-
# This test is in a <string> so the filterwarnings() can affect it
126-
all_one_bits = '0xffffffff'
127-
if sys.maxint != 2147483647:
122+
if sys.maxint == 2147483647:
123+
# 32-bit machine
124+
all_one_bits = '0xffffffff'
125+
self.assertEqual(eval(all_one_bits), 4294967295L)
126+
self.assertEqual(eval("-" + all_one_bits), -4294967295L)
127+
elif sys.maxint == 9223372036854775807:
128+
# 64-bit machine
128129
all_one_bits = '0xffffffffffffffff'
129-
self.assertEqual(eval(all_one_bits), -1)
130-
self.assertEqual(eval("-" + all_one_bits), 1)
130+
self.assertEqual(eval(all_one_bits), 18446744073709551615L)
131+
self.assertEqual(eval("-" + all_one_bits), -18446744073709551615L)
132+
else:
133+
self.fail("How many bits *does* this machine have???")
131134

132135
def test_sequence_unpacking_error(self):
133136
# Verify sequence packing/unpacking with "or". SF bug #757818

Lib/test/test_format.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ def testboth(formatstr, *args):
183183
testboth("%#X", 0L, "0X0")
184184

185185
testboth("%x", 0x42, "42")
186-
# testboth("%x", -0x42, "ffffffbe") # specific to 32-bit boxes; see below
186+
testboth("%x", -0x42, "-42")
187187
testboth("%x", 0x42L, "42")
188188
testboth("%x", -0x42L, "-42")
189189

190190
testboth("%o", 042, "42")
191-
# testboth("%o", -042, "37777777736") # specific to 32-bit boxes; see below
191+
testboth("%o", -042, "-42")
192192
testboth("%o", 042L, "42")
193193
testboth("%o", -042L, "-42")
194194

@@ -238,6 +238,3 @@ def test_exc(formatstr, args, exception, excmsg):
238238
pass
239239
else:
240240
raise TestFailed, '"%*d"%(sys.maxint, -127) should fail'
241-
# (different things go wrong on a 64 bit box...)
242-
testboth("%x", -0x42, "ffffffbe")
243-
testboth("%o", -042, "37777777736")

Lib/test/test_grammar.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@
3939
if maxint == 2147483647:
4040
# The following test will start to fail in Python 2.4;
4141
# change the 020000000000 to -020000000000
42-
if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
42+
if -2147483647-1 != -020000000000: raise TestFailed, 'max negative int'
4343
# XXX -2147483648
44-
if 037777777777 != -1: raise TestFailed, 'oct -1'
45-
if 0xffffffff != -1: raise TestFailed, 'hex -1'
44+
if 037777777777 < 0: raise TestFailed, 'large oct'
45+
if 0xffffffff < 0: raise TestFailed, 'large hex'
4646
for s in '2147483648', '040000000000', '0x100000000':
4747
try:
4848
x = eval(s)
4949
except OverflowError:
5050
print "OverflowError on huge integer literal " + `s`
5151
elif eval('maxint == 9223372036854775807'):
52-
if eval('-9223372036854775807-1 != 01000000000000000000000'):
52+
if eval('-9223372036854775807-1 != -01000000000000000000000'):
5353
raise TestFailed, 'max negative int'
54-
if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
55-
if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
54+
if eval('01777777777777777777777') < 0: raise TestFailed, 'large oct'
55+
if eval('0xffffffffffffffff') < 0: raise TestFailed, 'large hex'
5656
for s in '9223372036854775808', '02000000000000000000000', \
5757
'0x10000000000000000':
5858
try:

Lib/test/test_hexoct.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""Test correct treatment of hex/oct constants.
22
33
This is complex because of changes due to PEP 237.
4-
5-
Some of these tests will have to change in Python 2.4!
64
"""
75

86
import sys
@@ -41,31 +39,28 @@ def test_hex_baseline(self):
4139
self.assertEqual(-0x7fffffffffffffff, -9223372036854775807)
4240

4341
def test_hex_unsigned(self):
44-
# This test is in a <string> so we can ignore the warnings
45-
exec """if 1:
4642
if platform_long_is_32_bits:
47-
# Positive-looking constants with negavive values
48-
self.assertEqual(0x80000000, -2147483648L)
49-
self.assertEqual(0xffffffff, -1)
43+
# Positive constants
44+
self.assertEqual(0x80000000, 2147483648L)
45+
self.assertEqual(0xffffffff, 4294967295L)
5046
# Ditto with a minus sign and parentheses
51-
self.assertEqual(-(0x80000000), 2147483648L)
52-
self.assertEqual(-(0xffffffff), 1)
47+
self.assertEqual(-(0x80000000), -2147483648L)
48+
self.assertEqual(-(0xffffffff), -4294967295L)
5349
# Ditto with a minus sign and NO parentheses
5450
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
55-
self.assertEqual(-0x80000000, 2147483648L)
56-
self.assertEqual(-0xffffffff, 1)
51+
self.assertEqual(-0x80000000, -2147483648L)
52+
self.assertEqual(-0xffffffff, -4294967295L)
5753
else:
58-
# Positive-looking constants with negavive values
59-
self.assertEqual(0x8000000000000000, -9223372036854775808L)
60-
self.assertEqual(0xffffffffffffffff, -1)
54+
# Positive constants
55+
self.assertEqual(0x8000000000000000, 9223372036854775808L)
56+
self.assertEqual(0xffffffffffffffff, 18446744073709551615L)
6157
# Ditto with a minus sign and parentheses
62-
self.assertEqual(-(0x8000000000000000), 9223372036854775808L)
63-
self.assertEqual(-(0xffffffffffffffff), 1)
58+
self.assertEqual(-(0x8000000000000000), -9223372036854775808L)
59+
self.assertEqual(-(0xffffffffffffffff), -18446744073709551615L)
6460
# Ditto with a minus sign and NO parentheses
6561
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
66-
self.assertEqual(-0x8000000000000000, 9223372036854775808L)
67-
self.assertEqual(-0xffffffffffffffff, 1)
68-
\n"""
62+
self.assertEqual(-0x8000000000000000, -9223372036854775808L)
63+
self.assertEqual(-0xffffffffffffffff, -18446744073709551615L)
6964

7065
def test_oct_baseline(self):
7166
# Baseline tests
@@ -91,31 +86,28 @@ def test_oct_baseline(self):
9186
self.assertEqual(-0777777777777777777777, -9223372036854775807)
9287

9388
def test_oct_unsigned(self):
94-
# This test is in a <string> so we can ignore the warnings
95-
exec """if 1:
9689
if platform_long_is_32_bits:
97-
# Positive-looking constants with negavive values
98-
self.assertEqual(020000000000, -2147483648L)
99-
self.assertEqual(037777777777, -1)
90+
# Positive constants
91+
self.assertEqual(020000000000, 2147483648L)
92+
self.assertEqual(037777777777, 4294967295L)
10093
# Ditto with a minus sign and parentheses
101-
self.assertEqual(-(020000000000), 2147483648L)
102-
self.assertEqual(-(037777777777), 1)
94+
self.assertEqual(-(020000000000), -2147483648L)
95+
self.assertEqual(-(037777777777), -4294967295L)
10396
# Ditto with a minus sign and NO parentheses
10497
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
105-
self.assertEqual(-020000000000, 2147483648L)
106-
self.assertEqual(-037777777777, 1)
98+
self.assertEqual(-020000000000, -2147483648L)
99+
self.assertEqual(-037777777777, -4294967295L)
107100
else:
108-
# Positive-looking constants with negavive values
109-
self.assertEqual(01000000000000000000000, -9223372036854775808L)
110-
self.assertEqual(01777777777777777777777, -1)
101+
# Positive constants
102+
self.assertEqual(01000000000000000000000, 9223372036854775808L)
103+
self.assertEqual(01777777777777777777777, 18446744073709551615L)
111104
# Ditto with a minus sign and parentheses
112-
self.assertEqual(-(01000000000000000000000), 9223372036854775808L)
113-
self.assertEqual(-(01777777777777777777777), 1)
105+
self.assertEqual(-(01000000000000000000000), -9223372036854775808L)
106+
self.assertEqual(-(01777777777777777777777), -18446744073709551615L)
114107
# Ditto with a minus sign and NO parentheses
115108
# This failed in Python 2.2 through 2.2.2 and in 2.3a1
116-
self.assertEqual(-01000000000000000000000, 9223372036854775808L)
117-
self.assertEqual(-01777777777777777777777, 1)
118-
\n"""
109+
self.assertEqual(-01000000000000000000000, -9223372036854775808L)
110+
self.assertEqual(-01777777777777777777777, -18446744073709551615L)
119111

120112
def test_main():
121113
test_support.run_unittest(TextHexOct)

Misc/NEWS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ What's New in Python 2.4 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Removed FutureWarnings related to hex/oct literals and conversions
16+
and left shifts. (Thanks to Kalle Svensson for SF patch 849227.)
17+
This addresses most of the remaining semantic changes promised by
18+
PEP 237, except for repr() of a long, which still shows the trailing
19+
'L'. The PEP appears to promise warnings for operations that
20+
changed semantics compared to Python 2.3, but this is not
21+
implemented; we've suffered through enough warnings related to
22+
hex/oct literals and I think it's best to be silent now.
23+
1524
- For str and unicode objects, the ljust(), center(), and rjust()
1625
methods now accept an optional argument specifying a fill
1726
character other than a space.

Objects/intobject.c

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ PyInt_FromString(char *s, char **pend, int base)
278278
char *end;
279279
long x;
280280
char buffer[256]; /* For errors */
281-
int warn = 0;
282281

283282
if ((base != 0 && base < 2) || base > 36) {
284283
PyErr_SetString(PyExc_ValueError,
@@ -292,7 +291,7 @@ PyInt_FromString(char *s, char **pend, int base)
292291
if (base == 0 && s[0] == '0') {
293292
x = (long) PyOS_strtoul(s, &end, base);
294293
if (x < 0)
295-
warn = 1;
294+
return PyLong_FromString(s, pend, base);
296295
}
297296
else
298297
x = PyOS_strtol(s, &end, base);
@@ -312,11 +311,6 @@ PyInt_FromString(char *s, char **pend, int base)
312311
return NULL;
313312
return PyLong_FromString(s, pend, base);
314313
}
315-
if (warn) {
316-
if (PyErr_Warn(PyExc_FutureWarning,
317-
"int('0...', 0): sign will change in Python 2.4") < 0)
318-
return NULL;
319-
}
320314
if (pend)
321315
*pend = end;
322316
return PyInt_FromLong(x);
@@ -766,18 +760,13 @@ int_lshift(PyIntObject *v, PyIntObject *w)
766760
if (a == 0 || b == 0)
767761
return int_pos(v);
768762
if (b >= LONG_BIT) {
769-
if (PyErr_Warn(PyExc_FutureWarning,
770-
"x<<y losing bits or changing sign "
771-
"will return a long in Python 2.4 and up") < 0)
772-
return NULL;
773-
return PyInt_FromLong(0L);
763+
return PyNumber_Lshift(PyLong_FromLong(PyInt_AS_LONG(v)),
764+
PyLong_FromLong(PyInt_AS_LONG(w)));
774765
}
775766
c = a << b;
776767
if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
777-
if (PyErr_Warn(PyExc_FutureWarning,
778-
"x<<y losing bits or changing sign "
779-
"will return a long in Python 2.4 and up") < 0)
780-
return NULL;
768+
return PyNumber_Lshift(PyLong_FromLong(PyInt_AS_LONG(v)),
769+
PyLong_FromLong(PyInt_AS_LONG(w)));
781770
}
782771
return PyInt_FromLong(c);
783772
}
@@ -868,13 +857,9 @@ int_oct(PyIntObject *v)
868857
{
869858
char buf[100];
870859
long x = v -> ob_ival;
871-
if (x < 0) {
872-
if (PyErr_Warn(PyExc_FutureWarning,
873-
"hex()/oct() of negative int will return "
874-
"a signed string in Python 2.4 and up") < 0)
875-
return NULL;
876-
}
877-
if (x == 0)
860+
if (x < 0)
861+
PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
862+
else if (x == 0)
878863
strcpy(buf, "0");
879864
else
880865
PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
@@ -886,13 +871,10 @@ int_hex(PyIntObject *v)
886871
{
887872
char buf[100];
888873
long x = v -> ob_ival;
889-
if (x < 0) {
890-
if (PyErr_Warn(PyExc_FutureWarning,
891-
"hex()/oct() of negative int will return "
892-
"a signed string in Python 2.4 and up") < 0)
893-
return NULL;
894-
}
895-
PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
874+
if (x < 0)
875+
PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
876+
else
877+
PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
896878
return PyString_FromString(buf);
897879
}
898880

0 commit comments

Comments
 (0)