Skip to content

Commit

Permalink
Fix python bindings with versions older than 2.7
Browse files Browse the repository at this point in the history
Need fixing on the Capsule usage, the lack of PyBytes,
lack of io module and the way to access exception details.
  • Loading branch information
veillard committed Apr 2, 2013
1 parent 4d7a329 commit bf4a8f0
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 10 deletions.
4 changes: 3 additions & 1 deletion python/libxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def io_read(self, len = -1):
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
Expand Down
19 changes: 19 additions & 0 deletions python/libxml_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand Down
2 changes: 1 addition & 1 deletion python/tests/input_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def my_ctx_error_cb(arg, msg, severity, reserved):
try:
while True:
libxml2.popInputCallbacks()
except IndexError as e:
except IndexError:
pass

run_test(desc="Loading using standard i/o after unregistering all callbacks",
Expand Down
1 change: 0 additions & 1 deletion python/tests/reader7.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# this tests the entities substitutions with the XmlTextReader interface
#
import sys
import io
import libxml2

# Memory debug specific
Expand Down
1 change: 0 additions & 1 deletion python/tests/reader8.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# this tests the entities substitutions with the XmlTextReader interface
#
import sys
import io
import libxml2

# Memory debug specific
Expand Down
1 change: 0 additions & 1 deletion python/tests/walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# this tests the entities substitutions with the XmlTextReader interface
#
import sys
import io
import libxml2

# Memory debug specific
Expand Down
2 changes: 1 addition & 1 deletion python/tests/xpathleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def callback(ctx, str):
for expr in badexprs:
try:
ctxt.xpathEval(expr)
except libxml2.xpathError as e:
except libxml2.xpathError:
pass
else:
print("Unexpectedly legal expression:", expr)
Expand Down
18 changes: 14 additions & 4 deletions python/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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")) &&
Expand Down

0 comments on commit bf4a8f0

Please sign in to comment.