summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Objects/complexobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/tools/python3/src/Objects/complexobject.c')
-rw-r--r--contrib/tools/python3/src/Objects/complexobject.c135
1 files changed, 55 insertions, 80 deletions
diff --git a/contrib/tools/python3/src/Objects/complexobject.c b/contrib/tools/python3/src/Objects/complexobject.c
index e09cc15fe84..3e479497cfc 100644
--- a/contrib/tools/python3/src/Objects/complexobject.c
+++ b/contrib/tools/python3/src/Objects/complexobject.c
@@ -6,8 +6,11 @@
/* Submitted by Jim Hugunin */
#include "Python.h"
+#include "pycore_long.h" // _PyLong_GetZero()
+#include "pycore_object.h" // _PyObject_Init()
#include "structmember.h" // PyMemberDef
+
/*[clinic input]
class complex "PyComplexObject *" "&PyComplex_Type"
[clinic start generated code]*/
@@ -222,13 +225,12 @@ complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
PyObject *
PyComplex_FromCComplex(Py_complex cval)
{
- PyComplexObject *op;
-
/* Inline PyObject_New */
- op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
- if (op == NULL)
+ PyComplexObject *op = PyObject_Malloc(sizeof(PyComplexObject));
+ if (op == NULL) {
return PyErr_NoMemory();
- (void)PyObject_INIT(op, &PyComplex_Type);
+ }
+ _PyObject_Init((PyObject*)op, &PyComplex_Type);
op->cval = cval;
return (PyObject *) op;
}
@@ -403,10 +405,10 @@ static Py_hash_t
complex_hash(PyComplexObject *v)
{
Py_uhash_t hashreal, hashimag, combined;
- hashreal = (Py_uhash_t)_Py_HashDouble(v->cval.real);
+ hashreal = (Py_uhash_t)_Py_HashDouble((PyObject *) v, v->cval.real);
if (hashreal == (Py_uhash_t)-1)
return -1;
- hashimag = (Py_uhash_t)_Py_HashDouble(v->cval.imag);
+ hashimag = (Py_uhash_t)_Py_HashDouble((PyObject *)v, v->cval.imag);
if (hashimag == (Py_uhash_t)-1)
return -1;
/* Note: if the imaginary part is 0, hashimag is 0 now,
@@ -502,23 +504,6 @@ complex_div(PyObject *v, PyObject *w)
}
static PyObject *
-complex_remainder(PyObject *v, PyObject *w)
-{
- PyErr_SetString(PyExc_TypeError,
- "can't mod complex numbers.");
- return NULL;
-}
-
-
-static PyObject *
-complex_divmod(PyObject *v, PyObject *w)
-{
- PyErr_SetString(PyExc_TypeError,
- "can't take floor or mod of complex number.");
- return NULL;
-}
-
-static PyObject *
complex_pow(PyObject *v, PyObject *w, PyObject *z)
{
Py_complex p;
@@ -555,14 +540,6 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
}
static PyObject *
-complex_int_div(PyObject *v, PyObject *w)
-{
- PyErr_SetString(PyExc_TypeError,
- "can't take floor of complex number.");
- return NULL;
-}
-
-static PyObject *
complex_neg(PyComplexObject *v)
{
Py_complex neg;
@@ -660,62 +637,54 @@ Unimplemented:
Py_RETURN_NOTIMPLEMENTED;
}
-static PyObject *
-complex_int(PyObject *v)
-{
- PyErr_SetString(PyExc_TypeError,
- "can't convert complex to int");
- return NULL;
-}
+/*[clinic input]
+complex.conjugate
-static PyObject *
-complex_float(PyObject *v)
-{
- PyErr_SetString(PyExc_TypeError,
- "can't convert complex to float");
- return NULL;
-}
+Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.
+[clinic start generated code]*/
static PyObject *
-complex_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored))
+complex_conjugate_impl(PyComplexObject *self)
+/*[clinic end generated code: output=5059ef162edfc68e input=5fea33e9747ec2c4]*/
{
- Py_complex c;
- c = ((PyComplexObject *)self)->cval;
+ Py_complex c = self->cval;
c.imag = -c.imag;
return PyComplex_FromCComplex(c);
}
-PyDoc_STRVAR(complex_conjugate_doc,
-"complex.conjugate() -> complex\n"
-"\n"
-"Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
+/*[clinic input]
+complex.__getnewargs__
+
+[clinic start generated code]*/
static PyObject *
-complex_getnewargs(PyComplexObject *v, PyObject *Py_UNUSED(ignored))
+complex___getnewargs___impl(PyComplexObject *self)
+/*[clinic end generated code: output=689b8206e8728934 input=539543e0a50533d7]*/
{
- Py_complex c = v->cval;
+ Py_complex c = self->cval;
return Py_BuildValue("(dd)", c.real, c.imag);
}
-PyDoc_STRVAR(complex__format__doc,
-"complex.__format__() -> str\n"
-"\n"
-"Convert to a string according to format_spec.");
+
+/*[clinic input]
+complex.__format__
+
+ format_spec: unicode
+ /
+
+Convert to a string according to format_spec.
+[clinic start generated code]*/
static PyObject *
-complex__format__(PyObject* self, PyObject* args)
+complex___format___impl(PyComplexObject *self, PyObject *format_spec)
+/*[clinic end generated code: output=bfcb60df24cafea0 input=014ef5488acbe1d5]*/
{
- PyObject *format_spec;
_PyUnicodeWriter writer;
int ret;
-
- if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
- return NULL;
-
_PyUnicodeWriter_Init(&writer);
ret = _PyComplex_FormatAdvancedWriter(
&writer,
- self,
+ (PyObject *)self,
format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
if (ret == -1) {
_PyUnicodeWriter_Dealloc(&writer);
@@ -725,11 +694,9 @@ complex__format__(PyObject* self, PyObject* args)
}
static PyMethodDef complex_methods[] = {
- {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
- complex_conjugate_doc},
- {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
- {"__format__", (PyCFunction)complex__format__,
- METH_VARARGS, complex__format__doc},
+ COMPLEX_CONJUGATE_METHODDEF
+ COMPLEX___GETNEWARGS___METHODDEF
+ COMPLEX___FORMAT___METHODDEF
{NULL, NULL} /* sentinel */
};
@@ -897,7 +864,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
/*[clinic input]
@classmethod
complex.__new__ as complex_new
- real as r: object(c_default="_PyLong_Zero") = 0
+ real as r: object(c_default="NULL") = 0
imag as i: object(c_default="NULL") = 0
Create a complex number from a real part and an optional imaginary part.
@@ -907,7 +874,7 @@ This is equivalent to (real + imag*1j) where imag defaults to 0.
static PyObject *
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
-/*[clinic end generated code: output=b6c7dd577b537dc1 input=6f6b0bedba29bcb5]*/
+/*[clinic end generated code: output=b6c7dd577b537dc1 input=f4c667f2596d4fd1]*/
{
PyObject *tmp;
PyNumberMethods *nbr, *nbi = NULL;
@@ -916,6 +883,10 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
int cr_is_complex = 0;
int ci_is_complex = 0;
+ if (r == NULL) {
+ r = _PyLong_GetZero();
+ }
+
/* Special-case for a single argument when type(arg) is complex. */
if (PyComplex_CheckExact(r) && i == NULL &&
type == &PyComplex_Type) {
@@ -952,7 +923,9 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
}
nbr = Py_TYPE(r)->tp_as_number;
- if (nbr == NULL || (nbr->nb_float == NULL && nbr->nb_index == NULL)) {
+ if (nbr == NULL ||
+ (nbr->nb_float == NULL && nbr->nb_index == NULL && !PyComplex_Check(r)))
+ {
PyErr_Format(PyExc_TypeError,
"complex() first argument must be a string or a number, "
"not '%.200s'",
@@ -964,7 +937,9 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
}
if (i != NULL) {
nbi = Py_TYPE(i)->tp_as_number;
- if (nbi == NULL || (nbi->nb_float == NULL && nbi->nb_index == NULL)) {
+ if (nbi == NULL ||
+ (nbi->nb_float == NULL && nbi->nb_index == NULL && !PyComplex_Check(i)))
+ {
PyErr_Format(PyExc_TypeError,
"complex() second argument must be a number, "
"not '%.200s'",
@@ -1043,8 +1018,8 @@ static PyNumberMethods complex_as_number = {
(binaryfunc)complex_add, /* nb_add */
(binaryfunc)complex_sub, /* nb_subtract */
(binaryfunc)complex_mul, /* nb_multiply */
- (binaryfunc)complex_remainder, /* nb_remainder */
- (binaryfunc)complex_divmod, /* nb_divmod */
+ 0, /* nb_remainder */
+ 0, /* nb_divmod */
(ternaryfunc)complex_pow, /* nb_power */
(unaryfunc)complex_neg, /* nb_negative */
(unaryfunc)complex_pos, /* nb_positive */
@@ -1056,9 +1031,9 @@ static PyNumberMethods complex_as_number = {
0, /* nb_and */
0, /* nb_xor */
0, /* nb_or */
- complex_int, /* nb_int */
+ 0, /* nb_int */
0, /* nb_reserved */
- complex_float, /* nb_float */
+ 0, /* nb_float */
0, /* nb_inplace_add */
0, /* nb_inplace_subtract */
0, /* nb_inplace_multiply*/
@@ -1069,7 +1044,7 @@ static PyNumberMethods complex_as_number = {
0, /* nb_inplace_and */
0, /* nb_inplace_xor */
0, /* nb_inplace_or */
- (binaryfunc)complex_int_div, /* nb_floor_divide */
+ 0, /* nb_floor_divide */
(binaryfunc)complex_div, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */