Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / ext / run-func.txt
CommitLineData
86530b38
AT
1#include <Python.h>
2
3int
4main(int argc, char *argv[])
5{
6 PyObject *pName, *pModule, *pDict, *pFunc;
7 PyObject *pArgs, *pValue;
8 int i;
9
10 if (argc < 3) {
11 fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
12 return 1;
13 }
14
15 Py_Initialize();
16 pName = PyString_FromString(argv[1]);
17 /* Error checking of pName left out */
18
19 pModule = PyImport_Import(pName);
20 Py_DECREF(pName);
21
22 if (pModule != NULL) {
23 pFunc = PyDict_GetAttrString(pModule, argv[2]);
24 /* pFunc is a new reference */
25
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;
35 }
36 /* pValue reference stolen here: */
37 PyTuple_SetItem(pArgs, i, pValue);
38 }
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);
44 }
45 else {
46 Py_DECREF(pFunc);
47 Py_DECREF(pModule);
48 PyErr_Print();
49 fprintf(stderr,"Call failed\n");
50 return 1;
51 }
52 }
53 else {
54 if (PyErr_Occurred())
55 PyErr_Print();
56 fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
57 }
58 Py_XDECREF(pFunc);
59 Py_DECREF(pModule);
60 }
61 else {
62 PyErr_Print();
63 fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
64 return 1;
65 }
66 Py_Finalize();
67 return 0;
68}