[Python-checkins] python/dist/src/Objects complexobject.c,2.53.4.5,2.53.4.6

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Sat, 05 Oct 2002 13:43:47 -0700


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

Modified Files:
      Tag: release22-maint
	complexobject.c 
Log Message:
Backport 2.63 and 2.60:

Call me anal, but there was a particular phrase that was speading to
comments everywhere that bugged me: /* Foo is inlined */ instead of
/* Inline Foo */.  Somehow the "is inlined" phrase always confused me
for half a second (thinking, "No it isn't" until I added the missing
"here").  The new phrase is hopefully unambiguous.

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.53.4.5
retrieving revision 2.53.4.6
diff -C2 -d -r2.53.4.5 -r2.53.4.6
*** complexobject.c	18 Apr 2002 05:39:54 -0000	2.53.4.5
--- complexobject.c	5 Oct 2002 20:43:43 -0000	2.53.4.6
***************
*** 200,204 ****
  	register PyComplexObject *op;
  
! 	/* PyObject_New is inlined */
  	op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
  	if (op == NULL)
--- 200,204 ----
  	register PyComplexObject *op;
  
! 	/* Inline PyObject_New */
  	op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
  	if (op == NULL)
***************
*** 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)) {