2001-08-03 22:58:36 -03:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
PyObject *pName, *pModule, *pDict, *pFunc;
|
|
|
|
PyObject *pArgs, *pValue;
|
2002-12-02 00:40:21 -04:00
|
|
|
int i;
|
2001-08-03 22:58:36 -03:00
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_Initialize();
|
|
|
|
pName = PyString_FromString(argv[1]);
|
|
|
|
/* Error checking of pName left out */
|
|
|
|
|
|
|
|
pModule = PyImport_Import(pName);
|
2002-12-16 20:40:38 -04:00
|
|
|
Py_DECREF(pName);
|
|
|
|
|
2001-08-03 22:58:36 -03:00
|
|
|
if (pModule != NULL) {
|
2006-02-15 02:55:57 -04:00
|
|
|
pFunc = PyObject_GetAttrString(pModule, argv[2]);
|
2005-07-12 10:17:59 -03:00
|
|
|
/* pFunc is a new reference */
|
2001-08-03 22:58:36 -03:00
|
|
|
|
|
|
|
if (pFunc && PyCallable_Check(pFunc)) {
|
|
|
|
pArgs = PyTuple_New(argc - 3);
|
|
|
|
for (i = 0; i < argc - 3; ++i) {
|
|
|
|
pValue = PyInt_FromLong(atoi(argv[i + 3]));
|
|
|
|
if (!pValue) {
|
2002-12-16 20:40:38 -04:00
|
|
|
Py_DECREF(pArgs);
|
|
|
|
Py_DECREF(pModule);
|
2001-08-03 22:58:36 -03:00
|
|
|
fprintf(stderr, "Cannot convert argument\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* pValue reference stolen here: */
|
|
|
|
PyTuple_SetItem(pArgs, i, pValue);
|
|
|
|
}
|
|
|
|
pValue = PyObject_CallObject(pFunc, pArgs);
|
2002-12-16 20:40:38 -04:00
|
|
|
Py_DECREF(pArgs);
|
2001-08-03 22:58:36 -03:00
|
|
|
if (pValue != NULL) {
|
|
|
|
printf("Result of call: %ld\n", PyInt_AsLong(pValue));
|
|
|
|
Py_DECREF(pValue);
|
|
|
|
}
|
|
|
|
else {
|
2005-07-12 10:17:59 -03:00
|
|
|
Py_DECREF(pFunc);
|
2002-12-16 20:40:38 -04:00
|
|
|
Py_DECREF(pModule);
|
2001-08-03 22:58:36 -03:00
|
|
|
PyErr_Print();
|
|
|
|
fprintf(stderr,"Call failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-12-16 20:40:38 -04:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
PyErr_Print();
|
2001-08-03 22:58:36 -03:00
|
|
|
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
|
|
|
|
}
|
2005-07-12 10:17:59 -03:00
|
|
|
Py_XDECREF(pFunc);
|
2001-08-03 22:58:36 -03:00
|
|
|
Py_DECREF(pModule);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_Print();
|
|
|
|
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Py_Finalize();
|
|
|
|
return 0;
|
|
|
|
}
|