[Python-checkins] CVS: python/dist/src/Objects intobject.c,2.62,2.63

Guido van Rossum gvanrossum@users.sourceforge.net
Wed, 22 Aug 2001 19:59:06 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv18246

Modified Files:
	intobject.c 
Log Message:
Change all case where we used to raise OverflowError to issue a
warning and then redo the operation using long ints.


Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.62
retrieving revision 2.63
diff -C2 -d -r2.62 -r2.63
*** intobject.c	2001/08/17 18:39:25	2.62
--- intobject.c	2001/08/23 02:59:04	2.63
***************
*** 23,31 ****
  };
  
! static PyObject *
  err_ovf(char *msg)
  {
! 	PyErr_SetString(PyExc_OverflowError, msg);
! 	return NULL;
  }
  
--- 23,36 ----
  };
  
! /* Return 1 if exception raised, 0 if caller should retry using longs */
! static int
  err_ovf(char *msg)
  {
! 	if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
! 		PyErr_SetString(PyExc_OverflowError, msg);
! 		return 1;
! 	}
! 	else
! 		return 0;
  }
  
***************
*** 278,284 ****
  	CONVERT_TO_LONG(w, b);
  	x = a + b;
! 	if ((x^a) < 0 && (x^b) < 0)
! 		return err_ovf("integer addition");
! 	return PyInt_FromLong(x);
  }
  
--- 283,291 ----
  	CONVERT_TO_LONG(w, b);
  	x = a + b;
! 	if ((x^a) >= 0 || (x^b) >= 0)
! 		return PyInt_FromLong(x);
! 	if (err_ovf("integer addition"))
! 		return NULL;
! 	return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
  }
  
***************
*** 290,296 ****
  	CONVERT_TO_LONG(w, b);
  	x = a - b;
! 	if ((x^a) < 0 && (x^~b) < 0)
! 		return err_ovf("integer subtraction");
! 	return PyInt_FromLong(x);
  }
  
--- 297,306 ----
  	CONVERT_TO_LONG(w, b);
  	x = a - b;
! 	if ((x^a) >= 0 || (x^~b) >= 0)
! 		return PyInt_FromLong(x);
! 	if (err_ovf("integer subtraction"))
! 		return NULL;
! 	return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
! 						     (PyObject *)w);
  }
  
***************
*** 433,440 ****
  
   bad:
! 	return err_ovf("integer multiplication");
  }
  
! static int
  i_divmod(register long x, register long y,
           long *p_xdivy, long *p_xmody)
--- 443,459 ----
  
   bad:
! 	if (err_ovf("integer multiplication"))
! 		return NULL;
! 	return PyLong_Type.tp_as_number->nb_multiply(v, w);
  }
  
! /* Return type of i_divmod */
! enum divmod_result {
! 	DIVMOD_OK,		/* Correct result */
! 	DIVMOD_OVERFLOW,	/* Overflow, try again using longs */
! 	DIVMOD_ERROR		/* Exception raised */
! };
! 
! static enum divmod_result
  i_divmod(register long x, register long y,
           long *p_xdivy, long *p_xmody)
***************
*** 445,454 ****
  		PyErr_SetString(PyExc_ZeroDivisionError,
  				"integer division or modulo by zero");
! 		return -1;
  	}
  	/* (-sys.maxint-1)/-1 is the only overflow case. */
  	if (y == -1 && x < 0 && x == -x) {
! 		err_ovf("integer division");
! 		return -1;
  	}
  	xdivy = x / y;
--- 464,474 ----
  		PyErr_SetString(PyExc_ZeroDivisionError,
  				"integer division or modulo by zero");
! 		return DIVMOD_ERROR;
  	}
  	/* (-sys.maxint-1)/-1 is the only overflow case. */
  	if (y == -1 && x < 0 && x == -x) {
! 		if (err_ovf("integer division"))
! 			return DIVMOD_ERROR;
! 		return DIVMOD_OVERFLOW;
  	}
  	xdivy = x / y;
***************
*** 466,470 ****
  	*p_xdivy = xdivy;
  	*p_xmody = xmody;
! 	return 0;
  }
  
--- 486,490 ----
  	*p_xdivy = xdivy;
  	*p_xmody = xmody;
! 	return DIVMOD_OK;
  }
  
***************
*** 476,482 ****
  	CONVERT_TO_LONG(x, xi);
  	CONVERT_TO_LONG(y, yi);
! 	if (i_divmod(xi, yi, &d, &m) < 0)
  		return NULL;
! 	return PyInt_FromLong(d);
  }
  
--- 496,508 ----
  	CONVERT_TO_LONG(x, xi);
  	CONVERT_TO_LONG(y, yi);
! 	switch (i_divmod(xi, yi, &d, &m)) {
! 	case DIVMOD_OK:
! 		return PyInt_FromLong(d);
! 	case DIVMOD_OVERFLOW:
! 		return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
! 							   (PyObject *)y);
! 	default:
  		return NULL;
! 	}
  }
  
***************
*** 488,494 ****
  	CONVERT_TO_LONG(x, xi);
  	CONVERT_TO_LONG(y, yi);
! 	if (i_divmod(xi, yi, &d, &m) < 0)
  		return NULL;
