summaryrefslogtreecommitdiffstats
path: root/Objects/longobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-08-23 22:07:24 (GMT)
committerGuido van Rossum <guido@python.org>2007-08-23 22:07:24 (GMT)
commit2fa33db12b8cb6ec1dd1b87df6911e311d98457b (patch)
treecdc7d475186715895ae3457e91bf0d3c3b7c5e03 /Objects/longobject.c
parent0f3cff58b284e0098eab71b80d0001e173e6981a (diff)
downloadcpython-2fa33db12b8cb6ec1dd1b87df6911e311d98457b.zip
cpython-2fa33db12b8cb6ec1dd1b87df6911e311d98457b.tar.gz
cpython-2fa33db12b8cb6ec1dd1b87df6911e311d98457b.tar.bz2
Finish the work on __round__ and __trunc__.
With Alex Martelli and Keir Mierle.
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r--Objects/longobject.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 518e607..ddf359d 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -3592,9 +3592,45 @@ long_getN(PyLongObject *v, void *context) {
return PyLong_FromLong((intptr_t)context);
}
+static PyObject *
+long_round(PyObject *self, PyObject *args)
+{
+#define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
+ int ndigits = UNDEF_NDIGITS;
+ double x;
+ PyObject *res;
+
+ if (!PyArg_ParseTuple(args, "|i", &ndigits))
+ return NULL;
+
+ if (ndigits == UNDEF_NDIGITS)
+ return long_long(self);
+
+ /* If called with two args, defer to float.__round__(). */
+ x = PyLong_AsDouble(self);
+ if (x == -1.0 && PyErr_Occurred())
+ return NULL;
+ self = PyFloat_FromDouble(x);
+ if (self == NULL)
+ return NULL;
+ res = PyObject_CallMethod(self, "__round__", "i", ndigits);
+ Py_DECREF(self);
+ return res;
+#undef UNDEF_NDIGITS
+}
+
static PyMethodDef long_methods[] = {
{"conjugate", (PyCFunction)long_long, METH_NOARGS,
"Returns self, the complex conjugate of any int."},
+ {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
+ "Truncating an Integral returns itself."},
+ {"__floor__", (PyCFunction)long_long, METH_NOARGS,
+ "Flooring an Integral returns itself."},
+ {"__ceil__", (PyCFunction)long_long, METH_NOARGS,
+ "Ceiling of an Integral returns itself."},
+ {"__round__", (PyCFunction)long_round, METH_VARARGS,
+ "Rounding an Integral returns itself.\n"
+ "Rounding with an ndigits arguments defers to float.__round__."},
{"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
{NULL, NULL} /* sentinel */
};