mirror of
https://github.com/python/cpython.git
synced 2026-05-06 04:37:33 -04:00
508b49845d
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
59 lines
1.2 KiB
C
59 lines
1.2 KiB
C
/* This file needs to be kept in sync with the tutorial
|
|
* at Doc/extending/first-extension-module.rst
|
|
*/
|
|
|
|
/// Includes
|
|
|
|
#include <Python.h>
|
|
#include <stdlib.h> // for system()
|
|
|
|
/// Implementation of spam.system
|
|
|
|
static PyObject *
|
|
spam_system(PyObject *self, PyObject *arg)
|
|
{
|
|
const char *command = PyUnicode_AsUTF8AndSize(arg, NULL);
|
|
if (command == NULL) {
|
|
return NULL;
|
|
}
|
|
int status = system(command);
|
|
PyObject *result = PyLong_FromLong(status);
|
|
return result;
|
|
}
|
|
|
|
/// Module method table
|
|
|
|
static PyMethodDef spam_methods[] = {
|
|
{
|
|
.ml_name="system",
|
|
.ml_meth=spam_system,
|
|
.ml_flags=METH_O,
|
|
.ml_doc="Execute a shell command.",
|
|
},
|
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
|
};
|
|
|
|
/// Module slot table
|
|
|
|
PyABIInfo_VAR(abi_info);
|
|
|
|
static PySlot spam_slots[] = {
|
|
PySlot_STATIC_DATA(Py_mod_abi, &abi_info),
|
|
PySlot_STATIC_DATA(Py_mod_name, "spam"),
|
|
PySlot_STATIC_DATA(Py_mod_doc, "A wonderful module with an example function"),
|
|
PySlot_STATIC_DATA(Py_mod_methods, spam_methods),
|
|
PySlot_END
|
|
};
|
|
|
|
/// Export hook prototype
|
|
|
|
PyMODEXPORT_FUNC PyModExport_spam(void);
|
|
|
|
/// Module export hook
|
|
|
|
PyMODEXPORT_FUNC
|
|
PyModExport_spam(void)
|
|
{
|
|
return spam_slots;
|
|
}
|