Pythonからcallされるソースコード『py_param_list_v2.c』
#include "../../../include/python3.6m/Python.h"
static PyObject* c_param_list(PyObject* self, PyObject* args)
{
int a, b;
PyObject* c_list;
if (!PyArg_ParseTuple(args, "ii", &a, &b)){
return NULL;
}
a = a * 2;
b = b * 2;
c_list = PyList_New(2);
PyList_SET_ITEM(c_list, 0, PyLong_FromLong(a));
PyList_SET_ITEM(c_list, 1, PyLong_FromLong(b));
return c_list;
}
static PyObject* c_float_list(PyObject* self, PyObject* args)
{
double a, b;
PyObject* c_list;
if (!PyArg_ParseTuple(args, "dd", &a, &b)){
return NULL;
}
a = a * 2;
b = b * 2;
c_list = PyList_New(2);
PyList_SET_ITEM(c_list, 0, PyFloat_FromDouble(a));
PyList_SET_ITEM(c_list, 1, PyFloat_FromDouble(b));
return c_list;
}
static PyMethodDef plistMethods[] = {
{ "c_param_list", c_param_list, METH_VARARGS, "multiplication and make list"},
{ "c_float_list", c_float_list, METH_VARARGS, "support float"},
{ NULL }
};
static struct PyModuleDef plistModule = {
PyModuleDef_HEAD_INIT,
"plistModule",
"Python3 C API Module(Sample 6)",
-1,
plistMethods
};
PyMODINIT_FUNC PyInit_plistModule(void)
{
return PyModule_Create(&plistModule);
}
setup.py
from distutils.core import setup, Extension
setup(name = 'plistModule', version = '2.0.0', \
ext_modules = [Extension('plistModule', ['py_param_list.c'])])
『py_param_list_v2.c』を呼ぶソースコード『param_list_v2.py』
import plistModule as plm
get_list = []
get_list2 = []
get_list = plm.c_param_list(2, 3)
print(get_list)
get_list2 = plm.c_float_list(2.2, 3.3)
print(get_list2)