4 main(int argc
, char *argv
[])
6 PyObject
*pName
, *pModule
, *pDict
, *pFunc
;
7 PyObject
*pArgs
, *pValue
;
11 fprintf(stderr
,"Usage: call pythonfile funcname [args]\n");
16 pName
= PyString_FromString(argv
[1]);
17 /* Error checking of pName left out */
19 pModule
= PyImport_Import(pName
);
20 if (pModule
!= NULL
) {
21 pDict
= PyModule_GetDict(pModule
);
22 /* pDict is a borrowed reference */
24 pFunc
= PyDict_GetItemString(pDict
, argv
[2]);
25 /* pFun: Borrowed reference */
27 if (pFunc
&& PyCallable_Check(pFunc
)) {
28 pArgs
= PyTuple_New(argc
- 3);
29 for (i
= 0; i
< argc
- 3; ++i
) {
30 pValue
= PyInt_FromLong(atoi(argv
[i
+ 3]));
32 fprintf(stderr
, "Cannot convert argument\n");
35 /* pValue reference stolen here: */
36 PyTuple_SetItem(pArgs
, i
, pValue
);
38 pValue
= PyObject_CallObject(pFunc
, pArgs
);
40 printf("Result of call: %ld\n", PyInt_AsLong(pValue
));
45 fprintf(stderr
,"Call failed\n");
49 /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
53 fprintf(stderr
, "Cannot find function \"%s\"\n", argv
[2]);
59 fprintf(stderr
, "Failed to load \"%s\"\n", argv
[1]);