[Python-checkins] python/dist/src/Objects complexobject.c,2.59,2.60 abstract.c,2.102,2.103

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Thu, 06 Jun 2002 08:45:41 -0700


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

Modified Files:
	complexobject.c abstract.c 
Log Message:
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Made conversion failure error messages consistent between types.
Added related unittests. 


Index: complexobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/complexobject.c,v
retrieving revision 2.59
retrieving revision 2.60
diff -C2 -d -r2.59 -r2.60
*** complexobject.c	15 Apr 2002 12:39:12 -0000	2.59
--- complexobject.c	6 Jun 2002 15:45:38 -0000	2.60
***************
*** 812,819 ****
  complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
! 	PyObject *r, *i, *tmp;
  	PyNumberMethods *nbr, *nbi = NULL;
  	Py_complex cr, ci;
  	int own_r = 0;
  	static char *kwlist[] = {"real", "imag", 0};
  
--- 812,820 ----
  complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
! 	PyObject *r, *i, *tmp, *f;
  	PyNumberMethods *nbr, *nbi = NULL;
  	Py_complex cr, ci;
  	int own_r = 0;
+ 	static PyObject *complexstr;
  	static char *kwlist[] = {"real", "imag", 0};
  
***************
*** 838,841 ****
--- 839,862 ----
  	}
  
+ 	/* XXX Hack to support classes with __complex__ method */
+ 	if (complexstr == NULL) {
+ 		complexstr = PyString_InternFromString("__complex__");
+ 		if (complexstr == NULL)
+ 			return NULL;
+ 	}
+ 	f = PyObject_GetAttr(r, complexstr);
+ 	if (f == NULL)
+ 		PyErr_Clear();
+ 	else {
+ 		PyObject *args = Py_BuildValue("()");
+ 		if (args == NULL)
+ 			return NULL;
+ 		r = PyEval_CallObject(f, args);
+ 		Py_DECREF(args);
+ 		Py_DECREF(f);
+ 		if (r == NULL)
+ 			return NULL;
+ 		own_r = 1;
+ 	}
  	nbr = r->ob_type->tp_as_number;
  	if (i != NULL)
***************
*** 844,873 ****
  	    ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
  		PyErr_SetString(PyExc_TypeError,
! 			   "complex() arg can't be converted to complex");
  		return NULL;
- 	}
- 	/* XXX Hack to support classes with __complex__ method */
- 	if (PyInstance_Check(r)) {
- 		static PyObject *complexstr;
- 		PyObject *f;
- 		if (complexstr == NULL) {
- 			complexstr = PyString_InternFromString("__complex__");
- 			if (complexstr == NULL)
- 				return NULL;
- 		}
- 		f = PyObject_GetAttr(r, complexstr);
- 		if (f == NULL)
- 			PyErr_Clear();
- 		else {
- 			PyObject *args = Py_BuildValue("()");
- 			if (args == NULL)
- 				return NULL;
- 			r = PyEval_CallObject(f, args);
- 			Py_DECREF(args);
- 			Py_DECREF(f);
- 			if (r == NULL)
- 				return NULL;
- 			own_r = 1;
- 		}
  	}
  	if (PyComplex_Check(r)) {
--- 865,870 ----
  	    ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
  		PyErr_SetString(PyExc_TypeError,
! 			   "complex() argument must be a string or a number");
  		return NULL;
  	}
  	if (PyComplex_Check(r)) {

Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.102
retrieving revision 2.103
diff -C2 -d -r2.102 -r2.103
*** abstract.c	5 Jun 2002 12:55:19 -0000	2.102
--- abstract.c	6 Jun 2002 15:45:38 -0000	2.103
***************
*** 904,908 ****
  		return int_from_string((char*)buffer, buffer_len);
  
! 	return type_error("object can't be converted to int");
  }
  
--- 904,908 ----
  		return int_from_string((char*)buffer, buffer_len);
  
! 	return type_error("int() argument must be a string or a number");
  }
  
***************
*** 961,965 ****
  		return long_from_string(buffer, buffer_len);
  
! 	return type_error("object can't be converted to long");
  }
  
--- 961,965 ----
  		return long_from_string(buffer, buffer_len);
  
! 	return type_error("long() argument must be a string or a number");
  }