summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorAlex Martelli <aleaxit@gmail.com>2007-08-22 22:39:42 (GMT)
committerAlex Martelli <aleaxit@gmail.com>2007-08-22 22:39:42 (GMT)
commit86d8b3497fb509d1c52f6f29f74854ec96987f23 (patch)
treef7224a765686a781615ff73e3393f6bd106e1171 /Python
parenta62b45c95daa1172c7411062372f0cc3827a292f (diff)
downloadcpython-86d8b3497fb509d1c52f6f29f74854ec96987f23.zip
cpython-86d8b3497fb509d1c52f6f29f74854ec96987f23.tar.gz
cpython-86d8b3497fb509d1c52f6f29f74854ec96987f23.tar.bz2
Implement the trunc builtin for PEP 3141
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index ce8b7f5..2c163a1 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1486,6 +1486,27 @@ PyDoc_STRVAR(vars_doc,
Without arguments, equivalent to locals().\n\
With an argument, equivalent to object.__dict__.");
+static PyObject *
+builtin_trunc(PyObject *self, PyObject *v)
+{
+ PyObject *res;
+ PyObject *d = PyObject_GetAttrString(v, "__trunc__");
+ if (d == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "trunc() argument must have __trunc__ attribute");
+ return NULL;
+ }
+ res = PyObject_CallFunction(d, "");
+ Py_DECREF(d);
+ return res;
+}
+
+PyDoc_STRVAR(trunc_doc,
+"trunc(Real) -> Integral\n\
+\n\
+returns the integral closest to x between 0 and x.");
+
+
static PyObject*
builtin_sum(PyObject *self, PyObject *args)
@@ -1659,6 +1680,7 @@ static PyMethodDef builtin_methods[] = {
{"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
{"sum", builtin_sum, METH_VARARGS, sum_doc},
{"vars", builtin_vars, METH_VARARGS, vars_doc},
+ {"trunc", builtin_trunc, METH_O, trunc_doc},
{"zip", builtin_zip, METH_VARARGS, zip_doc},
{NULL, NULL},
};