summaryrefslogtreecommitdiffstats
path: root/Objects/complexobject.c
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2021-08-23 08:15:49 (GMT)
committerGitHub <noreply@github.com>2021-08-23 08:15:49 (GMT)
commit6082bb5addab93755ab6e2bd2ed6021b391e10d1 (patch)
treeef109d492632072d919a7ebeb2ce89342a2babdb /Objects/complexobject.c
parenteec340ea3af27887fcaac4029ebdee99f3713bff (diff)
downloadcpython-6082bb5addab93755ab6e2bd2ed6021b391e10d1.zip
cpython-6082bb5addab93755ab6e2bd2ed6021b391e10d1.tar.gz
cpython-6082bb5addab93755ab6e2bd2ed6021b391e10d1.tar.bz2
bpo-24234: implement complex.__complex__ (GH-27887)
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
Diffstat (limited to 'Objects/complexobject.c')
-rw-r--r--Objects/complexobject.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 3e47949..cfe6c73 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -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 */