Use the C language to extend the Python3.
The C function is called correctly in Python3.
1. File demo.c
#include <Python.h>//C functionStaticPyobject *Demo_system (Pyobject*self, Pyobject *args) { Const Char*command; intSTS; if(! Pyarg_parsetuple (args,"s", &command)) returnNULL; STS=System (command); returnPylong_fromlong (STS);} StaticPyobject *Demo_hello (Pyobject*self, Pyobject *args) {Pyobject*name, *result; if(! Pyarg_parsetuple (args,"U:demo_hello", &name)) returnNULL; Result= Pyunicode_fromformat ("Hello,%s!.", name); returnresult;} StaticPyobject *Demo_chinese (Pyobject*self, Pyobject *args) { Char*name; intAge ; if(! Pyarg_parsetuple (args,"si", &name, &Age )) returnNULL; //printf ("%d\n", age); Chartotal[10000]; memset (Total,0,sizeof(total)); Strcat (Total,"The strcat () function is used to concatenate strings:"); Strcat (Total,"Tset"); Pyobject*result = Py_buildvalue ("s", total); returnresult;} //Method TableStaticPymethoddef demomethods[] = { {"system",//Python method nameDemo_system,//matched C function nameMeth_varargs,/*a flag telling the interpreter the calling convention to being used for the C function. */ "I guess here's description." }, {"Hello", Demo_hello, Meth_varargs,"I guess here's description." }, {"Chinese", Demo_chinese, Meth_varargs, null}, {null, null ,0, NULL}/*Sentinel*/}; //The method table must is referenced in the module definition structure.Static structPymoduledef Demomodule ={pymoduledef_head_init,"Demo",/*Name of module*/NULL,/*module documentation, May is NULL*/-1,/*size of Per-interpreter state of the module, or-1 if the module keeps state in global variables. */Demomethods}; //The initialization function must be named Pyinit_name ()Pymodinit_funcpyinit_demo (void){ returnPymodule_create (&demomodule);}2. hello.py
ImportDemoPrint("---------------------------") Status= Demo.system ("ls-l")Print("---------------------------") Hi= Demo.hello ("Sink")Print(HI)Print("---------------------------") Hi= Demo.chinese ("Sink", 2014)Print(HI)Print("---------------------------")3. setup.py
fromDistutils.coreImportSetup, Extension Module1= Extension ('Demo', sources= ['demo.c']) Setup (name='Demo Hello', Version='1.0', Description='This was a demo package by Sink', Ext_modules= [Module1])
Using the C language extension Python3