diff options
author | Mark Dickinson <mdickinson@enthought.com> | 2021-08-23 08:15:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-23 08:15:49 (GMT) |
commit | 6082bb5addab93755ab6e2bd2ed6021b391e10d1 (patch) | |
tree | ef109d492632072d919a7ebeb2ce89342a2babdb /Objects | |
parent | eec340ea3af27887fcaac4029ebdee99f3713bff (diff) | |
download | cpython-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')
-rw-r--r-- | Objects/clinic/complexobject.c.h | 20 | ||||
-rw-r--r-- | Objects/complexobject.c | 21 |
2 files changed, 40 insertions, 1 deletions
diff --git a/Objects/clinic/complexobject.c.h b/Objects/clinic/complexobject.c.h index 557fbf9..e7d8065 100644 --- a/Objects/clinic/complexobject.c.h +++ b/Objects/clinic/complexobject.c.h @@ -69,6 +69,24 @@ exit: return return_value; } +PyDoc_STRVAR(complex___complex____doc__, +"__complex__($self, /)\n" +"--\n" +"\n" +"Convert this value to exact type complex."); + +#define COMPLEX___COMPLEX___METHODDEF \ + {"__complex__", (PyCFunction)complex___complex__, METH_NOARGS, complex___complex____doc__}, + +static PyObject * +complex___complex___impl(PyComplexObject *self); + +static PyObject * +complex___complex__(PyComplexObject *self, PyObject *Py_UNUSED(ignored)) +{ + return complex___complex___impl(self); +} + PyDoc_STRVAR(complex_new__doc__, "complex(real=0, imag=0)\n" "--\n" @@ -113,4 +131,4 @@ skip_optional_pos: exit: return return_value; } -/*[clinic end generated code: output=056cac3226d94967 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=6d85094ace15677e input=a9049054013a1b77]*/ 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 */ |