ยปCore Development>Code coverage>Doc/includes/run-func.c

Python code coverage for Doc/includes/run-func.c

#countcontent
1n/a#include <Python.h>
2n/a
3n/aint
4n/amain(int argc, char *argv[])
5n/a{
6n/a PyObject *pName, *pModule, *pDict, *pFunc;
7n/a PyObject *pArgs, *pValue;
8n/a int i;
9n/a
10n/a if (argc < 3) {
11n/a fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
12n/a return 1;
13n/a }
14n/a
15n/a Py_Initialize();
16n/a pName = PyUnicode_DecodeFSDefault(argv[1]);
17n/a /* Error checking of pName left out */
18n/a
19n/a pModule = PyImport_Import(pName);
20n/a Py_DECREF(pName);
21n/a
22n/a if (pModule != NULL) {
23n/a pFunc = PyObject_GetAttrString(pModule, argv[2]);
24n/a /* pFunc is a new reference */
25n/a
26n/a if (pFunc && PyCallable_Check(pFunc)) {
27n/a pArgs = PyTuple_New(argc - 3);
28n/a for (i = 0; i < argc - 3; ++i) {
29n/a pValue = PyLong_FromLong(atoi(argv[i + 3]));
30n/a if (!pValue) {
31n/a Py_DECREF(pArgs);
32n/a Py_DECREF(pModule);
33n/a fprintf(stderr, "Cannot convert argument\n");
34n/a return 1;
35n/a }
36n/a /* pValue reference stolen here: */
37n/a PyTuple_SetItem(pArgs, i, pValue);
38n/a }
39n/a pValue = PyObject_CallObject(pFunc, pArgs);
40n/a Py_DECREF(pArgs);
41n/a if (pValue != NULL) {
42n/a printf("Result of call: %ld\n", PyLong_AsLong(pValue));
43n/a Py_DECREF(pValue);
44n/a }
45n/a else {
46n/a Py_DECREF(pFunc);
47n/a Py_DECREF(pModule);
48n/a PyErr_Print();
49n/a fprintf(stderr,"Call failed\n");
50n/a return 1;
51n/a }
52n/a }
53n/a else {
54n/a if (PyErr_Occurred())
55n/a PyErr_Print();
56n/a fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
57n/a }
58n/a Py_XDECREF(pFunc);
59n/a Py_DECREF(pModule);
60n/a }
61n/a else {
62n/a PyErr_Print();
63n/a fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
64n/a return 1;
65n/a }
66n/a if (Py_FinalizeEx() < 0) {
67n/a return 120;
68n/a }
69n/a return 0;
70n/a}