diff Python/bltinmodule.c @ 42713:aad17b1ab0c9

Implement the trunc builtin for PEP 3141
author Alex Martelli <aleaxit@gmail.com>
date Wed, 22 Aug 2007 22:39:42 +0000
parents 85c22b4d4455
children 7fe2848fd41d
line wrap: on
line diff
--- a/Python/bltinmodule.c	Wed Aug 22 21:46:00 2007 +0000
+++ b/Python/bltinmodule.c	Wed Aug 22 22:39:42 2007 +0000
@@ -1486,6 +1486,27 @@
 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 @@
  	{"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},
 };