bpo-24234: implement complex.__complex__ (GH-27887)

Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
This commit is contained in:
Mark Dickinson 2021-08-23 09:15:49 +01:00 committed by GitHub
parent eec340ea3a
commit 6082bb5add
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 3 deletions

View file

@ -693,8 +693,29 @@ complex___format___impl(PyComplexObject *self, PyObject *format_spec)
return _PyUnicodeWriter_Finish(&writer);
}
/*[clinic input]
complex.__complex__
Convert this value to exact type complex.
[clinic start generated code]*/
static PyObject *
complex___complex___impl(PyComplexObject *self)
/*[clinic end generated code: output=e6b35ba3d275dc9c input=3589ada9d27db854]*/
{
if (PyComplex_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
}
else {
return PyComplex_FromCComplex(self->cval);
}
}
static PyMethodDef complex_methods[] = {
COMPLEX_CONJUGATE_METHODDEF
COMPLEX___COMPLEX___METHODDEF
COMPLEX___GETNEWARGS___METHODDEF
COMPLEX___FORMAT___METHODDEF
{NULL, NULL} /* sentinel */