Skip to content

Commit a296ddd

Browse files
committed
Cleaned up some code to support Python3, Python3-ified examples
1 parent 374ba52 commit a296ddd

14 files changed

+102
-57
lines changed

context.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,8 +1271,13 @@ context_general(getdns_ContextObject *self, PyObject *args, PyObject *keywds)
12711271
}
12721272
if (userarg)
12731273
strncpy(blob->userarg, userarg, BUFSIZ-1);
1274+
#if PY_MAJOR_VERSION >= 3
1275+
if (PyUnicode_Check(callback)) {
1276+
if ((callback_func = get_callback("__main__", PyBytes_AsString(PyUnicode_AsEncodedString(PyObject_Str(callback), "ascii", NULL)))) == (PyObject *)NULL) {
1277+
#else
12741278
if (PyString_Check(callback)) {
12751279
if ((callback_func = get_callback("__main__", PyString_AsString(callback))) == (PyObject *)NULL) {
1280+
#endif
12761281
PyObject *err_type, *err_value, *err_traceback;
12771282
PyErr_Fetch(&err_type, &err_value, &err_traceback);
12781283
PyErr_Restore(err_type, err_value, err_traceback);
@@ -1369,8 +1374,13 @@ context_address(getdns_ContextObject *self, PyObject *args, PyObject *keywds)
13691374
} else {
13701375
blob->userarg[0] = 0;
13711376
}
1372-
if (PyString_Check(callback)) {
1377+
#if PY_MAJOR_VERSION >= 3
1378+
if (PyUnicode_Check(callback)) {
1379+
if ((callback_func = get_callback("__main__", PyBytes_AsString(PyUnicode_AsEncodedString(PyObject_Str(callback), "ascii", NULL)))) == (PyObject *)NULL) {
1380+
#else
1381+
if (PyString_Check(callback)) {
13731382
if ((callback_func = get_callback("__main__", PyString_AsString(callback))) == (PyObject *)NULL) {
1383+
#endif
13741384
PyObject *err_type, *err_value, *err_traceback;
13751385
PyErr_Fetch(&err_type, &err_value, &err_traceback);
13761386
PyErr_Restore(err_type, err_value, err_traceback);
@@ -1472,8 +1482,13 @@ context_hostname(getdns_ContextObject *self, PyObject *args, PyObject *keywds)
14721482
} else {
14731483
blob->userarg[0] = 0;
14741484
}
1485+
#if PY_MAJOR_VERSION >= 3
1486+
if (PyUnicode_Check(callback)) {
1487+
if ((callback_func = get_callback("__main__", PyBytes_AsString(PyUnicode_AsEncodedString(PyObject_Str(callback), "ascii", NULL)))) == (PyObject *)NULL) {
1488+
#else
14751489
if (PyString_Check(callback)) {
14761490
if ((callback_func = get_callback("__main__", PyString_AsString(callback))) == (PyObject *)NULL) {
1491+
#endif
14771492
PyObject *err_type, *err_value, *err_traceback;
14781493
PyErr_Fetch(&err_type, &err_value, &err_traceback);
14791494
PyErr_Restore(err_type, err_value, err_traceback);
@@ -1568,8 +1583,13 @@ context_service(getdns_ContextObject *self, PyObject *args, PyObject *keywds)
15681583
} else {
15691584
blob->userarg[0] = 0;
15701585
}
1586+
#if PY_MAJOR_VERSION >= 3
1587+
if (PyUnicode_Check(callback)) {
1588+
if ((callback_func = get_callback("__main__", PyBytes_AsString(PyUnicode_AsEncodedString(PyObject_Str(callback), "ascii", NULL)))) == (PyObject *)NULL) {
1589+
#else
15711590
if (PyString_Check(callback)) {
15721591
if ((callback_func = get_callback("__main__", PyString_AsString(callback))) == (PyObject *)NULL) {
1592+
#endif
15731593
PyObject *err_type, *err_value, *err_traceback;
15741594
PyErr_Fetch(&err_type, &err_value, &err_traceback);
15751595
PyErr_Restore(err_type, err_value, err_traceback);

examples/async-get-ip.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,29 @@ def cbk(type, result, userarg, tid):
3030
for addr in result.just_address_answers:
3131
addr_type = addr['address_type']
3232
addr_data = addr['address_data']
33-
print '{0}: {1} {2}'.format(userarg, addr_type, addr_data)
33+
print("{0}: {1} {2}".format(userarg, addr_type, addr_data))
3434
elif status == getdns.RESPSTATUS_NO_SECURE_ANSWERS:
35-
print "%s: No DNSSEC secured responses found" % hostname
35+
print("{0}: No DNSSEC secured responses found".format(hostname))
3636
else:
37-
print "%s: getdns.address() returned error: %d" % (hostname, status)
37+
print("{0}: getdns.address() returned error: {1}".format(hostname, status))
3838
elif type == getdns.CALLBACK_CANCEL:
39-
print 'Callback cancelled'
39+
print('Callback cancelled')
4040
elif type == getdns.CALLBACK_TIMEOUT:
41-
print 'Query timed out'
41+
print('Query timed out')
4242
else:
43-
print 'Unknown error'
43+
print('Unknown error')
4444

4545

4646
def usage():
47-
print """\
47+
print("""\
4848
Usage: get-ip.py [-s] [-4|-6] <domain1> <domain2> ...
4949
5050
-s: only return DNSSEC secured answers
5151
-4: only return IPv4 address answers
5252
-6: only return IPv6 address answers
5353
5454
-4 and -6 are mutually exclusive. If both are specified, IPv6 wins.
55-
"""
55+
""")
5656
sys.exit(1)
5757

5858
try:
@@ -70,8 +70,8 @@ def usage():
7070
for hostname in args:
7171
try:
7272
tids.append(ctx.address(name=hostname, extensions=extensions, callback='cbk', userarg=hostname))
73-
print 'submitted query for {0}'.format(hostname)
74-
except getdns.error, e:
73+
print ('submitted query for {0}'.format(hostname))
74+
except getdns.error as e:
7575
print(str(e))
7676
break
7777
ctx.run()

examples/get-general.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def get_rrtype(qtype):
1212
try:
1313
rrtype = eval("getdns.RRTYPE_%s" % qtype.upper())
1414
except AttributeError:
15-
print "Unknown DNS record type: %s" % qtype
15+
print("Unknown DNS record type: {0}".format(qtype))
1616
sys.exit(1)
1717
else:
1818
return rrtype
@@ -32,7 +32,7 @@ def print_answer(r):
3232
try:
3333
results = ctx.general(name=qname, request_type=rrtype,
3434
extensions=extensions)
35-
except getdns.error, e:
35+
except getdns.error as e:
3636
print(str(e))
3737
sys.exit(1)
3838

@@ -45,9 +45,9 @@ def print_answer(r):
4545
if answer['type'] != getdns.RRTYPE_RRSIG:
4646
pprint.pprint(answer)
4747
elif status == getdns.RESPSTATUS_NO_NAME:
48-
print "%s, %s: no such name" % (qname, qtype)
48+
print("{0}, {1}: no such name".format(qname, qtype))
4949
elif status == getdns.RESPSTATUS_ALL_TIMEOUT:
50-
print "%s, %s: query timed out" % (qname, qtype)
50+
print("{0}, {1}: query timed out".format(qname, qtype))
5151
else:
52-
print "%s, %s: unknown return code: %d" % results.status
52+
print("{0}, {1}: unknown return code: {2}".format(qname, qtype, results.status))
5353

examples/get-ip.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
import getdns, sys, getopt
1919

2020
def usage():
21-
print """\
21+
print("""\
2222
Usage: get-ip.py [-s] [-4|-6] <domain1> <domain2> ...
2323
2424
-s: only return DNSSEC secured answers
2525
-4: only return IPv4 address answers
2626
-6: only return IPv6 address answers
2727
2828
-4 and -6 are mutually exclusive. If both are specified, IPv6 wins.
29-
"""
29+
""")
3030
sys.exit(1)
3131

3232
try:
@@ -53,7 +53,7 @@ def usage():
5353
for hostname in args:
5454
try:
5555
results = ctx.address(name=hostname, extensions=extensions)
56-
except getdns.error, e:
56+
except getdns.error as e:
5757
print(str(e))
5858
break
5959
status = results.status
@@ -62,9 +62,9 @@ def usage():
6262
addr_type = addr['address_type']
6363
addr_data = addr['address_data']
6464
if (desired_addr_type == None) or (addr_type == desired_addr_type):
65-
print "%s: %s %s" % (hostname, addr_type, addr_data)
65+
print("{0}: {1} {2}".format(hostname, addr_type, addr_data))
6666
elif status == getdns.RESPSTATUS_NO_SECURE_ANSWERS:
67-
print "%s: No DNSSEC secured responses found" % hostname
67+
print("{0}: No DNSSEC secured responses found".format(hostname))
6868
else:
69-
print "%s: getdns.address() returned error: %d" % (hostname, status)
69+
print("{0}: getdns.address() returned error: {1}".format(hostname, status))
7070

examples/get-mx-ip.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ def get_ip(ctx, qname):
1515
iplist = []
1616
try:
1717
results = ctx.address(name=qname, extensions=extensions)
18-
except getdns.error, e:
18+
except getdns.error as e:
1919
print(str(e))
2020
sys.exit(1)
2121

2222
if results.status == getdns.RESPSTATUS_GOOD:
2323
for addr in results.just_address_answers:
2424
iplist.append(addr['address_data'])
2525
else:
26-
print "getdns.address() returned an error: %d" % results.status
26+
print("getdns.address() returned an error: {0}".format(results.status))
2727
return iplist
2828

2929

@@ -34,7 +34,7 @@ def get_ip(ctx, qname):
3434
ctx = getdns.Context()
3535
try:
3636
results = ctx.general(name=qname, request_type=getdns.RRTYPE_MX)
37-
except getdns.error, e:
37+
except getdns.error as e:
3838
print(str(e))
3939
sys.exit(1)
4040

@@ -51,11 +51,11 @@ def get_ip(ctx, qname):
5151
hostlist.append( (answer['rdata']['preference'], \
5252
answer['rdata']['exchange'], ip) )
5353
elif status == getdns.RESPSTATUS_NO_NAME:
54-
print "%s, %s: no such name" % (qname, qtype)
54+
print("{0}, {1}: no such name".format(qname, qtype))
5555
elif status == getdns.RESPSTATUS_ALL_TIMEOUT:
56-
print "%s, %s: query timed out" % (qname, qtype)
56+
print("{0}, {1}: query timed out".format(qname, qtype))
5757
else:
58-
print "%s, %s: unknown return code: %d" % results["status"]
58+
print("{0}, {1}: unknown return code: {2}".format(qname, qtype, results["status"]))
5959

6060
for (pref, mx, addr) in sorted(hostlist):
61-
print pref, mx, addr
61+
print(pref, mx, addr)

examples/get-ns-ip.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212

1313

1414
def usage():
15-
print """Usage: get-ns-ip.py <zone>
15+
print("""Usage: get-ns-ip.py <zone>
1616
1717
where <zone> is a DNS zone (domain).
18-
"""
18+
""")
1919
sys.exit(1)
2020

2121

2222
def get_ip(ctx, qname):
2323
iplist = []
2424
try:
2525
results = ctx.address(name=qname, extensions=extensions)
26-
except getdns.error, e:
26+
except getdns.error as e:
2727
print(str(e))
2828
sys.exit(1)
2929

3030
if results.status == getdns.RESPSTATUS_GOOD:
3131
for addr in results.just_address_answers:
3232
iplist.append(addr['address_data'])
3333
else:
34-
print "getdns.address() returned an error: %d" % results['status']
34+
print("getdns.address() returned an error: {0}".format(results['status']))
3535
return iplist
3636

3737

@@ -45,7 +45,7 @@ def get_ip(ctx, qname):
4545
ctx = getdns.Context()
4646
try:
4747
results = ctx.general(name=qname, request_type=getdns.RRTYPE_NS)
48-
except getdns.error, e:
48+
except getdns.error as e:
4949
print(str(e))
5050
sys.exit(1)
5151
status = results.status
@@ -60,12 +60,12 @@ def get_ip(ctx, qname):
6060
for ip in iplist:
6161
hostlist.append( (answer['rdata']['nsdname'], ip) )
6262
elif status == getdns.RESPSTATUS_NO_NAME:
63-
print "%s: no such DNS zone" % qname
63+
print("{0}: no such DNS zone".format(qname))
6464
elif status == getdns.RESPSTATUS_ALL_TIMEOUT:
65-
print "%s, NS: query timed out" % qname
65+
print("{0}, NS: query timed out".format(qname))
6666
else:
67-
print "%s, %s: unknown return code: %d" % results["status"]
67+
print("{0}, NS: unknown return code: {1}".format(qname, results["status"]))
6868

6969
# Print out each NS server name and IP address
7070
for (nsdname, addr) in sorted(hostlist):
71-
print nsdname, addr
71+
print(nsdname, addr)

examples/get-srv.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
ctx = getdns.Context()
1414
try:
1515
results = ctx.service(name=srvname)
16-
except getdns.error, e:
16+
except getdns.error as e:
1717
print(str(e))
1818
sys.exit(1)
1919

@@ -25,7 +25,6 @@
2525
if rrtype == getdns.RRTYPE_SRV:
2626
rdata = a["rdata"]
2727
prio, weight, port, target = rdata['priority'], rdata['weight'], rdata['port'], rdata['target']
28-
print "SRV %s --> %d %d %d %s" % \
29-
(rrname, prio, weight, port, target)
28+
print("SRV {0} --> {1} {2} {3} {4}".format(rrname, prio, weight, port, target))
3029
else:
31-
print "getdns.service() returned an error: %d" % results["status"]
30+
print("getdns.service() returned an error: {0}".format(results.status))

examples/print-context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
ctx = getdns.Context()
77
try:
88
pprint.pprint(ctx.get_api_information())
9-
except getdns.error, e:
9+
except getdns.error as e:
1010
print(str(e))
1111
sys.exit(1)

examples/print-version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
ctx = getdns.Context()
66
try:
7-
print ctx.get_api_information()['version_string']
8-
except getdns.error, e:
7+
print(ctx.get_api_information()['version_string'])
8+
except getdns.error as e:
99
print(str(e))
1010
sys.exit(1)

examples/query-gdns.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323

2424
try:
2525
results = ctx.address(name=hostname, extensions=extensions)
26-
except getdns.error, e:
26+
except getdns.error as e:
2727
print(str(e))
2828
sys.exit(1)
2929

3030
if results.status == getdns.RESPSTATUS_GOOD:
3131
for addr in results.just_address_answers:
32-
print addr["address_data"]
32+
print(addr["address_data"])
3333
else:
34-
print "getdns.address() returned an error: %d" % results.status
34+
print("getdns.address() returned an error: %d".format(results.status))
3535

examples/query-stubmode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
try:
1414
results = ctx.address(name=hostname, extensions=extensions)
15-
except getdns.error, e:
15+
except getdns.error as e:
1616
print(str(e))
1717
sys.exit(1)
1818

1919
if results.status == getdns.RESPSTATUS_GOOD:
2020
for addr in results.just_address_answers:
21-
print addr["address_data"]
21+
print(addr["address_data"])
2222
else:
23-
print "getdns.address() returned an error: %d" % results["status"]
23+
print("getdns.address() returned an error: {0}".format(results["status"]))
2424

examples/simple.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
hostname = sys.argv[1]
1414

1515
ctx = getdns.Context()
16-
extensions = { "return_both_v4_and_v6" : getdns.EXTENSION_TRUE }
16+
extensions = { 'return_both_v4_and_v6' : getdns.EXTENSION_TRUE }
1717

1818
try:
1919
results = ctx.address(name=hostname, extensions=extensions)
20-
except getdns.error, e:
20+
except:
21+
e = sys.exc.info()[0]
2122
print(str(e))
2223
sys.exit(1)
2324

2425
status = results.status
2526

2627
if status == getdns.RESPSTATUS_GOOD:
2728
for addr in results.just_address_answers:
28-
print addr['address_data']
29+
print (addr['address_data'])
2930
else:
30-
print "%s: getdns.address() returned error: %d" % (hostname, status)
31+
print("{0}: getdns.address() returned error: {1}".format((hostname, status)))

0 commit comments

Comments
 (0)