[libxml2] Fix python bindings with versions older than 2.7



commit bf4a8f0ea8579f05eea2e6f43df73b2a239d41b3
Author: Daniel Veillard <veillard redhat com>
Date:   Tue Apr 2 10:27:57 2013 +0800

    Fix python bindings with versions older than 2.7
    
    Need fixing on the Capsule usage, the lack of PyBytes,
    lack of io module and the way to access exception details.

 python/libxml.py               |    4 +++-
 python/libxml_wrap.h           |   19 +++++++++++++++++++
 python/tests/input_callback.py |    2 +-
 python/tests/reader7.py        |    1 -
 python/tests/reader8.py        |    1 -
 python/tests/walker.py         |    1 -
 python/tests/xpathleak.py      |    2 +-
 python/types.c                 |   18 ++++++++++++++----
 8 files changed, 38 insertions(+), 10 deletions(-)
---
diff --git a/python/libxml.py b/python/libxml.py
index 117de82..e507e0f 100644
--- a/python/libxml.py
+++ b/python/libxml.py
@@ -77,7 +77,9 @@ class ioWrapper:
                 ret = self.__io.read()
             else:
                 ret = self.__io.read(len)
-        except Exception as e:
+        except Exception:
+            import sys
+            e = sys.exc_info()[1]
             print("failed to read from Python:", type(e))
             print("on IO:", self.__io)
             self.__io == None
diff --git a/python/libxml_wrap.h b/python/libxml_wrap.h
index 70248d7..a9b9739 100644
--- a/python/libxml_wrap.h
+++ b/python/libxml_wrap.h
@@ -23,6 +23,25 @@
 #include <libxml/xmlschemas.h>
 #endif
 
+/*
+ * for older versions of Python, we don't use PyBytes, but keep PyString
+ * and don't use Capsule but CObjects
+ */
+#if PY_VERSION_HEX < 0x02070000
+#ifndef PyBytes_Check
+#define PyBytes_Check PyString_Check
+#define PyBytes_Size PyString_Size
+#define PyBytes_AsString PyString_AsString
+#define PyBytes_AS_STRING PyString_AS_STRING
+#define PyBytes_GET_SIZE PyString_GET_SIZE
+
+#define PyCapsule_New PyCObject_FromVoidPtrAndDesc
+#define PyCapsule_CheckExact PyCObject_Check
+#define PyCapsule_GetPointer(o, n) PyCObject_GetDesc((o))
+
+#endif
+#endif
+
 /**
  * ATTRIBUTE_UNUSED:
  *
diff --git a/python/tests/input_callback.py b/python/tests/input_callback.py
index f8d08d5..495ab62 100755
--- a/python/tests/input_callback.py
+++ b/python/tests/input_callback.py
@@ -133,7 +133,7 @@ run_test(desc="Loading using standard i/o after unregistering callback",
 try:
     while True:
         libxml2.popInputCallbacks()
-except IndexError as e:
+except IndexError:
     pass
 
 run_test(desc="Loading using standard i/o after unregistering all callbacks",
diff --git a/python/tests/reader7.py b/python/tests/reader7.py
index a24ee11..c88e370 100755
--- a/python/tests/reader7.py
+++ b/python/tests/reader7.py
@@ -3,7 +3,6 @@
 # this tests the entities substitutions with the XmlTextReader interface
 #
 import sys
-import io
 import libxml2
 
 # Memory debug specific
diff --git a/python/tests/reader8.py b/python/tests/reader8.py
index d7064eb..de2dcd6 100755
--- a/python/tests/reader8.py
+++ b/python/tests/reader8.py
@@ -3,7 +3,6 @@
 # this tests the entities substitutions with the XmlTextReader interface
 #
 import sys
-import io
 import libxml2
 
 # Memory debug specific
diff --git a/python/tests/walker.py b/python/tests/walker.py
index 5c6f06d..47f0557 100755
--- a/python/tests/walker.py
+++ b/python/tests/walker.py
@@ -3,7 +3,6 @@
 # this tests the entities substitutions with the XmlTextReader interface
 #
 import sys
-import io
 import libxml2
 
 # Memory debug specific
diff --git a/python/tests/xpathleak.py b/python/tests/xpathleak.py
index d2196fa..33ab61c 100755
--- a/python/tests/xpathleak.py
+++ b/python/tests/xpathleak.py
@@ -42,7 +42,7 @@ badexprs = (
 for expr in badexprs:
        try:
                ctxt.xpathEval(expr)
-       except libxml2.xpathError as e:
+       except libxml2.xpathError:
                pass
        else:
                print("Unexpectedly legal expression:", expr)
diff --git a/python/types.c b/python/types.c
index c865b65..31c909a 100644
--- a/python/types.c
+++ b/python/types.c
@@ -384,19 +384,29 @@ libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
 
 /**
  * libxml_xmlXPathDestructNsNode:
- * cobj: xmlNsPtr namespace node capsule object
+ * cap: xmlNsPtr namespace node capsule object
  *
  * This function is called if and when a namespace node returned in
  * an XPath node set is to be destroyed. That's the only kind of
  * object returned in node set not directly linked to the original
  * xmlDoc document, see xmlXPathNodeSetDupNs.
  */
+#if PY_VERSION_HEX < 0x02070000
 static void
-libxml_xmlXPathDestructNsNode(PyObject *cap) {
+libxml_xmlXPathDestructNsNode(void *cap, void *desc ATTRIBUTE_UNUSED)
+#else
+static void
+libxml_xmlXPathDestructNsNode(PyObject *cap)
+#endif
+{
 #ifdef DEBUG
-    fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cobj);
+    fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cap);
 #endif
+#if PY_VERSION_HEX < 0x02070000
+    xmlXPathNodeSetFreeNs((xmlNsPtr) cap);
+#else
     xmlXPathNodeSetFreeNs((xmlNsPtr) PyCapsule_GetPointer(cap, "xmlNsPtr"));
+#endif
 }
 
 PyObject *
@@ -658,7 +668,7 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj)
             cur = NULL;
             if (PyCapsule_CheckExact(node)) {
 #ifdef DEBUG
-                printf("Got a CObject\n");
+                printf("Got a Capsule\n");
 #endif
                 cur = PyxmlNode_Get(node);
             } else if ((PyObject_HasAttrString(node, (char *) "_o")) &&


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