Using the C language extension Python3

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.