Py_complex PyComplex_AsCComplex(PyObject *op) { Py_complex cv; PyObject *newop = NULL; assert(op); /* If op is already of type PyComplex_Type, return its value */ if (PyComplex_Check(op)) { return ((PyComplexObject *)op)->cval; } /* If not, use op's __complex__ method, if it exists */ /* return -1 on failure */ cv.real = -1.; cv.imag = 0.; newop = try_complex_special_method(op); if (newop) { cv = ((PyComplexObject *)newop)->cval; Py_DECREF(newop); return cv; } else if (PyErr_Occurred()) { return cv; } /* If neither of the above works, interpret op as a float giving the real part of the result, and fill in the imaginary part as 0. */ else { /* PyFloat_AsDouble will return -1 on failure */ cv.real = PyFloat_AsDouble(op); return cv; } }
static PyObject * complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i) /*[clinic end generated code: output=b6c7dd577b537dc1 input=6f6b0bedba29bcb5]*/ { PyObject *tmp; PyNumberMethods *nbr, *nbi = NULL; Py_complex cr, ci; int own_r = 0; int cr_is_complex = 0; int ci_is_complex = 0; /* Special-case for a single argument when type(arg) is complex. */ if (PyComplex_CheckExact(r) && i == NULL && type == &PyComplex_Type) { /* 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. If either the input or the output is a complex subclass, it will be handled below as a non-orthogonal vector. */ Py_INCREF(r); return r; } if (PyUnicode_Check(r)) { if (i != NULL) { PyErr_SetString(PyExc_TypeError, "complex() can't take second arg" " if first is a string"); return NULL; } return complex_subtype_from_string(type, r); } if (i != NULL && PyUnicode_Check(i)) { PyErr_SetString(PyExc_TypeError, "complex() second arg can't be a string"); return NULL; } tmp = try_complex_special_method(r); if (tmp) { r = tmp; own_r = 1; } else if (PyErr_Occurred()) { return NULL; } nbr = r->ob_type->tp_as_number; if (nbr == NULL || nbr->nb_float == NULL) { PyErr_Format(PyExc_TypeError, "complex() first argument must be a string or a number, " "not '%.200s'", Py_TYPE(r)->tp_name); if (own_r) { Py_DECREF(r); } return NULL; } if (i != NULL) { nbi = i->ob_type->tp_as_number; if (nbi == NULL || nbi->nb_float == NULL) { PyErr_Format(PyExc_TypeError, "complex() second argument must be a number, " "not '%.200s'", Py_TYPE(i)->tp_name); if (own_r) { Py_DECREF(r); } return NULL; } } /* If we get this far, then the "real" and "imag" parts should both be treated as numbers, and the constructor should return a complex number equal to (real + imag*1j). Note that we do NOT assume the input to already be in canonical form; the "real" and "imag" parts might themselves be complex numbers, which slightly complicates the code below. */ 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; cr_is_complex = 1; if (own_r) { Py_DECREF(r); } } else { /* The "real" part really is entirely real, and contributes nothing in the imaginary direction. Just treat it as a double. */ tmp = PyNumber_Float(r); if (own_r) { /* r was a newly created complex number, rather than the original "real" argument. */ Py_DECREF(r); } if (tmp == NULL) return NULL; assert(PyFloat_Check(tmp)); cr.real = PyFloat_AsDouble(tmp); cr.imag = 0.0; Py_DECREF(tmp); } if (i == NULL) { ci.real = cr.imag; } else if (PyComplex_Check(i)) { ci = ((PyComplexObject*)i)->cval; ci_is_complex = 1; } else { /* The "imag" part really is entirely imaginary, and contributes nothing in the real direction. Just treat it as a double. */ tmp = (*nbi->nb_float)(i); if (tmp == NULL) return NULL; ci.real = PyFloat_AsDouble(tmp); Py_DECREF(tmp); } /* If the input was in canonical form, then the "real" and "imag" parts are real numbers, so that ci.imag and cr.imag are zero. We need this correction in case they were not real numbers. */ if (ci_is_complex) { cr.real -= ci.imag; } if (cr_is_complex && i != NULL) { ci.real += cr.imag; } return complex_subtype_from_doubles(type, cr.real, ci.real); }
static PyObject * complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *r, *i, *tmp; PyNumberMethods *nbr, *nbi = NULL; Py_complex cr, ci; int own_r = 0; int cr_is_complex = 0; int ci_is_complex = 0; static char *kwlist[] = {"real", "imag", 0}; r = Py_False; i = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, &r, &i)) return NULL; /* Special-case for a single argument when type(arg) is complex. */ if (PyComplex_CheckExact(r) && i == NULL && type == &PyComplex_Type) { /* 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. If either the input or the output is a complex subclass, it will be handled below as a non-orthogonal vector. */ Py_INCREF(r); return r; } if (PyString_Check(r) || PyUnicode_Check(r)) { if (i != NULL) { PyErr_SetString(PyExc_TypeError, "complex() can't take second arg" " if first is a string"); return NULL; } return complex_subtype_from_string(type, r); } if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) { PyErr_SetString(PyExc_TypeError, "complex() second arg can't be a string"); return NULL; } tmp = try_complex_special_method(r); if (tmp) { r = tmp; own_r = 1; } else if (PyErr_Occurred()) { return 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() argument must be a string or a number"); if (own_r) { Py_DECREF(r); } return NULL; } /* If we get this far, then the "real" and "imag" parts should both be treated as numbers, and the constructor should return a complex number equal to (real + imag*1j). Note that we do NOT assume the input to already be in canonical form; the "real" and "imag" parts might themselves be complex numbers, which slightly complicates the code below. */ 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; cr_is_complex = 1; if (own_r) { Py_DECREF(r); } } else { /* The "real" part really is entirely real, and contributes nothing in the imaginary direction. Just treat it as a double. */ tmp = PyNumber_Float(r); if (own_r) { /* r was a newly created complex number, rather than the original "real" argument. */ Py_DECREF(r); } if (tmp == NULL) return NULL; if (!PyFloat_Check(tmp)) { PyErr_SetString(PyExc_TypeError, "float(r) didn't return a float"); Py_DECREF(tmp); return NULL; } cr.real = PyFloat_AsDouble(tmp); cr.imag = 0.0; /* Shut up compiler warning */ Py_DECREF(tmp); } if (i == NULL) { ci.real = 0.0; } else if (PyComplex_Check(i)) { ci = ((PyComplexObject*)i)->cval; ci_is_complex = 1; } else { /* The "imag" part really is entirely imaginary, and contributes nothing in the real direction. Just treat it as a double. */ tmp = (*nbi->nb_float)(i); if (tmp == NULL) return NULL; ci.real = PyFloat_AsDouble(tmp); Py_DECREF(tmp); } /* If the input was in canonical form, then the "real" and "imag" parts are real numbers, so that ci.imag and cr.imag are zero. We need this correction in case they were not real numbers. */ if (ci_is_complex) { cr.real -= ci.imag; } if (cr_is_complex) { ci.real += cr.imag; } return complex_subtype_from_doubles(type, cr.real, ci.real); }