Skip to content

Commit ddefaf3

Browse files
committed
Merged the int/long unification branch, by very crude means (sorry Thomas!).
I banged on the code (beyond what's in that branch) to make fewer tests fail; the only tests that fail now are: test_descr -- can't pickle ints?! test_pickletools -- ??? test_socket -- See python.org/sf/1619659 test_sqlite -- ??? I'll deal with those later.
1 parent 5b787e8 commit ddefaf3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+798
-404
lines changed

BROKEN_TESTS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test_descr -- can't pickle int objects?!?!
2+
test_pickletools -- ???
3+
test_socket -- OverflowError: can't convert negative value to unsigned int
4+
test_sqlite -- ???

INTBENCH

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Measurements of _testcapi.profile_int, on an 800MHz G3, OSX 10.4.7
2+
best of three runs
3+
4+
r51476 (original p3yk)
5+
Test 1: 1.601978s
6+
Test 2: 1.696623s
7+
Test 3: 1.900683s
8+
Test 4: 5.307155s
9+
Test 5: 2.546707s
10+
Test 6: 1.670252s
11+
Test 7: 1.910734s
12+
13+
r51506 (int type dropped)
14+
Test 1: 4.134757s
15+
Test 2: 4.398235s
16+
Test 3: 4.611636s
17+
Test 4: 10.665429s
18+
19+
r51509 (small int cache)
20+
Test 1: 3.457184s
21+
Test 2: 4.514800s
22+
Test 3: 4.999010s
23+
Test 4: 10.818277s
24+
25+
r51452 (special-casing medium int allocation)
26+
Test 1: 3.258219s
27+
Test 2: 4.255007s
28+
Test 3: 4.547923s
29+
Test 4: 10.615123s
30+
Test 5: 5.255545s
31+
Test 6: 3.775941s
32+
Test 7: 4.001805s
33+
34+
r51562 (special-case one-digit operations)
35+
Test 1: 3.527860s
36+
Test 2: 3.975953s
37+
Test 3: 4.226751s
38+
Test 4: 10.605721s
39+
Test 5: 5.233576s
40+
Test 6: 2.161525s
41+
Test 7: 3.421624s
42+
43+
r51573 (speed up PyLong_FromLong)
44+
Test 1: 3.149116s
45+
Test 2: 3.948204s
46+
Test 3: 4.012080s
47+
Test 4: 8.589921s
48+
Test 5: 4.481723s
49+
Test 6: 2.259849s
50+
Test 7: 3.453212s

Include/abstract.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
716716
is cleared and the value is clipped.
717717
*/
718718

719-
PyAPI_FUNC(PyObject *) PyNumber_Int(PyObject *o);
719+
#define PyNumber_Int PyNumber_Long
720720

721721
/*
722722
Returns the o converted to an integer object on success, or

Include/boolobject.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ extern "C" {
77
#endif
88

99

10-
typedef PyIntObject PyBoolObject;
11-
1210
PyAPI_DATA(PyTypeObject) PyBool_Type;
1311

1412
#define PyBool_Check(x) ((x)->ob_type == &PyBool_Type)
@@ -17,10 +15,10 @@ PyAPI_DATA(PyTypeObject) PyBool_Type;
1715
Don't forget to apply Py_INCREF() when returning either!!! */
1816

1917
/* Don't use these directly */
20-
PyAPI_DATA(PyIntObject) _Py_ZeroStruct, _Py_TrueStruct;
18+
PyAPI_DATA(struct _longobject) _Py_FalseStruct, _Py_TrueStruct;
2119

2220
/* Use these macros */
23-
#define Py_False ((PyObject *) &_Py_ZeroStruct)
21+
#define Py_False ((PyObject *) &_Py_FalseStruct)
2422
#define Py_True ((PyObject *) &_Py_TrueStruct)
2523

2624
/* Macros for returning Py_True or Py_False, respectively */

Include/intobject.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,31 @@ _Py_TrueStruct and _Py_ZeroStruct in boolobject.h; don't use this.
2020
extern "C" {
2121
#endif
2222

23+
/*
2324
typedef struct {
2425
PyObject_HEAD
2526
long ob_ival;
2627
} PyIntObject;
2728
2829
PyAPI_DATA(PyTypeObject) PyInt_Type;
30+
*/
2931

30-
#define PyInt_Check(op) PyObject_TypeCheck(op, &PyInt_Type)
31-
#define PyInt_CheckExact(op) ((op)->ob_type == &PyInt_Type)
32+
#define PyInt_Check(op) PyLong_Check(op)
33+
#define PyInt_CheckExact(op) (PyLong_CheckExact(op) && _PyLong_FitsInLong(op))
3234

33-
PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
34-
#ifdef Py_USING_UNICODE
35-
PyAPI_FUNC(PyObject *) PyInt_FromUnicode(Py_UNICODE*, Py_ssize_t, int);
36-
#endif
37-
PyAPI_FUNC(PyObject *) PyInt_FromLong(long);
38-
PyAPI_FUNC(PyObject *) PyInt_FromSize_t(size_t);
39-
PyAPI_FUNC(PyObject *) PyInt_FromSsize_t(Py_ssize_t);
40-
PyAPI_FUNC(long) PyInt_AsLong(PyObject *);
41-
PyAPI_FUNC(Py_ssize_t) PyInt_AsSsize_t(PyObject *);
42-
PyAPI_FUNC(unsigned long) PyInt_AsUnsignedLongMask(PyObject *);
43-
#ifdef HAVE_LONG_LONG
44-
PyAPI_FUNC(unsigned PY_LONG_LONG) PyInt_AsUnsignedLongLongMask(PyObject *);
45-
#endif
35+
#define PyInt_FromString PyLong_FromString
36+
#define PyInt_FromUnicode PyLong_FromUnicode
37+
#define PyInt_FromLong PyLong_FromLong
38+
#define PyInt_FromSize_t PyLong_FromSize_t
39+
#define PyInt_FromSsize_t PyLong_FromSsize_t
40+
#define PyInt_AsLong PyLong_AsLong
41+
#define PyInt_AsSsize_t PyLong_AsSsize_t
42+
#define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
43+
#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
4644

4745
PyAPI_FUNC(long) PyInt_GetMax(void);
4846

49-
/* Macro, trading safety for speed */
50-
#define PyInt_AS_LONG(op) (((PyIntObject *)(op))->ob_ival)
47+
#define PyInt_AS_LONG(op) PyLong_AsLong(op)
5148

5249
/* These aren't really part of the Int object, but they're handy; the protos
5350
* are necessary for systems that need the magic of PyAPI_FUNC and that want

Include/longobject.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ PyAPI_DATA(PyTypeObject) PyLong_Type;
1616

1717
PyAPI_FUNC(PyObject *) PyLong_FromLong(long);
1818
PyAPI_FUNC(PyObject *) PyLong_FromUnsignedLong(unsigned long);
19+
PyAPI_FUNC(PyObject *) PyLong_FromSize_t(size_t);
20+
PyAPI_FUNC(PyObject *) PyLong_FromSsize_t(Py_ssize_t);
1921
PyAPI_FUNC(PyObject *) PyLong_FromDouble(double);
2022
PyAPI_FUNC(long) PyLong_AsLong(PyObject *);
23+
PyAPI_FUNC(ssize_t) PyLong_AsSsize_t(PyObject *);
24+
PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *);
2125
PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *);
2226
PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *);
2327

2428
/* For use by intobject.c only */
25-
PyAPI_FUNC(Py_ssize_t) _PyLong_AsSsize_t(PyObject *);
26-
PyAPI_FUNC(PyObject *) _PyLong_FromSize_t(size_t);
27-
PyAPI_FUNC(PyObject *) _PyLong_FromSsize_t(Py_ssize_t);
2829
PyAPI_DATA(int) _PyLong_DigitValue[256];
2930

3031
/* _PyLong_AsScaledDouble returns a double x and an exponent e such that
@@ -34,6 +35,7 @@ PyAPI_DATA(int) _PyLong_DigitValue[256];
3435
be multiplied by SHIFT! There may not be enough room in an int to store
3536
e*SHIFT directly. */
3637
PyAPI_FUNC(double) _PyLong_AsScaledDouble(PyObject *vv, int *e);
38+
PyAPI_FUNC(int) _PyLong_FitsInLong(PyObject* vv);
3739

3840
PyAPI_FUNC(double) PyLong_AsDouble(PyObject *);
3941
PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *);

Lib/pickle.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,29 @@ def save_int(self, obj, pack=struct.pack):
456456
return
457457
# Text pickle, or int too big to fit in signed 4-byte format.
458458
self.write(INT + repr(obj) + '\n')
459-
dispatch[IntType] = save_int
459+
# XXX save_int is merged into save_long
460+
# dispatch[IntType] = save_int
460461

461462
def save_long(self, obj, pack=struct.pack):
463+
if self.bin:
464+
# If the int is small enough to fit in a signed 4-byte 2's-comp
465+
# format, we can store it more efficiently than the general
466+
# case.
467+
# First one- and two-byte unsigned ints:
468+
if obj >= 0:
469+
if obj <= 0xff:
470+
self.write(BININT1 + chr(obj))
471+
return
472+
if obj <= 0xffff:
473+
self.write("%c%c%c" % (BININT2, obj&0xff, obj>>8))
474+
return
475+
# Next check for 4-byte signed ints:
476+
high_bits = obj >> 31 # note that Python shift sign-extends
477+
if high_bits == 0 or high_bits == -1:
478+
# All high bits are copies of bit 2**31, so the value
479+
# fits in a 4-byte signed int.
480+
self.write(BININT + pack("<i", obj))
481+
return
462482
if self.proto >= 2:
463483
bytes = encode_long(obj)
464484
n = len(bytes)

Lib/test/output/test_class

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ __neg__: ()
4444
__pos__: ()
4545
__abs__: ()
4646
__int__: ()
47-
__long__: ()
47+
__int__: ()
4848
__float__: ()
4949
__oct__: ()
5050
__hex__: ()

Lib/test/test_builtin.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,12 @@ def __long__(self):
10811081

10821082
self.assertEqual(long(Foo0()), 42L)
10831083
self.assertEqual(long(Foo1()), 42L)
1084-
self.assertEqual(long(Foo2()), 42L)
1084+
# XXX invokes __int__ now
1085+
# self.assertEqual(long(Foo2()), 42L)
10851086
self.assertEqual(long(Foo3()), 0)
1086-
self.assertEqual(long(Foo4()), 42)
1087-
self.assertRaises(TypeError, long, Foo5())
1087+
# XXX likewise
1088+
# self.assertEqual(long(Foo4()), 42)
1089+
# self.assertRaises(TypeError, long, Foo5())
10881090

10891091
def test_map(self):
10901092
self.assertEqual(

Lib/test/test_descr.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,6 @@ def __add__(self, other):
430430
pass
431431
else:
432432
raise TestFailed, "NotImplemented should have caused TypeError"
433-
import sys
434-
try:
435-
C(sys.maxint+1)
436-
except OverflowError:
437-
pass
438-
else:
439-
raise TestFailed, "should have raised OverflowError"
440433

441434
def longs():
442435
if verbose: print "Testing long operations..."

0 commit comments

Comments
 (0)