Python 3.4.6 gcc version 4.8.5 openSUSE Leap 42.3
When I was studying Python, I came up with a way to make an extension module and decided to make it myself. I personally got stuck in various ways, so I'll write it here instead of a notebook.
Create Python module in C language ↓ Build C using Python ↓ Try importing with Python and using C module
First, let's create a Python module in C language. We make heavy use of the PyObject type. For the time being, I made Hello_world, push and pop.
practice.c
#include <Python.h>
//Functions created in C and used in Python use PyObject
static PyObject*
hello_world (PyObject *self, PyObject *args) {
printf("Hello_world\n");
//The return of the function created on the C side is Py~~...Becomes
Py_RETURN_NONE;
}
static PyObject*
push(PyObject *self, PyObject *args){
PyObject *p_list, *inthert_val, *receive_list;
//Parse the value sent
if(!PyArg_ParseTuple(args, "O!i", &PyList_Type, &p_list, &inthert_val))
return NULL;
//Extract array
receive_list = PySequence_List(p_list);
//Add value to array
printf("%push d\n", inthert_val);
PyList_Append(receive_list, Py_BuildValue("i", inthert_val));
//Return array
return receive_list;
}
static PyObject*
pop (PyObject *self, PyObject *args){
PyObject *p_list, *p_value;
int size;
long val;
//Parse the value sent
if(!PyArg_ParseTuple(args, "O!", &PyList_Type, &p_list))
return NULL;
//Get list size
size = PyList_Size(p_list);
//Extract value
p_value = PyList_GetItem(p_list, size - 1);
val = PyLong_AsLong(p_value);
printf("%pop d\n", val);
//Return value
return p_value;
}
//Method definition
static PyMethodDef PracticeMethods[] = {
{"hello_world", (PyCFunction)hello_world, METH_NOARGS, "practice1: hello_world"},
{"push", (PyCFunction)push, METH_VARARGS, "practice2: push"},
{"pop", (PyCFunction)pop, METH_VARARGS, "practice3: pop"},
//Indicates the end
{NULL, NULL, 0, NULL}
};
//Module definition
static struct PyModuleDef practicetmodule = {
PyModuleDef_HEAD_INIT,
"practice",
NULL,
-1,
PracticeMethods
};
//Method initialization
PyMODINIT_FUNC PyInit_practice (void) {
return PyModule_Create(&practicetmodule);
}
I will itemize the places I am addicted to. All I had to do was check it out, but it took a long time.
Personally, this was the best demon gate. So, if you can get over even here, the rest will be easy.
As it is. Build C using Python. Specifically, use the following sources.
setup.py
from distutils.core import setup, Extension
setup(name='practice',
version='1.0',
ext_modules=[Extension('practice', ['practice.c'])]
)
After creating it, execute the following command to build it. This command is used when testing without installing the module.
command
python setup.py build_ext -i
Let's check.
command
ls
display
build practice.c practice.cpython-34m.so setup.py
You now have a directory called build and a file called practice.cpython-34m.so. This completes the build.
I will try to see if it can actually be used. I wrote the following code.
try_module.py
import practice as c_practice
#First, operation verification
c_practice.hello_world()
# push
test_list = c_practice.push([50, 51, 52], 53)
print(test_list)
# pop
result = c_practice.pop(test_list)
print(result)
I will try it.
output
Hello_world
Push 53
[50, 51, 52, 53]
Pop 53
53
For the time being, it worked. It was good because it took quite a long time.
Recommended Posts