spam.c 956 B

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