Tin mới
Extending and Embedding the Python Interpreter
Using the C API: Assorted topics
This tutorial will take you through creating a simple Python extension module written in C or C++.
This tutorial will take you through creating a simple Python extension module written in C or C++.
This tutorial will take you through creating a simple Python extension module written in C or C++.
The exception you got when you tried to import the module told you that Python is looking for a “module export function”, also known as a module export hook. Let’s define one.
Rather than NULL, the export hook should return the information needed to create a module. Let’s start with the basics: the name and docstring.
To expose the system() C function directly to Python, we’ll need to write a layer of glue code to convert arguments from Python objects to C values, and the C return value back to Python.
To expose the system() C function directly to Python, we’ll need to write a layer of glue code to convert arguments from Python objects to C values, and the C return value back to Python.
To expose the system() C function directly to Python, we’ll need to write a layer of glue code to convert arguments from Python objects to C values, and the C return value back to Python.
To expose the C function to Python, you will need to provide several pieces of information in a structure called PyMethodDef [4]:
Now, let’s take a look at the return value. Instead of None, we’ll want spam.system to return a number – that is, a Python int object. Eventually this will be the exit code of a system command, but let’s start with a fix
Now, let’s take a look at the return value. Instead of None, we’ll want spam.system to return a number – that is, a Python int object. Eventually this will be the exit code of a system command, but let’s start with a fix
Our C function, spam_system(), takes two arguments. The first one, PyObject *self, will be set to the spam module object. This isn’t useful in our case, so we’ll ignore it.
Our C function, spam_system(), takes two arguments. The first one, PyObject *self, will be set to the spam module object. This isn’t useful in our case, so we’ll ignore it.