Steps to write a module for python with C (getting started)

As the name suggests, it is one of the more commonly used methods to write the core code in C language and then package it into a form that can be used by PYTHON.

Not much nonsense,
the required files are as follows:
file1 - contains C code, and the C file that encapsulates the boilerplate code
file2 - setup.py file, used for compilation and installation

file1 -
must #include "Python.h"
//Location - if anaconda is installed, it is in anaconda3/include
//The default is in /usr/local/include/python3.x/

Use C to define a good function - such as a factorial function, if it is difficult to understand this function...
int fac(int n){ if (n<2) return 1; else return (n)*fac(n-1); } int main(){ ...//The middle part is the code to test the C function } and then the boilerplate function. The boilerplate code has several components:








//1. Each defined function requires a boilerplate function
static PyObject* module name_function name(PyObject self, PyObject args){ //Use PyArg_ParseTuple function to interpret parameters char * command; PyObject


retval;//Define return variable
/ /PyArg_ParseTuple(args,"s",&command);
//This sentence means to interpret args as a char type and store it in the command position
int res;
res = fac(command);//Assume the function is called fac
//Use Py_BuildValue to convert c variables into python variables returns
//Py_BuildValue() returns a tuple
//retval = (PyObject
)Py_BuildValue(type, C variable 1, C variable 2)
return retval;
}
//2. Method definition - this is the Which Methods does the module contain
static PyMethodDef ExtestMethods[] = { //Extest is the module name
{"fac", Extest_fac, METH_VARARGS},
/* The name used in python, corresponding to the name of the template function, METH_VARARGS means to expect the parameters of PYTHON-LEVEL */ {
"doppel", Extest_doppel, METH_VARARGS},
{NULL, NULL} //Indicates the end of the function information
};
// 3. Module definition
static struct PyModuleDef extestmodule = { PyModuleDef_HEAD_INIT, “Extest”, //name NULL, //doc -1, //do not understand ExtestMethods //method definition }; //4. Start the template function PyMODINIT_FUNC PyInit_Extest(void) { //Extest is the module name PyObject *m; m = PyModule_Create(&extestmodule); //Define if(m==NULL) return NULL for the module; / can trigger exceptions, etc./ return m; }















file 2 :
#setup.py
from distutils.core import setup,Extension

MOD = 'Extest' //name
setup(name=MOD,ext_modules=[Extension(MOD,sources=['Extest1.c'])])//source code location

#Order

python setup.py build
python setup.py install

#attach complete code

//Extest1.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Python.h"

int fac(int n){
	if (n<2)
		return 1;
	else
		return (n)*fac(n-1);
}

char *reverse(char *s){
	register char t,*p=s,*q=(s+(strlen(s)-1));
	while(p<q)
	{
		t = *p;
		*p++ = *q;
		*q--=t;
	}
	return s;
}

int main(){
	char s[1024];
	printf("4!==%d\n",fac(4));
	printf("8!==%d\n",fac(8));
	printf("12!==%d\n",fac(12));
	strcpy(s,"abcdef");
	
	printf("reversing 'abcdef', we get '%s'\n",reverse(s));
	strcpy(s,"madam");
	printf("reversing 'madam', we get '%s'\n",reverse(s));
	return 0;
}
static PyObject * Extest_fac(PyObject *self,PyObject *args){
    int num;
    if(!PyArg_ParseTuple(args,"i",&num)) return NULL;
	return (PyObject*)Py_BuildValue("i",fac(num));
}
static PyObject * Extest_doppel(PyObject *self,PyObject *args){
	char* orig_str;
	char* dup_str;
	PyObject* retval;
	
	if(!PyArg_ParseTuple(args,"s",&orig_str)) return NULL;
	retval = (PyObject*)Py_BuildValue("ss",orig_str,dup_str=reverse(strdup(orig_str)));
	
	free(dup_str);
	return retval;
}
static PyMethodDef ExtestMethods[] = {
	{"fac",Extest_fac,METH_VARARGS},
	{"doppel",Extest_doppel,METH_VARARGS},
	{NULL,NULL}
};
static struct PyModuleDef extestmodule = {
	PyModuleDef_HEAD_INIT,
	"Extest",
	NULL,
	-1,
	ExtestMethods
};
PyMODINIT_FUNC
PyInit_Extest(void){
	PyObject *m;
	m = PyModule_Create(&extestmodule);
	if(m==NULL)
		return NULL;
	return m;
}
#setup.py
from distutils.core import setup,Extension

MOD = 'Extest'
setup(name=MOD,ext_modules=[Extension(MOD,sources=['Extest1.c'])])

Guess you like

Origin blog.csdn.net/xiaozoom/article/details/83097136