Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46334cd

Browse files
committed
Kill div, mod and divmod on complex (already deprecated in 2.x).
Add docstring for conjugate(). Patch by Jeffrey Yasskin.
1 parent b31339f commit 46334cd

1 file changed

Lines changed: 15 additions & 55 deletions

File tree

Objects/complexobject.c

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -466,57 +466,18 @@ complex_div(PyObject *v, PyObject *w)
466466
static PyObject *
467467
complex_remainder(PyObject *v, PyObject *w)
468468
{
469-
Py_complex div, mod;
470-
Py_complex a, b;
471-
TO_COMPLEX(v, a);
472-
TO_COMPLEX(w, b);
473-
474-
if (PyErr_Warn(PyExc_DeprecationWarning,
475-
"complex divmod(), // and % are deprecated") < 0)
476-
return NULL;
477-
478-
errno = 0;
479-
div = c_quot(a, b); /* The raw divisor value. */
480-
if (errno == EDOM) {
481-
PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder");
482-
return NULL;
483-
}
484-
div.real = floor(div.real); /* Use the floor of the real part. */
485-
div.imag = 0.0;
486-
mod = c_diff(a, c_prod(b, div));
487-
488-
return PyComplex_FromCComplex(mod);
469+
PyErr_SetString(PyExc_TypeError,
470+
"can't mod complex numbers.");
471+
return NULL;
489472
}
490473

491474

492475
static PyObject *
493476
complex_divmod(PyObject *v, PyObject *w)
494477
{
495-
Py_complex div, mod;
496-
PyObject *d, *m, *z;
497-
Py_complex a, b;
498-
TO_COMPLEX(v, a);
499-
TO_COMPLEX(w, b);
500-
501-
if (PyErr_Warn(PyExc_DeprecationWarning,
502-
"complex divmod(), // and % are deprecated") < 0)
503-
return NULL;
504-
505-
errno = 0;
506-
div = c_quot(a, b); /* The raw divisor value. */
507-
if (errno == EDOM) {
508-
PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()");
509-
return NULL;
510-
}
511-
div.real = floor(div.real); /* Use the floor of the real part. */
512-
div.imag = 0.0;
513-
mod = c_diff(a, c_prod(b, div));
514-
d = PyComplex_FromCComplex(div);
515-
m = PyComplex_FromCComplex(mod);
516-
z = PyTuple_Pack(2, d, m);
517-
Py_XDECREF(d);
518-
Py_XDECREF(m);
519-
return z;
478+
PyErr_SetString(PyExc_TypeError,
479+
"can't take floor or mod of complex number.");
480+
return NULL;
520481
}
521482

522483
static PyObject *
@@ -560,15 +521,8 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
560521
static PyObject *
561522
complex_int_div(PyObject *v, PyObject *w)
562523
{
563-
PyObject *t, *r;
564-
565-
t = complex_divmod(v, w);
566-
if (t != NULL) {
567-
r = PyTuple_GET_ITEM(t, 0);
568-
Py_INCREF(r);
569-
Py_DECREF(t);
570-
return r;
571-
}
524+
PyErr_SetString(PyExc_TypeError,
525+
"can't take floor of complex number.");
572526
return NULL;
573527
}
574528

@@ -665,14 +619,20 @@ complex_conjugate(PyObject *self)
665619
return PyComplex_FromCComplex(c);
666620
}
667621

622+
PyDoc_STRVAR(complex_conjugate_doc,
623+
"complex.conjugate() -> complex\n"
624+
"\n"
625+
"Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
626+
668627
static PyObject *
669628
complex_getnewargs(PyComplexObject *v)
670629
{
671630
return Py_BuildValue("(D)", &v->cval);
672631
}
673632

674633
static PyMethodDef complex_methods[] = {
675-
{"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS},
634+
{"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
635+
complex_conjugate_doc},
676636
{"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
677637
{NULL, NULL} /* sentinel */
678638
};

0 commit comments

Comments
 (0)