spammodule.c 988 B

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