Using Codeblocks to implement the C language extension to Python under Windows

Source: Internet
Author: User
Tags assert python list

I am more lazy to stick to other people's configuration plan

From this start to the code excerpt from http://blog.csdn.net/yueguanghaidao/article/details/11538433

has been interested in Python extensions, just saw the extending and embedding the Python interpreter document, looking at the minimum version (due to the work of the 2.x, ̄- ̄), the official document

Links: http://docs.python.org/2.6/extending/index.html

The IDE I'm using is code::blocks 12.11, and I need to configure the environment first (Windows).

Because of the need to invoke the C API provided by Python, you need to set the Incldue path, and the Lib path.

Open Code::Blocks->> Settings,

I chose the compiler is GCC, it is best to look at the installation path, the choice is codeblocks installation of MinGW, if you have previously installed QT, etc., may change.

The following compilers and connectors are under the Mingw/bin directory.

Open the Search Directories tab and locate the Include and Libs directories under the Python installation path, set up as follows:

You also need to set the Pythonlib file.

Environment configured, need to encode, File->> New->> Project, in the type of projects we choose DLL or shared library (I choose DLL), next after the language selection C, Project title is set to Testmod, and the following defaults to it.

Overwrite the following code with the MAIN.C (which I extracted from the document)------code from http://blog.csdn.net/kosl90/article/details/6618367

1#include <stdio.h>2#include"Python.h"3 intSumintAintb) {4     returnA +b;5 }6 7 voidTest () {8printf"2 + 3 is%d\n", SUM (2,3));9printf"+ 177 is%d\n", SUM ( at,177));Ten } One  A Staticpyobject* testmod_sum (Pyobject *self, Pyobject *args) { -     intA, B; -  the     if(! Pyarg_parsetuple (args,"II", &a, &b)) { -         returnNULL; -     } -  +     return(pyobject*) Py_buildvalue ("I", sum (A, b)); - } +  A Staticpyobject* testmod_test (Pyobject *self, Pyobject *args) { at test (); -     returnNULL;//the original is py_none; - } -  - StaticPymethoddef testmodmethods[]= { -{"sum", Testmod_sum, Meth_varargs}, in{"Test", Testmod_test, Meth_varargs}, - {null, null}, to }; +  - Pymodinit_func Inittestmod () { thePy_initmodule ("Testmod", testmodmethods); *}

Then compile the above code to get

This testMod.dll is the dynamic library we need, rename it to Testmod.pyd (PYD in Python is actually a DLL) and copy it to your Python path/dlls directory.

Ok, you are done, you can use the Pyhon call below.

For code interpretation, refer to the original sticker of the code (link above)

Here are some tips for how to extend Python in C: the original sticker http://www.ibm.com/developerworks/cn/linux/l-pythc/

(If you just focus on the extension, skip to the extension section.)

First, Introduction

Python is a powerful high-level scripting language, its power not only in its own function, but also in its good scalability, because of this, Python has begun to be favored by more and more people, and has been repeatedly successfully used in various large-scale software system development process.

Unlike other common scripting languages, Python programmers can use the API provided by the Python language to extend the functionality of Python using C or C + +, which makes it possible to take advantage of Python's easy-to-use syntax and functionality, and to get a C or C + + Almost the same execution performance. Slow execution is a common feature of almost all scripting languages, and it is an important factor that has been criticized, and Python skillfully solves this problem with the organic combination of C language, which greatly expands the application scope of scripting language.

When developing a real software system in Python, it is often necessary to use C + + to extend Python. The most common scenario is that there is already a library written in C that requires some of the library's features in the Python language, which can be achieved with the extended functionality provided by Python. In addition, because Python is essentially a scripting language, some features Python implementations may be difficult to meet the actual software system for the performance of the requirements, you can also use the extension provided by Python, the key sections of the code are implemented in C or C + +, thus providing the execution performance of the program.

This article mainly introduces the C language extension interface provided by Python, and how to use these interfaces and the C + + language to extend the functionality of Python, supplemented by specific examples of how to implement Python's functional extensions.

Back to top of page

Second, the C language interface of Python

Python is a scripting language implemented in C, which has excellent openness and extensibility, and provides a convenient and flexible application interface (API) that enables C + + programmers to extend the capabilities of the Python interpreter at various levels. Before you can extend the functionality of Python using C/D + +, you must first grasp the C-language interface provided by Python interpretation.

2.1 Python object (Pyobject)

Python is an object-oriented scripting language in which all objects are represented in the Python interpreter as pyobject,pyobject structures that contain all the member pointers of the Python object and maintain the type information and reference count of the Python object. In Python's extended programming, once the Python object is processed in C or C + +, it means maintaining a pyobject structure.

In Python's C-language extension interface, most functions have one or more arguments for the Pyobject pointer type, and most of the return values are pyobject pointers.

2.2 Reference count

