blob: 8caa910d037dae3859ce6ba0b5cbeba6023f994e [file] [log] [blame]
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(complex_new__doc__,
6"complex(real=0, imag=0)\n"
7"--\n"
8"\n"
9"Create a complex number from a real part and an optional imaginary part.\n"
10"\n"
11"This is equivalent to (real + imag*1j) where imag defaults to 0.");
12
13static PyObject *
14complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i);
15
16static PyObject *
17complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
18{
19 PyObject *return_value = NULL;
20 static const char * const _keywords[] = {"real", "imag", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020021 static _PyArg_Parser _parser = {NULL, _keywords, "complex", 0};
22 PyObject *argsbuf[2];
23 PyObject * const *fastargs;
24 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
25 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
Serhiy Storchakabae68812017-04-05 12:00:42 +030026 PyObject *r = _PyLong_Zero;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +020027 PyObject *i = NULL;
28
Serhiy Storchaka31913912019-03-14 10:32:22 +020029 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf);
30 if (!fastargs) {
Serhiy Storchaka18b250f2017-03-19 08:51:07 +020031 goto exit;
32 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020033 if (!noptargs) {
34 goto skip_optional_pos;
35 }
36 if (fastargs[0]) {
37 r = fastargs[0];
38 if (!--noptargs) {
39 goto skip_optional_pos;
40 }
41 }
42 i = fastargs[1];
43skip_optional_pos:
Serhiy Storchaka18b250f2017-03-19 08:51:07 +020044 return_value = complex_new_impl(type, r, i);
45
46exit:
47 return return_value;
48}
Serhiy Storchaka31913912019-03-14 10:32:22 +020049/*[clinic end generated code: output=a0fe23fdbdc9b06b input=a9049054013a1b77]*/