Code # 1:
The title will match the library that was compiled separately. The code below illustrates the basics of writing extension functions with this assumption.
Code # 2:
Code # 3: Module Method Table and Structure
Code # 4: Create a python file
setup.py to build an extension. strong >
Code No. 5: Now just use python3 buildlib.py
build_ext --inplace to build the resulting library.
bash% python3 setup.py build_ext --inplace running build_ext building `sample` extension gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I / usr / local / include / python3.3m -c pysample.c -o build / temp.macosx -10.6-x86_64-3.3 / pysample.o gcc -bundle -undefined dynamic_lookup build / temp.macosx-10.6-x86_64-3 .3 / pysample.o -L / usr / local / lib -lsample -o sample.so bash%
The above code will create a shared library named sample.so . p>
Code # 6:
gcd = 7 distance = ( 5, 2)
" Extending and embedding the Python interpreter " — this is Python documentation that you can read before trying to do any handwritten extensions.
In extension modules, functions can be written as shown in the code snippet below.
Code # 4:
PyObject * args) and returns a new Python object as a result. The self argument of the function is not used for simple extension functions, but comes into play if you want to define new classes or object types in C.
PyArg_ParseTuple ()strong> is used to convert values from Python to C representation. As input, it accepts a format string that specifies the required values, such as "i" for an integer and "d" for double, as well as the addresses of C variables in to put the converted results.
Py_BuildValue()is used to create Python objects from C data types. It also accepts format code to indicate the desired type. In extension functions, it is used to return results back to Python. One feature of
Py_BuildValue ()is that it can create more complex kinds of objects such as tuples and dictionaries.