[libxml2] update all tests for Python3 and Python2



commit 2cb6bf8eb019cc88578093abee3e37a78e7b3020
Author: Daniel Veillard <veillard redhat com>
Date:   Sat Mar 30 21:38:20 2013 +0800

    update all tests for Python3 and Python2

 python/tests/attribs.py        |    6 +-
 python/tests/build.py          |   18 ++--
 python/tests/compareNodes.py   |   18 ++--
 python/tests/ctxterror.py      |   12 ++--
 python/tests/cutnpaste.py      |    6 +-
 python/tests/dtdvalid.py       |    6 +-
 python/tests/error.py          |   12 ++--
 python/tests/inbuf.py          |   13 ++-
 python/tests/indexes.py        |   20 ++--
 python/tests/input_callback.py |   25 +++--
 python/tests/nsdel.py          |    6 +-
 python/tests/outbuf.py         |   49 ++++++-----
 python/tests/push.py           |    8 +-
 python/tests/pushSAX.py        |    8 +-
 python/tests/pushSAXhtml.py    |    8 +-
 python/tests/reader.py         |  185 ++++++++++++++++++++-------------------
 python/tests/reader2.py        |   63 ++++++++------
 python/tests/reader3.py        |   63 +++++++------
 python/tests/reader4.py        |   17 +++--
 python/tests/reader5.py        |   15 ++--
 python/tests/reader6.py        |   27 ++++---
 python/tests/reader7.py        |   18 ++--
 python/tests/reader8.py        |    8 +-
 python/tests/readererr.py      |   21 +++--
 python/tests/readernext.py     |   39 +++++----
 python/tests/regexp.py         |   14 ++--
 python/tests/relaxng.py        |    6 +-
 python/tests/resolver.py       |   17 +++--
 python/tests/schema.py         |    6 +-
 python/tests/serialize.py      |   36 ++++----
 python/tests/sync.py           |   32 ++++----
 python/tests/thread2.py        |   21 +++--
 python/tests/tst.py            |   10 +-
 python/tests/tstLastError.py   |   20 ++--
 python/tests/tstURI.py         |   18 ++--
 python/tests/tstmem.py         |    6 +-
 python/tests/tstxpath.py       |   16 ++--
 python/tests/validDTD.py       |   10 +-
 python/tests/validRNG.py       |   10 +-
 python/tests/validSchemas.py   |   10 +-
 python/tests/validate.py       |   20 ++--
 python/tests/walker.py         |   24 +++---
 python/tests/xpath.py          |   14 ++--
 python/tests/xpathext.py       |   12 ++--
 python/tests/xpathleak.py      |   14 ++--
 python/tests/xpathns.py        |    8 +-
 python/tests/xpathret.py       |   14 ++--
 47 files changed, 535 insertions(+), 474 deletions(-)
