Extend Python's function with C language

xiaoxiao2021-03-06  18

Xiao Wenpeng (xiaowp@263.net) Master of Computer Department, Beijing University of Science and Technology, February 2003

Pyton and C have their own advantages and disadvantages. It uses Python development programs fast, high reliability, and there are many ready-made modules available, but the execution speed is relatively slow; the C language is just the opposite, its execution is fast, but development low efficiency. In order to make full use of their own advantages, better practice is to develop the entire software framework with Python, and implement their key modules in C language. This article describes how to use C language to extend Python's function and supplemented with specific instances to describe how to write Python's extension modules.

I. Introduction Python is a powerful advanced scripting language. Its power is not only in its own function, but also in its good scalability. For this reason, Python has begun to receive more and more The favor of people, and has been successfully applied to the development of various large software systems. Unlike other common scripting languages, Python programmers can use C or C to functionally expand the Python language, so that Python can make flexible grammar and function, and C or C almost the same performance performance. The execution speed is slower, and it is also an important factor that is accused by people. Python has smartfully solved this problem by subtly solving the problem with the C language, so that the application range of the scripting language is very Large expansion. When developing a actual software system with Python, it is often necessary to use C / C to expand Python. The most common situation is that there is already a library written in C, which requires some functions of the library in the Python language, which can be implemented with the extended features provided by Python. In addition, since Python is essentially a scripting language, some functions may be difficult to meet the actual software system's requirements for execution efficiency, and can also be used by the extended functionality provided by Python. C or C is implemented to provide performance performance. This article mainly introduces the C language extension interface provided by Python, and how to use these interfaces and C / C languages ​​to functionally extend Python, and supplemented with specific instances to describe how to implement Python. Second, Python's C language interface Python is a scripting language implemented in C language, which itself has excellent openness and scalability, and provides a convenient and flexible application interface (API), making C / C programmers The function of the Python interpreter can be expanded at each level. You must first grasp the C language interface provided by Python before using C / C to perform the function extension. 2.1 Python Object Python is an object-oriented scripting language, all objects are represented in the Python interpreter, and the PyObject structure contains all member pointers of the Python object, and counts the type information and references to the Python object. Maintenance. When performing Python extension programming, once you want to process the Python object in C or C , it means that a PyObject structure is maintained. In Python's C language extension interface, most of the functions have one or more parameters as the PyObject pointer type, and the return value is mostly a PyObject pointer. 2.2 Reference count To simplify memory management, Python implements an automatic garbage collection function by reference counting mechanism. Each object in Python has a reference count, used to count how many times the object is quoted in different places. Whenever a Python object is referenced, the corresponding reference count is 1, whenever a Python object is destroyed, the corresponding reference will be reduced. Only when the reference count is zero, the Python object is really deleted from memory. The following example illustrates how the Python interpreter uses a reference count to manage the Pyhon object:

Example 1: Refcount.pyclass Refcount: # etc. R1 = Refcount () # 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽When processing the Python object in C , the correct maintenance of the reference count is a key issue, and the processing is not easy to generate memory leaks. Python's C language interface provides some macro to maintain the reference count, the most common is to increase the reference count of the PyThon object by py_incref () to increase the reference count of the Python object with py_Decref () 1.2.3 Data type Python defines six data types: integer, floating point, string, tuple group, list, and dictionary, first to understand how to expand Python using the C language, first learn how to intermove how to C and Python Conversion. 2.3.1 Integer, floating-point and string In Python's C language extension, it is relatively simple when using three data types of floating point, and strings, just know how to generate and maintain them. Yes. The following example gives these three data types that use Python in the C language: 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 The tuple in the group Python language is a length fixed array. When the Python interpreter invokes the method in the C language extension, all non-keyword parameters are transmitted in a tetan. The following example demonstrates how to use Python type types in C language:

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 Python language The list is an array of length variables, and the list is more flexible, and the list can be randomly accessed using the list. The following example demonstrates how to use Python in C language Type: Example 4: Typelist.c // create the listpyObject * plist = pylist_new (3); // new reasonasurestert (pylist_check (plist); // set Some Initial Valuesfor (int i = 0; i <3; i) pylist_setitem (PLIST, I, PY_BUILDVALUE ("I", I)); // INSERT An Itempylist_insert (PLIST, 2, PY_BUILDVALUE ("S", "INSERTED ")); // append an itemPyList_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

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

