complex() was the only numeric constructor that created a new instance

when given its own type as an argument.
This commit is contained in:
Raymond Hettinger 2002-08-29 14:22:51 +00:00
parent 18bd11205d
commit 604cd6ae79
2 changed files with 13 additions and 0 deletions

View file

@ -823,6 +823,15 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
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,