This repository has been archived by the owner on Dec 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.c
200 lines (152 loc) · 4.24 KB
/
api.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "api.h"
#include "../module.h"
#undef global
static Function *global;
extern int python_isolated;
khash_t(callbacks) *callback_table;
static uint32_t callback_base_id = 0;
/* This flag is used to mark when the Eggdrop TCL API is usable by our
* interpreter threads since the main runloop isn't inherently thread-safe */
uint8_t api_available = 0;
/*** callback plumbing ***/
static int run_callback STDVAR
{
/* TODO:
* - needs a return value for certain binds
* - null checking everywhere
*/
PyObject *tmp, *args, *ret = NULL;
callback_t *callback;
uint32_t callback_id;
int32_t i, k;
API_ACTIVE();
callback_id = strtol(argv[0]+17, NULL, 10);
if (errno == EINVAL) {
Tcl_AppendResult(irp, "Invalid callback ID", NULL);
goto err;
}
k = kh_get(callbacks, callback_table, callback_id);
callback = &(kh_val(callback_table, k));
if (callback->subint != NULL) {
PyThreadState_Swap(callback->subint);
}
args = PyTuple_New(argc - 1);
for (i = 1; i < argc; i++) {
tmp = PyString_FromString(argv[i]);
PyTuple_SET_ITEM(args, i - 1, tmp);
}
ret = PyObject_Call(callback->callable, args, NULL);
Py_DECREF(args);
if (PyErr_Occurred() != NULL) {
Tcl_AppendResult(irp, "Uncaught Python exception", NULL);
PyErr_PrintEx(0);
goto err;
}
if ((ret != Py_None) && (ret != NULL)) {
tmp = PyObject_Str(ret);
Py_DECREF(ret);
if (tmp == NULL) {
Tcl_AppendResult(irp, "Invalid return value", NULL);
goto err;
} else {
Tcl_AppendResult(irp, PyString_AS_STRING(tmp), NULL);
Py_DECREF(tmp);
}
}
API_INACTIVE();
return TCL_OK;
err:
API_INACTIVE();
return TCL_ERROR;
}
static callback_t * install_callback(PyObject *callable)
{
callback_t *callback;
tcl_cmds *cmdtab;
uint32_t callback_id;
int32_t k, r;
callback_id = callback_base_id++;
k = kh_put(callbacks, callback_table, callback_id, &r);
callback = &(kh_val(callback_table, k));
callback->id = callback_id;
callback->name = (char *) nmalloc(sizeof(char) * 28);
snprintf(callback->name, 28, "python::callback_%u", callback_id);
callback->callable = callable;
Py_INCREF(callable);
if (python_isolated)
callback->subint = PyThreadState_Get();
else
callback->subint = NULL;
cmdtab = (tcl_cmds *) nmalloc(sizeof(tcl_cmds) * 2);
cmdtab[0].name = callback->name;
cmdtab[0].func = run_callback;
cmdtab[1].name = NULL;
cmdtab[2].name = NULL;
add_tcl_commands(cmdtab);
nfree(cmdtab);
return callback;
}
/*** TCL bridge ***/
/* we need to kill some of the global[] macros to avoid collisions */
#undef putlog
#undef putserv
APIDEF_METHOD(bind)
{
PyObject *callable;
callback_t *callback;
char *type, *flags, *parm, *bind;
API_CHECK();
if (! PyArg_ParseTuple(args, "sssO", &type, &flags, &parm, &callable))
return NULL;
callback = install_callback(callable);
bind = (char *) nmalloc(sizeof(char) * (60 + strlen(callback->name)));
sprintf(bind,
"bind $python::arg_type $python::arg_flags $python::arg_parm %s",
callback->name);
Tcl_SetVar(interp, "python::arg_type", type, 0);
Tcl_SetVar(interp, "python::arg_flags", flags, 0);
Tcl_SetVar(interp, "python::arg_parm", parm, 0);
Tcl_Eval(interp, bind);
Tcl_UnsetVar(interp, "python::arg_type", 0);
Tcl_UnsetVar(interp, "python::arg_flags", 0);
Tcl_UnsetVar(interp, "python::arg_parm", 0);
nfree(bind);
return Py_None;
}
APIDEF_METHOD(putlog)
{
char *msg;
API_CHECK();
if (! PyArg_ParseTuple(args, "s", &msg))
return NULL;
Tcl_SetVar(interp, "python::arg_msg", msg, 0);
Tcl_Eval(interp, "putlog $python::arg_msg");
Tcl_UnsetVar(interp, "python::arg_msg", 0);
return Py_None;
}
APIDEF_KWMETHOD(putserv)
{
PyObject *next = NULL;
static char *opts[] = {"msg", "next", NULL};
char *msg;
if (! PyArg_ParseTupleAndKeywords(args, kw, "s|O", opts, &msg, &next))
return NULL;
Tcl_SetVar(interp, "python::arg_msg", msg, 0);
if ((next == Py_False) || (next == NULL)) {
Tcl_Eval(interp, "putserv $python::arg_msg");
} else if (next == Py_True) {
Tcl_Eval(interp, "putserv $python::arg_msg -next");
} else {
PyErr_SetString(PyExc_TypeError,
"'next' kwarg to 'putserv' must be True or False");
return NULL;
}
Tcl_UnsetVar(interp, "python::arg_msg", 0);
return Py_None;
}
PyMethodDef api_table[] = {
API_METHOD(bind),
API_METHOD(putlog),
API_KWMETHOD(putserv),
API_END
};