Pythonからcallされるソースコード『py_param_list_v2.c』

// #include <Python.h> #include "../../../include/python3.6m/Python.h" // C function "get integer and return list" static PyObject* c_param_list(PyObject* self, PyObject* args) { int a, b; PyObject* c_list; // Decide variable type (int, int) if (!PyArg_ParseTuple(args, "ii", &a, &b)){ return NULL; } // multiplication a = a * 2; b = b * 2; // make python list (length 2) c_list = PyList_New(2); // set param PyList_SET_ITEM(c_list, 0, PyLong_FromLong(a)); PyList_SET_ITEM(c_list, 1, PyLong_FromLong(b)); return c_list; } // C function "get float and return list" static PyObject* c_float_list(PyObject* self, PyObject* args) { double a, b; PyObject* c_list; // Decide variable type (double, double) if (!PyArg_ParseTuple(args, "dd", &a, &b)){ return NULL; } // multiplication a = a * 2; b = b * 2; // make python list (length 2) c_list = PyList_New(2); // set param PyList_SET_ITEM(c_list, 0, PyFloat_FromDouble(a)); PyList_SET_ITEM(c_list, 1, PyFloat_FromDouble(b)); return c_list; } // Function Definition struct 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 } }; // Module Definition struct static struct PyModuleDef plistModule = { PyModuleDef_HEAD_INIT, "plistModule", "Python3 C API Module(Sample 6)", -1, plistMethods }; // Initializes our module using our above struct 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)