]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
* Fix python api to support python2 and python3 (thanks Karel Slany).
authorWouter Wijngaards <wouter@NLnetLabs.nl>
Fri, 19 Aug 2011 13:26:21 +0000 (13:26 +0000)
committerWouter Wijngaards <wouter@NLnetLabs.nl>
Fri, 19 Aug 2011 13:26:21 +0000 (13:26 +0000)
Changelog
Makefile.in
acx_python.m4
configure.ac
contrib/python/Makefile
contrib/python/docs/source/examples/example2.py
contrib/python/ldns.i
contrib/python/ldns_rdf.i
contrib/python/ldns_resolver.i

index 1de9dd56c5dbe153965c310fef64934146e93be8..b6b3e76c695050d61a2cf425c6a01aaf111a6e43 100644 (file)
--- 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.
index 596bef18579fe29688422299f4a3deaed6b1b4bf..42a69b2184297441127a4e1f071a514963697c36 100644 (file)
@@ -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 $@
index 6fa925af168405d3b456db11cdc69c9255db9e69..7eb2cdf8606c82d3697847602db0423eae30c0b7 100644 (file)
@@ -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)
index f74dc1e968cb465a8331afc7073704e24e3de76d..b2e53b383b28fc6bce75b2fa4898ab4508094614 100644 (file)
@@ -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)
index 7b8b3914741e29b8fbd9c527b42bc52ca000e1f3..c43970509a4ef7a41f6910c2bf69eae77bfe7eec 100644 (file)
@@ -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 
 
index bb2acfdf762ad3ee369080e9c075d9e470df7ebe..7dabb917902579ed27c3fb596e6c352a8082682f 100755 (executable)
@@ -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")
index c32de5ab2cf7923f0eb427dc3c754973ff1f5449..ad0f2c87a50856789fe744419bba3546e6c75d36 100644 (file)
@@ -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"
index aada36b32a52ed41a1455f077ee9da5223af7140..282c86c9af156f406bc1257bda0c89e87b05e25c 100644 (file)
@@ -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);
       }
index 0ddcb4e9d84fe44009343672dd907b896e67b657..b9640674b5a03863126a6958ad36b8d1ac2fde30 100644 (file)
@@ -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