[Python-checkins] python/dist/src/Objects floatobject.c,2.116,2.117 intobject.c,2.94,2.95 longobject.c,1.144,1.145

doerwalter@users.sourceforge.net doerwalter@users.sourceforge.net
Tue, 19 Nov 2002 12:49:17 -0800


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

Modified Files:
	floatobject.c intobject.c longobject.c 
Log Message:
Change int() so that passing a string, unicode, float or long argument
that is outside the integer range no longer raises OverflowError, but
returns a long object instead.

This fixes SF bug http://www.python.org/sf/635115


Index: floatobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/floatobject.c,v
retrieving revision 2.116
retrieving revision 2.117
diff -C2 -d -r2.116 -r2.117
*** floatobject.c	18 Nov 2002 16:06:21 -0000	2.116
--- floatobject.c	19 Nov 2002 20:49:15 -0000	2.117
***************
*** 643,646 ****
--- 643,653 ----
  
  static PyObject *
+ float_long(PyObject *v)
+ {
+ 	double x = PyFloat_AsDouble(v);
+ 	return PyLong_FromDouble(x);
+ }
+ 
+ static PyObject *
  float_int(PyObject *v)
  {
***************
*** 653,658 ****
  	/* conversion from floating to integral type would raise exception */
  	if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
! 		PyErr_SetString(PyExc_OverflowError, "float too large to convert");
! 		return NULL;
  	}
  #endif
--- 660,664 ----
  	/* conversion from floating to integral type would raise exception */
  	if (wholepart>LONG_MAX || wholepart<LONG_MIN) {
! 		return float_long(v);
  	}
  #endif
***************
*** 664,676 ****
  	if ((double)aslong == wholepart)
  		return PyInt_FromLong(aslong);
! 	PyErr_SetString(PyExc_OverflowError, "float too large to convert");
! 	return NULL;
! }
! 
! static PyObject *
! float_long(PyObject *v)
! {
! 	double x = PyFloat_AsDouble(v);
! 	return PyLong_FromDouble(x);
  }
  
--- 670,674 ----
  	if ((double)aslong == wholepart)
  		return PyInt_FromLong(aslong);
! 	return float_long(v);
  }
  

Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.94
retrieving revision 2.95
diff -C2 -d -r2.94 -r2.95
*** intobject.c	6 Nov 2002 16:15:12 -0000	2.94
--- intobject.c	19 Nov 2002 20:49:15 -0000	2.95
***************
*** 167,173 ****
  		return -1;
  	if (!PyInt_Check(io)) {
! 		PyErr_SetString(PyExc_TypeError,
! 				"nb_int should return int object");
! 		return -1;
  	}
  
--- 167,184 ----
  		return -1;
  	if (!PyInt_Check(io)) {
! 		if (PyLong_Check(io)) {
! 			/* got a long? => retry int conversion */
! 			val = PyLong_AsLong((PyObject *)io);
! 			if (PyErr_Occurred()) {
! 				Py_DECREF(io);
! 				return -1;
! 			}
! 		}
! 		else
! 		{
! 			PyErr_SetString(PyExc_TypeError,
! 					"nb_int should return int object");
! 			return -1;
! 		}
  	}
  
***************
*** 893,897 ****
  representation of a floating point number!)  When converting a string, use\n\
  the optional base.  It is an error to supply a base when converting a\n\
! non-string.");
  
  static PyNumberMethods int_as_number = {
--- 904,909 ----
  representation of a floating point number!)  When converting a string, use\n\
  the optional base.  It is an error to supply a base when converting a\n\
! non-string. If the argument is outside the integer range a long object\n\
! will be returned instead.");
  
  static PyNumberMethods int_as_number = {

Index: longobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/longobject.c,v
retrieving revision 1.144
retrieving revision 1.145
diff -C2 -d -r1.144 -r1.145
*** longobject.c	6 Nov 2002 16:15:14 -0000	1.144
--- longobject.c	19 Nov 2002 20:49:15 -0000	1.145
***************
*** 2518,2535 ****
  
  static PyObject *
! long_int(PyObject *v)
  {
! 	long x;
! 	x = PyLong_AsLong(v);
! 	if (PyErr_Occurred())
! 		return NULL;
! 	return PyInt_FromLong(x);
  }
  
  static PyObject *
! long_long(PyObject *v)
  {
! 	Py_INCREF(v);
! 	return v;
  }
  
--- 2518,2546 ----
  
  static PyObject *
! long_long(PyObject *v)
  {
! 	Py_INCREF(v);
! 	return v;
  }
  
  static PyObject *
! long_int(PyObject *v)
  {
! 	long x;
! 	x = PyLong_AsLong(v);
! 	if (PyErr_Occurred()) {
! 		if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
! 				PyErr_Clear();
! 				if (PyLong_CheckExact(v)) {
! 					Py_INCREF(v);
! 					return v;
! 				}
! 				else
! 					return _PyLong_Copy((PyLongObject *)v);
! 		}
! 		else
! 			return NULL;
! 	}
! 	return PyInt_FromLong(x);
  }