[Patches] Long ints for getitem,setitem,slices
Andrew M. Kuchling
akuchlin@mems-exchange.org
Thu, 17 Feb 2000 19:35:45 -0500 (EST)
Guido van Rossum writes:
>If you really want to be strict, catch the OverflowError, and then
>inspect the sign of the original long int to decide whether it should
>mean sys.maxint or 0. (This saves time in the common case -- no
>overflow.)
Done. Here's a revised version of the patch, which handles
overflowing long integers, and also includes a patch to the test suite
that exercises the changes, though only for lists.
(Guess I should look into implementing this for JPython now...)
--amk
Index: Python//ceval.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/ceval.c,v
retrieving revision 2.166
diff -C2 -r2.166 ceval.c
*** ceval.c 2000/01/12 22:45:54 2.166
--- ceval.c 2000/02/17 23:42:47
***************
*** 2538,2547 ****
if (v != NULL) {
long x;
! if (!PyInt_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"slice index must be int");
return -1;
}
- x = PyInt_AsLong(v);
/* Truncate -- very long indices are truncated anyway */
if (x > INT_MAX)
--- 2538,2579 ----
if (v != NULL) {
long x;
! if (PyInt_Check(v)) {
! x = PyInt_AsLong(v);
! } else if (PyLong_Check(v)) {
! x = PyLong_AsLong(v);
! if (x==-1 && PyErr_Occurred()) {
! PyObject *long_zero;
!
! if (!PyErr_ExceptionMatches( PyExc_OverflowError ) ) {
! /* It's not an overflow error, so just
! signal an error */
! return -1;
! }
!
! /* It's an overflow error, so we need to
! check the sign of the long integer,
! set the value to INT_MAX or 0, and clear
! the error. */
!
! /* Create a long integer with a value of 0 */
! long_zero = PyLong_FromLong( 0L );
! if (long_zero == NULL) return -1;
!
! /* Check sign */
! if (PyObject_Compare(long_zero, v) < 0)
! x = INT_MAX;
! else
! x = 0;
!
! /* Free the long integer we created, and clear the
! OverflowError */
! Py_DECREF(long_zero);
! PyErr_Clear();
! }
! } else {
PyErr_SetString(PyExc_TypeError,
"slice index must be int");
return -1;
}
/* Truncate -- very long indices are truncated anyway */
if (x > INT_MAX)
Index: Objects//abstract.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/abstract.c,v
retrieving revision 2.29
diff -C2 -r2.29 abstract.c
*** abstract.c 2000/02/14 22:22:04 2.29
--- abstract.c 2000/02/15 15:14:27
***************
*** 123,126 ****
--- 123,132 ----
if (PyInt_Check(key))
return PySequence_GetItem(o, PyInt_AsLong(key));
+ else if (PyLong_Check(key)) {
+ long key_value = PyLong_AsLong(key);
+ if (key_value == -1 && PyErr_Occurred())
+ return NULL;
+ return PySequence_GetItem(o, key_value);
+ }
return type_error("sequence index must be integer");
}
***************
*** 148,151 ****
--- 154,163 ----
if (PyInt_Check(key))
return PySequence_SetItem(o, PyInt_AsLong(key), value);
+ else if (PyLong_Check(key)) {
+ long key_value = PyLong_AsLong(key);
+ if (key_value == -1 && PyErr_Occurred())
+ return -1;
+ return PySequence_SetItem(o, key_value, value);
+ }
type_error("sequence index must be integer");
return -1;
***************
*** 174,177 ****
--- 186,195 ----
if (PyInt_Check(key))
return PySequence_DelItem(o, PyInt_AsLong(key));
+ else if (PyLong_Check(key)) {
+ long key_value = PyLong_AsLong(key);
+ if (key_value == -1 && PyErr_Occurred())
+ return -1;
+ return PySequence_DelItem(o, key_value);
+ }
type_error("sequence index must be integer");
return -1;
***************
*** 392,396 ****
else if (PyLong_Check(w)) {
mul_value = PyLong_AsLong(w);
! if (PyErr_Occurred())
return NULL;
}
--- 410,414 ----
else if (PyLong_Check(w)) {
mul_value = PyLong_AsLong(w);
! if (mul_value == -1 && PyErr_Occurred())
return NULL;
}
Index: Lib/test/test_types.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Lib/test/test_types.py,v
retrieving revision 1.12
diff -C2 -r1.12 test_types.py
*** test_types.py 1998/07/16 15:29:06 1.12
--- test_types.py 2000/02/18 00:18:08
***************
*** 131,135 ****
--- 131,137 ----
if [1,2]+[3,4] <> [1,2,3,4]: raise TestFailed, 'list concatenation'
if [1,2]*3 <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
+ if [1,2]*3L <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L'
if 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*'
+ if 0L*[1,2,3] <> []: raise TestFailed, 'list repetition 0L*'
if min([1,2]) <> 1 or max([1,2]) <> 2: raise TestFailed, 'min/max list'
if 0 in [0,1,2] and 1 in [0,1,2] and 2 in [0,1,2] and 3 not in [0,1,2]: pass
***************
*** 151,158 ****
--- 153,167 ----
print '6.5.3a Additional list operations'
a = [0,1,2,3,4]
+ a[0L] = 1
+ a[1L] = 2
+ a[2L] = 3
+ if a <> [1,2,3,3,4]: raise TestFailed, 'list item assignment [0L], [1L], [2L]'
a[0] = 5
a[1] = 6
a[2] = 7
if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
+ a[-2L] = 88
+ a[-1L] = 99
+ if a <> [5,6,7,88,99]: raise TestFailed, 'list item assignment [-2L], [-1L]'
a[-2] = 8
a[-1] = 9
***************
*** 162,165 ****
--- 171,176 ----
a[1:1] = [1,2,3]
if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
+ a[ 1L : 4L] = [7,8,9]
+ if a <> [0,7,8,9,4]: raise TestFailed, 'list slice assignment using long ints'
del a[1:4]
if a <> [0,4]: raise TestFailed, 'list slice deletion'
***************
*** 168,171 ****
--- 179,189 ----
del a[-1]
if a <> []: raise TestFailed, 'list item deletion [-1]'
+ a=range(0,5)
+ del a[1L:4L]
+ if a <> [0,4]: raise TestFailed, 'list slice deletion'
+ del a[0L]
+ if a <> [4]: raise TestFailed, 'list item deletion [0]'
+ del a[-1L]
+ if a <> []: raise TestFailed, 'list item deletion [-1]'
a.append(0)
a.append(1)
***************
*** 192,195 ****
--- 210,220 ----
z = range(12)
z.sort(myComparison)
+
+ # Test extreme cases with long ints
+ a = [0,1,2,3,4]
+ if a[ -pow(2,128L): 3 ] != [0,1,2]:
+ raise TestFailed, "list slicing with too-small long integer"
+ if a[ 3: pow(2,145L) ] != [3,4]:
+ raise TestFailed, "list slicing with too-large long integer"
print '6.6 Mappings == Dictionaries'