Python notes how to implement Python third-party expansion packages in C language

Source: Internet
Author: User

Python supports the 3rd party expansion package implemented by C + +, which is especially important when performance requirements are high.

This article explains the basic steps for customizing the Python expansion pack with an example.

1. Extension package source code implementation

follow the steps in the Python website tutorial extending python with C or C + +, where the source file for the extension module is implemented as follows:
#include <python.h>//forward declarationvoid initpyext (void);//self-defined error objstatic Pyobject * EXTERROR;    int main (int argc, char * argv[]) {//Pass argv[0] to the Python interpreter Py_setprogramname (argv[0]); Initialize the Python interpreter.    Required.        Py_initialize ();    Initpyext ();    Py_exit (0); Not reached, warning'll be printed if build the extension}static Pyobject * PY_EXT_TEST_FUNC (Pyobject * Self, pyobj    ECT * args) {const char * command;    int STS; if (!    Pyarg_parsetuple (args, "s", &command)) {return NULL;    STS = System (command);        if (STS < 0) {pyerr_setstring (exterror, "System command failed");    return NULL; } return Pylong_fromlong (STS);} Static Pymethoddef pyextmethods[] = {{"Ext_cmd", Py_ext_test_func, Meth_varargs, "Execute a shell command."}, {NUL L, NULL, 0, NULL}};    Pymodinit_func Initpyext (void) {Pyobject * m;    m = Py_initmodule ("Pyext", pyextmethods); if (NULL == m) {return;    } Exterror = Pyerr_newexception ("Pyext.error", NULL, NULL);    Py_incref (Exterror); Pymodule_addobject (M, "error", Exterror);}
The code above shows a typical C-extension source layout for Python:
1. Define the main function as the portal for the expansion module
2. The main function calls Initpyext (), the Initpyext function name must be initname format, where name is the extension module name (the extension name is specified by the Py_initmodule parameter)
3. In the Initpyext function, call Py_initmodule to initialize the extension module name and its supported method names, where method list is defined by the static array pyextmethods
4. The name of the methods supported by the extension module and its corresponding function pointer are registered in the Pyextmethods array.
5. Define and implement specific behavior for each method registered in the Pyextmethods array

2. extension file compilation/linking

according to the official website document Building C and C + + Extensions with distutils, you can compile the extension and install it to the Python third-party package directory:
$> python setup.py buildrunning buildrunning build_extbuilding ' Pyext ' extensiongcc-pthread-fno-strict-aliasing-g- O2-dndebug-g-fwrapv-o3-wall-wstrict-prototypes-fpic-i/home/slvher/tools/python-2.7.5/include/python2.7-c Pyext.c-o build/temp.linux-x86_64-2.7/pyext.opyext.c:in function ' main ':p Yext.c:28:warning:control reaches end of non -void functiongcc-pthread-shared Build/temp.linux-x86_64-2.7/pyext.o-o build/lib.linux-x86_64-2.7/pyext.so
Note: This is called setup.py directly in Python because the default environment variable for Python is set in path, and if the current system default Python interpreter is not the interpreter we want to use to compile the extension, it needs to be called with the absolute path of Python.
3. Install the expansion pack
$> python setup.py installrunning installrunning buildrunning build_extrunning install_libcopying build/ lib.linux-x86_64-2.7/pyext.so,/home/slvher/tools/python-2.7.5/lib/python2.7/site-packagesrunning Install_ Egg_infowriting/home/slvher/tools/python-2.7.5/lib/python2.7/site-packages/testpyextpackage-1.0-py2.7.egg-info
4. Verify that it is available
After the installation succeeds, after entering the Python interpreter, import the extension module, you can call its method name:
"References"
1. Extending Python with C or C + +

2. Building C and C + + Extensions with Distutils

====================== EOF ==================


Python notes how to implement Python third-party expansion packages in C language

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.