[Python-checkins] python/dist/src/Objects abstract.c, 2.130, 2.131 intobject.c, 2.111, 2.112

nascheme at users.sourceforge.net nascheme at users.sourceforge.net
Mon Jul 19 18:29:19 CEST 2004


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28866/Objects

Modified Files:
	abstract.c intobject.c 
Log Message:
Check the type of values returned by __int__, __float__, __long__,
__oct__, and __hex__.  Raise TypeError if an invalid type is
returned.  Note that PyNumber_Int and PyNumber_Long can still
return ints or longs.  Fixes SF bug #966618.


Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.130
retrieving revision 2.131
diff -C2 -d -r2.130 -r2.131
*** abstract.c	12 May 2004 21:35:06 -0000	2.130
--- abstract.c	19 Jul 2004 16:29:16 -0000	2.131
***************
*** 966,971 ****
  #endif
  	m = o->ob_type->tp_as_number;
! 	if (m && m->nb_int)
! 		return m->nb_int(o);
  	if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
  		return int_from_string((char*)buffer, buffer_len);
--- 966,980 ----
  #endif
  	m = o->ob_type->tp_as_number;
! 	if (m && m->nb_int) {
! 		PyObject *res = m->nb_int(o);
! 		if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
! 			PyErr_Format(PyExc_TypeError,
! 				     "__int__ returned non-int (type %.200s)",
! 				     res->ob_type->tp_name);
! 			Py_DECREF(res);
! 			return NULL;
! 		}
! 		return res;
! 	}
  	if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
  		return int_from_string((char*)buffer, buffer_len);
***************
*** 1023,1028 ****
  #endif
  	m = o->ob_type->tp_as_number;
! 	if (m && m->nb_long)
! 		return m->nb_long(o);
  	if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
  		return long_from_string(buffer, buffer_len);
--- 1032,1046 ----
  #endif
  	m = o->ob_type->tp_as_number;
! 	if (m && m->nb_long) {
! 		PyObject *res = m->nb_long(o);
! 		if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
! 			PyErr_Format(PyExc_TypeError,
! 				     "__long__ returned non-long (type %.200s)",
! 				     res->ob_type->tp_name);
! 			Py_DECREF(res);
! 			return NULL;
! 		}
! 		return res;
! 	}
  	if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
  		return long_from_string(buffer, buffer_len);
***************
*** 1048,1053 ****
  	if (!PyString_Check(o)) {
  		m = o->ob_type->tp_as_number;
! 		if (m && m->nb_float)
! 			return m->nb_float(o);
  	}
  	return PyFloat_FromString(o, NULL);
--- 1066,1080 ----
  	if (!PyString_Check(o)) {
  		m = o->ob_type->tp_as_number;
! 		if (m && m->nb_float) {
! 			PyObject *res = m->nb_float(o);
! 			if (res && !PyFloat_Check(res)) {
! 				PyErr_Format(PyExc_TypeError,
! 			          "__float__ returned non-float (type %.200s)",
! 			          res->ob_type->tp_name);
! 				Py_DECREF(res);
! 				return NULL;
! 			}
! 			return res;
! 		}
  	}
  	return PyFloat_FromString(o, NULL);

Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.111
retrieving revision 2.112
diff -C2 -d -r2.111 -r2.112
*** intobject.c	26 Jun 2004 23:22:57 -0000	2.111
--- intobject.c	19 Jul 2004 16:29:16 -0000	2.112
***************
*** 949,958 ****
  		return NULL;
  	if (!PyInt_Check(tmp)) {
- 		if (!PyLong_Check(tmp)) {
- 			PyErr_SetString(PyExc_ValueError,
- 					"value can't be converted to int");
- 			Py_DECREF(tmp);
- 			return NULL;
- 		}
  		ival = PyLong_AsLong(tmp);
  		if (ival == -1 && PyErr_Occurred()) {
--- 949,952 ----



More information about the Python-checkins mailing list