bpo-41343: Convert methods of complex to Argument Clinic (GH-21550)

This commit is contained in:
Dong-hee Na 2020-07-20 21:53:29 +09:00 committed by GitHub
parent eca2549f5a
commit e123012d79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 26 deletions

View File

@ -2,6 +2,73 @@
preserve preserve
[clinic start generated code]*/ [clinic start generated code]*/
PyDoc_STRVAR(complex_conjugate__doc__,
"conjugate($self, /)\n"
"--\n"
"\n"
"Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
#define COMPLEX_CONJUGATE_METHODDEF \
{"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, complex_conjugate__doc__},
static PyObject *
complex_conjugate_impl(PyComplexObject *self);
static PyObject *
complex_conjugate(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
{
return complex_conjugate_impl(self);
}
PyDoc_STRVAR(complex___getnewargs____doc__,
"__getnewargs__($self, /)\n"
"--\n"
"\n");
#define COMPLEX___GETNEWARGS___METHODDEF \
{"__getnewargs__", (PyCFunction)complex___getnewargs__, METH_NOARGS, complex___getnewargs____doc__},
static PyObject *
complex___getnewargs___impl(PyComplexObject *self);
static PyObject *
complex___getnewargs__(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
{
return complex___getnewargs___impl(self);
}
PyDoc_STRVAR(complex___format____doc__,
"__format__($self, format_spec, /)\n"
"--\n"
"\n"
"Convert to a string according to format_spec.");
#define COMPLEX___FORMAT___METHODDEF \
{"__format__", (PyCFunction)complex___format__, METH_O, complex___format____doc__},
static PyObject *
complex___format___impl(PyComplexObject *self, PyObject *format_spec);
static PyObject *
complex___format__(PyComplexObject *self, PyObject *arg)
{
PyObject *return_value = NULL;
PyObject *format_spec;
if (!PyUnicode_Check(arg)) {
_PyArg_BadArgument("__format__", "argument", "str", arg);
goto exit;
}
if (PyUnicode_READY(arg) == -1) {
goto exit;
}
format_spec = arg;
return_value = complex___format___impl(self, format_spec);
exit:
return return_value;
}
PyDoc_STRVAR(complex_new__doc__, PyDoc_STRVAR(complex_new__doc__,
"complex(real=0, imag=0)\n" "complex(real=0, imag=0)\n"
"--\n" "--\n"
@ -46,4 +113,4 @@ skip_optional_pos:
exit: exit:
return return_value; return return_value;
} }
/*[clinic end generated code: output=a0fe23fdbdc9b06b input=a9049054013a1b77]*/ /*[clinic end generated code: output=193a37aebaaa5f89 input=a9049054013a1b77]*/

View File

@ -684,46 +684,54 @@ complex_float(PyObject *v)
return NULL; return NULL;
} }
/*[clinic input]
complex.conjugate
Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.
[clinic start generated code]*/
static PyObject * static PyObject *
complex_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored)) complex_conjugate_impl(PyComplexObject *self)
/*[clinic end generated code: output=5059ef162edfc68e input=5fea33e9747ec2c4]*/
{ {
Py_complex c; Py_complex c = self->cval;
c = ((PyComplexObject *)self)->cval;
c.imag = -c.imag; c.imag = -c.imag;
return PyComplex_FromCComplex(c); return PyComplex_FromCComplex(c);
} }
PyDoc_STRVAR(complex_conjugate_doc, /*[clinic input]
"complex.conjugate() -> complex\n" complex.__getnewargs__
"\n"
"Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j."); [clinic start generated code]*/
static PyObject * static PyObject *
complex_getnewargs(PyComplexObject *v, PyObject *Py_UNUSED(ignored)) complex___getnewargs___impl(PyComplexObject *self)
/*[clinic end generated code: output=689b8206e8728934 input=539543e0a50533d7]*/
{ {
Py_complex c = v->cval; Py_complex c = self->cval;
return Py_BuildValue("(dd)", c.real, c.imag); return Py_BuildValue("(dd)", c.real, c.imag);
} }
PyDoc_STRVAR(complex__format__doc,
"complex.__format__() -> str\n" /*[clinic input]
"\n" complex.__format__
"Convert to a string according to format_spec.");
format_spec: unicode
/
Convert to a string according to format_spec.
[clinic start generated code]*/
static PyObject * static PyObject *
complex__format__(PyObject* self, PyObject* args) complex___format___impl(PyComplexObject *self, PyObject *format_spec)
/*[clinic end generated code: output=bfcb60df24cafea0 input=014ef5488acbe1d5]*/
{ {
PyObject *format_spec;
_PyUnicodeWriter writer; _PyUnicodeWriter writer;
int ret; int ret;
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
return NULL;
_PyUnicodeWriter_Init(&writer); _PyUnicodeWriter_Init(&writer);
ret = _PyComplex_FormatAdvancedWriter( ret = _PyComplex_FormatAdvancedWriter(
&writer, &writer,
self, (PyObject *)self,
format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
if (ret == -1) { if (ret == -1) {
_PyUnicodeWriter_Dealloc(&writer); _PyUnicodeWriter_Dealloc(&writer);
@ -733,11 +741,9 @@ complex__format__(PyObject* self, PyObject* args)
} }
static PyMethodDef complex_methods[] = { static PyMethodDef complex_methods[] = {
{"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, COMPLEX_CONJUGATE_METHODDEF
complex_conjugate_doc}, COMPLEX___GETNEWARGS___METHODDEF
{"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS}, COMPLEX___FORMAT___METHODDEF
{"__format__", (PyCFunction)complex__format__,
METH_VARARGS, complex__format__doc},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };