Skip to content

Commit 389b6f1

Browse files
committed
New async support
1 parent 58b7c77 commit 389b6f1

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

context_util.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <Python.h>
2+
#include <getdns/getdns.h>
3+
#include <arpa/inet.h>
4+
#include "pygetdns.h"
5+
6+
7+
/*
8+
* Get the address of the Python function object
9+
* being passed in by name to the context
10+
* query methods
11+
*/
12+
13+
PyObject *
14+
get_callback(char *py_main, char *callback)
15+
{
16+
PyObject *main_module;
17+
PyObject *main_dict;
18+
PyObject *callback_func;
19+
20+
if ((main_module = PyImport_AddModule(py_main)) == 0) {
21+
PyErr_SetString(getdns_error, "No 'main'");
22+
return NULL;
23+
}
24+
main_dict = PyModule_GetDict(main_module);
25+
if ((callback_func = PyDict_GetItemString(main_dict, callback)) == 0) {
26+
PyErr_SetString(getdns_error, "callback not found\n");
27+
return NULL;
28+
}
29+
if (!PyCallable_Check(callback_func)) {
30+
PyErr_SetString(getdns_error, "The callback function is not runnable");
31+
return NULL;
32+
}
33+
return callback_func;
34+
}
35+
36+
37+
void
38+
callback_shim(struct getdns_context *context,
39+
getdns_callback_type_t type,
40+
struct getdns_dict *response,
41+
void *userarg,
42+
getdns_transaction_t tid)
43+
{
44+
PyObject *py_callback_type;
45+
PyObject *py_result;
46+
PyObject *py_tid;
47+
PyObject *py_userarg;
48+
49+
userarg_blob *u = (userarg_blob *)userarg;
50+
if ((py_callback_type = PyInt_FromLong((long)type)) == NULL) {
51+
PyObject *err_type, *err_value, *err_traceback;
52+
PyErr_Fetch(&err_type, &err_value, &err_traceback);
53+
PyErr_Restore(err_type, err_value, err_traceback);
54+
return;
55+
}
56+
if (type == GETDNS_CALLBACK_CANCEL) {
57+
py_result = Py_None;
58+
py_tid = Py_None;
59+
py_userarg = Py_None;
60+
} else {
61+
py_result = result_create(response);
62+
py_tid = PyInt_FromLong((long)tid);
63+
if (u->userarg)
64+
py_userarg = PyString_FromString(u->userarg);
65+
else
66+
py_userarg = Py_None;
67+
}
68+
PyObject_CallFunctionObjArgs(u->callback_func, py_callback_type, py_result, py_userarg, py_tid, NULL);
69+
}

0 commit comments

Comments
 (0)