1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// test harness for python_plugin
#include "python_plugin.hh"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define CHK(bad, fmt, ...) \
do { \
if (bad) { \
fprintf(stderr,fmt, ## __VA_ARGS__); \
exit(1); \
} \
} while(0)
const char *strstore(const char *s) { return strdup(s); }
extern PythonPlugin *python_plugin;
void analyze(const char *what,bp::object retval)
{
PyObject *res_str = PyObject_Str(retval.ptr());
Py_XDECREF(res_str);
printf("analyze: %s returned '%s' - '%s'\n",what,
PyString_AsString(res_str),
retval.ptr()->ob_type->tp_name);
}
void exercise(PythonPlugin *pp,const char *mod, const char*func, bp::tuple tupleargs, bp::dict kwargs)
{
bp::object r;
bool callable = pp->is_callable(mod,func);
printf("callable(%s%s%s) = %s\n",mod ? mod : "", mod ? ".":"",
func, callable ? "TRUE": "FALSE");
int status = pp->call(mod,func, tupleargs,kwargs,r);
switch (status) {
case PLUGIN_EXCEPTION:
printf("call(%s%s%s): exception='%s' status = %d\n",
mod ? mod : "", mod ? ".":"",func,pp->last_exception().c_str(), status);
break;
case PLUGIN_OK:
printf("call(%s%s%s): OK\n", mod ? mod : "", mod ? ".":"",func);
analyze( func,r);
break;
default:
printf("call(%s%s%s): status = %d\n", mod ? mod : "", mod ? ".":"",func,status);
}
}
void run(PythonPlugin *pp,const char *cmd, bool as_file)
{
bp::object r;
int status = pp->run_string(cmd,r, as_file);
printf("run_string(%s): status = %d\n", cmd,status);
analyze(cmd,r);
}
void foo_init()
{
printf("foo init\n");
}
void bar_init()
{
printf("bar init\n");
}
const char *inifile = "test.ini";
const char *section = "PYTHON";
struct _inittab builtin_modules[] = {
{ (char *) "foo", foo_init },
{ (char *) "bar", bar_init },
// any others...
{ NULL, NULL }
};
int builtins;
bool as_file = false;
int
main (int argc, char **argv)
{
int index;
int c;
bp::object retval, pythis;
char *callablefunc = NULL;
char *callablemod = NULL;
char *xcallable = NULL;
int status;
opterr = 0;
while ((c = getopt (argc, argv, "fbi:C:c:x:")) != -1) {
switch (c) {
case 'f':
as_file = true;
break;
case 'b':
builtins++;
break;
case 'i':
inifile = optarg;
break;
case 'C':
callablemod = optarg;
break;
case 'c':
callablefunc = optarg;
break;
case 'x':
xcallable = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
}
if (!PythonPlugin::instantiate(builtins ? builtin_modules : NULL)) {
fprintf (stderr,"instantiate: cant instantiate Python plugin");
}
// creates the singleton instance
status = python_plugin->configure(inifile, section);
if (status != PLUGIN_OK) {
fprintf (stderr, "configure failed: %d\n",status);
}
// PythonPlugin two = python_plugin; // this fails since copy constructor is private.
// PythonPlugin &second = PythonPlugin::getInstance(); // returns the singleton instance
printf("status = %d\n", python_plugin->plugin_status());
bp::tuple tupleargs,nulltupleargs;
bp::dict kwargs,nullkwargs;
kwargs['x'] = 10;
kwargs['y'] = 20;
tupleargs = bp::make_tuple(3,2,1,"foo");
exercise(python_plugin,NULL,"func",nulltupleargs, nullkwargs);
system("/usr/bin/touch testmod.py");
exercise(python_plugin,NULL,"badfunc",tupleargs,kwargs);
exercise(python_plugin,NULL,"retstring",tupleargs,kwargs);
exercise(python_plugin,NULL,"retdouble",tupleargs,kwargs);
// exercise(python_plugin,"submod","subfunc");
for (index = optind; index < argc; index++) {
run(python_plugin, argv[index], as_file);
}
return 0;
}
|