! 	return PyInt_FromLong(m);
  }
  
--- 514,526 ----
  	CONVERT_TO_LONG(x, xi);
  	CONVERT_TO_LONG(y, yi);
! 	switch (i_divmod(xi, yi, &d, &m)) {
! 	case DIVMOD_OK:
! 		return PyInt_FromLong(m);
! 	case DIVMOD_OVERFLOW:
! 		return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
! 							      (PyObject *)y);
! 	default:
  		return NULL;
! 	}
  }
  
***************
*** 500,506 ****
  	CONVERT_TO_LONG(x, xi);
  	CONVERT_TO_LONG(y, yi);
! 	if (i_divmod(xi, yi, &d, &m) < 0)
  		return NULL;
! 	return Py_BuildValue("(ll)", d, m);
  }
  
--- 532,544 ----
  	CONVERT_TO_LONG(x, xi);
  	CONVERT_TO_LONG(y, yi);
! 	switch (i_divmod(xi, yi, &d, &m)) {
! 	case DIVMOD_OK:
! 		return Py_BuildValue("(ll)", d, m);
! 	case DIVMOD_OVERFLOW:
! 		return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
! 							   (PyObject *)y);
! 	default:
  		return NULL;
! 	}
  }
  
***************
*** 508,512 ****
  int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
  {
- #if 1
  	register long iv, iw, iz=0, ix, temp, prev;
  	CONVERT_TO_LONG(v, iv);
--- 546,549 ----
***************
*** 543,548 ****
  			if (temp == 0)
  				break; /* Avoid ix / 0 */
! 			if (ix / temp != prev)
! 				return err_ovf("integer exponentiation");
  		}
  	 	iw >>= 1;	/* Shift exponent down by 1 bit */
--- 580,591 ----
  			if (temp == 0)
  				break; /* Avoid ix / 0 */
! 			if (ix / temp != prev) {
! 				if (err_ovf("integer exponentiation"))
! 					return NULL;
! 				return PyLong_Type.tp_as_number->nb_power(
! 					(PyObject *)v,
! 					(PyObject *)w,
! 					(PyObject *)w);
! 			}
  		}
  	 	iw >>= 1;	/* Shift exponent down by 1 bit */
***************
*** 550,555 ****
  	 	prev = temp;
  	 	temp *= temp;	/* Square the value of temp */
! 	 	if (prev!=0 && temp/prev!=prev)
! 			return err_ovf("integer exponentiation");
  	 	if (iz) {
  			/* If we did a multiplication, perform a modulo */
--- 593,602 ----
  	 	prev = temp;
  	 	temp *= temp;	/* Square the value of temp */
! 	 	if (prev!=0 && temp/prev!=prev) {
! 			if (err_ovf("integer exponentiation"))
! 				return NULL;
! 			return PyLong_Type.tp_as_number->nb_power(
! 				(PyObject *)v, (PyObject *)w, (PyObject *)z);
! 		}
  	 	if (iz) {
  			/* If we did a multiplication, perform a modulo */
***************
*** 560,593 ****
  	if (iz) {
  	 	long div, mod;
! 	 	if (i_divmod(ix, iz, &div, &mod) < 0)
! 			return(NULL);
! 	 	ix=mod;
! 	}
! 	return PyInt_FromLong(ix);
! #else
! 	register long iv, iw, ix;
! 	CONVERT_TO_LONG(v, iv);
! 	CONVERT_TO_LONG(w, iw);
! 	if (iw < 0) {
! 		PyErr_SetString(PyExc_ValueError,
! 				"integer to the negative power");
! 		return NULL;
! 	}
! 	if ((PyObject *)z != Py_None) {
! 		PyErr_SetString(PyExc_TypeError,
! 				"pow(int, int, int) not yet supported");
! 		return NULL;
! 	}
! 	ix = 1;
! 	while (--iw >= 0) {
! 		long prev = ix;
! 		ix = ix * iv;
! 		if (iv == 0)
! 			break; /* 0 to some power -- avoid ix / 0 */
! 		if (ix / iv != prev)
! 			return err_ovf("integer exponentiation");
  	}
  	return PyInt_FromLong(ix);
- #endif
  }				
  
--- 607,622 ----
  	if (iz) {
  	 	long div, mod;
! 		switch (i_divmod(ix, iz, &div, &mod)) {
! 		case DIVMOD_OK:
! 			ix = mod;
! 			break;
! 		case DIVMOD_OVERFLOW:
! 			return PyLong_Type.tp_as_number->nb_power(
! 				(PyObject *)v, (PyObject *)w, (PyObject *)z);
! 		default:
! 			return NULL;
! 		}
  	}
  	return PyInt_FromLong(ix);
  }				
  
***************
*** 598,603 ****
  	a = v->ob_ival;
  	x = -a;
! 	if (a < 0 && x < 0)
! 		return err_ovf("integer negation");
  	return PyInt_FromLong(x);
  }
--- 627,635 ----
  	a = v->ob_ival;
  	x = -a;
! 	if (a < 0 && x < 0) {
! 		if (err_ovf("integer negation"))
! 			return NULL;
! 		return PyNumber_Negative(PyLong_FromLong(a));
! 	}
  	return PyInt_FromLong(x);
  }