[Python-checkins] CVS: python/dist/src/Objects complexobject.c,2.35.4.4,2.35.4.5

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 18 Jun 2001 14:33:19 -0700


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

Modified Files:
      Tag: descr-branch
	complexobject.c 
Log Message:
Make complex usable as a base type.  Wasn't so hard. :-)


Index: complexobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/complexobject.c,v
retrieving revision 2.35.4.4
retrieving revision 2.35.4.5
diff -C2 -r2.35.4.4 -r2.35.4.5
*** complexobject.c	2001/06/14 18:12:02	2.35.4.4
--- complexobject.c	2001/06/18 21:33:17	2.35.4.5
***************
*** 184,187 ****
--- 184,198 ----
  }
  
+ static PyObject *
+ complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
+ {
+ 	PyObject *op;
+ 
+ 	op = PyType_GenericAlloc(type, 0);
+ 	if (op != NULL)
+ 		((PyComplexObject *)op)->cval = cval;
+ 	return op;
+ }
+ 
  PyObject *
  PyComplex_FromCComplex(Py_complex cval)
***************
*** 198,201 ****
--- 209,221 ----
  }
  
+ static PyObject *
+ complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
+ {
+ 	Py_complex c;
+ 	c.real = real;
+ 	c.imag = imag;
+ 	return complex_subtype_from_c_complex(type, c);
+ }
+ 
  PyObject *
  PyComplex_FromDoubles(double real, double imag)
***************
*** 568,572 ****
  
  static PyObject *
! complex_from_string(PyObject *v)
  {
  	extern double strtod(const char *, char **);
--- 588,592 ----
  
  static PyObject *
! complex_subtype_from_string(PyTypeObject *type, PyObject *v)
  {
  	extern double strtod(const char *, char **);
***************
*** 716,720 ****
  	}
  
! 	return PyComplex_FromDoubles(x,y);
  }
  
--- 736,740 ----
  	}
  
! 	return complex_subtype_from_doubles(type, x, y);
  }
  
***************
*** 728,732 ****
  	static char *kwlist[] = {"real", "imag", 0};
  
- 	assert(type == &PyComplex_Type);
  	r = Py_False;
  	i = NULL;
--- 748,751 ----
***************
*** 735,739 ****
  		return NULL;
  	if (PyString_Check(r) || PyUnicode_Check(r))
! 		return complex_from_string(r);
  	if ((nbr = r->ob_type->tp_as_number) == NULL ||
  	    nbr->nb_float == NULL ||
--- 754,758 ----
  		return NULL;
  	if (PyString_Check(r) || PyUnicode_Check(r))
! 		return complex_subtype_from_string(type, r);
  	if ((nbr = r->ob_type->tp_as_number) == NULL ||
  	    nbr->nb_float == NULL ||
***************
*** 808,812 ****
  	cr.real -= ci.imag;
  	cr.imag += ci.real;
! 	return PyComplex_FromCComplex(cr);
  }
  
--- 827,831 ----
  	cr.real -= ci.imag;
  	cr.imag += ci.real;
! 	return complex_subtype_from_c_complex(type, cr);
  }
  
***************
*** 864,868 ****
  	0,					/* tp_setattro */
  	0,					/* tp_as_buffer */
! 	Py_TPFLAGS_DEFAULT,			/* tp_flags */
  	complex_doc,				/* tp_doc */
  	0,					/* tp_traverse */
--- 883,887 ----
  	0,					/* tp_setattro */
  	0,					/* tp_as_buffer */
! 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  	complex_doc,				/* tp_doc */
  	0,					/* tp_traverse */