main(int argc, char *argv[])
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
pName = PyString_FromString(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
pFunc = PyDict_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc)) {
pArgs = PyTuple_New(argc - 3);
for (i = 0; i < argc - 3; ++i) {
pValue = PyInt_FromLong(atoi(argv[i + 3]));
fprintf(stderr, "Cannot convert argument\n");
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, i, pValue);
pValue = PyObject_CallObject(pFunc, pArgs);
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
fprintf(stderr,"Call failed\n");
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);