-
Notifications
You must be signed in to change notification settings - Fork 0
/
cos_module.c
47 lines (39 loc) · 1.11 KB
/
cos_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Example of the Python 3 C API
*/
#include <Python.h>
#include <math.h>
/* wrapped cosine function */
static PyObject* cos_func(PyObject* self, PyObject* args)
{
double value, answer;
/* parse the input, from python float to c double */
if (!PyArg_ParseTuple(args, "d", &value))
{
return NULL;
}
/* if the above function returns -1, an appropriate Python exception will
have been set, and the function simply returns NULL */
/* actual call to the cos function from libm */
answer = cos(value);
/* construct the output from cos, from c double to python float */
return Py_BuildValue("f", answer);
}
/* Define functions in module */
static PyMethodDef CosMethods[] =
{
{"cos_func", cos_func, METH_VARARGS, "Evaluate the cosine of x"},
{NULL, NULL, 0, NULL}
};
/* Python module definition */
static struct PyModuleDef module =
{
PyModuleDef_HEAD_INIT,
"cos_module", "Example of the Python 3 C API",
-1, CosMethods
};
/* Name of the module should match the name above */
PyMODINIT_FUNC PyInit_cos_module(void)
{
return PyModule_Create(&module);
}