---
diff --git a/python/tests/attribs.py b/python/tests/attribs.py
index 9331a27..99132c2 100755
--- a/python/tests/attribs.py
+++ b/python/tests/attribs.py
@@ -20,7 +20,7 @@ doc = libxml2.parseDoc(
 elem = doc.getRootElement()
 attr = elem.hasNsProp('attr', 'http://abc.org')
 if attr == None or attr.serialize()[:-1] != """<!ATTLIST test abc:attr CDATA #FIXED "def">""":
-    print "Failed to find defaulted attribute abc:attr"
+    print("Failed to find defaulted attribute abc:attr")
     sys.exit(1)
 
 doc.freeDoc()
@@ -28,7 +28,7 @@ doc.freeDoc()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/build.py b/python/tests/build.py
index bfc776c..b2d3f78 100755
--- a/python/tests/build.py
+++ b/python/tests/build.py
@@ -23,29 +23,29 @@ doc = libxml2.parseFile("tmp.xml")
 comment = doc.children
 if comment.type != "comment" or \
    comment.content != "This is a generated document":
-   print "error rereading comment"
+   print("error rereading comment")
    sys.exit(1)
 pi = comment.next
 if pi.type != "pi" or pi.name != "test" or pi.content != "PI content":
-   print "error rereading PI"
+   print("error rereading PI")
    sys.exit(1)
 root = pi.next
 if root.name != "doc":
-   print "error rereading root"
+   print("error rereading root")
    sys.exit(1)
 ns = root.ns()
 if ns.name != "my" or ns.content != "http://example.com/doc":
-   print "error rereading namespace"
+   print("error rereading namespace")
    sys.exit(1)
 elem = root.children
 if elem.name != "foo":
-   print "error rereading elem"
+   print("error rereading elem")
    sys.exit(1)
 if elem.getBase(None) != "http://example.com/imgs":
-   print "error rereading base"
+   print("error rereading base")
    sys.exit(1)
 if elem.prop("img") != "image.gif":
-   print "error rereading property"
+   print("error rereading property")
    sys.exit(1)
 
 doc.freeDoc()
@@ -53,7 +53,7 @@ doc.freeDoc()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/compareNodes.py b/python/tests/compareNodes.py
index ec04323..ca5a5a2 100755
--- a/python/tests/compareNodes.py
+++ b/python/tests/compareNodes.py
@@ -17,25 +17,25 @@ foonode2 = root.children
 
 # Now check that [in]equality tests work ok
 if not ( foonode1 == foonode2 ):
-    print "Error comparing nodes with ==, nodes should be equal but are unequal"
+    print("Error comparing nodes with ==, nodes should be equal but are unequal")
     sys.exit(1)
 if not ( foonode1 != root ):
-    print "Error comparing nodes with ==, nodes should not be equal but are equal"
+    print("Error comparing nodes with ==, nodes should not be equal but are equal")
     sys.exit(1)
 if not ( foonode1 != root ):
-    print "Error comparing nodes with !=, nodes should not be equal but are equal"
+    print("Error comparing nodes with !=, nodes should not be equal but are equal")
 if ( foonode1 != foonode2 ):
-    print "Error comparing nodes with !=, nodes should be equal but are unequal"
+    print("Error comparing nodes with !=, nodes should be equal but are unequal")
 
 # Next check that the hash function for the objects also works ok
 if not (hash(foonode1) == hash(foonode2)):
-    print "Error hash values for two equal nodes are different"
+    print("Error hash values for two equal nodes are different")
     sys.exit(1)
 if not (hash(foonode1) != hash(root)):
-    print "Error hash values for two unequal nodes are not different"
+    print("Error hash values for two unequal nodes are not different")
     sys.exit(1)
 if hash(foonode1) == hash(root):
-    print "Error hash values for two unequal nodes are equal"
+    print("Error hash values for two unequal nodes are equal")
     sys.exit(1)
 
 # Basic tests successful
@@ -44,7 +44,7 @@ doc.freeDoc()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/ctxterror.py b/python/tests/ctxterror.py
index f2a897a..416e384 100755
--- a/python/tests/ctxterror.py
+++ b/python/tests/ctxterror.py
@@ -23,7 +23,7 @@ s = """<x xmlns="foo"></y>"""
 parserCtxt = libxml2.createPushParser(None,"",0,"test.xml")
 parserCtxt.setErrorHandler(callback, "-->")
 if parserCtxt.getErrorHandler() != (callback,"-->"):
-    print "getErrorHandler failed"
+    print("getErrorHandler failed")
     sys.exit(1)
 parserCtxt.parseChunk(s,len(s),1)
 doc = parserCtxt.doc()
@@ -31,9 +31,9 @@ doc.freeDoc()
 parserCtxt = None
 
 if err != expect:
-    print "error"
-    print "received %s" %(err)
-    print "expected %s" %(expect)
+    print("error")
+    print("received %s" %(err))
+    print("expected %s" %(expect))
     sys.exit(1)
 
 i = 10000
@@ -50,7 +50,7 @@ while i > 0:
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/cutnpaste.py b/python/tests/cutnpaste.py
index 0f9665a..7787246 100755
--- a/python/tests/cutnpaste.py
+++ b/python/tests/cutnpaste.py
@@ -35,14 +35,14 @@ source.freeDoc()
 # check the resulting tree
 str = dest.serialize()
 if str != """<root xmlns:foobar="http://example.org/bar"; xmlns:default="http://example.org/include"; 
xmlns:foo="http://example.org/foo";><default:fragment><foo:elem bar="tricky"/></default:fragment></root>""":
-    print "reconciliateNs() failed"
+    print("reconciliateNs() failed")
     sys.exit(1)
 target.freeDoc()
 
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/dtdvalid.py b/python/tests/dtdvalid.py
index 2011f2c..d4049b8 100755
--- a/python/tests/dtdvalid.py
+++ b/python/tests/dtdvalid.py
@@ -14,7 +14,7 @@ ctxt = libxml2.newValidCtxt()
 doc = libxml2.parseDoc(instance)
 ret = doc.validateDtd(ctxt, dtd)
 if ret != 1:
-    print "error doing DTD validation"
+    print("error doing DTD validation")
     sys.exit(1)
 
 doc.freeDoc()
@@ -25,8 +25,8 @@ del ctxt
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
 
diff --git a/python/tests/error.py b/python/tests/error.py
index da6d0c1..530c2ee 100755
--- a/python/tests/error.py
+++ b/python/tests/error.py
@@ -24,13 +24,13 @@ except libxml2.parserError:
     got_exc = 1
 
 if got_exc == 0:
-    print "Failed to get a parser exception"
+    print("Failed to get a parser exception")
     sys.exit(1)
 
 if err != expect:
-    print "error"
-    print "received %s" %(err)
-    print "expected %s" %(expect)
+    print("error")
+    print("received %s" %(err))
+    print("expected %s" %(expect))
     sys.exit(1)
 
 i = 10000
@@ -45,7 +45,7 @@ while i > 0:
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/inbuf.py b/python/tests/inbuf.py
index a7cc7a6..0c16674 100755
--- a/python/tests/inbuf.py
+++ b/python/tests/inbuf.py
@@ -1,14 +1,19 @@
 #!/usr/bin/python -u
 import sys
 import libxml2
-import StringIO
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 # Memory debug specific
 libxml2.debugMemory(1)
 
 i = 0
 while i < 5000:
-    f = StringIO.StringIO("foobar")
+    f = str_io("foobar")
     buf = libxml2.inputBuffer(f)
     i = i + 1
 
@@ -18,8 +23,8 @@ del buf
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
 
diff --git a/python/tests/indexes.py b/python/tests/indexes.py
index 8d7a7fc..e41a0d9 100755
--- a/python/tests/indexes.py
+++ b/python/tests/indexes.py
@@ -20,16 +20,16 @@ class callback:
     def startDocument(self):
         global ctxt
         if ctxt.byteConsumed() != self.startd:
-            print "document start at wrong index: %d expecting %d\n" % (
-                  ctxt.byteConsumed(), self.startd)
+            print("document start at wrong index: %d expecting %d\n" % (
+                  ctxt.byteConsumed(), self.startd))
             sys.exit(1)
 
     def endDocument(self):
         global ctxt
         expect = self.ende + self.delta * (self.count - 1) + self.endd
         if ctxt.byteConsumed() != expect:
-            print "document end at wrong index: %d expecting %d\n" % (
-                  ctxt.byteConsumed(), expect)
+            print("document end at wrong index: %d expecting %d\n" % (
+                  ctxt.byteConsumed(), expect))
             sys.exit(1)
 
     def startElement(self, tag, attrs):
@@ -37,8 +37,8 @@ class callback:
         if tag == "bar1":
             expect = self.starte + self.delta * self.count
             if ctxt.byteConsumed() != expect:
-                print "element start at wrong index: %d expecting %d\n" % (
-                   ctxt.byteConsumed(), expect)
+                print("element start at wrong index: %d expecting %d\n" % (
+                   ctxt.byteConsumed(), expect))
                 sys.exit(1)
             
 
@@ -47,8 +47,8 @@ class callback:
         if tag == "bar1":
             expect = self.ende + self.delta * self.count
             if ctxt.byteConsumed() != expect:
-                print "element end at wrong index: %d expecting %d\n" % (
-                      ctxt.byteConsumed(), expect)
+                print("element end at wrong index: %d expecting %d\n" % (
+                      ctxt.byteConsumed(), expect))
                 sys.exit(1)
             self.count = self.count + 1
 
@@ -107,7 +107,7 @@ ctxt=None
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/input_callback.py b/python/tests/input_callback.py
index 7131691..f8d08d5 100755
--- a/python/tests/input_callback.py
+++ b/python/tests/input_callback.py
@@ -3,8 +3,13 @@
 # This tests custom input callbacks
 #
 import sys
-import StringIO
 import libxml2
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 # We implement a new scheme, py://strings/ that will reference this dictionary
 pystrings = {
@@ -36,7 +41,7 @@ def my_input_cb(URI):
     path = URI[len(prefix):]
     if path not in pystrings:
         return None
-    return StringIO.StringIO(pystrings[path])
+    return str_io(pystrings[path])
 
 
 def run_test(desc, docpath, catalog, exp_status="verified", exp_err=[], test_callback=None,
@@ -68,14 +73,14 @@ def run_test(desc, docpath, catalog, exp_status="verified", exp_err=[], test_cal
         actual_status = "not loaded"
 
     if actual_status != exp_status:
-        print "Test '%s' failed: expect status '%s', actual '%s'" % (desc, exp_status, actual_status)
+        print("Test '%s' failed: expect status '%s', actual '%s'" % (desc, exp_status, actual_status))
         sys.exit(1)
     elif actual_err != exp_err:
-        print "Test '%s' failed" % desc
-        print "Expect errors:"
-        for s,m in exp_err: print "  [%2d] '%s'" % (s,m)
-        print "Actual errors:"
-        for s,m in actual_err: print "  [%2d] '%s'" % (s,m)
+        print("Test '%s' failed" % desc)
+        print("Expect errors:")
+        for s,m in exp_err: print("  [%2d] '%s'" % (s,m))
+        print("Actual errors:")
+        for s,m in actual_err: print("  [%2d] '%s'" % (s,m))
         sys.exit(1)
 
 
@@ -128,7 +133,7 @@ run_test(desc="Loading using standard i/o after unregistering callback",
 try:
     while True:
         libxml2.popInputCallbacks()
-except IndexError, e:
+except IndexError as e:
     pass
 
 run_test(desc="Loading using standard i/o after unregistering all callbacks",
@@ -139,5 +144,5 @@ run_test(desc="Loading using standard i/o after unregistering all callbacks",
             (-1, "failed to load external entity \"tst.xml\"\n")
             ])
 
-print "OK"
+print("OK")
 sys.exit(0);
diff --git a/python/tests/nsdel.py b/python/tests/nsdel.py
index c06ded3..079399a 100755
--- a/python/tests/nsdel.py
+++ b/python/tests/nsdel.py
@@ -19,7 +19,7 @@ def checkNamespaceDefs(node, count):
     nsList = list(namespaceDefs(node))
     #print nsList
     if len(nsList) != count :
-        raise Exception, "Error: saw %d namespace declarations.  Expected %d" % (len(nsList), count)
+        raise Exception("Error: saw %d namespace declarations.  Expected %d" % (len(nsList), count))
     
 # Memory debug specific
 libxml2.debugMemory(1)
@@ -56,7 +56,7 @@ doc.freeDoc()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/outbuf.py b/python/tests/outbuf.py
index 09cd9b5..62761cc 100755
--- a/python/tests/outbuf.py
+++ b/python/tests/outbuf.py
@@ -1,17 +1,22 @@
 #!/usr/bin/python -u
 import sys
 import libxml2
-import StringIO
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 def testSimpleBufferWrites():
-    f = StringIO.StringIO()
+    f = str_io()
     buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
     buf.write(3, "foo")
     buf.writeString("bar")
     buf.close()
-    
+
     if f.getvalue() != "foobar":
-        print "Failed to save to StringIO"
+        print("Failed to save to StringIO")
         sys.exit(1)
 
 def testSaveDocToBuffer():
@@ -23,15 +28,15 @@ def testSaveDocToBuffer():
 <?xml version="1.0" encoding="UTF-8"?>
 <foo>Hello</foo>
 '''
-    f = StringIO.StringIO()
+    f = str_io()
     buf = libxml2.createOutputBuffer(f, 'UTF-8')
     doc = libxml2.parseDoc(input)
     doc.saveFileTo(buf, 'UTF-8')
     doc.freeDoc()
     if f.getvalue() != expected:
-        print 'xmlDoc.saveFileTo() call failed.'
-        print '     got: %s' % repr(f.getvalue())
-        print 'expected: %s' % repr(expected)
+        print('xmlDoc.saveFileTo() call failed.')
+        print('     got: %s' % repr(f.getvalue()))
+        print('expected: %s' % repr(expected))
         sys.exit(1)
 
 def testSaveFormattedDocToBuffer():
@@ -49,13 +54,13 @@ def testSaveFormattedDocToBuffer():
 ''')
     doc = libxml2.parseDoc(input)
     for i in (0, 1):
-        f = StringIO.StringIO()
+        f = str_io()
         buf = libxml2.createOutputBuffer(f, 'UTF-8')
         doc.saveFormatFileTo(buf, 'UTF-8', i)
         if f.getvalue() != expected[i]:
-            print 'xmlDoc.saveFormatFileTo() call failed.'
-            print '     got: %s' % repr(f.getvalue())
-            print 'expected: %s' % repr(expected[i])
+            print('xmlDoc.saveFormatFileTo() call failed.')
+            print('     got: %s' % repr(f.getvalue()))
+            print('expected: %s' % repr(expected[i]))
             sys.exit(1)
     doc.freeDoc()
 
@@ -69,22 +74,22 @@ def testSaveIntoOutputBuffer():
 <?xml version="1.0" encoding="UTF-8"?>
 <foo>Hello</foo>
 '''
-    f = StringIO.StringIO()
+    f = str_io()
     doc = libxml2.parseDoc(input)
     buf = libxml2.createOutputBuffer(f, 'UTF-8')
     buf.saveFileTo(doc, 'UTF-8')
     if f.getvalue() != expected:
-        print 'outputBuffer.saveFileTo() call failed.'
-        print '     got: %s' % repr(f.getvalue())
-        print 'expected: %s' % repr(expected)
+        print('outputBuffer.saveFileTo() call failed.')
+        print('     got: %s' % repr(f.getvalue()))
+        print('expected: %s' % repr(expected))
         sys.exit(1)
-    f = StringIO.StringIO()
+    f = str_io()
     buf = libxml2.createOutputBuffer(f, 'UTF-8')
     buf.saveFormatFileTo(doc, 'UTF-8', 1)
     if f.getvalue() != expected:
-        print 'outputBuffer.saveFormatFileTo() call failed.'
-        print '     got: %s' % repr(f.getvalue())
-        print 'expected: %s' % repr(expected)
+        print('outputBuffer.saveFormatFileTo() call failed.')
+        print('     got: %s' % repr(f.getvalue()))
+        print('expected: %s' % repr(expected))
         sys.exit(1)
     doc.freeDoc()
 
@@ -99,7 +104,7 @@ if __name__ == '__main__':
 
     libxml2.cleanupParser()
     if libxml2.debugMemory(1) == 0:
-        print "OK"
+        print("OK")
     else:
-        print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+        print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
         libxml2.dumpMemory()
diff --git a/python/tests/push.py b/python/tests/push.py
index 5b60a16..0edd61d 100755
--- a/python/tests/push.py
+++ b/python/tests/push.py
@@ -10,11 +10,11 @@ ctxt.parseChunk("/>", 2, 1)
 doc = ctxt.doc()
 ctxt=None
 if doc.name != "test.xml":
-    print "document name error"
+    print("document name error")
     sys.exit(1)
 root = doc.children
 if root.name != "foo":
-    print "root element name error"
+    print("root element name error")
     sys.exit(1)
 doc.freeDoc()
 i = 10000
@@ -29,7 +29,7 @@ ctxt=None
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/pushSAX.py b/python/tests/pushSAX.py
index 1c1e786..48f6e82 100755
--- a/python/tests/pushSAX.py
+++ b/python/tests/pushSAX.py
@@ -51,14 +51,14 @@ ctxt=None
 
 reference = "startDocument:startElement foo {'url': 'tst'}:characters: bar:endElement foo:endDocument:"
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/pushSAXhtml.py b/python/tests/pushSAXhtml.py
index e6e89d9..159d308 100755
--- a/python/tests/pushSAXhtml.py
+++ b/python/tests/pushSAXhtml.py
@@ -52,14 +52,14 @@ ctxt=None
 reference = """startDocument:startElement html None:startElement body None:startElement foo {'url': 
'tst'}:error: Tag foo invalid
 :characters: bar:endElement foo:endElement body:endElement html:endDocument:"""
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/reader.py b/python/tests/reader.py
index 6c37762..173ce66 100755
--- a/python/tests/reader.py
+++ b/python/tests/reader.py
@@ -4,91 +4,96 @@
 # this tests the basic APIs of the XmlTextReader interface
 #
 import libxml2
-import StringIO
 import sys
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 # Memory debug specific
 libxml2.debugMemory(1)
 
-f = StringIO.StringIO("""<a><b b1="b1"/><c>content of c</c></a>""")
+f = str_io("""<a><b b1="b1"/><c>content of c</c></a>""")
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test1")
 ret = reader.Read()
 if ret != 1:
-    print "test1: Error reading to first element"
+    print("test1: Error reading to first element")
     sys.exit(1)
 if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 1 or reader.HasAttributes() != 0:
-    print "test1: Error reading the first element"
+    print("test1: Error reading the first element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test1: Error reading to second element"
+    print("test1: Error reading to second element")
     sys.exit(1)
 if reader.Name() != "b" or reader.IsEmptyElement() != 1 or \
    reader.NodeType() != 1 or reader.HasAttributes() != 1:
-    print "test1: Error reading the second element"
+    print("test1: Error reading the second element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test1: Error reading to third element"
+    print("test1: Error reading to third element")
     sys.exit(1)
 if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 1 or reader.HasAttributes() != 0:
-    print "test1: Error reading the third element"
+    print("test1: Error reading the third element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test1: Error reading to text node"
+    print("test1: Error reading to text node")
     sys.exit(1)
 if reader.Name() != "#text" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 3 or reader.HasAttributes() != 0 or \
    reader.Value() != "content of c":
-    print "test1: Error reading the text node"
+    print("test1: Error reading the text node")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test1: Error reading to end of third element"
+    print("test1: Error reading to end of third element")
     sys.exit(1)
 if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 15 or reader.HasAttributes() != 0:
-    print "test1: Error reading the end of third element"
+    print("test1: Error reading the end of third element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test1: Error reading to end of first element"
+    print("test1: Error reading to end of first element")
     sys.exit(1)
 if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 15 or reader.HasAttributes() != 0:
-    print "test1: Error reading the end of first element"
+    print("test1: Error reading the end of first element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 0:
-    print "test1: Error reading to end of document"
+    print("test1: Error reading to end of document")
     sys.exit(1)
 
 #
 # example from the XmlTextReader docs
 #
-f = StringIO.StringIO("""<test xmlns:dt="urn:datatypes" dt:type="int"/>""")
+f = str_io("""<test xmlns:dt="urn:datatypes" dt:type="int"/>""")
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test2")
 
 ret = reader.Read()
 if ret != 1:
-    print "Error reading test element"
+    print("Error reading test element")
     sys.exit(1)
 if reader.GetAttributeNo(0) != "urn:datatypes" or \
    reader.GetAttributeNo(1) != "int" or \
    reader.GetAttributeNs("type", "urn:datatypes") != "int" or \
    reader.GetAttribute("dt:type") != "int":
-    print "error reading test attributes"
+    print("error reading test attributes")
     sys.exit(1)
 
 #
 # example from the XmlTextReader docs
 #
-f = StringIO.StringIO("""<root xmlns:a="urn:456">
+f = str_io("""<root xmlns:a="urn:456">
 <item>
 <ref href="a:b"/>
 </item>
@@ -100,203 +105,203 @@ ret = reader.Read()
 while ret == 1:
     if reader.Name() == "ref":
         if reader.LookupNamespace("a") != "urn:456":
-            print "error resolving namespace prefix"
+            print("error resolving namespace prefix")
             sys.exit(1)
         break
     ret = reader.Read()
 if ret != 1:
-    print "Error finding the ref element"
+    print("Error finding the ref element")
     sys.exit(1)
 
 #
 # Home made example for the various attribute access functions
 #
-f = StringIO.StringIO("""<testattr xmlns="urn:1" xmlns:a="urn:2" b="b" a:b="a:b"/>""")
+f = str_io("""<testattr xmlns="urn:1" xmlns:a="urn:2" b="b" a:b="a:b"/>""")
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test4")
 ret = reader.Read()
 if ret != 1:
-    print "Error reading the testattr element"
+    print("Error reading the testattr element")
     sys.exit(1)
 #
 # Attribute exploration by index
 #
 if reader.MoveToAttributeNo(0) != 1:
-    print "Failed moveToAttribute(0)"
+    print("Failed moveToAttribute(0)")
     sys.exit(1)
 if reader.Value() != "urn:1":
-    print "Failed to read attribute(0)"
+    print("Failed to read attribute(0)")
     sys.exit(1)
 if reader.Name() != "xmlns":
-    print "Failed to read attribute(0) name"
+    print("Failed to read attribute(0) name")
     sys.exit(1)
 if reader.MoveToAttributeNo(1) != 1:
-    print "Failed moveToAttribute(1)"
+    print("Failed moveToAttribute(1)")
     sys.exit(1)
 if reader.Value() != "urn:2":
-    print "Failed to read attribute(1)"
+    print("Failed to read attribute(1)")
     sys.exit(1)
 if reader.Name() != "xmlns:a":
-    print "Failed to read attribute(1) name"
+    print("Failed to read attribute(1) name")
     sys.exit(1)
 if reader.MoveToAttributeNo(2) != 1:
-    print "Failed moveToAttribute(2)"
+    print("Failed moveToAttribute(2)")
     sys.exit(1)
 if reader.Value() != "b":
-    print "Failed to read attribute(2)"
+    print("Failed to read attribute(2)")
     sys.exit(1)
 if reader.Name() != "b":
-    print "Failed to read attribute(2) name"
+    print("Failed to read attribute(2) name")
     sys.exit(1)
 if reader.MoveToAttributeNo(3) != 1:
-    print "Failed moveToAttribute(3)"
+    print("Failed moveToAttribute(3)")
     sys.exit(1)
 if reader.Value() != "a:b":
-    print "Failed to read attribute(3)"
+    print("Failed to read attribute(3)")
     sys.exit(1)
 if reader.Name() != "a:b":
-    print "Failed to read attribute(3) name"
+    print("Failed to read attribute(3) name")
     sys.exit(1)
 #
 # Attribute exploration by name
 #
 if reader.MoveToAttribute("xmlns") != 1:
-    print "Failed moveToAttribute('xmlns')"
+    print("Failed moveToAttribute('xmlns')")
     sys.exit(1)
 if reader.Value() != "urn:1":
-    print "Failed to read attribute('xmlns')"
+    print("Failed to read attribute('xmlns')")
     sys.exit(1)
 if reader.MoveToAttribute("xmlns:a") != 1:
-    print "Failed moveToAttribute('xmlns')"
+    print("Failed moveToAttribute('xmlns')")
     sys.exit(1)
 if reader.Value() != "urn:2":
-    print "Failed to read attribute('xmlns:a')"
+    print("Failed to read attribute('xmlns:a')")
     sys.exit(1)
 if reader.MoveToAttribute("b") != 1:
-    print "Failed moveToAttribute('b')"
+    print("Failed moveToAttribute('b')")
     sys.exit(1)
 if reader.Value() != "b":
-    print "Failed to read attribute('b')"
+    print("Failed to read attribute('b')")
     sys.exit(1)
 if reader.MoveToAttribute("a:b") != 1:
-    print "Failed moveToAttribute('a:b')"
+    print("Failed moveToAttribute('a:b')")
     sys.exit(1)
 if reader.Value() != "a:b":
-    print "Failed to read attribute('a:b')"
+    print("Failed to read attribute('a:b')")
     sys.exit(1)
 if reader.MoveToAttributeNs("b", "urn:2") != 1:
-    print "Failed moveToAttribute('b', 'urn:2')"
+    print("Failed moveToAttribute('b', 'urn:2')")
     sys.exit(1)
 if reader.Value() != "a:b":
-    print "Failed to read attribute('b', 'urn:2')"
+    print("Failed to read attribute('b', 'urn:2')")
     sys.exit(1)
 #
 # Go back and read in sequence
 #
 if reader.MoveToElement() != 1:
-    print "Failed to move back to element"
+    print("Failed to move back to element")
     sys.exit(1)
 if reader.MoveToFirstAttribute() != 1:
-    print "Failed to move to first attribute"
+    print("Failed to move to first attribute")
     sys.exit(1)
 if reader.Value() != "urn:1":
-    print "Failed to read attribute(0)"
+    print("Failed to read attribute(0)")
     sys.exit(1)
 if reader.Name() != "xmlns":
-    print "Failed to read attribute(0) name"
+    print("Failed to read attribute(0) name")
     sys.exit(1)
 if reader.MoveToNextAttribute() != 1:
-    print "Failed to move to next attribute"
+    print("Failed to move to next attribute")
     sys.exit(1)
 if reader.Value() != "urn:2":
-    print "Failed to read attribute(1)"
+    print("Failed to read attribute(1)")
     sys.exit(1)
 if reader.Name() != "xmlns:a":
-    print "Failed to read attribute(1) name"
+    print("Failed to read attribute(1) name")
     sys.exit(1)
 if reader.MoveToNextAttribute() != 1:
-    print "Failed to move to next attribute"
+    print("Failed to move to next attribute")
     sys.exit(1)
 if reader.Value() != "b":
-    print "Failed to read attribute(2)"
+    print("Failed to read attribute(2)")
     sys.exit(1)
 if reader.Name() != "b":
-    print "Failed to read attribute(2) name"
+    print("Failed to read attribute(2) name")
     sys.exit(1)
 if reader.MoveToNextAttribute() != 1:
-    print "Failed to move to next attribute"
+    print("Failed to move to next attribute")
     sys.exit(1)
 if reader.Value() != "a:b":
-    print "Failed to read attribute(3)"
+    print("Failed to read attribute(3)")
     sys.exit(1)
 if reader.Name() != "a:b":
-    print "Failed to read attribute(3) name"
+    print("Failed to read attribute(3) name")
     sys.exit(1)
 if reader.MoveToNextAttribute() != 0:
-    print "Failed to detect last attribute"
+    print("Failed to detect last attribute")
     sys.exit(1)
 
     
 #
 # a couple of tests for namespace nodes
 #
-f = StringIO.StringIO("""<a xmlns="http://example.com/foo"/>""")
+f = str_io("""<a xmlns="http://example.com/foo"/>""")
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test6")
 ret = reader.Read()
 if ret != 1:
-    print "test6: failed to Read()"
+    print("test6: failed to Read()")
     sys.exit(1)
 ret = reader.MoveToFirstAttribute()
 if ret != 1:
-    print "test6: failed to MoveToFirstAttribute()"
+    print("test6: failed to MoveToFirstAttribute()")
     sys.exit(1)
 if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/"; or \
    reader.LocalName() != "xmlns" or reader.Name() != "xmlns" or \
    reader.Value() != "http://example.com/foo"; or reader.NodeType() != 2:
-    print "test6: failed to read the namespace node"
+    print("test6: failed to read the namespace node")
     sys.exit(1)
 
-f = StringIO.StringIO("""<a xmlns:prefix="http://example.com/foo"/>""")
+f = str_io("""<a xmlns:prefix="http://example.com/foo"/>""")
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test7")
 ret = reader.Read()
 if ret != 1:
-    print "test7: failed to Read()"
+    print("test7: failed to Read()")
     sys.exit(1)
 ret = reader.MoveToFirstAttribute()
 if ret != 1:
-    print "test7: failed to MoveToFirstAttribute()"
+    print("test7: failed to MoveToFirstAttribute()")
     sys.exit(1)
 if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/"; or \
    reader.LocalName() != "prefix" or reader.Name() != "xmlns:prefix" or \
    reader.Value() != "http://example.com/foo"; or reader.NodeType() != 2:
-    print "test7: failed to read the namespace node"
+    print("test7: failed to read the namespace node")
     sys.exit(1)
 
 #
 # Test for a limit case:
 #
-f = StringIO.StringIO("""<a/>""")
+f = str_io("""<a/>""")
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test8")
 ret = reader.Read()
 if ret != 1:
-    print "test8: failed to read the node"
+    print("test8: failed to read the node")
     sys.exit(1)
 if reader.Name() != "a" or reader.IsEmptyElement() != 1:
-    print "test8: failed to analyze the node"
+    print("test8: failed to analyze the node")
     sys.exit(1)
 ret = reader.Read()
 if ret != 0:
-    print "test8: failed to detect the EOF"
+    print("test8: failed to detect the EOF")
     sys.exit(1)
 
 #
 # Another test provided by St�phane Bidoul and checked with C#
 #
 def tst_reader(s):
-    f = StringIO.StringIO(s)
+    f = str_io(s)
     input = libxml2.inputBuffer(f)
     reader = input.newTextReader("tst")
     res = ""
@@ -322,8 +327,8 @@ expect="""1 (a) [None] 0 0
 """
 res = tst_reader(doc)
 if res != expect:
-    print "test5 failed"
-    print res
+    print("test5 failed")
+    print(res)
     sys.exit(1)
 
 doc="""<test><b/><c/></test>"""
@@ -334,8 +339,8 @@ expect="""1 (test) [None] 0 0
 """
 res = tst_reader(doc)
 if res != expect:
-    print "test9 failed"
-    print res
+    print("test9 failed")
+    print(res)
     sys.exit(1)
 
 doc="""<a><b>bbb</b><c>ccc</c></a>"""
@@ -350,8 +355,8 @@ expect="""1 (a) [None] 0 0
 """
 res = tst_reader(doc)
 if res != expect:
-    print "test10 failed"
-    print res
+    print("test10 failed")
+    print(res)
     sys.exit(1)
 
 doc="""<test a="a"/>"""
@@ -360,8 +365,8 @@ expect="""1 (test) [None] 1 0
 """
 res = tst_reader(doc)
 if res != expect:
-    print "test11 failed"
-    print res
+    print("test11 failed")
+    print(res)
     sys.exit(1)
 
 doc="""<test><a>aaa</a><b/></test>"""
@@ -374,8 +379,8 @@ expect="""1 (test) [None] 0 0
 """
 res = tst_reader(doc)
 if res != expect:
-    print "test12 failed"
-    print res
+    print("test12 failed")
+    print(res)
     sys.exit(1)
 
 doc="""<test><p></p></test>"""
@@ -386,8 +391,8 @@ expect="""1 (test) [None] 0 0
 """
 res = tst_reader(doc)
 if res != expect:
-    print "test13 failed"
-    print res
+    print("test13 failed")
+    print(res)
     sys.exit(1)
 
 doc="""<p></p>"""
@@ -396,8 +401,8 @@ expect="""1 (p) [None] 0 0
 """
 res = tst_reader(doc)
 if res != expect:
-    print "test14 failed"
-    print res
+    print("test14 failed")
+    print(res)
     sys.exit(1)
 
 #
@@ -421,8 +426,8 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 """
 res = tst_reader(doc)
 if res != expect:
-    print "test15 failed"
-    print res
+    print("test15 failed")
+    print(res)
     sys.exit(1)
 
 #
@@ -435,7 +440,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/reader2.py b/python/tests/reader2.py
index ea890ec..8570575 100755
--- a/python/tests/reader2.py
+++ b/python/tests/reader2.py
@@ -5,8 +5,13 @@
 import sys
 import glob
 import string
-import StringIO
 import libxml2
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 # Memory debug specific
 libxml2.debugMemory(1)
@@ -30,7 +35,11 @@ libxml2.registerErrorHandler(callback, "")
 valid_files = glob.glob("../../test/valid/*.x*")
 valid_files.sort()
 for file in valid_files:
-    if string.find(file, "t8") != -1:
+    if file.find("t8") != -1:
+        continue
+    if file == "../../test/valid/rss.xml":
+        continue
+    if file == "../../test/valid/xlink.xml":
         continue
     reader = libxml2.newTextReaderFilename(file)
     #print "%s:" % (file)
@@ -39,11 +48,11 @@ for file in valid_files:
     while ret == 1:
         ret = reader.Read()
     if ret != 0:
-        print "Error parsing and validating %s" % (file)
+        print("Error parsing and validating %s" % (file))
        #sys.exit(1)
 
 if err != expect:
-    print err
+    print(err)
 
 #
 # another separate test based on Stephane Bidoul one
@@ -79,7 +88,7 @@ expect="""10,test
 res=""
 err=""
 
-input = libxml2.inputBuffer(StringIO.StringIO(s))
+input = libxml2.inputBuffer(str_io(s))
 reader = input.newTextReader("test2")
 reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
 reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
@@ -89,12 +98,12 @@ while reader.Read() == 1:
     res = res + "%s,%s\n" % (reader.NodeType(),reader.Name())
 
 if res != expect:
-    print "test2 failed: unexpected output"
-    print res
+    print("test2 failed: unexpected output")
+    print(res)
     sys.exit(1)
 if err != "":
-    print "test2 failed: validation error found"
-    print err
+    print("test2 failed: validation error found")
+    print(err)
     sys.exit(1)
 
 #
@@ -124,12 +133,12 @@ res=""
 
 def myResolver(URL, ID, ctxt):
     if URL == "tst.ent":
-        return(StringIO.StringIO(tst_ent))
+        return(str_io(tst_ent))
     return None
 
 libxml2.setEntityLoader(myResolver)
 
-input = libxml2.inputBuffer(StringIO.StringIO(s))
+input = libxml2.inputBuffer(str_io(s))
 reader = input.newTextReader("test3")
 reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
 reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
@@ -139,12 +148,12 @@ while reader.Read() == 1:
     res = res + "%s %s\n" % (reader.NodeType(),reader.Name())
 
 if res != expect:
-    print "test3 failed: unexpected output"
-    print res
+    print("test3 failed: unexpected output")
+    print(res)
     sys.exit(1)
 if err != "":
-    print "test3 failed: validation error found"
-    print err
+    print("test3 failed: validation error found")
+    print(err)
     sys.exit(1)
 
 #
@@ -183,7 +192,7 @@ expect="""10 test 0
 res=""
 err=""
 
-input = libxml2.inputBuffer(StringIO.StringIO(s))
+input = libxml2.inputBuffer(str_io(s))
 reader = input.newTextReader("test4")
 reader.SetParserProp(libxml2.PARSER_LOADDTD,1)
 reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS,1)
@@ -193,12 +202,12 @@ while reader.Read() == 1:
     res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth())
 
 if res != expect:
-    print "test4 failed: unexpected output"
-    print res
+    print("test4 failed: unexpected output")
+    print(res)
     sys.exit(1)
 if err != "":
-    print "test4 failed: validation error found"
-    print err
+    print("test4 failed: validation error found")
+    print(err)
     sys.exit(1)
 
 #
@@ -228,18 +237,18 @@ expect="""10 test 0
 res=""
 err=""
 
-input = libxml2.inputBuffer(StringIO.StringIO(s))
+input = libxml2.inputBuffer(str_io(s))
 reader = input.newTextReader("test5")
 reader.SetParserProp(libxml2.PARSER_VALIDATE,1)
 while reader.Read() == 1:
     res = res + "%s %s %d\n" % (reader.NodeType(),reader.Name(),reader.Depth())
 
 if res != expect:
-    print "test5 failed: unexpected output"
-    print res
+    print("test5 failed: unexpected output")
+    print(res)
 if err != "":
-    print "test5 failed: validation error found"
-    print err
+    print("test5 failed: validation error found")
+    print(err)
 
 #
 # cleanup
@@ -250,7 +259,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/reader3.py b/python/tests/reader3.py
index 2458c89..4302b6c 100755
--- a/python/tests/reader3.py
+++ b/python/tests/reader3.py
@@ -3,8 +3,13 @@
 # this tests the entities substitutions with the XmlTextReader interface
 #
 import sys
-import StringIO
 import libxml2
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 docstr="""<?xml version='1.0'?>
 <!DOCTYPE doc [
@@ -18,90 +23,90 @@ libxml2.debugMemory(1)
 #
 # First test, normal don't substitute entities.
 #
-f = StringIO.StringIO(docstr)
+f = str_io(docstr)
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test_noent")
 ret = reader.Read()
 if ret != 1:
-    print "Error reading to root"
+    print("Error reading to root")
     sys.exit(1)
 if reader.Name() == "doc" or reader.NodeType() == 10:
     ret = reader.Read()
 if ret != 1:
-    print "Error reading to root"
+    print("Error reading to root")
     sys.exit(1)
 if reader.Name() != "doc" or reader.NodeType() != 1:
-    print "test_normal: Error reading the root element"
+    print("test_normal: Error reading the root element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_normal: Error reading to the entity"
+    print("test_normal: Error reading to the entity")
     sys.exit(1)
 if reader.Name() != "tst" or reader.NodeType() != 5:
-    print "test_normal: Error reading the entity"
+    print("test_normal: Error reading the entity")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_normal: Error reading to the end of root"
+    print("test_normal: Error reading to the end of root")
     sys.exit(1)
 if reader.Name() != "doc" or reader.NodeType() != 15:
-    print "test_normal: Error reading the end of the root element"
+    print("test_normal: Error reading the end of the root element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 0:
-    print "test_normal: Error detecting the end"
+    print("test_normal: Error detecting the end")
     sys.exit(1)
 
 #
 # Second test, completely substitute the entities.
 #
-f = StringIO.StringIO(docstr)
+f = str_io(docstr)
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test_noent")
 reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1)
 ret = reader.Read()
 if ret != 1:
-    print "Error reading to root"
+    print("Error reading to root")
     sys.exit(1)
 if reader.Name() == "doc" or reader.NodeType() == 10:
     ret = reader.Read()
 if ret != 1:
-    print "Error reading to root"
+    print("Error reading to root")
     sys.exit(1)
 if reader.Name() != "doc" or reader.NodeType() != 1:
-    print "test_noent: Error reading the root element"
+    print("test_noent: Error reading the root element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_noent: Error reading to the entity content"
+    print("test_noent: Error reading to the entity content")
     sys.exit(1)
 if reader.Name() != "p" or reader.NodeType() != 1:
-    print "test_noent: Error reading the p element from entity"
+    print("test_noent: Error reading the p element from entity")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_noent: Error reading to the text node"
+    print("test_noent: Error reading to the text node")
     sys.exit(1)
 if reader.NodeType() != 3 or reader.Value() != "test":
-    print "test_noent: Error reading the text node"
+    print("test_noent: Error reading the text node")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_noent: Error reading to the end of p element"
+    print("test_noent: Error reading to the end of p element")
     sys.exit(1)
 if reader.Name() != "p" or reader.NodeType() != 15:
-    print "test_noent: Error reading the end of the p element"
+    print("test_noent: Error reading the end of the p element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_noent: Error reading to the end of root"
+    print("test_noent: Error reading to the end of root")
     sys.exit(1)
 if reader.Name() != "doc" or reader.NodeType() != 15:
-    print "test_noent: Error reading the end of the root element"
+    print("test_noent: Error reading the end of the root element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 0:
-    print "test_noent: Error detecting the end"
+    print("test_noent: Error detecting the end")
     sys.exit(1)
 
 #
@@ -122,12 +127,12 @@ simplestruct2_ent="""<descr/>"""
 
 def myResolver(URL, ID, ctxt):
     if URL == "simplestruct2.ent":
-        return(StringIO.StringIO(simplestruct2_ent))
+        return(str_io(simplestruct2_ent))
     return None
 
 libxml2.setEntityLoader(myResolver)
 
-input = libxml2.inputBuffer(StringIO.StringIO(s))
+input = libxml2.inputBuffer(str_io(s))
 reader = input.newTextReader("test3")
 reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
 while reader.Read() == 1:
@@ -135,8 +140,8 @@ while reader.Read() == 1:
                                    reader.Depth(),reader.IsEmptyElement())
 
 if res != expect:
-    print "test3 failed: unexpected output"
-    print res
+    print("test3 failed: unexpected output")
+    print(res)
     sys.exit(1)
 
 #
@@ -149,7 +154,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/reader4.py b/python/tests/reader4.py
index 0824929..0bb3e3f 100755
--- a/python/tests/reader4.py
+++ b/python/tests/reader4.py
@@ -3,14 +3,19 @@
 # this tests the basic APIs of the XmlTextReader interface
 #
 import libxml2
-import StringIO
 import sys
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 # Memory debug specific
 libxml2.debugMemory(1)
 
 def tst_reader(s):
-    f = StringIO.StringIO(s)
+    f = str_io(s)
     input = libxml2.inputBuffer(f)
     reader = input.newTextReader("tst")
     res = ""
@@ -32,14 +37,14 @@ expect="""1 (test) [None] 0
 res = tst_reader("""<test><b/><c/></test>""")
 
 if res != expect:
-    print "Did not get the expected error message:"
-    print res
+    print("Did not get the expected error message:")
+    print(res)
     sys.exit(1)
 
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/reader5.py b/python/tests/reader5.py
index fcbad61..82d0dae 100755
--- a/python/tests/reader5.py
+++ b/python/tests/reader5.py
@@ -4,7 +4,6 @@
 # this extract the Dragon bibliography entries from the XML specification
 #
 import libxml2
-import StringIO
 import sys
 
 # Memory debug specific
@@ -15,11 +14,11 @@ Ravi Sethi, and Jeffrey D. Ullman.
 <emph>Compilers:  Principles, Techniques, and Tools</emph>.
 Reading:  Addison-Wesley, 1986, rpt. corr. 1988.</bibl>"""
 
-f = open('../../test/valid/REC-xml-19980210.xml')
+f = open('../../test/valid/REC-xml-19980210.xml', 'rb')
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("REC")
 res=""
-while reader.Read():
+while reader.Read() > 0:
     while reader.Name() == 'bibl':
         node = reader.Expand()            # expand the subtree
         if node.xpathEval("@id = 'Aho'"): # use XPath on it
@@ -28,9 +27,9 @@ while reader.Read():
             break;
 
 if res != expect:
-    print "Error: didn't get the expected output"
-    print "got '%s'" % (res)
-    print "expected '%s'" % (expect)
+    print("Error: didn't get the expected output")
+    print("got '%s'" % (res))
+    print("expected '%s'" % (expect))
     
 
 #
@@ -42,7 +41,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/reader6.py b/python/tests/reader6.py
index 7a34601..ef33b18 100755
--- a/python/tests/reader6.py
+++ b/python/tests/reader6.py
@@ -3,8 +3,13 @@
 # this tests the entities substitutions with the XmlTextReader interface
 #
 import sys
-import StringIO
 import libxml2
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 schema="""<element name="foo" xmlns="http://relaxng.org/ns/structure/1.0";
          datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes";>
@@ -41,7 +46,7 @@ docstr="""<foo>
 <item>100</item>
 </foo>"""
 
-f = StringIO.StringIO(docstr)
+f = str_io(docstr)
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("correct")
 reader.RelaxNGSetSchema(rngs)
@@ -50,11 +55,11 @@ while ret == 1:
     ret = reader.Read()
 
 if ret != 0:
-    print "Error parsing the document"
+    print("Error parsing the document")
     sys.exit(1)
 
 if reader.IsValid() != 1:
-    print "Document failed to validate"
+    print("Document failed to validate")
     sys.exit(1)
 
 #
@@ -84,7 +89,7 @@ def callback(ctx, str):
     err = err + "%s" % (str)
 libxml2.registerErrorHandler(callback, "")
 
-f = StringIO.StringIO(docstr)
+f = str_io(docstr)
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("error")
 reader.RelaxNGSetSchema(rngs)
@@ -93,16 +98,16 @@ while ret == 1:
     ret = reader.Read()
 
 if ret != 0:
-    print "Error parsing the document"
+    print("Error parsing the document")
     sys.exit(1)
 
 if reader.IsValid() != 0:
-    print "Document failed to detect the validation error"
+    print("Document failed to detect the validation error")
     sys.exit(1)
 
 if err != expect:
-    print "Did not get the expected error message:"
-    print err
+    print("Did not get the expected error message:")
+    print(err)
     sys.exit(1)
 
 #
@@ -117,7 +122,7 @@ libxml2.relaxNGCleanupTypes()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/reader7.py b/python/tests/reader7.py
index 35a426f..a24ee11 100755
--- a/python/tests/reader7.py
+++ b/python/tests/reader7.py
@@ -3,7 +3,7 @@
 # this tests the entities substitutions with the XmlTextReader interface
 #
 import sys
-import StringIO
+import io
 import libxml2
 
 # Memory debug specific
@@ -44,12 +44,12 @@ while ret == 1:
     ret = reader.Read()
 
 if ret != 0:
-    print "Error parsing the document test1"
+    print("Error parsing the document test1")
     sys.exit(1)
 
 if result != expect:
-    print "Unexpected result for test1"
-    print result
+    print("Unexpected result for test1")
+    print(result)
     sys.exit(1)
 
 #
@@ -80,12 +80,12 @@ while ret == 1:
     ret = reader.Read()
 
 if ret != 0:
-    print "Error parsing the document test2"
+    print("Error parsing the document test2")
     sys.exit(1)
 
 if result != expect:
-    print "Unexpected result for test2"
-    print result
+    print("Unexpected result for test2")
+    print(result)
     sys.exit(1)
 
 #
@@ -96,7 +96,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/reader8.py b/python/tests/reader8.py
index 53b7f27..d7064eb 100755
--- a/python/tests/reader8.py
+++ b/python/tests/reader8.py
@@ -3,7 +3,7 @@
 # this tests the entities substitutions with the XmlTextReader interface
 #
 import sys
-import StringIO
+import io
 import libxml2
 
 # Memory debug specific
@@ -23,7 +23,7 @@ ret = reader.Read()
 ret = reader.Close()
 
 if ret != 0:
-    print "Error closing the document test1"
+    print("Error closing the document test1")
     sys.exit(1)
 
 del reader
@@ -31,7 +31,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/readererr.py b/python/tests/readererr.py
old mode 100644
new mode 100755
index 0dad36e..c8ceba5
--- a/python/tests/readererr.py
+++ b/python/tests/readererr.py
@@ -3,8 +3,13 @@
 # this tests the basic APIs of the XmlTextReader interface
 #
 import libxml2
-import StringIO
 import sys
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 # Memory debug specific
 libxml2.debugMemory(1)
@@ -17,7 +22,7 @@ def myErrorHandler(arg,msg,severity,locator):
     global err
     err = err + "%s (%d) %s:%d:%s" % (arg,severity,locator.BaseURI(),locator.LineNumber(),msg)
 
-f = StringIO.StringIO("""<a xmlns="foo"><b b1="b1"/><c>content of c</a>""")
+f = str_io("""<a xmlns="foo"><b b1="b1"/><c>content of c</a>""")
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test1")
 reader.SetErrorHandler(myErrorHandler,"-->")
@@ -25,14 +30,14 @@ while reader.Read() == 1:
     pass
 
 if err != expect:
-    print "error"
-    print "received %s" %(err)
-    print "expected %s" %(expect)
+    print("error")
+    print("received %s" %(err))
+    print("expected %s" %(expect))
     sys.exit(1)
 
 reader.SetErrorHandler(None,None)
 if reader.GetErrorHandler() != (None,None):
-    print "GetErrorHandler failed"
+    print("GetErrorHandler failed")
     sys.exit(1)
 
 #
@@ -45,7 +50,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/readernext.py b/python/tests/readernext.py
index b01a49d..fcb9ae3 100755
--- a/python/tests/readernext.py
+++ b/python/tests/readernext.py
@@ -4,65 +4,70 @@
 # this tests the next API of the XmlTextReader interface
 #
 import libxml2
-import StringIO
 import sys
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 # Memory debug specific
 libxml2.debugMemory(1)
 
-f = StringIO.StringIO("""<a><b><c /></b><d>content of d</d></a>""")
+f = str_io("""<a><b><c /></b><d>content of d</d></a>""")
 input = libxml2.inputBuffer(f)
 reader = input.newTextReader("test_next")
 ret = reader.Read()
 if ret != 1:
-    print "test_next: Error reading to first element"
+    print("test_next: Error reading to first element")
     sys.exit(1)
 if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 1 or reader.HasAttributes() != 0:
-    print "test_next: Error reading the first element"
+    print("test_next: Error reading the first element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_next: Error reading to second element"
+    print("test_next: Error reading to second element")
     sys.exit(1)
 if reader.Name() != "b" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 1 or reader.HasAttributes() != 0:
-    print "test_next: Error reading the second element"
+    print("test_next: Error reading the second element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_next: Error reading to third element"
+    print("test_next: Error reading to third element")
     sys.exit(1)
 if reader.Name() != "c" or reader.NodeType() != 1 or \
    reader.HasAttributes() != 0:
-    print "test_next: Error reading the third element"
+    print("test_next: Error reading the third element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 1:
-    print "test_next: Error reading to end of third element"
+    print("test_next: Error reading to end of third element")
     sys.exit(1)
 if reader.Name() != "b" or reader.NodeType() != 15:
-    print "test_next: Error reading to end of second element"
+    print("test_next: Error reading to end of second element")
     sys.exit(1)
 ret = reader.Next()
 if ret != 1:
-    print "test_next: Error moving to third element"
+    print("test_next: Error moving to third element")
     sys.exit(1)
 if reader.Name() != "d" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 1 or reader.HasAttributes() != 0:
-    print "test_next: Error reading third element"
+    print("test_next: Error reading third element")
     sys.exit(1)
 ret = reader.Next()
 if ret != 1:
-    print "test_next: Error reading to end of first element"
+    print("test_next: Error reading to end of first element")
     sys.exit(1)
 if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
    reader.NodeType() != 15 or reader.HasAttributes() != 0:
-    print "test_next: Error reading the end of first element"
+    print("test_next: Error reading the end of first element")
     sys.exit(1)
 ret = reader.Read()
 if ret != 0:
-    print "test_next: Error reading to end of document"
+    print("test_next: Error reading to end of document")
     sys.exit(1)
 
 #
@@ -75,7 +80,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/regexp.py b/python/tests/regexp.py
old mode 100644
new mode 100755
index 4c05502..a03e459
--- a/python/tests/regexp.py
+++ b/python/tests/regexp.py
@@ -6,19 +6,19 @@ libxml2.debugMemory(1)
 
 re = libxml2.regexpCompile("a|b")
 if re.regexpExec("a") != 1:
-    print "error checking 'a'"
+    print("error checking 'a'")
     sys.exit(1)
 if re.regexpExec("b") != 1:
-    print "error checking 'b'"
+    print("error checking 'b'")
     sys.exit(1)
 if re.regexpExec("ab") != 0:
-    print "error checking 'ab'"
+    print("error checking 'ab'")
     sys.exit(1)
 if re.regexpExec("") != 0:
-    print "error checking 'ab'"
+    print("error checking 'ab'")
     sys.exit(1)
 if re.regexpIsDeterminist() != 1:
-    print "error checking determinism"
+    print("error checking determinism")
     sys.exit(1)
 del re
     
@@ -26,7 +26,7 @@ del re
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/relaxng.py b/python/tests/relaxng.py
index 2c83635..fa3d327 100755
--- a/python/tests/relaxng.py
+++ b/python/tests/relaxng.py
@@ -29,7 +29,7 @@ ctxt = rngs.relaxNGNewValidCtxt()
 doc = libxml2.parseDoc(instance)
 ret = doc.relaxNGValidateDoc(ctxt)
 if ret != 0:
-    print "error doing RelaxNG validation"
+    print("error doing RelaxNG validation")
     sys.exit(1)
 
 doc.freeDoc()
@@ -41,8 +41,8 @@ libxml2.relaxNGCleanupTypes()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
 
diff --git a/python/tests/resolver.py b/python/tests/resolver.py
index 9f4a478..6f21f52 100755
--- a/python/tests/resolver.py
+++ b/python/tests/resolver.py
@@ -1,20 +1,25 @@
 #!/usr/bin/python -u
 import sys
 import libxml2
-import StringIO
+try:
+    import StringIO
+    str_io = StringIO.StringIO
+except:
+    import io
+    str_io = io.StringIO
 
 # Memory debug specific
 libxml2.debugMemory(1)
 
 def myResolver(URL, ID, ctxt):
-    return(StringIO.StringIO("<foo/>"))
+    return(str_io("<foo/>"))
 
 libxml2.setEntityLoader(myResolver)
 
 doc = libxml2.parseFile("doesnotexist.xml")
 root = doc.children
 if root.name != "foo":
-    print "root element name error"
+    print("root element name error")
     sys.exit(1)
 doc.freeDoc()
 
@@ -23,7 +28,7 @@ while i < 5000:
     doc = libxml2.parseFile("doesnotexist.xml")
     root = doc.children
     if root.name != "foo":
-        print "root element name error"
+        print("root element name error")
         sys.exit(1)
     doc.freeDoc()
     i = i + 1
@@ -32,8 +37,8 @@ while i < 5000:
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
 
diff --git a/python/tests/schema.py b/python/tests/schema.py
index bfa8423..8089272 100755
--- a/python/tests/schema.py
+++ b/python/tests/schema.py
@@ -33,7 +33,7 @@ ctxt_valid  = ctxt_schema.schemaNewValidCtxt()
 doc = libxml2.parseDoc(instance)
 ret = doc.schemaValidateDoc(ctxt_valid)
 if ret != 0:
-    print "error doing schema validation"
+    print("error doing schema validation")
     sys.exit(1)
 
 doc.freeDoc()
@@ -45,8 +45,8 @@ libxml2.schemaCleanupTypes()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
 
diff --git a/python/tests/serialize.py b/python/tests/serialize.py
index 91753e7..80b901a 100755
--- a/python/tests/serialize.py
+++ b/python/tests/serialize.py
@@ -13,13 +13,13 @@ str = doc.serialize()
 if str != """<?xml version="1.0"?>
 <root><foo>hello</foo></root>
 """:
-   print "error serializing XML document 1"
+   print("error serializing XML document 1")
    sys.exit(1)
 str = doc.serialize("iso-8859-1")
 if str != """<?xml version="1.0" encoding="iso-8859-1"?>
 <root><foo>hello</foo></root>
 """:
-   print "error serializing XML document 2"
+   print("error serializing XML document 2")
    sys.exit(1)
 str = doc.serialize(format=1)
 if str != """<?xml version="1.0"?>
@@ -27,7 +27,7 @@ if str != """<?xml version="1.0"?>
   <foo>hello</foo>
 </root>
 """:
-   print "error serializing XML document 3"
+   print("error serializing XML document 3")
    sys.exit(1)
 str = doc.serialize("iso-8859-1", 1)
 if str != """<?xml version="1.0" encoding="iso-8859-1"?>
@@ -35,7 +35,7 @@ if str != """<?xml version="1.0" encoding="iso-8859-1"?>
   <foo>hello</foo>
 </root>
 """:
-   print "error serializing XML document 4"
+   print("error serializing XML document 4")
    sys.exit(1)
 
 #
@@ -44,23 +44,23 @@ if str != """<?xml version="1.0" encoding="iso-8859-1"?>
 root = doc.getRootElement()
 str = root.serialize()
 if str != """<root><foo>hello</foo></root>""":
-   print "error serializing XML root 1"
+   print("error serializing XML root 1")
    sys.exit(1)
 str = root.serialize("iso-8859-1")
 if str != """<root><foo>hello</foo></root>""":
-   print "error serializing XML root 2"
+   print("error serializing XML root 2")
    sys.exit(1)
 str = root.serialize(format=1)
 if str != """<root>
   <foo>hello</foo>
 </root>""":
-   print "error serializing XML root 3"
+   print("error serializing XML root 3")
    sys.exit(1)
 str = root.serialize("iso-8859-1", 1)
 if str != """<root>
   <foo>hello</foo>
 </root>""":
-   print "error serializing XML root 4"
+   print("error serializing XML root 4")
    sys.exit(1)
 doc.freeDoc()
 
@@ -72,13 +72,13 @@ str = doc.serialize()
 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
"http://www.w3.org/TR/REC-html40/loose.dtd";>
 <html><head><title>Hello</title></head><body><p>hello</p></body></html>
 """:
-   print "error serializing HTML document 1"
+   print("error serializing HTML document 1")
    sys.exit(1)
 str = doc.serialize("ISO-8859-1")
 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
"http://www.w3.org/TR/REC-html40/loose.dtd";>
 <html><head><meta http-equiv="Content-Type" content="text/html; 
charset=ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>
 """:
-   print "error serializing HTML document 2"
+   print("error serializing HTML document 2")
    sys.exit(1)
 str = doc.serialize(format=1)
 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
"http://www.w3.org/TR/REC-html40/loose.dtd";>
@@ -90,7 +90,7 @@ if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http
 <body><p>hello</p></body>
 </html>
 """:
-   print "error serializing HTML document 3"
+   print("error serializing HTML document 3")
    sys.exit(1)
 str = doc.serialize("iso-8859-1", 1)
 if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
"http://www.w3.org/TR/REC-html40/loose.dtd";>
@@ -102,7 +102,7 @@ if str != """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http
 <body><p>hello</p></body>
 </html>
 """:
-   print "error serializing HTML document 4"
+   print("error serializing HTML document 4")
    sys.exit(1)
 
 #
@@ -112,11 +112,11 @@ doc.htmlSetMetaEncoding(None)
 root = doc.getRootElement()
 str = root.serialize()
 if str != """<html><head><title>Hello</title></head><body><p>hello</p></body></html>""":
-   print "error serializing HTML root 1"
+   print("error serializing HTML root 1")
    sys.exit(1)
 str = root.serialize("ISO-8859-1")
 if str != """<html><head><meta http-equiv="Content-Type" content="text/html; 
charset=ISO-8859-1"><title>Hello</title></head><body><p>hello</p></body></html>""":
-   print "error serializing HTML root 2"
+   print("error serializing HTML root 2")
    sys.exit(1)
 str = root.serialize(format=1)
 if str != """<html>
@@ -126,7 +126,7 @@ if str != """<html>
 </head>
 <body><p>hello</p></body>
 </html>""":
-   print "error serializing HTML root 3"
+   print("error serializing HTML root 3")
    sys.exit(1)
 str = root.serialize("iso-8859-1", 1)
 if str != """<html>
@@ -136,7 +136,7 @@ if str != """<html>
 </head>
 <body><p>hello</p></body>
 </html>""":
-   print "error serializing HTML root 4"
+   print("error serializing HTML root 4")
    sys.exit(1)
 
 doc.freeDoc()
@@ -144,7 +144,7 @@ doc.freeDoc()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/sync.py b/python/tests/sync.py
index 2141875..9d565db 100755
--- a/python/tests/sync.py
+++ b/python/tests/sync.py
@@ -50,8 +50,8 @@ ctxt=None
 
 reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:"
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 log=""
@@ -62,8 +62,8 @@ ctxt=None
 
 reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:"
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 log=""
@@ -74,8 +74,8 @@ ctxt=None
 
 reference = "startDocument:startElement foo None:startElement bar2 None:"
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 log=""
@@ -86,8 +86,8 @@ ctxt=None
 
 reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 log=""
@@ -98,8 +98,8 @@ ctxt=None
 
 reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:"
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 log=""
@@ -110,8 +110,8 @@ ctxt=None
 
 reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 log=""
@@ -122,14 +122,14 @@ ctxt=None
 
 reference = "startDocument:startElement foo None:"
 if log != reference:
-    print "Error got: %s" % log
-    print "Exprected: %s" % reference
+    print("Error got: %s" % log)
+    print("Exprected: %s" % reference)
     sys.exit(1)
 
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/thread2.py b/python/tests/thread2.py
index c8ac5ed..2749eb0 100755
--- a/python/tests/thread2.py
+++ b/python/tests/thread2.py
@@ -1,6 +1,9 @@
 #!/usr/bin/python -u
 import string, sys, time
-import thread
+try:
+    from _thread import get_ident
+except:
+    from thread import get_ident
 from threading import Thread, Lock
 
 import libxml2
@@ -31,8 +34,8 @@ def test(expectedLineNumbersDefault):
     # check a per thread-global
     if expectedLineNumbersDefault != getLineNumbersDefault():
         failed = 1
-        print "FAILED to obtain correct value for " \
-              "lineNumbersDefault in thread %d" % thread.get_ident()
+        print("FAILED to obtain correct value for " \
+              "lineNumbersDefault in thread %d" % get_ident())
     # check ther global error handler 
     # (which is NOT per-thread in the python bindings)
     try:
@@ -51,7 +54,7 @@ libxml2.lineNumbersDefault(1)
 test(1)
 ec = len(eh.errors)
 if ec == 0:
-    print "FAILED: should have obtained errors"
+    print("FAILED: should have obtained errors")
     sys.exit(1)
 
 ts = []
@@ -65,7 +68,7 @@ for t in ts:
     t.join()
 
 if len(eh.errors) != ec+THREADS_COUNT*ec:
-    print "FAILED: did not obtain the correct number of errors"
+    print("FAILED: did not obtain the correct number of errors")
     sys.exit(1)
 
 # set lineNumbersDefault for future new threads
@@ -80,17 +83,17 @@ for t in ts:
     t.join()
 
 if len(eh.errors) != ec+THREADS_COUNT*ec*2:
-    print "FAILED: did not obtain the correct number of errors"
+    print("FAILED: did not obtain the correct number of errors")
     sys.exit(1)
 
 if failed:
-    print "FAILED"
+    print("FAILED")
     sys.exit(1)
 
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/tst.py b/python/tests/tst.py
index 9540cda..57a7318 100755
--- a/python/tests/tst.py
+++ b/python/tests/tst.py
@@ -7,22 +7,22 @@ libxml2.debugMemory(1)
 
 doc = libxml2.parseFile("tst.xml")
 if doc.name != "tst.xml":
-    print "doc.name failed"
+    print("doc.name failed")
     sys.exit(1)
 root = doc.children
 if root.name != "doc":
-    print "root.name failed"
+    print("root.name failed")
     sys.exit(1)
 child = root.children
 if child.name != "foo":
-    print "child.name failed"
+    print("child.name failed")
     sys.exit(1)
 doc.freeDoc()
 
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/tstLastError.py b/python/tests/tstLastError.py
index d26e82c..d5f9be7 100755
--- a/python/tests/tstLastError.py
+++ b/python/tests/tstLastError.py
@@ -17,8 +17,8 @@ class TestCase(unittest.TestCase):
         if libxml2.debugMemory(1) != 0:
             libxml2.dumpMemory() 
             self.fail("Memory leak %d bytes" % (libxml2.debugMemory(1),))
-       else:
-           print "OK"
+        else:
+            print("OK")
 
     def failUnlessXmlError(self,f,args,exc,domain,code,message,level,file,line):
         """Run function f, with arguments args and expect an exception exc;
@@ -27,19 +27,19 @@ class TestCase(unittest.TestCase):
         # disable the default error handler
         libxml2.registerErrorHandler(None,None)
         try:
-           apply(f,args)
+            f(*args)
         except exc:
             e = libxml2.lastError()
             if e is None:
                 self.fail("lastError not set")
             if 0:
-                print "domain = ",e.domain()
-                print "code = ",e.code()
-                print "message =",repr(e.message())
-                print "level =",e.level()
-                print "file =",e.file()
-                print "line =",e.line()
-                print
+                print("domain = ",e.domain())
+                print("code = ",e.code())
+                print("message =",repr(e.message()))
+                print("level =",e.level())
+                print("file =",e.file())
+                print("line =",e.line())
+                print()
             self.failUnlessEqual(domain,e.domain())
             self.failUnlessEqual(code,e.code())
             self.failUnlessEqual(message,e.message())
diff --git a/python/tests/tstURI.py b/python/tests/tstURI.py
index 58b7ad8..e4d58af 100755
--- a/python/tests/tstURI.py
+++ b/python/tests/tstURI.py
@@ -7,35 +7,35 @@ libxml2.debugMemory(1)
 
 uri = libxml2.parseURI("http://example.org:8088/foo/bar?query=simple#fragid";)
 if uri.scheme() != 'http':
-    print "Error parsing URI: wrong scheme"
+    print("Error parsing URI: wrong scheme")
     sys.exit(1)
 if uri.server() != 'example.org':
-    print "Error parsing URI: wrong server"
+    print("Error parsing URI: wrong server")
     sys.exit(1)
 if uri.port() != 8088:
-    print "Error parsing URI: wrong port"
+    print("Error parsing URI: wrong port")
     sys.exit(1)
 if uri.path() != '/foo/bar':
-    print "Error parsing URI: wrong path"
+    print("Error parsing URI: wrong path")
     sys.exit(1)
 if uri.query() != 'query=simple':
-    print "Error parsing URI: wrong query"
+    print("Error parsing URI: wrong query")
     sys.exit(1)
 if uri.fragment() != 'fragid':
-    print "Error parsing URI: wrong query"
+    print("Error parsing URI: wrong query")
     sys.exit(1)
 uri.setScheme("https")
 uri.setPort(223)
 uri.setFragment(None)
 result=uri.saveUri()
 if result != "https://example.org:223/foo/bar?query=simple":
-    print "Error modifying or saving the URI"
+    print("Error modifying or saving the URI")
 uri = None
 
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/tstmem.py b/python/tests/tstmem.py
index 553096d..6b34cf3 100755
--- a/python/tests/tstmem.py
+++ b/python/tests/tstmem.py
@@ -19,7 +19,7 @@ libxml2mod.xmlSetValidErrors(ctxt._o, error, error)
 doc = libxml2.parseDoc(instance)
 ret = doc.validateDtd(ctxt, dtd)
 if ret != 1:
-    print "error doing DTD validation"
+    print("error doing DTD validation")
     sys.exit(1)
 
 doc.freeDoc()
@@ -30,7 +30,7 @@ del ctxt
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/tstxpath.py b/python/tests/tstxpath.py
index e47c34d..0ba5a6d 100755
--- a/python/tests/tstxpath.py
+++ b/python/tests/tstxpath.py
@@ -25,10 +25,10 @@ doc = libxml2.parseFile("tst.xml")
 ctxt = doc.xpathNewContext()
 res = ctxt.xpathEval("//*")
 if len(res) != 2:
-    print "xpath query: wrong node set size"
+    print("xpath query: wrong node set size")
     sys.exit(1)
 if res[0].name != "doc" or res[1].name != "foo":
-    print "xpath query: wrong node set value"
+    print("xpath query: wrong node set value")
     sys.exit(1)
 libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
 libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
@@ -36,28 +36,28 @@ i = 10000
 while i > 0:
     res = ctxt.xpathEval("foo(1)")
     if res != 2:
-        print "xpath extension failure"
+        print("xpath extension failure")
         sys.exit(1)
     i = i - 1
 i = 10000
 while i > 0:
     res = ctxt.xpathEval("bar(1)")
     if res != "3":
-        print "xpath extension failure got %s expecting '3'"
+        print("xpath extension failure got %s expecting '3'")
         sys.exit(1)
     i = i - 1
 doc.freeDoc()
 ctxt.xpathFreeContext()
 
 if called != "foo":
-    print "xpath function: failed to access the context"
-    print "xpath function: %s" % (called)
+    print("xpath function: failed to access the context")
+    print("xpath function: %s" % (called))
     sys.exit(1)
 
 #memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/validDTD.py b/python/tests/validDTD.py
index 1222f9f..4b03b8e 100755
--- a/python/tests/validDTD.py
+++ b/python/tests/validDTD.py
@@ -11,7 +11,7 @@ class ErrorHandler:
 
     def handler(self, msg, data):
         if data != ARG:
-            raise Exception, "Error handler did not receive correct argument"
+            raise Exception("Error handler did not receive correct argument")
         self.errors.append(msg)
 
 
@@ -34,7 +34,7 @@ ctxt.setValidityErrorHandler(e.handler, e.handler, ARG)
 doc = libxml2.parseDoc(valid)
 ret = doc.validateDtd(ctxt, dtd)
 if ret != 1 or e.errors:
-    print "error doing DTD validation"
+    print("error doing DTD validation")
     sys.exit(1)
 doc.freeDoc()
 
@@ -42,7 +42,7 @@ doc.freeDoc()
 doc = libxml2.parseDoc(invalid)
 ret = doc.validateDtd(ctxt, dtd)
 if ret != 0 or not e.errors:
-    print "Error: document supposed to be invalid"
+    print("Error: document supposed to be invalid")
 doc.freeDoc()
 
 dtd.freeDtd()
@@ -52,8 +52,8 @@ del ctxt
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
 
diff --git a/python/tests/validRNG.py b/python/tests/validRNG.py
index 7022efe..57f13a4 100755
--- a/python/tests/validRNG.py
+++ b/python/tests/validRNG.py
@@ -11,7 +11,7 @@ class ErrorHandler:
 
     def handler(self, msg, data):
         if data != ARG:
-            raise Exception, "Error handler did not receive correct argument"
+            raise Exception("Error handler did not receive correct argument")
         self.errors.append(msg)
 
 # Memory debug specific
@@ -49,7 +49,7 @@ ctxt.setValidityErrorHandler(e.handler, e.handler, ARG)
 doc = libxml2.parseDoc(valid)
 ret = doc.relaxNGValidateDoc(ctxt)
 if ret != 0 or e.errors:
-    print "error doing RelaxNG validation"
+    print("error doing RelaxNG validation")
     sys.exit(1)
 doc.freeDoc()
 
@@ -57,7 +57,7 @@ doc.freeDoc()
 doc = libxml2.parseDoc(invalid)
 ret = doc.relaxNGValidateDoc(ctxt)
 if ret == 0 or not e.errors:
-    print "Error: document supposed to be RelaxNG invalid"
+    print("Error: document supposed to be RelaxNG invalid")
     sys.exit(1)
 doc.freeDoc()
 
@@ -69,8 +69,8 @@ libxml2.relaxNGCleanupTypes()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
 
diff --git a/python/tests/validSchemas.py b/python/tests/validSchemas.py
index d4d62b2..cc543f3 100755
--- a/python/tests/validSchemas.py
+++ b/python/tests/validSchemas.py
@@ -11,7 +11,7 @@ class ErrorHandler:
 
     def handler(self, msg, data):
         if data != ARG:
-            raise Exception, "Error handler did not receive correct argument"
+            raise Exception("Error handler did not receive correct argument")
         self.errors.append(msg)
 
 # Memory debug specific
@@ -56,7 +56,7 @@ ctxt_valid.setValidityErrorHandler(e.handler, e.handler, ARG)
 doc = libxml2.parseDoc(valid)
 ret = doc.schemaValidateDoc(ctxt_valid)
 if ret != 0 or e.errors:
-    print "error doing schema validation"
+    print("error doing schema validation")
     sys.exit(1)
 doc.freeDoc()
 
@@ -64,7 +64,7 @@ doc.freeDoc()
 doc = libxml2.parseDoc(invalid)
 ret = doc.schemaValidateDoc(ctxt_valid)
 if ret == 0 or not e.errors:
-    print "Error: document supposer to be schema invalid"
+    print("Error: document supposer to be schema invalid")
     sys.exit(1)
 doc.freeDoc()
 
@@ -76,8 +76,8 @@ libxml2.schemaCleanupTypes()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
 
diff --git a/python/tests/validate.py b/python/tests/validate.py
index 0dc86f4..16c0386 100755
--- a/python/tests/validate.py
+++ b/python/tests/validate.py
@@ -12,14 +12,14 @@ doc = ctxt.doc()
 valid = ctxt.isValid()
 
 if doc.name != "valid.xml":
-    print "doc.name failed"
+    print("doc.name failed")
     sys.exit(1)
 root = doc.children
 if root.name != "doc":
-    print "root.name failed"
+    print("root.name failed")
     sys.exit(1)
 if valid != 1:
-    print "validity chec failed"
+    print("validity chec failed")
     sys.exit(1)
 doc.freeDoc()
 
@@ -32,7 +32,7 @@ while i > 0:
     valid = ctxt.isValid()
     doc.freeDoc()
     if valid != 1:
-        print "validity check failed"
+        print("validity check failed")
         sys.exit(1)
     i = i - 1
 
@@ -48,14 +48,14 @@ ctxt.parseDocument()
 doc = ctxt.doc()
 valid = ctxt.isValid()
 if doc.name != "invalid.xml":
-    print "doc.name failed"
+    print("doc.name failed")
     sys.exit(1)
 root = doc.children
 if root.name != "doc":
-    print "root.name failed"
+    print("root.name failed")
     sys.exit(1)
 if valid != 0:
-    print "validity chec failed"
+    print("validity chec failed")
     sys.exit(1)
 doc.freeDoc()
 
@@ -68,7 +68,7 @@ while i > 0:
     valid = ctxt.isValid()
     doc.freeDoc()
     if valid != 0:
-        print "validity check failed"
+        print("validity check failed")
         sys.exit(1)
     i = i - 1
 del ctxt
@@ -76,7 +76,7 @@ del ctxt
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/walker.py b/python/tests/walker.py
index 3b5a62a..5c6f06d 100755
--- a/python/tests/walker.py
+++ b/python/tests/walker.py
@@ -3,7 +3,7 @@
 # this tests the entities substitutions with the XmlTextReader interface
 #
 import sys
-import StringIO
+import io
 import libxml2
 
 # Memory debug specific
@@ -45,12 +45,12 @@ while ret == 1:
     ret = reader.Read()
 
 if ret != 0:
-    print "Error parsing the document test1"
+    print("Error parsing the document test1")
     sys.exit(1)
 
 if result != expect:
-    print "Unexpected result for test1"
-    print result
+    print("Unexpected result for test1")
+    print(result)
     sys.exit(1)
 
 doc.freeDoc()
@@ -85,12 +85,12 @@ while ret == 1:
     ret = reader.Read()
 
 if ret != 0:
-    print "Error parsing the document test2"
+    print("Error parsing the document test2")
     sys.exit(1)
 
 if result != expect:
-    print "Unexpected result for test2"
-    print result
+    print("Unexpected result for test2")
+    print(result)
     sys.exit(1)
 
 doc.freeDoc()
@@ -123,12 +123,12 @@ while ret == 1:
     ret = reader.Read()
 
 if ret != 0:
-    print "Error parsing the document test3"
+    print("Error parsing the document test3")
     sys.exit(1)
 
 if result != expect:
-    print "Unexpected result for test3"
-    print result
+    print("Unexpected result for test3")
+    print(result)
     sys.exit(1)
 
 #
@@ -139,7 +139,7 @@ del reader
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/xpath.py b/python/tests/xpath.py
index 2e036e1..72e6c9d 100755
--- a/python/tests/xpath.py
+++ b/python/tests/xpath.py
@@ -11,24 +11,24 @@ libxml2.debugMemory(1)
 
 doc = libxml2.parseFile("tst.xml")
 if doc.name != "tst.xml":
-    print "doc.name error"
+    print("doc.name error")
     sys.exit(1);
 
 ctxt = doc.xpathNewContext()
 res = ctxt.xpathEval("//*")
 if len(res) != 2:
-    print "xpath query: wrong node set size"
+    print("xpath query: wrong node set size")
     sys.exit(1)
 if res[0].name != "doc" or res[1].name != "foo":
-    print "xpath query: wrong node set value"
+    print("xpath query: wrong node set value")
     sys.exit(1)
 ctxt.setContextNode(res[0])
 res = ctxt.xpathEval("foo")
 if len(res) != 1:
-    print "xpath query: wrong node set size"
+    print("xpath query: wrong node set size")
     sys.exit(1)
 if res[0].name != "foo":
-    print "xpath query: wrong node set value"
+    print("xpath query: wrong node set value")
     sys.exit(1)
 doc.freeDoc()
 ctxt.xpathFreeContext()
@@ -45,7 +45,7 @@ del ctxt
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/xpathext.py b/python/tests/xpathext.py
index 4769626..b83d283 100755
--- a/python/tests/xpathext.py
+++ b/python/tests/xpathext.py
@@ -15,10 +15,10 @@ doc = libxml2.parseFile("tst.xml")
 ctxt = doc.xpathNewContext()
 res = ctxt.xpathEval("//*")
 if len(res) != 2:
-    print "xpath query: wrong node set size"
+    print("xpath query: wrong node set size")
     sys.exit(1)
 if res[0].name != "doc" or res[1].name != "foo":
-    print "xpath query: wrong node set value"
+    print("xpath query: wrong node set value")
     sys.exit(1)
 
 libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
@@ -27,14 +27,14 @@ i = 10000
 while i > 0:
     res = ctxt.xpathEval("foo(1)")
     if res != 2:
-        print "xpath extension failure"
+        print("xpath extension failure")
         sys.exit(1)
     i = i - 1
 i = 10000
 while i > 0:
     res = ctxt.xpathEval("bar(1)")
     if res != "3":
-        print "xpath extension failure got %s expecting '3'"
+        print("xpath extension failure got %s expecting '3'")
         sys.exit(1)
     i = i - 1
 doc.freeDoc()
@@ -43,7 +43,7 @@ ctxt.xpathFreeContext()
 # Memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/xpathleak.py b/python/tests/xpathleak.py
old mode 100644
new mode 100755
index 39f49e3..d2196fa
--- a/python/tests/xpathleak.py
+++ b/python/tests/xpathleak.py
@@ -42,24 +42,24 @@ badexprs = (
 for expr in badexprs:
        try:
                ctxt.xpathEval(expr)
-       except libxml2.xpathError, e:
+       except libxml2.xpathError as e:
                pass
        else:
-               print "Unexpectedly legal expression:", expr
+               print("Unexpectedly legal expression:", expr)
 ctxt.xpathFreeContext()
 doc.freeDoc()
 
 if err != expect:
-    print "error"
-    print "received %s" %(err)
-    print "expected %s" %(expect)
+    print("error")
+    print("received %s" %(err))
+    print("expected %s" %(expect))
     sys.exit(1)
 
 libxml2.cleanupParser()
 leakedbytes = libxml2.debugMemory(True)
 if leakedbytes == 0:
-       print "OK"
+       print("OK")
 else:
-       print "Memory leak", leakedbytes, "bytes"
+       print("Memory leak", leakedbytes, "bytes")
        # drop file to .memdump file in cwd, but won't work if not compiled in
        libxml2.dumpMemory()
diff --git a/python/tests/xpathns.py b/python/tests/xpathns.py
index e67e550..379535e 100755
--- a/python/tests/xpathns.py
+++ b/python/tests/xpathns.py
@@ -14,8 +14,8 @@ for n in d.xpathEval("//namespace::*"):
 d.freeDoc()
 
 if res != expect:
-    print "test5 failed: unexpected output"
-    print res
+    print("test5 failed: unexpected output")
+    print(res)
 del res
 del d
 del n
@@ -23,7 +23,7 @@ del n
 libxml2.cleanupParser()
 
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()
diff --git a/python/tests/xpathret.py b/python/tests/xpathret.py
index 2b5576a..11c8b32 100755
--- a/python/tests/xpathret.py
+++ b/python/tests/xpathret.py
@@ -27,21 +27,21 @@ ctxt = doc.xpathNewContext()
 libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
 res = ctxt.xpathEval("foo('hello')")
 if type(res) != type([]):
-    print "Failed to return a nodeset"
+    print("Failed to return a nodeset")
     sys.exit(1)
 if len(res) != 1:
-    print "Unexpected nodeset size"
+    print("Unexpected nodeset size")
     sys.exit(1)
 node = res[0]
 if node.name != 'p':
-    print "Unexpected nodeset element result"
+    print("Unexpected nodeset element result")
     sys.exit(1)
 node = node.children
 if node.type != 'text':
-    print "Unexpected nodeset element children type"
+    print("Unexpected nodeset element children type")
     sys.exit(1)
 if node.content != 'hello':
-    print "Unexpected nodeset element children content"
+    print("Unexpected nodeset element children content")
     sys.exit(1)
 
 doc.freeDoc()
@@ -51,7 +51,7 @@ ctxt.xpathFreeContext()
 #memory debug specific
 libxml2.cleanupParser()
 if libxml2.debugMemory(1) == 0:
-    print "OK"
+    print("OK")
 else:
-    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
     libxml2.dumpMemory()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]