まずモジュールの名前を決める。
某という名前のモジュールのインプリメンテーションが入った C のソースファイルは、 某module.c としよう。
#include <Python.h> |
static PyObject *
某_なんとか(PyObject *self, PyObject *args)
{
const char *command;
int sts;
if (!PyArg_ParseTuple(args, "s", &command))
return NULL;
sts = system(command); // コマンドを実行する
return PyLong_FromLong(sts);
}
|
static PyMethodDef 某Methods[] = {
...
{"なんとか", 某_なんとか, METH_VARARGS,
"Execute a shell command."},
...
{NULL, NULL, 0, NULL} /* Sentinel */
};
|
static struct PyModuleDef 某module = {
PyModuleDef_HEAD_INIT,
"某", /* name of module */
某_doc, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
某Methods
};
|
PyMODINIT_FUNC
PyInit_某(void)
{
return PyModule_Create(&某module);
}
|