diff Objects/complexobject.c @ 25064:81a36d2a2d13 legacy-trunk

complex() was the only numeric constructor that created a new instance when given its own type as an argument.
author Raymond Hettinger <python@rcn.com>
date Thu, 29 Aug 2002 14:22:51 +0000
parents a869d2de536f
children 17bfd5dbc0f1
line wrap: on
line diff
--- a/Objects/complexobject.c	Thu Aug 29 13:09:47 2002 +0000
+++ b/Objects/complexobject.c	Thu Aug 29 14:22:51 2002 +0000
@@ -823,6 +823,15 @@
 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
 					 &r, &i))
 		return NULL;
+
+	/* Special-case for single argumet that is already complex */
+	if (PyComplex_CheckExact(r) && i == NULL) {
+		/* Note that we can't know whether it's safe to return
+		   a complex *subclass* instance as-is, hence the restriction
+		   to exact complexes here.  */
+		Py_INCREF(r);
+		return r;
+	}
 	if (PyString_Check(r) || PyUnicode_Check(r)) {
 		if (i != NULL) {
 			PyErr_SetString(PyExc_TypeError,