diff options
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/mathmodule.c | 34 | ||||
-rw-r--r-- | Modules/posixmodule.c | 13 |
2 files changed, 41 insertions, 6 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index baef569..f50a4bb 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -206,6 +206,39 @@ FUNC1(tanh, tanh, "tanh(x)\n\nReturn the hyperbolic tangent of x.") static PyObject * +math_trunc(PyObject *self, PyObject *number) +{ + static PyObject *trunc_str = NULL; + PyObject *trunc; + + if (Py_TYPE(number)->tp_dict == NULL) { + if (PyType_Ready(Py_TYPE(number)) < 0) + return NULL; + } + + if (trunc_str == NULL) { + trunc_str = PyUnicode_InternFromString("__trunc__"); + if (trunc_str == NULL) + return NULL; + } + + trunc = _PyType_Lookup(Py_TYPE(number), trunc_str); + if (trunc == NULL) { + PyErr_Format(PyExc_TypeError, + "type %.100s doesn't define __trunc__ method", + Py_TYPE(number)->tp_name); + return NULL; + } + return PyObject_CallFunctionObjArgs(trunc, number, NULL); +} + +PyDoc_STRVAR(math_trunc_doc, +"trunc(x:Real) -> Integral\n" +"\n" +"Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic" +"method."); + +static PyObject * math_frexp(PyObject *self, PyObject *arg) { int i; @@ -428,6 +461,7 @@ static PyMethodDef math_methods[] = { {"sqrt", math_sqrt, METH_O, math_sqrt_doc}, {"tan", math_tan, METH_O, math_tan_doc}, {"tanh", math_tanh, METH_O, math_tanh_doc}, + {"trunc", math_trunc, METH_O, math_trunc_doc}, {NULL, NULL} /* sentinel */ }; diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 9fe5790..842f971 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -3616,11 +3616,11 @@ Return 0 to child process and PID of child to parent process."); static PyObject * posix_fork1(PyObject *self, PyObject *noargs) { - int pid = fork1(); + pid_t pid = fork1(); if (pid == -1) return posix_error(); PyOS_AfterFork(); - return PyLong_FromLong((long)pid); + return PyLong_FromLong(pid); } #endif @@ -3634,12 +3634,12 @@ Return 0 to child process and PID of child to parent process."); static PyObject * posix_fork(PyObject *self, PyObject *noargs) { - int pid = fork(); + pid_t pid = fork(); if (pid == -1) return posix_error(); if (pid == 0) PyOS_AfterFork(); - return PyLong_FromLong((long)pid); + return PyLong_FromLong(pid); } #endif @@ -3741,14 +3741,15 @@ To both, return fd of newly opened pseudo-terminal.\n"); static PyObject * posix_forkpty(PyObject *self, PyObject *noargs) { - int master_fd = -1, pid; + int master_fd = -1; + pid_t pid; pid = forkpty(&master_fd, NULL, NULL, NULL); if (pid == -1) return posix_error(); if (pid == 0) PyOS_AfterFork(); - return Py_BuildValue("(ii)", pid, master_fd); + return Py_BuildValue("(li)", pid, master_fd); } #endif |