From 4cab831abcd3ea1042fb8992acac54428f448a95 Mon Sep 17 00:00:00 2001 From: Wouter Wijngaards Date: Mon, 22 Sep 2014 11:39:00 +0000 Subject: [PATCH] - Fix swig and python examples for Python 3.x. git-svn-id: file:///svn/unbound/trunk@3227 be551aaa-1e26-0410-a405-d3ace91eadb9 --- doc/Changelog | 3 +++ libunbound/python/examples/async-lookup.py | 6 ++--- libunbound/python/examples/dns-lookup.py | 4 ++-- libunbound/python/examples/dnssec-valid.py | 8 +++---- libunbound/python/examples/dnssec_test.py | 14 ++++++------ libunbound/python/examples/example8-1.py | 18 +++++++-------- libunbound/python/examples/idn-lookup.py | 16 ++++++------- libunbound/python/examples/mx-lookup.py | 12 +++++----- libunbound/python/examples/ns-lookup.py | 6 ++--- libunbound/python/examples/reverse-lookup.py | 2 +- libunbound/python/libunbound.i | 24 ++++++++++++++------ 11 files changed, 63 insertions(+), 50 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index 2daebcee0..3944d29e7 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,6 @@ +22 September 2014: Wouter + - Fix swig and python examples for Python 3.x. + 19 September 2014: Wouter - improve python configuration detection to build on Fedora 22. diff --git a/libunbound/python/examples/async-lookup.py b/libunbound/python/examples/async-lookup.py index 52a2d3c75..cbb8ea02d 100644 --- a/libunbound/python/examples/async-lookup.py +++ b/libunbound/python/examples/async-lookup.py @@ -39,9 +39,9 @@ ctx = unbound.ub_ctx() ctx.resolvconf("/etc/resolv.conf") def call_back(my_data,status,result): - print "Call_back:", my_data + print("Call_back:", my_data) if status == 0 and result.havedata: - print "Result:", result.data.address_list + print("Result:", result.data.address_list) my_data['done_flag'] = True @@ -53,4 +53,4 @@ while (status == 0) and (not my_data['done_flag']): time.sleep(0.1) if (status != 0): - print "Resolve error:", unbound.ub_strerror(status) + print("Resolve error:", unbound.ub_strerror(status)) diff --git a/libunbound/python/examples/dns-lookup.py b/libunbound/python/examples/dns-lookup.py index 2821ed3ba..b3f4008fd 100644 --- a/libunbound/python/examples/dns-lookup.py +++ b/libunbound/python/examples/dns-lookup.py @@ -39,6 +39,6 @@ ctx.resolvconf("/etc/resolv.conf") status, result = ctx.resolve("www.nic.cz", unbound.RR_TYPE_A, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:", result.data.address_list + print("Result:", result.data.address_list) elif status != 0: - print "Error:", unbound.ub_strerror(status) + print("Error:", unbound.ub_strerror(status)) diff --git a/libunbound/python/examples/dnssec-valid.py b/libunbound/python/examples/dnssec-valid.py index 3e05ddd7a..5c3cad9e9 100644 --- a/libunbound/python/examples/dnssec-valid.py +++ b/libunbound/python/examples/dnssec-valid.py @@ -48,12 +48,12 @@ if os.path.isfile("keys"): status, result = ctx.resolve("www.nic.cz", RR_TYPE_A, RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:", result.data.address_list + print("Result:", result.data.address_list) if result.secure: - print "Result is secure" + print("Result is secure") elif result.bogus: - print "Result is bogus" + print("Result is bogus") else: - print "Result is insecure" + print("Result is insecure") diff --git a/libunbound/python/examples/dnssec_test.py b/libunbound/python/examples/dnssec_test.py index 138e19b51..0d62b9ff2 100644 --- a/libunbound/python/examples/dnssec_test.py +++ b/libunbound/python/examples/dnssec_test.py @@ -3,27 +3,27 @@ from unbound import ub_ctx, RR_TYPE_A, RR_TYPE_RRSIG, RR_TYPE_NSEC, RR_TYPE_NSEC import ldns def dnssecParse(domain, rrType=RR_TYPE_A): - print "Resolving domain", domain + print("Resolving domain", domain) s, r = resolver.resolve(domain) - print "status: %s, secure: %s, rcode: %s, havedata: %s, answer_len; %s" % (s, r.secure, r.rcode_str, r.havedata, r.answer_len) + print("status: %s, secure: %s, rcode: %s, havedata: %s, answer_len; %s" % (s, r.secure, r.rcode_str, r.havedata, r.answer_len)) s, pkt = ldns.ldns_wire2pkt(r.packet) if s != 0: raise RuntimeError("Error parsing DNS packet") rrsigs = pkt.rr_list_by_type(RR_TYPE_RRSIG, ldns.LDNS_SECTION_ANSWER) - print "RRSIGs from answer:", rrsigs + print("RRSIGs from answer:", rrsigs) rrsigs = pkt.rr_list_by_type(RR_TYPE_RRSIG, ldns.LDNS_SECTION_AUTHORITY) - print "RRSIGs from authority:", rrsigs + print("RRSIGs from authority:", rrsigs) nsecs = pkt.rr_list_by_type(RR_TYPE_NSEC, ldns.LDNS_SECTION_AUTHORITY) - print "NSECs:", nsecs + print("NSECs:", nsecs) nsec3s = pkt.rr_list_by_type(RR_TYPE_NSEC3, ldns.LDNS_SECTION_AUTHORITY) - print "NSEC3s:", nsec3s + print("NSEC3s:", nsec3s) - print "---" + print("---") resolver = ub_ctx() diff --git a/libunbound/python/examples/example8-1.py b/libunbound/python/examples/example8-1.py index 6816da0c2..ca868e510 100644 --- a/libunbound/python/examples/example8-1.py +++ b/libunbound/python/examples/example8-1.py @@ -40,22 +40,22 @@ ctx.resolvconf("/etc/resolv.conf") status, result = ctx.resolve("nic.cz", unbound.RR_TYPE_MX, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:" - print " raw data:", result.data + print("Result:") + print(" raw data:", result.data) for k in result.data.mx_list: - print " priority:%d address:%s" % k + print(" priority:%d address:%s" % k) status, result = ctx.resolve("nic.cz", unbound.RR_TYPE_A, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:" - print " raw data:", result.data + print("Result:") + print(" raw data:", result.data) for k in result.data.address_list: - print " address:%s" % k + print(" address:%s" % k) status, result = ctx.resolve("nic.cz", unbound.RR_TYPE_NS, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:" - print " raw data:", result.data + print("Result:") + print(" raw data:", result.data) for k in result.data.domain_list: - print " host: %s" % k + print(" host: %s" % k) diff --git a/libunbound/python/examples/idn-lookup.py b/libunbound/python/examples/idn-lookup.py index 7cfdc9e94..2170637d3 100644 --- a/libunbound/python/examples/idn-lookup.py +++ b/libunbound/python/examples/idn-lookup.py @@ -43,20 +43,20 @@ ctx.resolvconf("/etc/resolv.conf") #The unicode IDN string is automatically converted (if necessary) status, result = ctx.resolve(u"www.háčkyčárky.cz", unbound.RR_TYPE_A, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:" - print " raw data:", result.data + print("Result:") + print(" raw data:", result.data) for k in result.data.address_list: - print " address:%s" % k + print(" address:%s" % k) status, result = ctx.resolve(u"háčkyčárky.cz", unbound.RR_TYPE_MX, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:" - print " raw data:", result.data + print("Result:") + print(" raw data:", result.data) for k in result.data.mx_list_idn: - print " priority:%d address:%s" % k + print(" priority:%d address:%s" % k) status, result = ctx.resolve(unbound.reverse('217.31.204.66')+'.in-addr.arpa', unbound.RR_TYPE_PTR, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result.data:", result.data + print("Result.data:", result.data) for k in result.data.domain_list_idn: - print " dname:%s" % k + print(" dname:%s" % k) diff --git a/libunbound/python/examples/mx-lookup.py b/libunbound/python/examples/mx-lookup.py index cdcd1b166..f83f690f8 100644 --- a/libunbound/python/examples/mx-lookup.py +++ b/libunbound/python/examples/mx-lookup.py @@ -40,14 +40,14 @@ ctx.resolvconf("/etc/resolv.conf") status, result = ctx.resolve("nic.cz", unbound.RR_TYPE_MX, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:" - print " raw data:", result.data + print("Result:") + print(" raw data:", result.data) for k in result.data.mx_list: - print " priority:%d address:%s" % k + print(" priority:%d address:%s" % k) status, result = ctx.resolve("nic.cz", unbound.RR_TYPE_A, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:" - print " raw data:", result.data + print("Result:") + print(" raw data:", result.data) for k in result.data.address_list: - print " address:%s" % k + print(" address:%s" % k) diff --git a/libunbound/python/examples/ns-lookup.py b/libunbound/python/examples/ns-lookup.py index f9eafb28a..bcd51de6d 100644 --- a/libunbound/python/examples/ns-lookup.py +++ b/libunbound/python/examples/ns-lookup.py @@ -40,8 +40,8 @@ ctx.resolvconf("/etc/resolv.conf") status, result = ctx.resolve("vutbr.cz", unbound.RR_TYPE_NS, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result:" - print " raw data:", result.data + print("Result:") + print(" raw data:", result.data) for k in result.data.domain_list: - print " host: %s" % k + print(" host: %s" % k) diff --git a/libunbound/python/examples/reverse-lookup.py b/libunbound/python/examples/reverse-lookup.py index 4d3e0bb36..7e06844ec 100644 --- a/libunbound/python/examples/reverse-lookup.py +++ b/libunbound/python/examples/reverse-lookup.py @@ -39,5 +39,5 @@ ctx.resolvconf("/etc/resolv.conf") status, result = ctx.resolve(unbound.reverse("74.125.43.147") + ".in-addr.arpa.", unbound.RR_TYPE_PTR, unbound.RR_CLASS_IN) if status == 0 and result.havedata: - print "Result.data:", result.data, result.data.domain_list + print("Result.data:", result.data, result.data.domain_list) diff --git a/libunbound/python/libunbound.i b/libunbound/python/libunbound.i index 78a0ed6e4..313c74862 100644 --- a/libunbound/python/libunbound.i +++ b/libunbound/python/libunbound.i @@ -44,6 +44,15 @@ %pythoncode %{ import encodings.idna + + # Ensure compatibility with older python versions + if 'bytes' not in vars(): + bytes = str + + def ord(s): + if isinstance(s, int): + return s + return __builtins__.ord(s) %} //%include "doc.i" @@ -559,10 +568,10 @@ Result: ['74.125.43.147', '74.125.43.99', '74.125.43.103', '74.125.43.104'] :returns: * (int) 0 if OK, else error. * (:class:`ub_result`) the result data is returned in a newly allocated result structure. May be None on return, return value is set to an error in that case (out of memory). """ - if isinstance(name, unicode): #probably IDN - return _unbound.ub_resolve(self,idn2dname(name),rrtype,rrclass) - else: + if isinstance(name, bytes): #probably IDN return _unbound.ub_resolve(self,name,rrtype,rrclass) + else: + return _unbound.ub_resolve(self,idn2dname(name),rrtype,rrclass) #parameters: struct ub_ctx *,char *,int,int, #retvals: int,struct ub_result ** @@ -597,10 +606,10 @@ Result: ['74.125.43.147', '74.125.43.99', '74.125.43.103', '74.125.43.104'] * `result` - the result structure. The result may be None, in that case err is set. """ - if isinstance(name, unicode): #probably IDN - return _unbound._ub_resolve_async(self,idn2dname(name),rrtype,rrclass,mydata,callback) - else: + if isinstance(name, bytes): #probably IDN return _unbound._ub_resolve_async(self,name,rrtype,rrclass,mydata,callback) + else: + return _unbound._ub_resolve_async(self,idn2dname(name),rrtype,rrclass,mydata,callback) #parameters: struct ub_ctx *,char *,int,int,void *,ub_callback_t, #retvals: int, int @@ -689,7 +698,8 @@ Result: ['74.125.43.147', '74.125.43.99', '74.125.43.103', '74.125.43.104'] idx = ofs while (idx < slen): complen = ord(s[idx]) - res.append(s[idx+1:idx+1+complen]) + # In python 3.x `str()` converts the string to unicode which is the expected text string type + res.append(str(s[idx+1:idx+1+complen])) idx += complen + 1 return res -- 2.47.2