diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index c8d348a48225981..61e4bff039192ef 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -609,6 +609,12 @@ def check(z, x, y): check(complex(imag=1.5), 0.0, 1.5) check(complex(real=4.25, imag=1.5), 4.25, 1.5) check(complex(4.25, imag=1.5), 4.25, 1.5) + self.assertRaisesRegex(TypeError, r"given by name \('real'\) and position \(1\)", + complex, 4.25, real=1.5) + self.assertRaisesRegex(TypeError, r"takes at most 2 arguments \(3 given\)", + complex, 4.25, 1.5, imag=1.5) + self.assertRaisesRegex(TypeError, r"takes at most 2 arguments \(3 given\)", + complex, 1, 2, real=3) # check that the sign of a zero in the real or imaginary part # is preserved when constructing from two floats. diff --git a/Objects/clinic/complexobject.c.h b/Objects/clinic/complexobject.c.h index c7303380de55f4b..fd29584c19c1b3f 100644 --- a/Objects/clinic/complexobject.c.h +++ b/Objects/clinic/complexobject.c.h @@ -102,10 +102,12 @@ PyDoc_STRVAR(complex_new__doc__, "with the specified real and imaginary components."); static PyObject * -complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i); +complex_new_impl(PyTypeObject *type, PyObject *real_pos, PyObject *imag_pos, + PyObject *r, PyObject *i); static PyObject * -complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +complex_new_helper(PyTypeObject *type, PyObject *const *args, + Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames) { PyObject *return_value = NULL; #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) @@ -128,42 +130,78 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) # define KWTUPLE NULL #endif // !Py_BUILD_CORE - static const char * const _keywords[] = {"real", "imag", NULL}; + static const char * const _keywords[] = {"", "", "real", "imag", NULL}; static _PyArg_Parser _parser = { .keywords = _keywords, .fname = "complex", .kwtuple = KWTUPLE, }; #undef KWTUPLE - PyObject *argsbuf[2]; + PyObject *argsbuf[4]; PyObject * const *fastargs; - Py_ssize_t nargs = PyTuple_GET_SIZE(args); - Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; + Py_ssize_t noptargs = nargs + nkw - 0; + PyObject *real_pos = NULL; + PyObject *imag_pos = NULL; PyObject *r = NULL; PyObject *i = NULL; - fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, + fastargs = _PyArg_UnpackKeywords(args, nargs, kwargs, kwnames, &_parser, /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf); if (!fastargs) { goto exit; } + if (nargs < 1) { + goto skip_optional_posonly; + } + noptargs--; + real_pos = fastargs[0]; + if (nargs < 2) { + goto skip_optional_posonly; + } + noptargs--; + imag_pos = fastargs[1]; +skip_optional_posonly: if (!noptargs) { - goto skip_optional_pos; + goto skip_optional_kwonly; } - if (fastargs[0]) { - r = fastargs[0]; + if (fastargs[2]) { + r = fastargs[2]; if (!--noptargs) { - goto skip_optional_pos; + goto skip_optional_kwonly; } } - i = fastargs[1]; -skip_optional_pos: - return_value = complex_new_impl(type, r, i); + i = fastargs[3]; +skip_optional_kwonly: + return_value = complex_new_impl(type, real_pos, imag_pos, r, i); exit: return return_value; } +static PyObject * +complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +{ + return complex_new_helper(type, _PyTuple_CAST(args)->ob_item, + PyTuple_GET_SIZE(args), + kwargs ? PyDict_GET_SIZE(kwargs) : 0, + kwargs, NULL); +} + +static PyObject * +complex_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + + assert(_PyType_CAST(type)->tp_new == complex_new); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + return complex_new_helper(_PyType_CAST(type), args, nargs, + kwnames ? PyTuple_GET_SIZE(kwnames) : 0, + NULL, kwnames); +} + PyDoc_STRVAR(complex_from_number__doc__, "from_number($type, number, /)\n" "--\n" @@ -185,4 +223,4 @@ complex_from_number(PyObject *type, PyObject *number) return return_value; } -/*[clinic end generated code: output=05d2ff43fc409733 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=35512ea9996f9935 input=a9049054013a1b77]*/ diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 4d2b5dc8e4613f3..78295e82e744a41 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -1121,27 +1121,19 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v) /* The constructor should only accept a string as a positional argument, * not as by the 'real' keyword. But Argument Clinic does not allow * to distinguish between argument passed positionally and by keyword. - * So the constructor must be split into two parts: actual_complex_new() - * handles the case of no arguments and one positional argument, and calls - * complex_new(), implemented with Argument Clinic, to handle the remaining - * cases: 'real' and 'imag' arguments. This separation is well suited - * for different constructor roles: converting a string or number to a complex + * So complex_new() declares 'real' and 'imag' twice: as positional-only and + * as keyword-only parameters. complex_new_impl() calls complex_from_arg() + * for one positional argument and handles the remaining cases itself: + * 'real' and 'imag' arguments. This separation is well suited for + * different constructor roles: converting a string or number to a complex * number and constructing a complex number from real and imaginary parts. */ static PyObject * -actual_complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) +complex_from_arg(PyTypeObject *type, PyObject *arg) { PyObject *res = NULL; PyNumberMethods *nbr; - if (PyTuple_GET_SIZE(args) > 1 || (kwargs != NULL && PyDict_GET_SIZE(kwargs))) { - return complex_new(type, args, kwargs); - } - if (!PyTuple_GET_SIZE(args)) { - return complex_subtype_from_doubles(type, 0, 0); - } - - PyObject *arg = PyTuple_GET_ITEM(args, 0); /* Special-case for a single argument when type(arg) is complex. */ if (PyComplex_CheckExact(arg) && type == &PyComplex_Type) { /* Note that we can't know whether it's safe to return @@ -1190,10 +1182,16 @@ actual_complex_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) } /*[clinic input] +@vectorcall @classmethod +@text_signature "(real=0, imag=0)" complex.__new__ as complex_new - real as r: object(c_default="NULL") = 0 - imag as i: object(c_default="NULL") = 0 + real_pos: object = NULL + imag_pos: object = NULL + / + * + real as r: object = NULL + imag as i: object = NULL Create a complex number from a string or numbers. @@ -1204,8 +1202,9 @@ with the specified real and imaginary components. [clinic start generated code]*/ static PyObject * -complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i) -/*[clinic end generated code: output=b6c7dd577b537dc1 input=ff4268dc540958a4]*/ +complex_new_impl(PyTypeObject *type, PyObject *real_pos, PyObject *imag_pos, + PyObject *r, PyObject *i) +/*[clinic end generated code: output=fdc6ecf8914112b7 input=09930f0cd44fd3a8]*/ { PyObject *tmp; PyNumberMethods *nbr, *nbi = NULL; @@ -1214,7 +1213,31 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i) int cr_is_complex = 0; int ci_is_complex = 0; + /* See the comment before complex_from_arg(). */ + int nargs = (real_pos != NULL) + (imag_pos != NULL) + (r != NULL) + (i != NULL); + if (nargs > 2) { + PyErr_Format(PyExc_TypeError, + "complex() takes at most 2 arguments (%d given)", nargs); + return NULL; + } + if (real_pos != NULL) { + if (r != NULL) { + PyErr_SetString(PyExc_TypeError, "argument for complex() given " + "by name ('real') and position (1)"); + return NULL; + } + if (imag_pos == NULL && i == NULL) { + return complex_from_arg(type, real_pos); + } + r = real_pos; + } + if (imag_pos != NULL) { + i = imag_pos; + } if (r == NULL) { + if (i == NULL) { + return complex_subtype_from_doubles(type, 0, 0); + } r = _PyLong_GetZero(); } PyObject *orig_r = r; @@ -1454,7 +1477,8 @@ PyTypeObject PyComplex_Type = { 0, /* tp_dictoffset */ 0, /* tp_init */ PyType_GenericAlloc, /* tp_alloc */ - actual_complex_new, /* tp_new */ + complex_new, /* tp_new */ PyObject_Free, /* tp_free */ + .tp_vectorcall = complex_vectorcall, .tp_version_tag = _Py_TYPE_VERSION_COMPLEX, };