Added updates with respect to recent changes to TimedRotatingFileHandler.
[python.git] / Doc / includes / run-func.c
blob5a7df0d98056bc371aaaf4e4fdadf616527803e9
1 #include <Python.h>
3 int
4 main(int argc, char *argv[])
6 PyObject *pName, *pModule, *pDict, *pFunc;
7 PyObject *pArgs, *pValue;
8 int i;
10 if (argc < 3) {
11 fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
12 return 1;
15 Py_Initialize();
16 pName = PyString_FromString(argv[1]);
17 /* Error checking of pName left out */
19 pModule = PyImport_Import(pName);
20 Py_DECREF(pName);
22 if (pModule != NULL) {
23 pFunc = PyObject_GetAttrString(pModule, argv[2]);
24 /* pFunc is a new reference */
26 if (pFunc && PyCallable_Check(pFunc)) {
27 pArgs = PyTuple_New(argc - 3);
28 for (i = 0; i < argc - 3; ++i) {
29 pValue = PyInt_FromLong(atoi(argv[i + 3]));
30 if (!pValue) {
31 Py_DECREF(pArgs);
32 Py_DECREF(pModule);
33 fprintf(stderr, "Cannot convert argument\n");
34 return 1;
36 /* pValue reference stolen here: */
37 PyTuple_SetItem(pArgs, i, pValue);
39 pValue = PyObject_CallObject(pFunc, pArgs);
40 Py_DECREF(pArgs);
41 if (pValue != NULL) {
42 printf("Result of call: %ld\n", PyInt_AsLong(pValue));
43 Py_DECREF(pValue);
45 else {
46 Py_DECREF(pFunc);
47 Py_DECREF(pModule);
48 PyErr_Print();
49 fprintf(stderr,"Call failed\n");
50 return 1;
53 else {
54 if (PyErr_Occurred())
55 PyErr_Print();
56 fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
58 Py_XDECREF(pFunc);
59 Py_DECREF(pModule);
61 else {
62 PyErr_Print();
63 fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
64 return 1;
66 Py_Finalize();
67 return 0;