spam.c 980 B

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