libsemanage: throw exceptions in python rather than return NULL

Python doesn't really work on the basis of negative error code.  It
throws exceptions.  This patch automatically generates little stub
functions which will catch negative error codes and will throw
exceptions in their place.

Signed-off-by: Eric Paris <eparis@redhat.com>
Acked-by: Dan Walsh <dwalsh@redhat.com>
This commit is contained in:
Eric Paris 2011-06-29 00:57:40 -04:00
parent 78d58d73b4
commit 9406ace82b
4 changed files with 21 additions and 2 deletions

View File

@ -1,2 +1,3 @@
semanageswig_wrap.c
semanageswig_python_exception.i
semanage.py

View File

@ -36,7 +36,7 @@ LIBVERSION = 1
LIBA=libsemanage.a
TARGET=libsemanage.so
LIBPC=libsemanage.pc
SWIGIF= semanageswig_python.i
SWIGIF= semanageswig_python.i semanageswig_python_exception.i
SWIGRUBYIF= semanageswig_ruby.i
SWIGCOUT= semanageswig_wrap.c
SWIGRUBYCOUT= semanageswig_ruby_wrap.c
@ -47,7 +47,7 @@ SWIGFILES=$(SWIGSO) semanage.py
SWIGRUBYSO=_rubysemanage.so
LIBSO=$(TARGET).$(LIBVERSION)
GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT)
GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) semanageswig_python_exception.i
SRCS= $(filter-out $(GENERATED),$(wildcard *.c))
OBJS= $(patsubst %.c,%.o,$(SRCS)) conf-scan.o conf-parse.o
@ -92,6 +92,9 @@ $(LIBSO): $(LOBJS)
$(LIBPC): $(LIBPC).in
sed -e 's/@VERSION@/$(VERSION)/; s:@prefix@:$(PREFIX):; s:@libdir@:$(LIBBASE):; s:@includedir@:$(INCLUDEDIR):' < $< > $@
semanageswig_python_exception.i: ../include/semanage/semanage.h
bash exception.sh > $@
conf-scan.c: conf-scan.l conf-parse.h
$(LEX) $(LFLAGS) -t $< > $@

View File

@ -0,0 +1,14 @@
function except() {
echo "
%exception $1 {
\$action
if (result < 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
}
"
}
gcc -x c -c - -aux-info temp.aux < ../include/semanage/semanage.h
for i in `awk '/extern int/ { print $6 }' temp.aux`; do except $i ; done
rm -f -- temp.aux -.o

View File

@ -460,4 +460,5 @@
$1 = &temp;
}
%include "semanageswig_python_exception.i"
%include "semanageswig.i"