Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Objects/clinic/complexobject.c.h
12 views
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
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(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 *
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 *
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 *
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 real part and an optional imaginary part.\n"
98
"\n"
99
"This is equivalent to (real + imag*1j) where imag defaults to 0.");
100
101
static PyObject *
102
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i);
103
104
static PyObject *
105
complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
106
{
107
PyObject *return_value = NULL;
108
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
109
110
#define NUM_KEYWORDS 2
111
static struct {
112
PyGC_Head _this_is_not_used;
113
PyObject_VAR_HEAD
114
PyObject *ob_item[NUM_KEYWORDS];
115
} _kwtuple = {
116
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
117
.ob_item = { &_Py_ID(real), &_Py_ID(imag), },
118
};
119
#undef NUM_KEYWORDS
120
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
121
122
#else // !Py_BUILD_CORE
123
# define KWTUPLE NULL
124
#endif // !Py_BUILD_CORE
125
126
static const char * const _keywords[] = {"real", "imag", NULL};
127
static _PyArg_Parser _parser = {
128
.keywords = _keywords,
129
.fname = "complex",
130
.kwtuple = KWTUPLE,
131
};
132
#undef KWTUPLE
133
PyObject *argsbuf[2];
134
PyObject * const *fastargs;
135
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
136
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
137
PyObject *r = NULL;
138
PyObject *i = NULL;
139
140
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
141
if (!fastargs) {
142
goto exit;
143
}
144
if (!noptargs) {
145
goto skip_optional_pos;
146
}
147
if (fastargs[0]) {
148
r = fastargs[0];
149
if (!--noptargs) {
150
goto skip_optional_pos;
151
}
152
}
153
i = fastargs[1];
154
skip_optional_pos:
155
return_value = complex_new_impl(type, r, i);
156
157
exit:
158
return return_value;
159
}
160
/*[clinic end generated code: output=d438b7ed87f8459e input=a9049054013a1b77]*/
161
162