test.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <Python.h>
  2. static int numargs=0;
  3. static PyObject* emb_numargs(PyObject *self, PyObject *args)
  4. {
  5. if(!PyArg_ParseTuple(args, ":numargs"))
  6. return NULL;
  7. return Py_BuildValue("i", numargs);
  8. }
  9. static PyMethodDef EmbMethods[] = {
  10. {"numargs", emb_numargs, METH_VARARGS,
  11. "Return the number of arguments received by the process."},
  12. {NULL, NULL, 0, NULL}
  13. };
  14. #if PY_VERSION_HEX >= 0x03000000
  15. /* Python 3.x code */
  16. static struct PyModuleDef embmodule = {
  17. PyModuleDef_HEAD_INIT,
  18. "emb", /* name of module */
  19. "emb_doc", /* module documentation, may be NULL */
  20. -1, /* size of per-interpreter state of the module,
  21. or -1 if the module keeps state in global variables. */
  22. EmbMethods
  23. };
  24. PyMODINIT_FUNC
  25. PyInit_emb(void)
  26. {
  27. (void) PyModule_Create(&embmodule);
  28. }
  29. #endif
  30. int main(int argc, char *argv[])
  31. {
  32. #if PY_VERSION_HEX >= 0x03000000
  33. PyImport_AppendInittab("emb", PyInit_emb);
  34. #endif
  35. Py_Initialize();
  36. numargs = argc;
  37. #if PY_VERSION_HEX < 0x03000000
  38. Py_InitModule("emb", EmbMethods);
  39. #endif
  40. PyRun_SimpleString("import emb; print('Number of arguments', emb.numargs())");
  41. Py_Finalize();
  42. return 0;
  43. }