/src/cpython/Objects/clinic/complexobject.c.h
Line | Count | Source |
1 | | /*[clinic input] |
2 | | preserve |
3 | | [clinic start generated code]*/ |
4 | | |
5 | | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
6 | | # include "pycore_gc.h" // PyGC_Head |
7 | | # include "pycore_runtime.h" // _Py_ID() |
8 | | #endif |
9 | | #include "pycore_modsupport.h" // _PyArg_BadArgument() |
10 | | |
11 | | PyDoc_STRVAR(complex_conjugate__doc__, |
12 | | "conjugate($self, /)\n" |
13 | | "--\n" |
14 | | "\n" |
15 | | "Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j."); |
16 | | |
17 | | #define COMPLEX_CONJUGATE_METHODDEF \ |
18 | | {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, complex_conjugate__doc__}, |
19 | | |
20 | | static PyObject * |
21 | | complex_conjugate_impl(PyComplexObject *self); |
22 | | |
23 | | static PyObject * |
24 | | complex_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored)) |
25 | 0 | { |
26 | 0 | return complex_conjugate_impl((PyComplexObject *)self); |
27 | 0 | } |
28 | | |
29 | | PyDoc_STRVAR(complex___getnewargs____doc__, |
30 | | "__getnewargs__($self, /)\n" |
31 | | "--\n" |
32 | | "\n"); |
33 | | |
34 | | #define COMPLEX___GETNEWARGS___METHODDEF \ |
35 | | {"__getnewargs__", (PyCFunction)complex___getnewargs__, METH_NOARGS, complex___getnewargs____doc__}, |
36 | | |
37 | | static PyObject * |
38 | | complex___getnewargs___impl(PyComplexObject *self); |
39 | | |
40 | | static PyObject * |
41 | | complex___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
42 | 0 | { |
43 | 0 | return complex___getnewargs___impl((PyComplexObject *)self); |
44 | 0 | } |
45 | | |
46 | | PyDoc_STRVAR(complex___format____doc__, |
47 | | "__format__($self, format_spec, /)\n" |
48 | | "--\n" |
49 | | "\n" |
50 | | "Convert to a string according to format_spec."); |
51 | | |
52 | | #define COMPLEX___FORMAT___METHODDEF \ |
53 | | {"__format__", (PyCFunction)complex___format__, METH_O, complex___format____doc__}, |
54 | | |
55 | | static PyObject * |
56 | | complex___format___impl(PyComplexObject *self, PyObject *format_spec); |
57 | | |
58 | | static PyObject * |
59 | | complex___format__(PyObject *self, PyObject *arg) |
60 | 0 | { |
61 | 0 | PyObject *return_value = NULL; |
62 | 0 | PyObject *format_spec; |
63 | |
|
64 | 0 | if (!PyUnicode_Check(arg)) { |
65 | 0 | _PyArg_BadArgument("__format__", "argument", "str", arg); |
66 | 0 | goto exit; |
67 | 0 | } |
68 | 0 | format_spec = arg; |
69 | 0 | return_value = complex___format___impl((PyComplexObject *)self, format_spec); |
70 | |
|
71 | 0 | exit: |
72 | 0 | return return_value; |
73 | 0 | } |
74 | | |
75 | | PyDoc_STRVAR(complex___complex____doc__, |
76 | | "__complex__($self, /)\n" |
77 | | "--\n" |
78 | | "\n" |
79 | | "Convert this value to exact type complex."); |
80 | | |
81 | | #define COMPLEX___COMPLEX___METHODDEF \ |
82 | | {"__complex__", (PyCFunction)complex___complex__, METH_NOARGS, complex___complex____doc__}, |
83 | | |
84 | | static PyObject * |
85 | | complex___complex___impl(PyComplexObject *self); |
86 | | |
87 | | static PyObject * |
88 | | complex___complex__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
89 | 0 | { |
90 | 0 | return complex___complex___impl((PyComplexObject *)self); |
91 | 0 | } |
92 | | |
93 | | PyDoc_STRVAR(complex_new__doc__, |
94 | | "complex(real=0, imag=0)\n" |
95 | | "--\n" |
96 | | "\n" |
97 | | "Create a complex number from a string or numbers.\n" |
98 | | "\n" |
99 | | "If a string is given, parse it as a complex number.\n" |
100 | | "If a single number is given, convert it to a complex number.\n" |
101 | | "If the \'real\' or \'imag\' arguments are given, create a complex number\n" |
102 | | "with the specified real and imaginary components."); |
103 | | |
104 | | static PyObject * |
105 | | complex_new_impl(PyTypeObject *type, PyObject *real_pos, PyObject *imag_pos, |
106 | | PyObject *r, PyObject *i); |
107 | | |
108 | | static PyObject * |
109 | | complex_new_helper(PyTypeObject *type, PyObject *const *args, |
110 | | Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) |
111 | 0 | { |
112 | 0 | PyObject *return_value = NULL; |
113 | 0 | #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
114 | |
|
115 | 0 | #define NUM_KEYWORDS 2 |
116 | 0 | static struct { |
117 | 0 | PyGC_Head _this_is_not_used; |
118 | 0 | PyObject_VAR_HEAD |
119 | 0 | Py_hash_t ob_hash; |
120 | 0 | PyObject *ob_item[NUM_KEYWORDS]; |
121 | 0 | } _kwtuple = { |
122 | 0 | .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
123 | 0 | .ob_hash = -1, |
124 | 0 | .ob_item = { &_Py_ID(real), &_Py_ID(imag), }, |
125 | 0 | }; |
126 | 0 | #undef NUM_KEYWORDS |
127 | 0 | #define KWTUPLE (&_kwtuple.ob_base.ob_base) |
128 | |
|
129 | | #else // !Py_BUILD_CORE |
130 | | # define KWTUPLE NULL |
131 | | #endif // !Py_BUILD_CORE |
132 | |
|
133 | 0 | static const char * const _keywords[] = {"", "", "real", "imag", NULL}; |
134 | 0 | static _PyArg_Parser _parser = { |
135 | 0 | .keywords = _keywords, |
136 | 0 | .fname = "complex", |
137 | 0 | .kwtuple = KWTUPLE, |
138 | 0 | }; |
139 | 0 | #undef KWTUPLE |
140 | 0 | PyObject *argsbuf[4]; |
141 | 0 | PyObject * const *fastargs; |
142 | 0 | Py_ssize_t noptargs = nargs + nkw - 0; |
143 | 0 | PyObject *real_pos = NULL; |
144 | 0 | PyObject *imag_pos = NULL; |
145 | 0 | PyObject *r = NULL; |
146 | 0 | PyObject *i = NULL; |
147 | |
|
148 | 0 | fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, |
149 | 0 | /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
150 | 0 | if (!fastargs) { |
151 | 0 | goto exit; |
152 | 0 | } |
153 | 0 | if (nargs < 1) { |
154 | 0 | goto skip_optional_posonly; |
155 | 0 | } |
156 | 0 | noptargs--; |
157 | 0 | real_pos = fastargs[0]; |
158 | 0 | if (nargs < 2) { |
159 | 0 | goto skip_optional_posonly; |
160 | 0 | } |
161 | 0 | noptargs--; |
162 | 0 | imag_pos = fastargs[1]; |
163 | 0 | skip_optional_posonly: |
164 | 0 | if (!noptargs) { |
165 | 0 | goto skip_optional_kwonly; |
166 | 0 | } |
167 | 0 | if (fastargs[2]) { |
168 | 0 | r = fastargs[2]; |
169 | 0 | if (!--noptargs) { |
170 | 0 | goto skip_optional_kwonly; |
171 | 0 | } |
172 | 0 | } |
173 | 0 | i = fastargs[3]; |
174 | 0 | skip_optional_kwonly: |
175 | 0 | return_value = complex_new_impl(type, real_pos, imag_pos, r, i); |
176 | |
|
177 | 0 | exit: |
178 | 0 | return return_value; |
179 | 0 | } |
180 | | |
181 | | static PyObject * |
182 | | complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
183 | 0 | { |
184 | 0 | return complex_new_helper(type, _PyTuple_CAST(args)->ob_item, |
185 | 0 | PyTuple_GET_SIZE(args), |
186 | 0 | kwargs ? PyDict_GET_SIZE(kwargs) : 0, |
187 | 0 | kwargs, NULL); |
188 | 0 | } |
189 | | |
190 | | static PyObject * |
191 | | complex_vectorcall(PyObject *type, PyObject *const *args, |
192 | | size_t nargsf, PyObject *kwnames) |
193 | 0 | { |
194 | 0 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
195 | |
|
196 | 0 | assert(_PyType_CAST(type)->tp_new == complex_new); |
197 | | /* Make sure the type object is immutable: the generated |
198 | | * vectorcall doesn't deal e.g. with users reassigning __init__. */ |
199 | 0 | assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); |
200 | 0 | return complex_new_helper(_PyType_CAST(type), args, nargs, |
201 | 0 | kwnames ? PyTuple_GET_SIZE(kwnames) : 0, |
202 | 0 | NULL, kwnames); |
203 | 0 | } |
204 | | |
205 | | PyDoc_STRVAR(complex_from_number__doc__, |
206 | | "from_number($type, number, /)\n" |
207 | | "--\n" |
208 | | "\n" |
209 | | "Convert number to a complex floating-point number."); |
210 | | |
211 | | #define COMPLEX_FROM_NUMBER_METHODDEF \ |
212 | | {"from_number", (PyCFunction)complex_from_number, METH_O|METH_CLASS, complex_from_number__doc__}, |
213 | | |
214 | | static PyObject * |
215 | | complex_from_number_impl(PyTypeObject *type, PyObject *number); |
216 | | |
217 | | static PyObject * |
218 | | complex_from_number(PyObject *type, PyObject *number) |
219 | 0 | { |
220 | 0 | PyObject *return_value = NULL; |
221 | |
|
222 | 0 | return_value = complex_from_number_impl((PyTypeObject *)type, number); |
223 | |
|
224 | 0 | return return_value; |
225 | 0 | } |
226 | | /*[clinic end generated code: output=35512ea9996f9935 input=a9049054013a1b77]*/ |