cpython/Doc/includes/capi-extension/spammodule-01.c

56 lines
1.1 KiB
C
Raw Normal View History

/* This file needs to be kept in sync with the tutorial
* at Doc/extending/first-extension-module.rst
*/
2025-11-28 18:11:04 +01:00
/// Includes
#include <Python.h>
#include <stdlib.h>
/// Implementation of spam.system
static PyObject *
spam_system(PyObject *self, PyObject *arg)
{
const char *command = PyUnicode_AsUTF8(arg);
if (command == NULL) {
return NULL;
}
int status = system(command);
PyObject *result = PyLong_FromLong(status);
return result;
2025-11-28 18:11:04 +01:00
}
/// Module method table
static PyMethodDef spam_methods[] = {
{
.ml_name="system",
.ml_meth=spam_system,
.ml_flags=METH_O,
.ml_doc="Execute a shell command.",
2025-11-28 18:11:04 +01:00
},
{NULL, NULL, 0, NULL} /* Sentinel */
};
/// Module slot table
2025-12-03 16:12:32 +01:00
static PyModuleDef_Slot spam_slots[] = {
2025-11-28 18:11:04 +01:00
{Py_mod_name, "spam"},
{Py_mod_doc, "A wonderful module with an example function"},
2025-11-28 18:11:04 +01:00
{Py_mod_methods, spam_methods},
2025-12-01 17:45:03 +01:00
{0, NULL}
2025-11-28 18:11:04 +01:00
};
/// Export hook prototype
PyMODEXPORT_FUNC PyModExport_spam(void);
/// Module export hook
PyMODEXPORT_FUNC
PyModExport_spam(void)
{
return spam_slots;
2025-11-28 18:11:04 +01:00
}