To simplify memory management, Python implements automatic garbage collection through a reference counting mechanism, and each object in Python has a reference count that counts how many times the object has been referenced in different places. Whenever a Python object is referenced, the corresponding reference count increases by 1, and whenever a Python object is destroyed, the corresponding reference is reduced by 1, and the Python object is actually removed from memory only if the reference count is zero.

The following example illustrates how the Python interpreter can use reference counting to manage Pyhon objects:

Example 1:refcount.pyclass refcount:    # etc.r1 = RefCount () # Reference count = 1R2 = R1         # Reference count is 2del (R1)         # Reference count is 1del (R2)         # Reference Meter Number is 0, delete object

Proper maintenance of reference counts is a key issue when working with Python objects in C/s + +, and it is easy to handle bad memory leaks. Python's C-language interface provides some macros to maintain reference counts, most commonly using py_incref () to increase the reference count of Python objects by 1, and py_decref () to reduce the reference count of Python objects by 1.

2.3 Data types

Python defines six types of data: integer, float, string, tuple, list, and dictionary, and first understand how to convert between C and Python data types when using the C language to extend Python functionality.

2.3.1 Integer, floating-point, and string

In Python's C language extension, it is relatively easy to use the three data types of integer, float, and string, just know how to build and maintain them. The following example shows how to use Python in the C language for these three types of data:

Example 2:typeifs.c//Build an integerpyobject* pInt = Py_buildvalue ("I", 2003); Assert (Pyint_check (pInt)); int i = Pyint_aslong ( PINT); Py_decref (PINT);//Build a floatpyobject* Pfloat = Py_buildvalue ("f", 3.14f); Assert (Pyfloat_check (pfloat)); float F = Pyfloat_asdouble (pfloat); Py_decref (pfloat);//Build a stringpyobject* pstring = Py_buildvalue ("s", "Python"); Assert (Pystring_check (pstring); int nlen = pystring_size (pstring); char* s = pystring_asstring (pstring); Py_decref (pstring);

2.3.2 tuples

A tuple in the Python language is a fixed-length array, and all non-keyword (non-keyword) parameters are passed as tuples when the Python interpreter invokes a method in the C language extension. The following example demonstrates how to use Python's tuple types in C:

Example 3:typetuple.c//Create the tuplepyobject* ptuple = pytuple_new (3); Assert (Pytuple_check (ptuple)); Assert (pytuple_size (ptuple) = = 3);//Set the Itempytuple_setitem (ptuple, 0, Py_buildvalue ("I", 2003)); Pytuple_setitem (ptuple, 1, Py_buildvalue ("F", 3.14f)); Pytuple_setitem (Ptuple, 2, Py_buildvalue ("s", "Python"));//Parse tuple itemsint i;float F;char *s;if (! Pyarg_parsetuple (ptuple, "ifs", &i, &f, &s))    pyerr_setstring (pyexc_typeerror, "invalid parameter"); /Cleanuppy_decref (ptuple);

2.3.3 List

The list in the Python language is a variable-length array, and the list is more flexible than the tuple, and the list can be used to randomly access the Python objects it stores. The following example demonstrates how to use the Python list type in C:

Example 4:typelist.c//Create the listpyobject* pList = pylist_new (3); New Referenceassert (Pylist_check (pList));//Set some initial valuesfor (int i = 0; i < 3; ++i)    Pylist_setitem (pLi St, I, Py_buildvalue ("I", i));//Insert an Itempylist_insert (PList, 2, Py_buildvalue ("s", "inserted"));//Append an Itempy List_append (PList, Py_buildvalue ("s", "appended"));//Sort the listpylist_sort (pList);//Reverse the Listpylist_ Reverse (pList);//Fetch and manipulate a list slicepyobject* Pslice = Pylist_getslice (PList, 2, 4); New Referencefor (int j = 0; J < pylist_size (Pslice); ++j) {  Pyobject *pvalue = Pylist_getitem (PList, j);  ASSERT (PValue);} Py_decref (Pslice);//Cleanuppy_decref (pList);

2.3.4 Dictionary

A dictionary in the Python language is a data type that is accessed based on a keyword. The following example demonstrates how to use the Python dictionary type in C:

Example 5:typedic.c//Create the dictionarypyobject* pdict = Pydict_new (); New Referenceassert (Pydict_check (pdict));//Add a few named Valuespydict_setitemstring (Pdict, "first",                      Py_ Buildvalue ("I", 2003)); Pydict_setitemstring (Pdict, "second",                      Py_buildvalue ("F", 3.14f));//Enumerate all named valuespyobject* Pkeys = Pydict_keys (); New Referencefor (int i = 0; i < pylist_size (Pkeys); ++i) {  Pyobject *pkey = Pylist_getitem (Pkeys, i);  Pyobject *pvalue = Pydict_getitem (pdict, PKey);  ASSERT (PValue);} Py_decref (Pkeys);//Remove a named Valuepydict_delitemstring (Pdict, "second");//Cleanuppy_decref (PDICT);

