From: Wouter Wijngaards Date: Fri, 19 Aug 2011 13:26:21 +0000 (+0000) Subject: * Fix python api to support python2 and python3 (thanks Karel Slany). X-Git-Tag: release-1.6.11rc1~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1825e437b08978c9639cf726520c8174fabc7dea;p=thirdparty%2Fldns.git * Fix python api to support python2 and python3 (thanks Karel Slany). --- diff --git a/Changelog b/Changelog index 1de9dd56..b6b3e76c 100644 --- a/Changelog +++ b/Changelog @@ -3,6 +3,7 @@ * bugfix #392: Apex only and percentage checks for ldns-verify-zone * bugfix #398: Allow NSEC RRSIGs before the NSEC3 in ldns-verify-zone * Fix python site package path to /usr/lib64. + * Fix python api to support python2 and python3 (thanks Karel Slany). 1.6.10 2011-05-31 * New example tool added: ldns-gen-zone. diff --git a/Makefile.in b/Makefile.in index 596bef18..42a69b21 100644 --- a/Makefile.in +++ b/Makefile.in @@ -15,7 +15,8 @@ libdir = @libdir@ includedir = @includedir@ doxygen = @doxygen@ pywrapdir = ${srcdir}/contrib/python -swig = @swig@ +swig = @swig@ +swigpy_flags = -python @SWIGPY3@ python_site =@PYTHON_SITE_PKG@ pyldns_inst =@PYLDNS@ pyldns_uninst =@PYLDNS@ @@ -172,7 +173,7 @@ manpages: $(srcdir)/doc/function_manpages pyldns: _ldns.la $(pywrapdir)/ldns_wrapper.c: $(pywrapdir)/ldns.i $(wildcard $(pywrapdir)/*.i) $(LIBDNS_HEADERS) ldns/common.h ldns/util.h ldns/config.h - $(swig) -python -o $@ $(CPPFLAGS) $(PYTHON_CPPFLAGS) $< + $(swig) $(swigpy_flags) -o $@ $(CPPFLAGS) $(PYTHON_CPPFLAGS) $< ldns_wrapper.lo: $(pywrapdir)/ldns_wrapper.c $(LIBDNS_HEADERS) ldns/common.h ldns/util.h ldns/config.h $(COMP_LIB) -I./include/ldns $(PYTHON_CPPFLAGS) $(PYTHON_X_CFLAGS) -c $< -o $@ diff --git a/acx_python.m4 b/acx_python.m4 index 6fa925af..7eb2cdf8 100644 --- a/acx_python.m4 +++ b/acx_python.m4 @@ -14,17 +14,19 @@ AC_DEFUN([AC_PYTHON_DEVEL],[ fi if test -z "$PYTHON_VERSION"; then - PYTHON_VERSION=`$PYTHON -c "import sys, string; \ - print string.split(sys.version)[[0]]"` - fi + PYTHON_VERSION=`$PYTHON -c "import sys; \ + ver = sys.version.split()[[0]]; \ + major,minor = ver.split('.')[[0:2]]; \ + print(major + '.' + minor)"` + fi # # Check for a version of Python >= 2.1.0 # AC_MSG_CHECKING([for a version of Python >= '2.1.0']) - ac_supports_python_ver=`$PYTHON -c "import sys, string; \ - ver = string.split(sys.version)[[0]]; \ - print ver >= '2.1.0'"` + ac_supports_python_ver=`$PYTHON -c "import sys; \ + ver = sys.version.split()[[0]]; \ + print(ver >= '2.1.0')"` if test "$ac_supports_python_ver" != "True"; then if test -z "$PYTHON_NOVERSIONCHECK"; then AC_MSG_RESULT([no]) @@ -49,8 +51,8 @@ to something else than an empty string. # if test -n "$1"; then AC_MSG_CHECKING([for a version of Python $1]) - ac_supports_python_ver=`$PYTHON -c "import sys, string; \ - ver = string.split(sys.version)[[0]]; \ + ac_supports_python_ver=`$PYTHON -c "import sys; \ + ver = sys.version.split()[[0]]; \ print ver $1"` if test "$ac_supports_python_ver" = "True"; then AC_MSG_RESULT([yes]) @@ -86,7 +88,7 @@ $ac_distutils_result]) AC_MSG_CHECKING([for Python include path]) if test -z "$PYTHON_CPPFLAGS"; then python_path=`$PYTHON -c "import distutils.sysconfig; \ - print distutils.sysconfig.get_python_inc();"` + print(distutils.sysconfig.get_python_inc())"` if test -n "${python_path}"; then python_path="-I$python_path" fi @@ -103,22 +105,20 @@ $ac_distutils_result]) # (makes two attempts to ensure we've got a version number # from the interpreter) py_version=`$PYTHON -c "from distutils.sysconfig import *; \ - from string import join; \ - print join(get_config_vars('VERSION'))"` + print(get_config_vars('VERSION')[[0]])"` if test "$py_version" = "[None]"; then if test -n "$PYTHON_VERSION"; then py_version=$PYTHON_VERSION else py_version=`$PYTHON -c "import sys; \ - print sys.version[[:3]]"` + print(sys.version[[:3]])"` fi fi PYTHON_LDFLAGS=`$PYTHON -c "from distutils.sysconfig import *; \ - from string import join; \ - print '-L' + get_python_lib(0,1), \ - '-L' + os.path.dirname(get_python_lib(0,1)), \ - '-lpython';"`$py_version + print('-L' + get_python_lib(0,1) + \ + ' -L' + os.path.dirname(get_python_lib(0,1)) + \ + ' -lpython')"`$py_version fi AC_MSG_RESULT([$PYTHON_LDFLAGS]) AC_SUBST([PYTHON_LDFLAGS]) @@ -129,7 +129,7 @@ $ac_distutils_result]) AC_MSG_CHECKING([for Python site-packages path]) if test -z "$PYTHON_SITE_PKG"; then PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \ - print distutils.sysconfig.get_python_lib(1,0);"` + print(distutils.sysconfig.get_python_lib(1,0))"` fi AC_MSG_RESULT([$PYTHON_SITE_PKG]) AC_SUBST([PYTHON_SITE_PKG]) @@ -141,7 +141,7 @@ $ac_distutils_result]) if test -z "$PYTHON_EXTRA_LIBS"; then PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \ conf = distutils.sysconfig.get_config_var; \ - print conf('LOCALMODLIBS'), conf('LIBS')"` + print(conf('LOCALMODLIBS') + conf('LIBS'))"` fi AC_MSG_RESULT([$PYTHON_EXTRA_LIBS]) AC_SUBST(PYTHON_EXTRA_LIBS) @@ -153,7 +153,7 @@ $ac_distutils_result]) if test -z "$PYTHON_EXTRA_LDFLAGS"; then PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \ conf = distutils.sysconfig.get_config_var; \ - print conf('LINKFORSHARED')"` + print(conf('LINKFORSHARED'))"` fi AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS]) AC_SUBST(PYTHON_EXTRA_LDFLAGS) diff --git a/configure.ac b/configure.ac index f74dc1e9..b2e53b38 100644 --- a/configure.ac +++ b/configure.ac @@ -94,7 +94,9 @@ if test x_$withval != x_no; then ac_save_LIBS="$LIBS" dnl otherwise AC_PYTHON_DEVEL thrashes $LIBS AC_PYTHON_DEVEL if test ! -z "$PYTHON_VERSION"; then - if test `$PYTHON -c "print '$PYTHON_VERSION' >= '2.4.0'"` = "False"; then + if test `$PYTHON -c "import sys; \ + ver = sys.version.split()[[0]]; \ + print(ver >= '2.4.0')"` = "False"; then AC_ERROR([Python version >= 2.4.0 is required]) fi # Have Python @@ -102,6 +104,13 @@ if test x_$withval != x_no; then ldns_have_python=yes fi + # pass additional Python 3 option to swig + if test `$PYTHON -c "import sys; \ + ver = sys.version.split()[[0]]; \ + print(ver >= '3.0.0')"` = "True"; then + AC_SUBST(SWIGPY3, [-py3]) + fi + # check for swig if test x_$ldns_have_python != x_no; then sinclude(ac_pkg_swig.m4) diff --git a/contrib/python/Makefile b/contrib/python/Makefile index 7b8b3914..c4397050 100644 --- a/contrib/python/Makefile +++ b/contrib/python/Makefile @@ -61,7 +61,7 @@ doc: ../../.libs/ldns.so.1 _ldns.so #for development only swig: ldns.i - swig -python -o ldns_wrapper.c -I../.. ldns.i - gcc -c ldns_wrapper.c -O9 -fPIC -I../.. -I../../ldns -I/usr/local/include/python2.6 -I. -o ldns_wrapper.o + swig -python -py3 -o ldns_wrapper.c -I../.. ldns.i + gcc -c ldns_wrapper.c -O9 -fPIC -I../.. -I../../ldns -I/usr/include/python3.1 -I. -o ldns_wrapper.o ld -shared ldns_wrapper.o -L../../.libs -lldns -o _ldns.so diff --git a/contrib/python/docs/source/examples/example2.py b/contrib/python/docs/source/examples/example2.py index bb2acfdf..7dabb917 100755 --- a/contrib/python/docs/source/examples/example2.py +++ b/contrib/python/docs/source/examples/example2.py @@ -9,7 +9,7 @@ debug = True argc = len(sys.argv) name = "www.nic.cz" if argc < 2: - print "Usage:", sys.argv[0], "domain [resolver_addr]" + print("Usage:", sys.argv[0], "domain [resolver_addr]") sys.exit(1) else: name = sys.argv[1] @@ -33,13 +33,13 @@ if pkt and pkt.answer(): # Debug if debug: - print "NS returned:", pkt.get_rcode(), "(AA: %d AD: %d)" % ( pkt.ad(), pkt.ad() ) + print("NS returned:", pkt.get_rcode(), "(AA: %d AD: %d)" % ( pkt.ad(), pkt.ad() )) # SERVFAIL indicated bogus name if pkt.get_rcode() is ldns.LDNS_RCODE_SERVFAIL: - print name, "is bogus" + print(name, "is bogus") # Check AD (Authenticated) bit if pkt.get_rcode() is ldns.LDNS_RCODE_NOERROR: - if pkt.ad(): print name, "is secure" - else: print name, "is insecure" + if pkt.ad(): print(name, "is secure") + else: print(name, "is insecure") diff --git a/contrib/python/ldns.i b/contrib/python/ldns.i index c32de5ab..ad0f2c87 100644 --- a/contrib/python/ldns.i +++ b/contrib/python/ldns.i @@ -101,6 +101,17 @@ uint32_t ldns_read_timeval_usec(struct timeval* t) { %include "ldns_packet.i" %include "ldns_resolver.i" %include "ldns_rr.i" + +%inline %{ +int Python_str_Check(PyObject *o) { +#if PY_VERSION_HEX>=0x03000000 + return PyUnicode_Check(o); +#else + return PyString_Check(o); +#endif +} +%} + %include "ldns_rdf.i" %include "ldns_zone.i" %include "ldns_key.i" diff --git a/contrib/python/ldns_rdf.i b/contrib/python/ldns_rdf.i index aada36b3..282c86c9 100644 --- a/contrib/python/ldns_rdf.i +++ b/contrib/python/ldns_rdf.i @@ -32,8 +32,8 @@ //automatic conversion of const ldns_rdf* parameter from string %typemap(in,noblock=1) const ldns_rdf * (void* argp, $1_ltype tmp = 0, int res) { - if (PyString_Check($input)) { - tmp = ldns_dname_new_frm_str(PyString_AsString($input)); + if (Python_str_Check($input)) { + tmp = ldns_dname_new_frm_str(SWIG_Python_str_AsChar($input)); if (tmp == NULL) { %argument_fail(SWIG_TypeError, "char *", $symname, $argnum); } diff --git a/contrib/python/ldns_resolver.i b/contrib/python/ldns_resolver.i index 0ddcb4e9..b9640674 100644 --- a/contrib/python/ldns_resolver.i +++ b/contrib/python/ldns_resolver.i @@ -217,7 +217,10 @@ This simple example instances a resolver in order to resolve www.nic.cz record o print rr """ - return _ldns.ldns_axfr_start(self, domain, aclass) + rdf = domain + if isinstance(domain, str): + rdf = _ldns.ldns_dname_new_frm_str(domain) + return _ldns.ldns_axfr_start(self, rdf, aclass) #parameters: ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class c #retvals: int