diff Objects/complexobject.c @ 19889:733314fcfdaa legacy-trunk

Again perhaps the end of [#460020] bug or feature: unicode() and subclasses. Inhibited complex unary plus optimization when applied to a complex subtype. Added PyComplex_CheckExact macro. Some comments and minor code fiddling.
author Tim Peters <tim.peters@gmail.com>
date Wed, 12 Sep 2001 19:12:49 +0000
parents 64bcef1702ad
children 8edc44bd0ac7
line wrap: on
line diff
--- a/Objects/complexobject.c	Wed Sep 12 18:59:25 2001 +0000
+++ b/Objects/complexobject.c	Wed Sep 12 19:12:49 2001 +0000
@@ -489,8 +489,12 @@
 static PyObject *
 complex_pos(PyComplexObject *v)
 {
-	Py_INCREF(v);
-	return (PyObject *)v;
+	if (PyComplex_CheckExact(v)) {
+		Py_INCREF(v);
+		return (PyObject *)v;
+	}
+	else
+		return PyComplex_FromCComplex(v->cval);
 }
 
 static PyObject *
@@ -792,11 +796,12 @@
 		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 ||
-	    (i != NULL &&
-	     ((nbi = i->ob_type->tp_as_number) == NULL ||
-	      nbi->nb_float == NULL))) {
+
+	nbr = r->ob_type->tp_as_number;
+	if (i != NULL)
+		nbi = i->ob_type->tp_as_number;
+	if (nbr == NULL || nbr->nb_float == NULL ||
+	    ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
 		PyErr_SetString(PyExc_TypeError,
 			   "complex() arg can't be converted to complex");
 		return NULL;
@@ -826,6 +831,9 @@
 		}
 	}
 	if (PyComplex_Check(r)) {
+		/* Note that if r is of a complex subtype, we're only
+		   retaining its real & imag parts here, and the return
+		   value is (properly) of the builtin complex type. */
 		cr = ((PyComplexObject*)r)->cval;
 		if (own_r) {
 			Py_DECREF(r);
@@ -868,10 +876,10 @@
 }
 
 static char complex_doc[] =
-"complex(real[, imag]) -> complex number\n\
-\n\
-Create a complex number from a real part and an optional imaginary part.\n\
-This is equivalent to (real + imag*1j) where imag defaults to 0.";
+"complex(real[, imag]) -> complex number\n"
+"\n"
+"Create a complex number from a real part and an optional imaginary part.\n"
+"This is equivalent to (real + imag*1j) where imag defaults to 0.";
 
 static PyNumberMethods complex_as_number = {
 	(binaryfunc)complex_add, 		/* nb_add */