Back to top of page

Iii. Python's C language Extension 3.1 module package

After understanding the C language interface of Python, you can use these interfaces provided by the Python interpreter to write the C language extension of Python, assuming the following C-language function:

Example 6:example.cint fact (int n) {  if (n <= 1)     return 1;  else     return n * fact (n-1);}

The function is to calculate the factorial of a given natural number, if you want to invoke the function in the Python interpreter, you should first implement it as a module in Python, which requires writing the appropriate encapsulation interface as follows:

Example 7:wrap.c#include <python.h>pyobject* wrap_fact (pyobject* self, pyobject* args) {  int n, result;    if (! Pyarg_parsetuple (args, "i:fact", &n))    return NULL;  result = fact (n);  Return Py_buildvalue ("I", result);} Static Pymethoddef examplemethods[] = {  {"fact", Wrap_fact, Meth_varargs, "Caculate n!"},  {NULL, null}};void Initexample () {  pyobject* m;  m = Py_initmodule ("Example", Examplemethods);}

A typical Python extension should contain at least three parts: Export functions, method lists, and initialization functions.

3.2 Exporting functions

To use a function in the C language in the Python interpreter, first write the appropriate export function for it, and the export function in the example above is wrap_fact. In Python's C language extension, all the exported functions have the same function prototypes:

Pyobject* method (pyobject* self, pyobject* args);

The function is an interface between the Python interpreter and the C function, with two parameters: Self and args. The parameter self is only used when the C function is implemented as an inline method (built-in), usually with a value of NULL (NULL). The parameter args contains all the parameters that the Python interpreter will pass to the C function, usually using the function pyarg_parsetuple () provided by the Python C extension interface to obtain these parameter values.

All the exported functions return a pyobject pointer, and if the corresponding C function does not have a true return value (that is, the return value type is void), then a global None object (Py_none) should be returned, and its reference count will be increased by 1, as follows:

Pyobject* method (Pyobject *self, Pyobject *args) {  py_incref (py_none);  return py_none;}
3.3 Method List

The method list shows all the methods that can be used by the Python interpreter, and the corresponding method list is:

Static Pymethoddef examplemethods[] = {{  "fact", Wrap_fact, Meth_varargs, "Caculate n!"},  {null, null}};

Each item in the method list consists of four parts: The method name, the export function, the parameter passing method, and the method description. The method name is the name used when calling the method from the Python interpreter. The parameter passing method specifies the specific form of the python to pass the parameter to the C function, the optional two ways are Meth_varargs and meth_keywords, in which Meth_varargs is the standard form of the parameter passing, It passes the parameters between the Python interpreter and the C function through a python tuple, and if Meth_keyword is used, the Python interpreter and the C function will pass the argument between the two through the Python dictionary type.

3.4 Initialization function

All Python extensions must have an initialization function so that the Python interpreter can initialize the module correctly. The Python interpreter specifies that all function names for the initialization function must begin with INIT and add the module's name. For module example, the corresponding initialization function is:

void Initexample () {  pyobject* m;  m = Py_initmodule ("Example", Examplemethods);}

When the Python interpreter needs to import the module, it will find the appropriate initialization function based on the name of the module and, once found, call the function to do the appropriate initialization, and initialize the function by calling the function Py_initmodule () provided by Python's C extension interface. To register all the available methods in the module with the Python interpreter.

3.5 Compiling Links

To use the extension module written in C in the Python interpreter, you must compile it into the form of a dynamic-link library. The following is an example of how to compile a Python extension module written in C into a dynamic-link library, Redhat Linux 8.0:

[Email protected] code]$ gcc-fpic-c-i/usr/include/python2.2                     -i/usr/lib/python2.2/config                     example.c Wrapper.c[[email protected] code]$ gcc-shared-o example.so example.o wrapper.o
3.6 Introducing the Python interpreter

Once the dynamic-link library of the Python extension is generated, the extension can be used in the Python interpreter, as is the case with Python's own module, which is also introduced through the Import command, as follows:

[Email protected] code]$ Pythonpython 2.2.1 (#1, 2002, 12:15:30) [GCC 3.2 20020822 (Red Hat Linux Rawhide 3.2-4)] O n Linux2type "Help", "copyright", "credits" or "license" for more information.>>> import example>>> exam Ple.fact (4) 24>>>

Back to top of page

Iv. concluding remarks

As a powerful scripting language, Python will be more widely used in various fields. To overcome the slow execution of scripting languages, Python provides the appropriate C language extension interface, which, by implementing the C language with the key code that affects execution performance, greatly improves the speed at which scripts written in Python can run at runtime to meet actual needs.

Using Codeblocks to implement the C language extension to Python under Windows

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.