1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Python bindings for tevent
|
---|
4 |
|
---|
5 | Copyright (C) Jelmer Vernooij 2010
|
---|
6 |
|
---|
7 | ** NOTE! The following LGPL license applies to the tevent
|
---|
8 | ** library. This does NOT imply that all of Samba is released
|
---|
9 | ** under the LGPL
|
---|
10 |
|
---|
11 | This library is free software; you can redistribute it and/or
|
---|
12 | modify it under the terms of the GNU Lesser General Public
|
---|
13 | License as published by the Free Software Foundation; either
|
---|
14 | version 3 of the License, or (at your option) any later version.
|
---|
15 |
|
---|
16 | This library is distributed in the hope that it will be useful,
|
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | Lesser General Public License for more details.
|
---|
20 |
|
---|
21 | You should have received a copy of the GNU Lesser General Public
|
---|
22 | License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include <Python.h>
|
---|
26 | #include "replace.h"
|
---|
27 | #include <tevent.h>
|
---|
28 |
|
---|
29 | #if PY_MAJOR_VERSION >= 3
|
---|
30 | #define PyStr_Check PyUnicode_Check
|
---|
31 | #define PyStr_FromString PyUnicode_FromString
|
---|
32 | #define PyStr_AsUTF8 PyUnicode_AsUTF8
|
---|
33 | #define PyInt_FromLong PyLong_FromLong
|
---|
34 | #else
|
---|
35 | #define PyStr_Check PyString_Check
|
---|
36 | #define PyStr_FromString PyString_FromString
|
---|
37 | #define PyStr_AsUTF8 PyString_AsString
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | void init_tevent(void);
|
---|
41 |
|
---|
42 | typedef struct {
|
---|
43 | PyObject_HEAD
|
---|
44 | struct tevent_context *ev;
|
---|
45 | } TeventContext_Object;
|
---|
46 |
|
---|
47 | typedef struct {
|
---|
48 | PyObject_HEAD
|
---|
49 | struct tevent_queue *queue;
|
---|
50 | } TeventQueue_Object;
|
---|
51 |
|
---|
52 | typedef struct {
|
---|
53 | PyObject_HEAD
|
---|
54 | struct tevent_req *req;
|
---|
55 | } TeventReq_Object;
|
---|
56 |
|
---|
57 | typedef struct {
|
---|
58 | PyObject_HEAD
|
---|
59 | struct tevent_signal *signal;
|
---|
60 | } TeventSignal_Object;
|
---|
61 |
|
---|
62 | typedef struct {
|
---|
63 | PyObject_HEAD
|
---|
64 | struct tevent_timer *timer;
|
---|
65 | PyObject *callback;
|
---|
66 | } TeventTimer_Object;
|
---|
67 |
|
---|
68 | typedef struct {
|
---|
69 | PyObject_HEAD
|
---|
70 | struct tevent_fd *fd;
|
---|
71 | } TeventFd_Object;
|
---|
72 |
|
---|
73 | static PyTypeObject TeventContext_Type;
|
---|
74 | static PyTypeObject TeventReq_Type;
|
---|
75 | static PyTypeObject TeventQueue_Type;
|
---|
76 | static PyTypeObject TeventSignal_Type;
|
---|
77 | static PyTypeObject TeventTimer_Type;
|
---|
78 | static PyTypeObject TeventFd_Type;
|
---|
79 |
|
---|
80 | static int py_context_init(struct tevent_context *ev)
|
---|
81 | {
|
---|
82 | /* FIXME */
|
---|
83 | return 0;
|
---|
84 | }
|
---|
85 |
|
---|
86 | static struct tevent_fd *py_add_fd(struct tevent_context *ev,
|
---|
87 | TALLOC_CTX *mem_ctx,
|
---|
88 | int fd, uint16_t flags,
|
---|
89 | tevent_fd_handler_t handler,
|
---|
90 | void *private_data,
|
---|
91 | const char *handler_name,
|
---|
92 | const char *location)
|
---|
93 | {
|
---|
94 | /* FIXME */
|
---|
95 | return NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | static void py_set_fd_close_fn(struct tevent_fd *fde,
|
---|
99 | tevent_fd_close_fn_t close_fn)
|
---|
100 | {
|
---|
101 | /* FIXME */
|
---|
102 | }
|
---|
103 |
|
---|
104 | static uint16_t py_get_fd_flags(struct tevent_fd *fde)
|
---|
105 | {
|
---|
106 | /* FIXME */
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void py_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
|
---|
111 | {
|
---|
112 | /* FIXME */
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* timed_event functions */
|
---|
116 | static struct tevent_timer *py_add_timer(struct tevent_context *ev,
|
---|
117 | TALLOC_CTX *mem_ctx,
|
---|
118 | struct timeval next_event,
|
---|
119 | tevent_timer_handler_t handler,
|
---|
120 | void *private_data,
|
---|
121 | const char *handler_name,
|
---|
122 | const char *location)
|
---|
123 | {
|
---|
124 | /* FIXME */
|
---|
125 | return NULL;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* immediate event functions */
|
---|
129 | static void py_schedule_immediate(struct tevent_immediate *im,
|
---|
130 | struct tevent_context *ev,
|
---|
131 | tevent_immediate_handler_t handler,
|
---|
132 | void *private_data,
|
---|
133 | const char *handler_name,
|
---|
134 | const char *location)
|
---|
135 | {
|
---|
136 | /* FIXME */
|
---|
137 | }
|
---|
138 |
|
---|
139 | /* signal functions */
|
---|
140 | static struct tevent_signal *py_add_signal(struct tevent_context *ev,
|
---|
141 | TALLOC_CTX *mem_ctx,
|
---|
142 | int signum, int sa_flags,
|
---|
143 | tevent_signal_handler_t handler,
|
---|
144 | void *private_data,
|
---|
145 | const char *handler_name,
|
---|
146 | const char *location)
|
---|
147 | {
|
---|
148 | /* FIXME */
|
---|
149 | return NULL;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* loop functions */
|
---|
153 | static int py_loop_once(struct tevent_context *ev, const char *location)
|
---|
154 | {
|
---|
155 | /* FIXME */
|
---|
156 | return 0;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int py_loop_wait(struct tevent_context *ev, const char *location)
|
---|
160 | {
|
---|
161 | /* FIXME */
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | const static struct tevent_ops py_tevent_ops = {
|
---|
166 | .context_init = py_context_init,
|
---|
167 | .add_fd = py_add_fd,
|
---|
168 | .set_fd_close_fn = py_set_fd_close_fn,
|
---|
169 | .get_fd_flags = py_get_fd_flags,
|
---|
170 | .set_fd_flags = py_set_fd_flags,
|
---|
171 | .add_timer = py_add_timer,
|
---|
172 | .schedule_immediate = py_schedule_immediate,
|
---|
173 | .add_signal = py_add_signal,
|
---|
174 | .loop_wait = py_loop_wait,
|
---|
175 | .loop_once = py_loop_once,
|
---|
176 | };
|
---|
177 |
|
---|
178 | static PyObject *py_register_backend(PyObject *self, PyObject *args)
|
---|
179 | {
|
---|
180 | PyObject *name, *py_backend;
|
---|
181 |
|
---|
182 | if (!PyArg_ParseTuple(args, "O", &py_backend))
|
---|
183 | return NULL;
|
---|
184 |
|
---|
185 | name = PyObject_GetAttrString(py_backend, "name");
|
---|
186 | if (name == NULL) {
|
---|
187 | PyErr_SetNone(PyExc_AttributeError);
|
---|
188 | return NULL;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (!PyStr_Check(name)) {
|
---|
192 | PyErr_SetNone(PyExc_TypeError);
|
---|
193 | Py_DECREF(name);
|
---|
194 | return NULL;
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (!tevent_register_backend(PyStr_AsUTF8(name), &py_tevent_ops)) { /* FIXME: What to do with backend */
|
---|
198 | PyErr_SetNone(PyExc_RuntimeError);
|
---|
199 | Py_DECREF(name);
|
---|
200 | return NULL;
|
---|
201 | }
|
---|
202 |
|
---|
203 | Py_DECREF(name);
|
---|
204 |
|
---|
205 | Py_RETURN_NONE;
|
---|
206 | }
|
---|
207 |
|
---|
208 | static PyObject *py_tevent_context_reinitialise(TeventContext_Object *self)
|
---|
209 | {
|
---|
210 | int ret = tevent_re_initialise(self->ev);
|
---|
211 | if (ret != 0) {
|
---|
212 | PyErr_SetNone(PyExc_RuntimeError);
|
---|
213 | return NULL;
|
---|
214 | }
|
---|
215 | Py_RETURN_NONE;
|
---|
216 | }
|
---|
217 |
|
---|
218 | static PyObject *py_tevent_queue_stop(TeventQueue_Object *self)
|
---|
219 | {
|
---|
220 | tevent_queue_stop(self->queue);
|
---|
221 | Py_RETURN_NONE;
|
---|
222 | }
|
---|
223 |
|
---|
224 | static PyObject *py_tevent_queue_start(TeventQueue_Object *self)
|
---|
225 | {
|
---|
226 | tevent_queue_start(self->queue);
|
---|
227 | Py_RETURN_NONE;
|
---|
228 | }
|
---|
229 |
|
---|
230 | static void py_queue_trigger(struct tevent_req *req, void *private_data)
|
---|
231 | {
|
---|
232 | PyObject *callback = private_data, *ret;
|
---|
233 |
|
---|
234 | ret = PyObject_CallFunction(callback, discard_const_p(char, ""));
|
---|
235 | Py_XDECREF(ret);
|
---|
236 | }
|
---|
237 |
|
---|
238 | static PyObject *py_tevent_queue_add(TeventQueue_Object *self, PyObject *args)
|
---|
239 | {
|
---|
240 | TeventContext_Object *py_ev;
|
---|
241 | TeventReq_Object *py_req;
|
---|
242 | PyObject *trigger;
|
---|
243 | bool ret;
|
---|
244 |
|
---|
245 | if (!PyArg_ParseTuple(args, "O!O!O",
|
---|
246 | &TeventContext_Type, &py_ev,
|
---|
247 | &TeventReq_Type, &py_req,
|
---|
248 | &trigger))
|
---|
249 | return NULL;
|
---|
250 |
|
---|
251 | Py_INCREF(trigger);
|
---|
252 |
|
---|
253 | ret = tevent_queue_add(self->queue, py_ev->ev, py_req->req,
|
---|
254 | py_queue_trigger, trigger);
|
---|
255 | if (!ret) {
|
---|
256 | PyErr_SetString(PyExc_RuntimeError, "queue add failed");
|
---|
257 | Py_DECREF(trigger);
|
---|
258 | return NULL;
|
---|
259 | }
|
---|
260 |
|
---|
261 | Py_RETURN_NONE;
|
---|
262 | }
|
---|
263 |
|
---|
264 | static PyMethodDef py_tevent_queue_methods[] = {
|
---|
265 | { "stop", (PyCFunction)py_tevent_queue_stop, METH_NOARGS,
|
---|
266 | "S.stop()" },
|
---|
267 | { "start", (PyCFunction)py_tevent_queue_start, METH_NOARGS,
|
---|
268 | "S.start()" },
|
---|
269 | { "add", (PyCFunction)py_tevent_queue_add, METH_VARARGS,
|
---|
270 | "S.add(ctx, req, trigger, baton)" },
|
---|
271 | { NULL },
|
---|
272 | };
|
---|
273 |
|
---|
274 | static PyObject *py_tevent_context_wakeup_send(PyObject *self, PyObject *args)
|
---|
275 | {
|
---|
276 | /* FIXME */
|
---|
277 |
|
---|
278 | Py_RETURN_NONE;
|
---|
279 | }
|
---|
280 |
|
---|
281 | static PyObject *py_tevent_context_loop_wait(TeventContext_Object *self)
|
---|
282 | {
|
---|
283 | if (tevent_loop_wait(self->ev) != 0) {
|
---|
284 | PyErr_SetNone(PyExc_RuntimeError);
|
---|
285 | return NULL;
|
---|
286 | }
|
---|
287 | Py_RETURN_NONE;
|
---|
288 | }
|
---|
289 |
|
---|
290 | static PyObject *py_tevent_context_loop_once(TeventContext_Object *self)
|
---|
291 | {
|
---|
292 | if (tevent_loop_once(self->ev) != 0) {
|
---|
293 | PyErr_SetNone(PyExc_RuntimeError);
|
---|
294 | return NULL;
|
---|
295 | }
|
---|
296 | Py_RETURN_NONE;
|
---|
297 | }
|
---|
298 |
|
---|
299 | static void py_tevent_signal_handler(struct tevent_context *ev,
|
---|
300 | struct tevent_signal *se,
|
---|
301 | int signum,
|
---|
302 | int count,
|
---|
303 | void *siginfo,
|
---|
304 | void *private_data)
|
---|
305 | {
|
---|
306 | PyObject *callback = (PyObject *)private_data, *ret;
|
---|
307 |
|
---|
308 | ret = PyObject_CallFunction(callback, discard_const_p(char, "ii"), signum, count);
|
---|
309 | Py_XDECREF(ret);
|
---|
310 | }
|
---|
311 |
|
---|
312 | static void py_tevent_signal_dealloc(TeventSignal_Object *self)
|
---|
313 | {
|
---|
314 | talloc_free(self->signal);
|
---|
315 | PyObject_Del(self);
|
---|
316 | }
|
---|
317 |
|
---|
318 | static PyTypeObject TeventSignal_Type = {
|
---|
319 | .tp_name = "tevent.Signal",
|
---|
320 | .tp_basicsize = sizeof(TeventSignal_Object),
|
---|
321 | .tp_dealloc = (destructor)py_tevent_signal_dealloc,
|
---|
322 | .tp_flags = Py_TPFLAGS_DEFAULT,
|
---|
323 | };
|
---|
324 |
|
---|
325 | static PyObject *py_tevent_context_add_signal(TeventContext_Object *self, PyObject *args)
|
---|
326 | {
|
---|
327 | int signum, sa_flags;
|
---|
328 | PyObject *handler;
|
---|
329 | struct tevent_signal *sig;
|
---|
330 | TeventSignal_Object *ret;
|
---|
331 |
|
---|
332 | if (!PyArg_ParseTuple(args, "iiO", &signum, &sa_flags, &handler))
|
---|
333 | return NULL;
|
---|
334 |
|
---|
335 | Py_INCREF(handler);
|
---|
336 | sig = tevent_add_signal(self->ev, NULL, signum, sa_flags,
|
---|
337 | py_tevent_signal_handler, handler);
|
---|
338 |
|
---|
339 | ret = PyObject_New(TeventSignal_Object, &TeventSignal_Type);
|
---|
340 | if (ret == NULL) {
|
---|
341 | PyErr_NoMemory();
|
---|
342 | talloc_free(sig);
|
---|
343 | return NULL;
|
---|
344 | }
|
---|
345 |
|
---|
346 | ret->signal = sig;
|
---|
347 |
|
---|
348 | return (PyObject *)ret;
|
---|
349 | }
|
---|
350 |
|
---|
351 | static void py_timer_handler(struct tevent_context *ev,
|
---|
352 | struct tevent_timer *te,
|
---|
353 | struct timeval current_time,
|
---|
354 | void *private_data)
|
---|
355 | {
|
---|
356 | TeventTimer_Object *self = private_data;
|
---|
357 | PyObject *ret;
|
---|
358 |
|
---|
359 | ret = PyObject_CallFunction(self->callback, discard_const_p(char, "l"), te);
|
---|
360 | if (ret == NULL) {
|
---|
361 | /* No Python stack to propagate exception to; just print traceback */
|
---|
362 | PyErr_PrintEx(0);
|
---|
363 | }
|
---|
364 | Py_XDECREF(ret);
|
---|
365 | }
|
---|
366 |
|
---|
367 | static void py_tevent_timer_dealloc(TeventTimer_Object *self)
|
---|
368 | {
|
---|
369 | if (self->timer) {
|
---|
370 | talloc_free(self->timer);
|
---|
371 | }
|
---|
372 | Py_DECREF(self->callback);
|
---|
373 | PyObject_Del(self);
|
---|
374 | }
|
---|
375 |
|
---|
376 | static int py_tevent_timer_traverse(TeventTimer_Object *self, visitproc visit, void *arg)
|
---|
377 | {
|
---|
378 | Py_VISIT(self->callback);
|
---|
379 | return 0;
|
---|
380 | }
|
---|
381 |
|
---|
382 | static PyObject* py_tevent_timer_get_active(TeventTimer_Object *self) {
|
---|
383 | return PyBool_FromLong(self->timer != NULL);
|
---|
384 | }
|
---|
385 |
|
---|
386 | struct PyGetSetDef py_tevent_timer_getset[] = {
|
---|
387 | {
|
---|
388 | .name = discard_const_p(char, "active"),
|
---|
389 | .get = (getter)py_tevent_timer_get_active,
|
---|
390 | .doc = discard_const_p(char, "true if the timer is scheduled to run"),
|
---|
391 | },
|
---|
392 | {NULL},
|
---|
393 | };
|
---|
394 |
|
---|
395 | static PyTypeObject TeventTimer_Type = {
|
---|
396 | .tp_name = "tevent.Timer",
|
---|
397 | .tp_basicsize = sizeof(TeventTimer_Object),
|
---|
398 | .tp_dealloc = (destructor)py_tevent_timer_dealloc,
|
---|
399 | .tp_traverse = (traverseproc)py_tevent_timer_traverse,
|
---|
400 | .tp_getset = py_tevent_timer_getset,
|
---|
401 | .tp_flags = Py_TPFLAGS_DEFAULT,
|
---|
402 | };
|
---|
403 |
|
---|
404 | struct TeventTimer_Object_ref {
|
---|
405 | TeventTimer_Object *obj;
|
---|
406 | };
|
---|
407 |
|
---|
408 | static int TeventTimer_Object_ref_destructor(struct TeventTimer_Object_ref *ref)
|
---|
409 | {
|
---|
410 | ref->obj->timer = NULL;
|
---|
411 | Py_DECREF(ref->obj);
|
---|
412 | return 0;
|
---|
413 | }
|
---|
414 |
|
---|
415 | static PyObject *py_tevent_context_add_timer_internal(TeventContext_Object *self,
|
---|
416 | struct timeval next_event,
|
---|
417 | PyObject *callback)
|
---|
418 | {
|
---|
419 | /* Ownership notes:
|
---|
420 | *
|
---|
421 | * There are 5 pieces in play; two tevent contexts and 3 Python objects:
|
---|
422 | * - The tevent timer
|
---|
423 | * - The tevent context
|
---|
424 | * - The Python context -- "self"
|
---|
425 | * - The Python timer (TeventTimer_Object) -- "ret"
|
---|
426 | * - The Python callback function -- "callback"
|
---|
427 | *
|
---|
428 | * We only use the Python context for getting the tevent context,
|
---|
429 | * afterwards it can be destroyed.
|
---|
430 | *
|
---|
431 | * The tevent context owns the tevent timer.
|
---|
432 | *
|
---|
433 | * The tevent timer holds a reference to the Python timer, so the Python
|
---|
434 | * timer must always outlive the tevent timer.
|
---|
435 | * The Python timer has a pointer to the tevent timer; a destructor is
|
---|
436 | * used to set this to NULL when the tevent timer is deallocated.
|
---|
437 | *
|
---|
438 | * The tevent timer can be deallocated in these cases:
|
---|
439 | * 1) when the context is destroyed
|
---|
440 | * 2) after the event fires
|
---|
441 | * Posssibly, API might be added to cancel (free the tevent timer).
|
---|
442 | *
|
---|
443 | * The Python timer holds a reference to the callback.
|
---|
444 | */
|
---|
445 | TeventTimer_Object *ret;
|
---|
446 | struct TeventTimer_Object_ref *ref;
|
---|
447 |
|
---|
448 | ret = PyObject_New(TeventTimer_Object, &TeventTimer_Type);
|
---|
449 | if (ret == NULL) {
|
---|
450 | PyErr_NoMemory();
|
---|
451 | return NULL;
|
---|
452 | }
|
---|
453 | Py_INCREF(callback);
|
---|
454 | ret->callback = callback;
|
---|
455 | ret->timer = tevent_add_timer(self->ev, NULL, next_event, py_timer_handler,
|
---|
456 | ret);
|
---|
457 | if (ret->timer == NULL) {
|
---|
458 | Py_DECREF(ret);
|
---|
459 | PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
|
---|
460 | return NULL;
|
---|
461 | }
|
---|
462 | ref = talloc(ret->timer, struct TeventTimer_Object_ref);
|
---|
463 | if (ref == NULL) {
|
---|
464 | talloc_free(ret->timer);
|
---|
465 | Py_DECREF(ret);
|
---|
466 | PyErr_SetString(PyExc_RuntimeError, "Could not initialize timer");
|
---|
467 | return NULL;
|
---|
468 | }
|
---|
469 | Py_INCREF(ret);
|
---|
470 | ref->obj = ret;
|
---|
471 |
|
---|
472 | talloc_set_destructor(ref, TeventTimer_Object_ref_destructor);
|
---|
473 |
|
---|
474 | return (PyObject *)ret;
|
---|
475 | }
|
---|
476 |
|
---|
477 | static PyObject *py_tevent_context_add_timer(TeventContext_Object *self, PyObject *args)
|
---|
478 | {
|
---|
479 | struct timeval next_event;
|
---|
480 | PyObject *callback;
|
---|
481 | if (!PyArg_ParseTuple(args, "lO", &next_event, &callback))
|
---|
482 | return NULL;
|
---|
483 |
|
---|
484 | return py_tevent_context_add_timer_internal(self, next_event, callback);
|
---|
485 | }
|
---|
486 |
|
---|
487 | static PyObject *py_tevent_context_add_timer_offset(TeventContext_Object *self, PyObject *args)
|
---|
488 | {
|
---|
489 | struct timeval next_event;
|
---|
490 | double offset;
|
---|
491 | int seconds;
|
---|
492 | PyObject *callback;
|
---|
493 | if (!PyArg_ParseTuple(args, "dO", &offset, &callback))
|
---|
494 | return NULL;
|
---|
495 |
|
---|
496 | seconds = offset;
|
---|
497 | offset -= seconds;
|
---|
498 | next_event = tevent_timeval_current_ofs(seconds, (int)(offset*1000000));
|
---|
499 | return py_tevent_context_add_timer_internal(self, next_event, callback);
|
---|
500 | }
|
---|
501 |
|
---|
502 | static void py_fd_handler(struct tevent_context *ev,
|
---|
503 | struct tevent_fd *fde,
|
---|
504 | uint16_t flags,
|
---|
505 | void *private_data)
|
---|
506 | {
|
---|
507 | PyObject *callback = private_data, *ret;
|
---|
508 |
|
---|
509 | ret = PyObject_CallFunction(callback, discard_const_p(char, "i"), flags);
|
---|
510 | Py_XDECREF(ret);
|
---|
511 | }
|
---|
512 |
|
---|
513 | static void py_tevent_fp_dealloc(TeventFd_Object *self)
|
---|
514 | {
|
---|
515 | talloc_free(self->fd);
|
---|
516 | PyObject_Del(self);
|
---|
517 | }
|
---|
518 |
|
---|
519 | static PyTypeObject TeventFd_Type = {
|
---|
520 | .tp_name = "tevent.Fd",
|
---|
521 | .tp_basicsize = sizeof(TeventFd_Object),
|
---|
522 | .tp_dealloc = (destructor)py_tevent_fp_dealloc,
|
---|
523 | .tp_flags = Py_TPFLAGS_DEFAULT,
|
---|
524 | };
|
---|
525 |
|
---|
526 | static PyObject *py_tevent_context_add_fd(TeventContext_Object *self, PyObject *args)
|
---|
527 | {
|
---|
528 | int fd, flags;
|
---|
529 | PyObject *handler;
|
---|
530 | struct tevent_fd *tfd;
|
---|
531 | TeventFd_Object *ret;
|
---|
532 |
|
---|
533 | if (!PyArg_ParseTuple(args, "iiO", &fd, &flags, &handler))
|
---|
534 | return NULL;
|
---|
535 |
|
---|
536 | tfd = tevent_add_fd(self->ev, NULL, fd, flags, py_fd_handler, handler);
|
---|
537 | if (tfd == NULL) {
|
---|
538 | PyErr_SetNone(PyExc_RuntimeError);
|
---|
539 | return NULL;
|
---|
540 | }
|
---|
541 |
|
---|
542 | ret = PyObject_New(TeventFd_Object, &TeventFd_Type);
|
---|
543 | if (ret == NULL) {
|
---|
544 | talloc_free(tfd);
|
---|
545 | return NULL;
|
---|
546 | }
|
---|
547 | ret->fd = tfd;
|
---|
548 |
|
---|
549 | return (PyObject *)ret;
|
---|
550 | }
|
---|
551 |
|
---|
552 | static PyMethodDef py_tevent_context_methods[] = {
|
---|
553 | { "reinitialise", (PyCFunction)py_tevent_context_reinitialise, METH_NOARGS,
|
---|
554 | "S.reinitialise()" },
|
---|
555 | { "wakeup_send", (PyCFunction)py_tevent_context_wakeup_send,
|
---|
556 | METH_VARARGS, "S.wakeup_send(wakeup_time) -> req" },
|
---|
557 | { "loop_wait", (PyCFunction)py_tevent_context_loop_wait,
|
---|
558 | METH_NOARGS, "S.loop_wait()" },
|
---|
559 | { "loop_once", (PyCFunction)py_tevent_context_loop_once,
|
---|
560 | METH_NOARGS, "S.loop_once()" },
|
---|
561 | { "add_signal", (PyCFunction)py_tevent_context_add_signal,
|
---|
562 | METH_VARARGS, "S.add_signal(signum, sa_flags, handler) -> signal" },
|
---|
563 | { "add_timer", (PyCFunction)py_tevent_context_add_timer,
|
---|
564 | METH_VARARGS, "S.add_timer(next_event, handler) -> timer" },
|
---|
565 | { "add_timer_offset", (PyCFunction)py_tevent_context_add_timer_offset,
|
---|
566 | METH_VARARGS, "S.add_timer(offset_seconds, handler) -> timer" },
|
---|
567 | { "add_fd", (PyCFunction)py_tevent_context_add_fd,
|
---|
568 | METH_VARARGS, "S.add_fd(fd, flags, handler) -> fd" },
|
---|
569 | { NULL },
|
---|
570 | };
|
---|
571 |
|
---|
572 | static PyObject *py_tevent_req_wakeup_recv(PyObject *self)
|
---|
573 | {
|
---|
574 | /* FIXME */
|
---|
575 | Py_RETURN_NONE;
|
---|
576 | }
|
---|
577 |
|
---|
578 | static PyObject *py_tevent_req_received(PyObject *self)
|
---|
579 | {
|
---|
580 | /* FIXME */
|
---|
581 | Py_RETURN_NONE;
|
---|
582 | }
|
---|
583 |
|
---|
584 | static PyObject *py_tevent_req_is_error(PyObject *self)
|
---|
585 | {
|
---|
586 | /* FIXME */
|
---|
587 | Py_RETURN_NONE;
|
---|
588 | }
|
---|
589 |
|
---|
590 | static PyObject *py_tevent_req_poll(PyObject *self)
|
---|
591 | {
|
---|
592 | /* FIXME */
|
---|
593 | Py_RETURN_NONE;
|
---|
594 | }
|
---|
595 |
|
---|
596 | static PyObject *py_tevent_req_is_in_progress(PyObject *self)
|
---|
597 | {
|
---|
598 | /* FIXME */
|
---|
599 | Py_RETURN_NONE;
|
---|
600 | }
|
---|
601 |
|
---|
602 | static PyGetSetDef py_tevent_req_getsetters[] = {
|
---|
603 | {
|
---|
604 | .name = discard_const_p(char, "in_progress"),
|
---|
605 | .get = (getter)py_tevent_req_is_in_progress,
|
---|
606 | .doc = discard_const_p(char, "Whether the request is in progress"),
|
---|
607 | },
|
---|
608 | { NULL }
|
---|
609 | };
|
---|
610 |
|
---|
611 | static PyObject *py_tevent_req_post(PyObject *self, PyObject *args)
|
---|
612 | {
|
---|
613 | /* FIXME */
|
---|
614 | Py_RETURN_NONE;
|
---|
615 | }
|
---|
616 |
|
---|
617 | static PyObject *py_tevent_req_set_error(PyObject *self, PyObject *args)
|
---|
618 | {
|
---|
619 | /* FIXME */
|
---|
620 | Py_RETURN_NONE;
|
---|
621 | }
|
---|
622 |
|
---|
623 | static PyObject *py_tevent_req_done(PyObject *self)
|
---|
624 | {
|
---|
625 | /* FIXME */
|
---|
626 | Py_RETURN_NONE;
|
---|
627 | }
|
---|
628 |
|
---|
629 | static PyObject *py_tevent_req_notify_callback(PyObject *self)
|
---|
630 | {
|
---|
631 | /* FIXME */
|
---|
632 | Py_RETURN_NONE;
|
---|
633 | }
|
---|
634 |
|
---|
635 | static PyObject *py_tevent_req_set_endtime(PyObject *self, PyObject *args)
|
---|
636 | {
|
---|
637 | /* FIXME */
|
---|
638 | Py_RETURN_NONE;
|
---|
639 | }
|
---|
640 |
|
---|
641 | static PyObject *py_tevent_req_cancel(TeventReq_Object *self)
|
---|
642 | {
|
---|
643 | if (!tevent_req_cancel(self->req)) {
|
---|
644 | PyErr_SetNone(PyExc_RuntimeError);
|
---|
645 | return NULL;
|
---|
646 | }
|
---|
647 | Py_RETURN_NONE;
|
---|
648 | }
|
---|
649 |
|
---|
650 | static PyMethodDef py_tevent_req_methods[] = {
|
---|
651 | { "wakeup_recv", (PyCFunction)py_tevent_req_wakeup_recv, METH_NOARGS,
|
---|
652 | "Wakeup received" },
|
---|
653 | { "received", (PyCFunction)py_tevent_req_received, METH_NOARGS,
|
---|
654 | "Receive finished" },
|
---|
655 | { "is_error", (PyCFunction)py_tevent_req_is_error, METH_NOARGS,
|
---|
656 | "is_error() -> (error, state)" },
|
---|
657 | { "poll", (PyCFunction)py_tevent_req_poll, METH_VARARGS,
|
---|
658 | "poll(ctx)" },
|
---|
659 | { "post", (PyCFunction)py_tevent_req_post, METH_VARARGS,
|
---|
660 | "post(ctx) -> req" },
|
---|
661 | { "set_error", (PyCFunction)py_tevent_req_set_error, METH_VARARGS,
|
---|
662 | "set_error(error)" },
|
---|
663 | { "done", (PyCFunction)py_tevent_req_done, METH_NOARGS,
|
---|
664 | "done()" },
|
---|
665 | { "notify_callback", (PyCFunction)py_tevent_req_notify_callback,
|
---|
666 | METH_NOARGS, "notify_callback()" },
|
---|
667 | { "set_endtime", (PyCFunction)py_tevent_req_set_endtime,
|
---|
668 | METH_VARARGS, "set_endtime(ctx, endtime)" },
|
---|
669 | { "cancel", (PyCFunction)py_tevent_req_cancel,
|
---|
670 | METH_NOARGS, "cancel()" },
|
---|
671 | { NULL }
|
---|
672 | };
|
---|
673 |
|
---|
674 | static void py_tevent_req_dealloc(TeventReq_Object *self)
|
---|
675 | {
|
---|
676 | talloc_free(self->req);
|
---|
677 | PyObject_DEL(self);
|
---|
678 | }
|
---|
679 |
|
---|
680 | static PyTypeObject TeventReq_Type = {
|
---|
681 | .tp_name = "tevent.Request",
|
---|
682 | .tp_basicsize = sizeof(TeventReq_Object),
|
---|
683 | .tp_methods = py_tevent_req_methods,
|
---|
684 | .tp_dealloc = (destructor)py_tevent_req_dealloc,
|
---|
685 | .tp_getset = py_tevent_req_getsetters,
|
---|
686 | /* FIXME: .tp_new = py_tevent_req_new, */
|
---|
687 | };
|
---|
688 |
|
---|
689 | static PyObject *py_tevent_queue_get_length(TeventQueue_Object *self)
|
---|
690 | {
|
---|
691 | return PyInt_FromLong(tevent_queue_length(self->queue));
|
---|
692 | }
|
---|
693 |
|
---|
694 | static PyGetSetDef py_tevent_queue_getsetters[] = {
|
---|
695 | {
|
---|
696 | .name = discard_const_p(char, "length"),
|
---|
697 | .get = (getter)py_tevent_queue_get_length,
|
---|
698 | .doc = discard_const_p(char, "The number of elements in the queue."),
|
---|
699 | },
|
---|
700 | { NULL },
|
---|
701 | };
|
---|
702 |
|
---|
703 | static void py_tevent_queue_dealloc(TeventQueue_Object *self)
|
---|
704 | {
|
---|
705 | talloc_free(self->queue);
|
---|
706 | PyObject_Del(self);
|
---|
707 | }
|
---|
708 |
|
---|
709 | static PyTypeObject TeventQueue_Type = {
|
---|
710 | .tp_name = "tevent.Queue",
|
---|
711 | .tp_basicsize = sizeof(TeventQueue_Object),
|
---|
712 | .tp_dealloc = (destructor)py_tevent_queue_dealloc,
|
---|
713 | .tp_flags = Py_TPFLAGS_DEFAULT,
|
---|
714 | .tp_getset = py_tevent_queue_getsetters,
|
---|
715 | .tp_methods = py_tevent_queue_methods,
|
---|
716 | };
|
---|
717 |
|
---|
718 | static PyObject *py_tevent_context_signal_support(PyObject *_self)
|
---|
719 | {
|
---|
720 | TeventContext_Object *self = (TeventContext_Object *)_self;
|
---|
721 | return PyBool_FromLong(tevent_signal_support(self->ev));
|
---|
722 | }
|
---|
723 |
|
---|
724 | static PyGetSetDef py_tevent_context_getsetters[] = {
|
---|
725 | {
|
---|
726 | .name = discard_const_p(char, "signal_support"),
|
---|
727 | .get = (getter)py_tevent_context_signal_support,
|
---|
728 | .doc = discard_const_p(char, "if this platform and tevent context support signal handling"),
|
---|
729 | },
|
---|
730 | { NULL }
|
---|
731 | };
|
---|
732 |
|
---|
733 | static void py_tevent_context_dealloc(TeventContext_Object *self)
|
---|
734 | {
|
---|
735 | talloc_free(self->ev);
|
---|
736 | PyObject_Del(self);
|
---|
737 | }
|
---|
738 |
|
---|
739 | static PyObject *py_tevent_context_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
---|
740 | {
|
---|
741 | const char * const kwnames[] = { "name", NULL };
|
---|
742 | char *name = NULL;
|
---|
743 | struct tevent_context *ev;
|
---|
744 | TeventContext_Object *ret;
|
---|
745 |
|
---|
746 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", discard_const_p(char *, kwnames), &name))
|
---|
747 | return NULL;
|
---|
748 |
|
---|
749 | if (name == NULL) {
|
---|
750 | ev = tevent_context_init(NULL);
|
---|
751 | } else {
|
---|
752 | ev = tevent_context_init_byname(NULL, name);
|
---|
753 | }
|
---|
754 |
|
---|
755 | if (ev == NULL) {
|
---|
756 | PyErr_SetNone(PyExc_RuntimeError);
|
---|
757 | return NULL;
|
---|
758 | }
|
---|
759 |
|
---|
760 | ret = PyObject_New(TeventContext_Object, type);
|
---|
761 | if (ret == NULL) {
|
---|
762 | PyErr_NoMemory();
|
---|
763 | talloc_free(ev);
|
---|
764 | return NULL;
|
---|
765 | }
|
---|
766 |
|
---|
767 | ret->ev = ev;
|
---|
768 | return (PyObject *)ret;
|
---|
769 | }
|
---|
770 |
|
---|
771 | static PyTypeObject TeventContext_Type = {
|
---|
772 | .tp_name = "tevent.Context",
|
---|
773 | .tp_new = py_tevent_context_new,
|
---|
774 | .tp_basicsize = sizeof(TeventContext_Object),
|
---|
775 | .tp_dealloc = (destructor)py_tevent_context_dealloc,
|
---|
776 | .tp_methods = py_tevent_context_methods,
|
---|
777 | .tp_getset = py_tevent_context_getsetters,
|
---|
778 | .tp_flags = Py_TPFLAGS_DEFAULT,
|
---|
779 | };
|
---|
780 |
|
---|
781 | static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
|
---|
782 | {
|
---|
783 | char *backend_name;
|
---|
784 | if (!PyArg_ParseTuple(args, "s", &backend_name))
|
---|
785 | return NULL;
|
---|
786 |
|
---|
787 | tevent_set_default_backend(backend_name);
|
---|
788 |
|
---|
789 | Py_RETURN_NONE;
|
---|
790 | }
|
---|
791 |
|
---|
792 | static PyObject *py_backend_list(PyObject *self)
|
---|
793 | {
|
---|
794 | PyObject *ret = NULL;
|
---|
795 | PyObject *string = NULL;
|
---|
796 | int i, result;
|
---|
797 | const char **backends = NULL;
|
---|
798 |
|
---|
799 | ret = PyList_New(0);
|
---|
800 | if (ret == NULL) {
|
---|
801 | return NULL;
|
---|
802 | }
|
---|
803 |
|
---|
804 | backends = tevent_backend_list(NULL);
|
---|
805 | if (backends == NULL) {
|
---|
806 | PyErr_SetNone(PyExc_RuntimeError);
|
---|
807 | goto err;
|
---|
808 | }
|
---|
809 | for (i = 0; backends[i]; i++) {
|
---|
810 | string = PyStr_FromString(backends[i]);
|
---|
811 | if (!string) {
|
---|
812 | goto err;
|
---|
813 | }
|
---|
814 | result = PyList_Append(ret, string);
|
---|
815 | if (result) {
|
---|
816 | goto err;
|
---|
817 | }
|
---|
818 | Py_DECREF(string);
|
---|
819 | string = NULL;
|
---|
820 | }
|
---|
821 |
|
---|
822 | talloc_free(backends);
|
---|
823 |
|
---|
824 | return ret;
|
---|
825 |
|
---|
826 | err:
|
---|
827 | Py_XDECREF(ret);
|
---|
828 | Py_XDECREF(string);
|
---|
829 | talloc_free(backends);
|
---|
830 | return NULL;
|
---|
831 | }
|
---|
832 |
|
---|
833 | static PyMethodDef tevent_methods[] = {
|
---|
834 | { "register_backend", (PyCFunction)py_register_backend, METH_VARARGS,
|
---|
835 | "register_backend(backend)" },
|
---|
836 | { "set_default_backend", (PyCFunction)py_set_default_backend,
|
---|
837 | METH_VARARGS, "set_default_backend(backend)" },
|
---|
838 | { "backend_list", (PyCFunction)py_backend_list,
|
---|
839 | METH_NOARGS, "backend_list() -> list" },
|
---|
840 | { NULL },
|
---|
841 | };
|
---|
842 |
|
---|
843 | #define MODULE_DOC PyDoc_STR("Python wrapping of talloc-maintained objects.")
|
---|
844 |
|
---|
845 | #if PY_MAJOR_VERSION >= 3
|
---|
846 | static struct PyModuleDef moduledef = {
|
---|
847 | PyModuleDef_HEAD_INIT,
|
---|
848 | .m_name = "_tevent",
|
---|
849 | .m_doc = MODULE_DOC,
|
---|
850 | .m_size = -1,
|
---|
851 | .m_methods = tevent_methods,
|
---|
852 | };
|
---|
853 | #endif
|
---|
854 |
|
---|
855 | PyObject * module_init(void);
|
---|
856 | PyObject * module_init(void)
|
---|
857 | {
|
---|
858 | PyObject *m;
|
---|
859 |
|
---|
860 | if (PyType_Ready(&TeventContext_Type) < 0)
|
---|
861 | return NULL;
|
---|
862 |
|
---|
863 | if (PyType_Ready(&TeventQueue_Type) < 0)
|
---|
864 | return NULL;
|
---|
865 |
|
---|
866 | if (PyType_Ready(&TeventReq_Type) < 0)
|
---|
867 | return NULL;
|
---|
868 |
|
---|
869 | if (PyType_Ready(&TeventSignal_Type) < 0)
|
---|
870 | return NULL;
|
---|
871 |
|
---|
872 | if (PyType_Ready(&TeventTimer_Type) < 0)
|
---|
873 | return NULL;
|
---|
874 |
|
---|
875 | if (PyType_Ready(&TeventFd_Type) < 0)
|
---|
876 | return NULL;
|
---|
877 |
|
---|
878 | #if PY_MAJOR_VERSION >= 3
|
---|
879 | m = PyModule_Create(&moduledef);
|
---|
880 | #else
|
---|
881 | m = Py_InitModule3("_tevent", tevent_methods, MODULE_DOC);
|
---|
882 | #endif
|
---|
883 | if (m == NULL)
|
---|
884 | return NULL;
|
---|
885 |
|
---|
886 | Py_INCREF(&TeventContext_Type);
|
---|
887 | PyModule_AddObject(m, "Context", (PyObject *)&TeventContext_Type);
|
---|
888 |
|
---|
889 | Py_INCREF(&TeventQueue_Type);
|
---|
890 | PyModule_AddObject(m, "Queue", (PyObject *)&TeventQueue_Type);
|
---|
891 |
|
---|
892 | Py_INCREF(&TeventReq_Type);
|
---|
893 | PyModule_AddObject(m, "Request", (PyObject *)&TeventReq_Type);
|
---|
894 |
|
---|
895 | Py_INCREF(&TeventSignal_Type);
|
---|
896 | PyModule_AddObject(m, "Signal", (PyObject *)&TeventSignal_Type);
|
---|
897 |
|
---|
898 | Py_INCREF(&TeventTimer_Type);
|
---|
899 | PyModule_AddObject(m, "Timer", (PyObject *)&TeventTimer_Type);
|
---|
900 |
|
---|
901 | Py_INCREF(&TeventFd_Type);
|
---|
902 | PyModule_AddObject(m, "Fd", (PyObject *)&TeventFd_Type);
|
---|
903 |
|
---|
904 | PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
|
---|
905 |
|
---|
906 | return m;
|
---|
907 | }
|
---|
908 |
|
---|
909 | #if PY_MAJOR_VERSION >= 3
|
---|
910 | PyMODINIT_FUNC PyInit__tevent(void);
|
---|
911 | PyMODINIT_FUNC PyInit__tevent(void)
|
---|
912 | {
|
---|
913 | return module_init();
|
---|
914 | }
|
---|
915 | #else
|
---|
916 | void init_tevent(void);
|
---|
917 | void init_tevent(void)
|
---|
918 | {
|
---|
919 | module_init();
|
---|
920 | }
|
---|
921 | #endif
|
---|