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 *
complex_conjugate(PyComplexObject * self,PyObject * Py_UNUSED (ignored))24 complex_conjugate(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
25 {
26 return complex_conjugate_impl(self);
27 }
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 *
complex___getnewargs__(PyComplexObject * self,PyObject * Py_UNUSED (ignored))41 complex___getnewargs__(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
42 {
43 return complex___getnewargs___impl(self);
44 }
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 *
complex___format__(PyComplexObject * self,PyObject * arg)59 complex___format__(PyComplexObject *self, PyObject *arg)
60 {
61 PyObject *return_value = NULL;
62 PyObject *format_spec;
63
64 if (!PyUnicode_Check(arg)) {
65 _PyArg_BadArgument("__format__", "argument", "str", arg);
66 goto exit;
67 }
68 format_spec = arg;
69 return_value = complex___format___impl(self, format_spec);
70
71 exit:
72 return return_value;
73 }
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 *
complex___complex__(PyComplexObject * self,PyObject * Py_UNUSED (ignored))88 complex___complex__(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
89 {
90 return complex___complex___impl(self);
91 }
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 *r, PyObject *i);
106
107 static PyObject *
complex_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)108 complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
109 {
110 PyObject *return_value = NULL;
111 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
112
113 #define NUM_KEYWORDS 2
114 static struct {
115 PyGC_Head _this_is_not_used;
116 PyObject_VAR_HEAD
117 PyObject *ob_item[NUM_KEYWORDS];
118 } _kwtuple = {
119 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
120 .ob_item = { &_Py_ID(real), &_Py_ID(imag), },
121 };
122 #undef NUM_KEYWORDS
123 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
124
125 #else // !Py_BUILD_CORE
126 # define KWTUPLE NULL
127 #endif // !Py_BUILD_CORE
128
129 static const char * const _keywords[] = {"real", "imag", NULL};
130 static _PyArg_Parser _parser = {
131 .keywords = _keywords,
132 .fname = "complex",
133 .kwtuple = KWTUPLE,
134 };
135 #undef KWTUPLE
136 PyObject *argsbuf[2];
137 PyObject * const *fastargs;
138 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
139 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
140 PyObject *r = NULL;
141 PyObject *i = NULL;
142
143 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
144 if (!fastargs) {
145 goto exit;
146 }
147 if (!noptargs) {
148 goto skip_optional_pos;
149 }
150 if (fastargs[0]) {
151 r = fastargs[0];
152 if (!--noptargs) {
153 goto skip_optional_pos;
154 }
155 }
156 i = fastargs[1];
157 skip_optional_pos:
158 return_value = complex_new_impl(type, r, i);
159
160 exit:
161 return return_value;
162 }
163 /*[clinic end generated code: output=295ecfd71389d7fe input=a9049054013a1b77]*/
164