python module compiles.

git-svn-id: file:///svn/unbound/trunk@1557 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2009-03-25 16:20:34 +00:00
parent 0799d77798
commit 11f11a5377
8 changed files with 96 additions and 82 deletions

View file

@ -39,7 +39,9 @@ configfile=@ub_conf_file@
UNBOUND_RUN_DIR=@UNBOUND_RUN_DIR@ UNBOUND_RUN_DIR=@UNBOUND_RUN_DIR@
CHECKLOCK_SRC=@CHECKLOCK_SRC@ CHECKLOCK_SRC=@CHECKLOCK_SRC@
UB_ON_WINDOWS=@UB_ON_WINDOWS@ UB_ON_WINDOWS=@UB_ON_WINDOWS@
WITH_PYTHONMODULE=@WITH_PYTHONMODULE@
SWIG=@SWIG@
YACC=@YACC@ YACC=@YACC@
LEX=@LEX@ LEX=@LEX@
CC=@CC@ CC=@CC@
@ -68,11 +70,14 @@ LINTFLAGS=+quiet -weak -warnposix -unrecog -Din_addr_t=uint32_t -Du_int=unsigned
LINTFLAGS+="-DBN_ULONG=unsigned long" -Dkrb5_int32=int "-Dkrb5_ui_4=unsigned int" -DPQ_64BIT=uint64_t -DRC4_INT=unsigned LINTFLAGS+="-DBN_ULONG=unsigned long" -Dkrb5_int32=int "-Dkrb5_ui_4=unsigned int" -DPQ_64BIT=uint64_t -DRC4_INT=unsigned
INSTALL=$(srcdir)/install-sh INSTALL=$(srcdir)/install-sh
ifeq "$(WITH_PYTHONMODULE)" "yes"
PYTHONMOD_SRC=$(srcdir)/pythonmod/*.c
endif
COMMON_SRC=$(patsubst $(srcdir)/%,%, $(wildcard $(srcdir)/services/*.c \ COMMON_SRC=$(patsubst $(srcdir)/%,%, $(wildcard $(srcdir)/services/*.c \
$(srcdir)/services/cache/*.c $(srcdir)/util/*.c \ $(srcdir)/services/cache/*.c $(srcdir)/util/*.c \
$(srcdir)/util/data/*.c $(srcdir)/util/storage/*.c \ $(srcdir)/util/data/*.c $(srcdir)/util/storage/*.c \
$(srcdir)/iterator/*.c $(srcdir)/validator/*.c)) \ $(srcdir)/iterator/*.c $(srcdir)/validator/*.c $(PYTHONMOD_SRC))) \
util/configparser.c util/configlexer.c $(CHECKLOCK_SRC) util/configparser.c util/configlexer.c $(CHECKLOCK_SRC)
COMMON_OBJ=$(addprefix $(BUILD),$(COMMON_SRC:.c=.lo)) COMMON_OBJ=$(addprefix $(BUILD),$(COMMON_SRC:.c=.lo))
COMPAT_OBJ=$(addprefix $(BUILD)compat/,$(LIBOBJS:.o=.lo)) COMPAT_OBJ=$(addprefix $(BUILD)compat/,$(LIBOBJS:.o=.lo))
@ -272,6 +277,13 @@ unbound-control-setup: $(srcdir)/smallapp/unbound-control-setup.sh
# cp $(ldnsdir)/examples/ldns-testpkts.c testcode/ldns-testpkts.c # cp $(ldnsdir)/examples/ldns-testpkts.c testcode/ldns-testpkts.c
# cp $(ldnsdir)/examples/ldns-testpkts.h testcode/ldns-testpkts.h # cp $(ldnsdir)/examples/ldns-testpkts.h testcode/ldns-testpkts.h
ifeq "$(WITH_PYTHONMODULE)" "yes"
pythonmod/pythonmod.c: pythonmod/interface.h
%.h: $(srcdir)/%.i $(srcdir)/config.h
$(INFO) Swig $<
$Q$(SWIG) $(CPPFLAGS) -o $@ -python $<
endif
util/config_file.c: util/configparser.h util/config_file.c: util/configparser.h
util/configlexer.c: $(srcdir)/util/configlexer.lex util/configparser.h util/configlexer.c: $(srcdir)/util/configlexer.lex util/configparser.h
$(INFO) Lex $< $(INFO) Lex $<

26
configure vendored
View file

@ -22199,11 +22199,13 @@ echo "$as_me: WARNING: cannot determine SWIG version" >&2;}
{ echo "$as_me:$LINENO: checking SWIG" >&5 { echo "$as_me:$LINENO: checking SWIG" >&5
echo $ECHO_N "checking SWIG... $ECHO_C" >&6; } echo $ECHO_N "checking SWIG... $ECHO_C" >&6; }
if test -z SWIG; then if test -z "$SWIG"; then
{ echo "$as_me:$LINENO: result: failed" >&5 { echo "$as_me:$LINENO: result: failed" >&5
echo "${ECHO_T}failed" >&6; } echo "${ECHO_T}failed" >&6; }
swig="" swig=""
ub_with_pyunbound=no
ub_with_pythonmod=no
else else
cat >>confdefs.h <<\_ACEOF cat >>confdefs.h <<\_ACEOF
@ -22212,17 +22214,12 @@ _ACEOF
swig="$SWIG" swig="$SWIG"
{ echo "$as_me:$LINENO: result: done" >&5 { echo "$as_me:$LINENO: result: present" >&5
echo "${ECHO_T}done" >&6; } echo "${ECHO_T}present" >&6; }
fi
# If have Python & SWIG
if test ! -z "SWIG"; then
# If have Python & SWIG
# Declare PythonMod # Declare PythonMod
if test x_$ub_with_pythonmod != x_no; then if test x_$ub_with_pythonmod != x_no; then
{ echo "$as_me:$LINENO: result: Will build Python module." >&5
echo "${ECHO_T}Will build Python module." >&6; }
cat >>confdefs.h <<\_ACEOF cat >>confdefs.h <<\_ACEOF
#define WITH_PYTHONMODULE 1 #define WITH_PYTHONMODULE 1
@ -22234,8 +22231,6 @@ _ACEOF
# Declare PyUnbound # Declare PyUnbound
if test x_$ub_with_pyunbound != x_no; then if test x_$ub_with_pyunbound != x_no; then
{ echo "$as_me:$LINENO: result: Will build PyUnbound." >&5
echo "${ECHO_T}Will build PyUnbound." >&6; }
cat >>confdefs.h <<\_ACEOF cat >>confdefs.h <<\_ACEOF
#define WITH_PYUNBOUND 1 #define WITH_PYUNBOUND 1
@ -22244,15 +22239,10 @@ _ACEOF
WITH_PYUNBOUND=yes WITH_PYUNBOUND=yes
fi fi
else
{ echo "$as_me:$LINENO: result: SWIG libraries not found" >&5
echo "${ECHO_T}SWIG libraries not found" >&6; }
ub_with_pyunbound=no
ub_with_pythonmod=no
fi fi
else else
{ echo "$as_me:$LINENO: result: Python libraries not found" >&5 { echo "$as_me:$LINENO: result: Python libraries not found, won't build PythonMod or PyUnbound" >&5
echo "${ECHO_T}Python libraries not found" >&6; } echo "${ECHO_T}Python libraries not found, won't build PythonMod or PyUnbound" >&6; }
ub_with_pyunbound=no ub_with_pyunbound=no
ub_with_pythonmod=no ub_with_pythonmod=no
fi fi

View file

@ -250,21 +250,19 @@ if test x_$ub_test_python != x_no; then
ub_have_swig=no ub_have_swig=no
AC_PROG_SWIG AC_PROG_SWIG
AC_MSG_CHECKING(SWIG) AC_MSG_CHECKING(SWIG)
if test -z SWIG; then if test -z "$SWIG"; then
AC_MSG_RESULT(failed, won't build Python module and PyUnbound) AC_MSG_RESULT(failed, won't build Python module and PyUnbound)
AC_SUBST(swig, "") AC_SUBST(swig, "")
ub_with_pyunbound=no
ub_with_pythonmod=no
else else
AC_DEFINE(HAVE_SWIG, 1, [Define if you have Swig libraries and header files.]) AC_DEFINE(HAVE_SWIG, 1, [Define if you have Swig libraries and header files.])
AC_SUBST(swig, "$SWIG") AC_SUBST(swig, "$SWIG")
AC_MSG_RESULT(done) AC_MSG_RESULT(present)
fi
# If have Python & SWIG
if test ! -z "SWIG"; then
# If have Python & SWIG
# Declare PythonMod # Declare PythonMod
if test x_$ub_with_pythonmod != x_no; then if test x_$ub_with_pythonmod != x_no; then
AC_MSG_RESULT(Will build Python module.)
AC_DEFINE(WITH_PYTHONMODULE, 1, [Define if you want Python module.]) AC_DEFINE(WITH_PYTHONMODULE, 1, [Define if you want Python module.])
WITH_PYTHONMODULE=yes WITH_PYTHONMODULE=yes
AC_SUBST(WITH_PYTHONMODULE) AC_SUBST(WITH_PYTHONMODULE)
@ -272,18 +270,13 @@ if test x_$ub_test_python != x_no; then
# Declare PyUnbound # Declare PyUnbound
if test x_$ub_with_pyunbound != x_no; then if test x_$ub_with_pyunbound != x_no; then
AC_MSG_RESULT(Will build PyUnbound.)
AC_DEFINE(WITH_PYUNBOUND, 1, [Define if you want PyUnbound.]) AC_DEFINE(WITH_PYUNBOUND, 1, [Define if you want PyUnbound.])
WITH_PYUNBOUND=yes WITH_PYUNBOUND=yes
AC_SUBST(WITH_PYUNBOUND) AC_SUBST(WITH_PYUNBOUND)
fi fi
else
AC_MSG_RESULT(SWIG libraries not found, won't build PythonMod or PyUnbound)
ub_with_pyunbound=no
ub_with_pythonmod=no
fi fi
else else
AC_MSG_RESULT(Python libraries not found, won't build PythonMod or PyUnbound) AC_MSG_RESULT([Python libraries not found, won't build PythonMod or PyUnbound])
ub_with_pyunbound=no ub_with_pyunbound=no
ub_with_pythonmod=no ub_with_pythonmod=no
fi fi

View file

@ -1,6 +1,7 @@
25 March 2009: Wouter 25 March 2009: Wouter
- initial import of the python contribution from Zdenek Vasicek and - initial import of the python contribution from Zdenek Vasicek and
Marek Vavrusa. Marek Vavrusa.
- pythonmod in Makefile; changes to remove warnings/errors for 1.3.0.
24 March 2009: Wouter 24 March 2009: Wouter
- more neat configure.ac. Removed duplicate config.h includes. - more neat configure.ac. Removed duplicate config.h includes.

View file

@ -16,6 +16,7 @@
#include "util/config_file.h" #include "util/config_file.h"
#include "util/data/msgreply.h" #include "util/data/msgreply.h"
#include "util/data/packed_rrset.h" #include "util/data/packed_rrset.h"
#include "util/data/dname.h"
#include "util/storage/lruhash.h" #include "util/storage/lruhash.h"
#include "services/cache/dns.h" #include "services/cache/dns.h"
%} %}
@ -137,11 +138,11 @@ struct query_info {
}; };
PyObject* _get_qname(struct query_info* q) { PyObject* _get_qname(struct query_info* q) {
return PyString_FromStringAndSize(q->qname, q->qname_len); return PyString_FromStringAndSize((char*)q->qname, q->qname_len);
} }
PyObject* _get_qname_components(struct query_info* q) { PyObject* _get_qname_components(struct query_info* q) {
return GetNameAsLabelList(q->qname, q->qname_len); return GetNameAsLabelList((const char*)q->qname, q->qname_len);
} }
%} %}
@ -149,7 +150,7 @@ struct query_info {
PyObject* dnameAsStr(const char* dname) { PyObject* dnameAsStr(const char* dname) {
char buf[LDNS_MAX_DOMAINLEN+1]; char buf[LDNS_MAX_DOMAINLEN+1];
buf[0] = '\0'; buf[0] = '\0';
dname_str(dname, buf); dname_str((uint8_t*)dname, buf);
return PyString_FromString(buf); return PyString_FromString(buf);
} }
%} %}
@ -199,10 +200,10 @@ uint16_t ntohs(uint16_t netshort);
%inline %{ %inline %{
PyObject* _get_dname(struct packed_rrset_key* k) { PyObject* _get_dname(struct packed_rrset_key* k) {
return PyString_FromStringAndSize(k->dname, k->dname_len); return PyString_FromStringAndSize((char*)k->dname, k->dname_len);
} }
PyObject* _get_dname_components(struct packed_rrset_key* k) { PyObject* _get_dname_components(struct packed_rrset_key* k) {
return GetNameAsLabelList(k->dname, k->dname_len); return GetNameAsLabelList((char*)k->dname, k->dname_len);
} }
%} %}
@ -286,23 +287,28 @@ struct packed_rrset_data {
%inline %{ %inline %{
PyObject* _get_data_rr_len(struct packed_rrset_data* d, int idx) { PyObject* _get_data_rr_len(struct packed_rrset_data* d, int idx) {
if ((d != NULL) && (idx >= 0) && (idx < (d->count+d->rrsig_count))) if ((d != NULL) && (idx >= 0) &&
((size_t)idx < (d->count+d->rrsig_count)))
return PyInt_FromLong(d->rr_len[idx]); return PyInt_FromLong(d->rr_len[idx]);
return Py_None; return Py_None;
} }
void _set_data_rr_ttl(struct packed_rrset_data* d, int idx, uint32_t ttl) void _set_data_rr_ttl(struct packed_rrset_data* d, int idx, uint32_t ttl)
{ {
if ((d != NULL) && (idx >= 0) && (idx < (d->count+d->rrsig_count))) if ((d != NULL) && (idx >= 0) &&
((size_t)idx < (d->count+d->rrsig_count)))
d->rr_ttl[idx] = ttl; d->rr_ttl[idx] = ttl;
} }
PyObject* _get_data_rr_ttl(struct packed_rrset_data* d, int idx) { PyObject* _get_data_rr_ttl(struct packed_rrset_data* d, int idx) {
if ((d != NULL) && (idx >= 0) && (idx < (d->count+d->rrsig_count))) if ((d != NULL) && (idx >= 0) &&
((size_t)idx < (d->count+d->rrsig_count)))
return PyInt_FromLong(d->rr_ttl[idx]); return PyInt_FromLong(d->rr_ttl[idx]);
return Py_None; return Py_None;
} }
PyObject* _get_data_rr_data(struct packed_rrset_data* d, int idx) { PyObject* _get_data_rr_data(struct packed_rrset_data* d, int idx) {
if ((d != NULL) && (idx >= 0) && (idx < (d->count+d->rrsig_count))) if ((d != NULL) && (idx >= 0) &&
return PyString_FromStringAndSize(d->rr_data[idx],d->rr_len[idx]); ((size_t)idx < (d->count+d->rrsig_count)))
return PyString_FromStringAndSize((char*)d->rr_data[idx],
d->rr_len[idx]);
return Py_None; return Py_None;
} }
%} %}
@ -346,8 +352,8 @@ struct reply_info {
}; };
struct rrset_ref { struct rrset_ref {
struct ub_packed_rrset_key* key; struct ub_packed_rrset_key* key;
rrset_id_t id; rrset_id_t id;
}; };
struct dns_msg { struct dns_msg {
@ -369,13 +375,13 @@ struct dns_msg {
%inline %{ %inline %{
struct ub_packed_rrset_key* _rrset_rrsets_get(struct reply_info* r, int idx) { struct ub_packed_rrset_key* _rrset_rrsets_get(struct reply_info* r, int idx) {
if ((r != NULL) && (idx >= 0) && (idx < r->rrset_count)) if ((r != NULL) && (idx >= 0) && ((size_t)idx < r->rrset_count))
return r->rrsets[idx]; return r->rrsets[idx];
return NULL; return NULL;
} }
struct rrset_ref* _rrset_ref_get(struct reply_info* r, int idx) { struct rrset_ref* _rrset_ref_get(struct reply_info* r, int idx) {
if ((r != NULL) && (idx >= 0) && (idx < r->rrset_count)) { if ((r != NULL) && (idx >= 0) && ((size_t)idx < r->rrset_count)) {
//printf("_rrset_ref_get: %lX key:%lX\n", r->ref + idx, r->ref[idx].key); //printf("_rrset_ref_get: %lX key:%lX\n", r->ref + idx, r->ref[idx].key);
return &(r->ref[idx]); return &(r->ref[idx]);
// return &(r->ref[idx]); // return &(r->ref[idx]);
@ -705,9 +711,7 @@ int set_return_msg(struct module_qstate* qstate,
ldns_pkt* pkt = 0; ldns_pkt* pkt = 0;
ldns_status status; ldns_status status;
ldns_rr_list* rr_list1 = 0,*rr_list2 = 0,*rr_list3 = 0,*rr_list4 = 0; ldns_rr_list* rr_list1 = 0,*rr_list2 = 0,*rr_list3 = 0,*rr_list4 = 0;
ldns_buffer *qb = 0; ldns_buffer *qb = 0;
struct dns_msg* m = 0;
struct msg_parse* p = 0;
int res = 1; int res = 1;
if ((!checkList(question)) || (!checkList(answer)) || (!checkList(authority)) || (!checkList(additional))) if ((!checkList(question)) || (!checkList(answer)) || (!checkList(authority)) || (!checkList(additional)))

View file

@ -40,12 +40,14 @@
#include "pythonmod_utils.h" #include "pythonmod_utils.h"
#include <Python.h> #include <Python.h>
// Generated /* Generated */
#include "pythonmod/interface.h" #include "pythonmod/interface.h"
int pythonmod_init(struct module_env* env, int id) int pythonmod_init(struct module_env* env, int id)
{ {
// Initialize module /* Initialize module */
FILE* script_py = NULL;
PyObject* py_cfg, *res;
struct pythonmod_env* pe = (struct pythonmod_env*)calloc(1, sizeof(struct pythonmod_env)); struct pythonmod_env* pe = (struct pythonmod_env*)calloc(1, sizeof(struct pythonmod_env));
if (!pe) if (!pe)
{ {
@ -60,14 +62,14 @@ int pythonmod_init(struct module_env* env, int id)
pe->data = NULL; pe->data = NULL;
pe->qstate = NULL; pe->qstate = NULL;
// Initialize module /* Initialize module */
if ((pe->fname = env->cfg->python_script) == NULL) if ((pe->fname = env->cfg->python_script) == NULL)
{ {
log_err("pythonmod: no script given."); log_err("pythonmod: no script given.");
return 0; return 0;
} }
// Initialize Python libraries /* Initialize Python libraries */
if (!Py_IsInitialized()) if (!Py_IsInitialized())
{ {
Py_SetProgramName("unbound"); Py_SetProgramName("unbound");
@ -78,7 +80,7 @@ int pythonmod_init(struct module_env* env, int id)
SWIG_init(); SWIG_init();
} }
// Initialize Python /* Initialize Python */
PyRun_SimpleString("import sys \n"); PyRun_SimpleString("import sys \n");
PyRun_SimpleString("sys.path.append('.') \n"); PyRun_SimpleString("sys.path.append('.') \n");
PyRun_SimpleString("sys.path.append('"RUN_DIR"') \n"); PyRun_SimpleString("sys.path.append('"RUN_DIR"') \n");
@ -88,22 +90,21 @@ int pythonmod_init(struct module_env* env, int id)
return 0; return 0;
} }
// Check Python file load /* Check Python file load */
FILE* script_py = NULL;
if ((script_py = fopen(pe->fname, "r")) == NULL) if ((script_py = fopen(pe->fname, "r")) == NULL)
{ {
log_err("pythonmod: can't open file %s for reading", pe->fname); log_err("pythonmod: can't open file %s for reading", pe->fname);
return 0; return 0;
} }
// Load file /* Load file */
pe->module = PyImport_AddModule("__main__"); pe->module = PyImport_AddModule("__main__");
pe->dict = PyModule_GetDict(pe->module); pe->dict = PyModule_GetDict(pe->module);
pe->data = Py_None; pe->data = Py_None;
Py_INCREF(pe->data); Py_INCREF(pe->data);
PyModule_AddObject(pe->module, "mod_env", pe->data); PyModule_AddObject(pe->module, "mod_env", pe->data);
//TODO: deallocation of pe->... if an error occurs /* TODO: deallocation of pe->... if an error occurs */
if (PyRun_SimpleFile(script_py, pe->fname) < 0) if (PyRun_SimpleFile(script_py, pe->fname) < 0)
{ {
@ -135,8 +136,8 @@ int pythonmod_init(struct module_env* env, int id)
} }
PyEval_AcquireLock(); PyEval_AcquireLock();
PyObject* py_cfg = SWIG_NewPointerObj((void*) env->cfg, SWIGTYPE_p_config_file, 0); py_cfg = SWIG_NewPointerObj((void*) env->cfg, SWIGTYPE_p_config_file, 0);
PyObject* res = PyObject_CallFunction(pe->func_init, "iO", id, py_cfg); res = PyObject_CallFunction(pe->func_init, "iO", id, py_cfg);
if (PyErr_Occurred()) if (PyErr_Occurred())
{ {
log_err("pythonmod: Exception occurred in function init"); log_err("pythonmod: Exception occurred in function init");
@ -156,25 +157,27 @@ void pythonmod_deinit(struct module_env* env, int id)
if(pe == NULL) if(pe == NULL)
return; return;
// Free Python resources /* Free Python resources */
if(pe->module != NULL) if(pe->module != NULL)
{ {
// Deinit module PyObject* res;
/* Deinit module */
PyEval_AcquireLock(); PyEval_AcquireLock();
PyObject* res = PyObject_CallFunction(pe->func_deinit, "i", id); res = PyObject_CallFunction(pe->func_deinit, "i", id);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
log_err("pythonmod: Exception occurred in function deinit"); log_err("pythonmod: Exception occurred in function deinit");
PyErr_Print(); PyErr_Print();
} }
// Free result if any /* Free result if any */
Py_XDECREF(res); Py_XDECREF(res);
// Free shared data if any /* Free shared data if any */
Py_XDECREF(pe->data); Py_XDECREF(pe->data);
Py_Finalize(); Py_Finalize();
} }
// Module is deallocated in Python /* Module is deallocated in Python */
env->modinfo[id] = NULL; env->modinfo[id] = NULL;
} }
@ -182,15 +185,17 @@ void pythonmod_inform_super(struct module_qstate* qstate, int id, struct module_
{ {
struct pythonmod_env* pe = (struct pythonmod_env*)qstate->env->modinfo[id]; struct pythonmod_env* pe = (struct pythonmod_env*)qstate->env->modinfo[id];
struct pythonmod_qstate* pq = (struct pythonmod_qstate*)qstate->minfo[id]; struct pythonmod_qstate* pq = (struct pythonmod_qstate*)qstate->minfo[id];
PyObject* py_qstate, *py_sqstate, *res;
log_query_info(VERB_ALGO, "pythonmod: inform_super, sub is", &qstate->qinfo); log_query_info(VERB_ALGO, "pythonmod: inform_super, sub is", &qstate->qinfo);
log_query_info(VERB_ALGO, "super is", &super->qinfo); log_query_info(VERB_ALGO, "super is", &super->qinfo);
PyObject* py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0); py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0);
PyObject* py_sqstate = SWIG_NewPointerObj((void*) super, SWIGTYPE_p_module_qstate, 0); py_sqstate = SWIG_NewPointerObj((void*) super, SWIGTYPE_p_module_qstate, 0);
PyEval_AcquireLock(); PyEval_AcquireLock();
PyObject* res = PyObject_CallFunction(pe->func_inform, "iOOO", id, py_qstate, py_sqstate, pq->data); res = PyObject_CallFunction(pe->func_inform, "iOOO", id, py_qstate,
py_sqstate, pq->data);
if (PyErr_Occurred()) if (PyErr_Occurred())
{ {
@ -211,27 +216,30 @@ void pythonmod_inform_super(struct module_qstate* qstate, int id, struct module_
PyEval_ReleaseLock(); PyEval_ReleaseLock();
} }
void pythonmod_operate(struct module_qstate* qstate, enum module_ev event, int id, struct outbound_entry* outbound) void pythonmod_operate(struct module_qstate* qstate, enum module_ev event,
int id, struct outbound_entry* ATTR_UNUSED(outbound))
{ {
struct pythonmod_env* pe = (struct pythonmod_env*)qstate->env->modinfo[id]; struct pythonmod_env* pe = (struct pythonmod_env*)qstate->env->modinfo[id];
struct pythonmod_qstate* pq = (struct pythonmod_qstate*)qstate->minfo[id]; struct pythonmod_qstate* pq = (struct pythonmod_qstate*)qstate->minfo[id];
PyObject* py_qstate, *res;
if ( pq == NULL) if ( pq == NULL)
{ {
// create qstate /* create qstate */
pq = qstate->minfo[id] = malloc(sizeof(struct pythonmod_qstate)); pq = qstate->minfo[id] = malloc(sizeof(struct pythonmod_qstate));
//Initialize per query data /* Initialize per query data */
pq->data = Py_None; pq->data = Py_None;
Py_INCREF(pq->data); Py_INCREF(pq->data);
} }
// Lock Python /* Lock Python */
PyEval_AcquireLock(); PyEval_AcquireLock();
// Call operate /* Call operate */
PyObject* py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0); py_qstate = SWIG_NewPointerObj((void*) qstate, SWIGTYPE_p_module_qstate, 0);
PyObject* res = PyObject_CallFunction(pe->func_operate, "iiOO", id, (int) event, py_qstate, pq->data); res = PyObject_CallFunction(pe->func_operate, "iiOO", id, (int) event,
py_qstate, pq->data);
if (PyErr_Occurred()) if (PyErr_Occurred())
{ {
log_err("pythonmod: Exception occurred in function operate, event: %s", strmodulevent(event)); log_err("pythonmod: Exception occurred in function operate, event: %s", strmodulevent(event));
@ -246,22 +254,22 @@ void pythonmod_operate(struct module_qstate* qstate, enum module_ev event, int i
Py_XDECREF(res); Py_XDECREF(res);
Py_XDECREF(py_qstate); Py_XDECREF(py_qstate);
// Unlock Python /* Unlock Python */
PyEval_ReleaseLock(); PyEval_ReleaseLock();
} }
void pythonmod_clear(struct module_qstate* qstate, int id) void pythonmod_clear(struct module_qstate* qstate, int id)
{ {
struct pythonmod_qstate* pq;
if (qstate == NULL) if (qstate == NULL)
return; return;
struct pythonmod_qstate* pq = qstate->minfo[id]; pq = (struct pythonmod_qstate*)qstate->minfo[id];
log_info("pythonmod: clear, id: %d, pq:%lX", id, (unsigned long int)pq); log_info("pythonmod: clear, id: %d, pq:%lX", id, (unsigned long int)pq);
if(pq != NULL) if(pq != NULL)
{ {
Py_DECREF(pq->data); Py_DECREF(pq->data);
// Free qstate /* Free qstate */
free(pq); free(pq);
} }
@ -278,8 +286,8 @@ size_t pythonmod_get_mem(struct module_env* env, int id)
} }
/** /**
* The module function block * The module function block
*/ */
static struct module_func_block pythonmod_block = { static struct module_func_block pythonmod_block = {
"python", "python",
&pythonmod_init, &pythonmod_deinit, &pythonmod_operate, &pythonmod_inform_super, &pythonmod_init, &pythonmod_deinit, &pythonmod_operate, &pythonmod_inform_super,

View file

@ -40,6 +40,9 @@
#include <Python.h> #include <Python.h>
struct PyObject; struct PyObject;
#if S_SPLINT_S
typedef struct PyObject PyObject;
#endif
/** /**
* Global state for the module. * Global state for the module.

View file

@ -33,11 +33,14 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "config.h"
#include "util/module.h" #include "util/module.h"
#include "util/net_help.h" #include "util/net_help.h"
#include "services/cache/dns.h" #include "services/cache/dns.h"
#include "util/data/msgparse.h" #include "util/data/msgparse.h"
#include "util/data/msgreply.h" #include "util/data/msgreply.h"
#include "util/storage/slabhash.h"
#include "util/regional.h"
#include <Python.h> #include <Python.h>
/** Store the reply_info and query_info pair in message cache (qstate->msg_cache) */ /** Store the reply_info and query_info pair in message cache (qstate->msg_cache) */