spammodule.c (988B)
1 #include <Python.h> 2 3 static PyObject * 4 spam_system(PyObject *self, PyObject *args) 5 { 6 const char *command; 7 int sts; 8 9 if (!PyArg_ParseTuple(args, "s", &command)) 10 return NULL; 11 sts = system(command); 12 return Py_BuildValue("i", sts); 13 } 14 15 static PyMethodDef SpamMethods[] = { 16 {"system", spam_system, METH_VARARGS, 17 "Execute a shell command."}, 18 {NULL, NULL, 0, NULL} /* Sentinel */ 19 }; 20 21 #if PY_VERSION_HEX >= 0x03000000 22 23 /* Python 3.x code */ 24 25 static struct PyModuleDef spammodule = { 26 PyModuleDef_HEAD_INIT, 27 "spam", /* name of module */ 28 "spam_doc", /* module documentation, may be NULL */ 29 -1, /* size of per-interpreter state of the module, 30 or -1 if the module keeps state in global variables. */ 31 SpamMethods 32 }; 33 34 PyMODINIT_FUNC 35 PyInit_spam(void) 36 { 37 (void) PyModule_Create(&spammodule); 38 } 39 40 #else 41 42 /* Python 2.x code */ 43 44 PyMODINIT_FUNC 45 initspam(void) 46 { 47 (void) Py_InitModule("spam", SpamMethods); 48 } 49 50 #endif