Writing a simple C extension module directly using Python’s extension API and no other tools. It is straightforward to make a handcrafted extension module for a simple C code. But first, we have to make sure that the C code has a proper header file.
Code #1 :
The header would correspond to a library that has been separately compiled. The code below illustrates the basics of writing extension functions, following this assumption.
Code #3 : Module method table and structure
Code #4: Creating a
setup.py python file for building the extension module.
Code #5: Now simply 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 called sample.so.
Code #6 :
gcd = 7 distance = (5, 2)
“Extending and Embedding the Python Interpreter” is a Python’s documentation that can be consulted before attempting any kind of handwritten extension.
In extension modules, functions can be written as shown in code snippet below.
Code #4 :
- PyObject – C data type that represents any Python object. At a very high level, an extension function is a C function that receives a tuple of Python objects (in
PyObject *args) and returns a new Python object as a result. The self argument to the function is unused for simple extension functions, but comes into play should you want to define new classes or object types in C.
PyArg_ParseTuple()function is used to convert values from Python to a C representation. As input, it takes a format string that indicates the required values, such as “i” for integer and “d” for double, as well as the addresses of C variables in which to place the converted results.
Py_BuildValue()function is used to create Python objects from C data types. It also accepts a format code to indicate the desired type. In the extension functions, it is used to return results back to Python. One feature of
Py_BuildValue()is that it can build more complicated kinds of objects, such as tuples and dictionaries.
- C API from Extension Module in Python | Set 1
- C API from Extension Module in Python | Set 2
- Python | Passing Filenames to Extension in C
- Python | Sort and store files with same extension
- Python | Opaque Pointers in C extension modules
- Python | Extension function operating on Arrays
- Python | Repeat and Multiply list extension
- grp module in Python
- pwd module in Python
- OS Module in Python with Examples
- Import module in Python
- Fraction module in Python
- OS Path module in Python
- Pylatex module in python
- struct module in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.