Example 7: Wrap.c # include 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 inItemple () {PyObject * m; M = py_initmodule ("eXample", exampleMethods;} A typical Python extension module should at least include three parts: export functions, method list, and initialization functions. 3.2 Export Functions To use a function in the C language in the Python interpreter, first prepare the corresponding export function, the export function in the above example is Wrap_FACT. In Python's C language extension, all export functions have the same function prototype: pyObject * method (pyobject * args); this function is an interface for interacting the Python interpreter and C function with two Parameters: Self and Args. The parameter SELF is only used when the C function is implemented as a built-in method, and the value of the parameter is null. Parameter Args contains all parameters to pass to the C function, usually use the function Pyarg_Parsetuple () provided by Python's C language extension interface to get these parameter values. All export functions returns a PyObject pointer. If the corresponding C function does not have a real return value (ie the return value type void), a global NONE object (PY_NONE) should be returned, and the reference count is increased by 1, as follows As shown: PyObject * method (pyobject * self, pyobject * args) {py_incref (py_none);} 3.3 Method list method list gives all methods used by the Python interpreter, the above example corresponding method list for:

Static Pymethoddef ExampleMethods [] = {{"Fact", Wrap_FACT, Meth_VARARGS, "CACLATE N!"}, {null, null}}; each item in the method list consists of four parts: method name, export function, parameter transmission Methods and method descriptions. The method name is the name used when calling the method from the Python interpreter. The parameter transfer method specifies the specific form of Python to pass the parameters to the C function. Optional two ways is Meth_VARARGS and METH_KEYWORDS, where Meth_VARARGS is the standard form of parameter passes, which passes Python's tuplers in the Python interpreter and C function. Transfer parameters, if the method_keyword method is used, the Python interpreter and the C function will be passed through the Python dictionary type between the two. 3.4 Initialization Functions All Python Expansion Modules must have an initialization function so that the Python interpreter can initialize the module. The Python Interpreter specifies that all of the function names of all initialization must begin with init and add the name of the module. 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, the name of the module is based on the name of the module. Find the corresponding initialization function, once you find the function to make the corresponding initialization work, the initialization function will be used to register all the modules to the Python interpreter by calling the function py_initmodule () provided by calling Python's C language extension interface. Methods. 3.5 Compile Links Use the extended modules written in the Python interpreter to compile them into a dynamic link library. The following is taken as an example for Redhat Linux 8.0, describe how the Python extension module written into a dynamic link library:

[xiaowp @ Gary Code] $ gcc-fpic -c -i / usr / include / python2.2/ -i /usr/lib/python2.2/config / example.c wrapper.c [xiaowp @ Gary Code] $ GCC -shared -o example.so example.o wrapper.o3.6 Introducing a Python Interpreter When generating a dynamic link library of a Python extension module, you can use the expansion module in the Python interpreter, the same as the module itself with Python. The expansion module is also used after introducing the import command, as shown below:

[xiaowp @ Gary Code] $ PythonPython 2.2.1 (# 1, AUG 30 2002, 12:15:30) [GCC 3.2 20020822 (Red Hat Linux Rawhide 3.2-4)] on Linux2type "Help", "CopyRight", " Credits "or" license "for more information. >>> Import Example >>> EXAMPLE.FACT (4) 24 >>> Four, end language As a powerful scripting language, Python will be more widely used in various fields . In order to overcome the problem of slow execution of the scripting language, Python provides the corresponding C-language extension interface. By implementing the key code affecting the performance performance, it can largely improve the speed of the script written in Python in the speed of the runtime. To meet the actual needs. 5. References can be made from python (http://www.python.org) website to understand all the contents of Python. You can find a formal Python C / API document on the Python website (http://www.python.org/doc/current/api/api.html). You can find a formally written Python extension module on the Python website (http://www.python.org/doc/current/api/api.html). 6. About the author Xiao Wen Peng, a master's degree in the computer department of Beijing Institute of Technology, mainly engaged in the research of operating systems and distributed computing environments, love Linux and Python. You can contact him through xiaowp@263.net.

转载请注明原文地址:https://www.9cbs.com/read-50091.html

New Post(0)