test.c (1198B)
1 #include <Python.h> 2 3 static int numargs=0; 4 5 static PyObject* emb_numargs(PyObject *self, PyObject *args) 6 { 7 if(!PyArg_ParseTuple(args, ":numargs")) 8 return NULL; 9 return Py_BuildValue("i", numargs); 10 } 11 12 static PyMethodDef EmbMethods[] = { 13 {"numargs", emb_numargs, METH_VARARGS, 14 "Return the number of arguments received by the process."}, 15 {NULL, NULL, 0, NULL} 16 }; 17 18 19 #if PY_VERSION_HEX >= 0x03000000 20 21 /* Python 3.x code */ 22 23 static struct PyModuleDef embmodule = { 24 PyModuleDef_HEAD_INIT, 25 "emb", /* name of module */ 26 "emb_doc", /* 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 EmbMethods 30 }; 31 32 PyMODINIT_FUNC 33 PyInit_emb(void) 34 { 35 (void) PyModule_Create(&embmodule); 36 } 37 38 #endif 39 40 41 int main(int argc, char *argv[]) 42 { 43 #if PY_VERSION_HEX >= 0x03000000 44 PyImport_AppendInittab("emb", PyInit_emb); 45 #endif 46 47 Py_Initialize(); 48 numargs = argc; 49 50 #if PY_VERSION_HEX < 0x03000000 51 Py_InitModule("emb", EmbMethods); 52 #endif 53 54 PyRun_SimpleString("import emb; print('Number of arguments', emb.numargs())"); 55 Py_Finalize(); 56 return 0; 57 }