1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-01-12 09:17:37 +03:00

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.
This commit is contained in:
Daniel Veillard 2013-04-02 10:27:57 +08:00
parent 4d7a32959b
commit bf4a8f0ea8
8 changed files with 39 additions and 11 deletions

View File

@ -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

View File

@ -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:
*

View File

@ -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",

View File

@ -3,7 +3,6 @@
# this tests the entities substitutions with the XmlTextReader interface
#
import sys
import io
import libxml2
# Memory debug specific

View File

@ -3,7 +3,6 @@
# this tests the entities substitutions with the XmlTextReader interface
#
import sys
import io
import libxml2
# Memory debug specific

View File

@ -3,7 +3,6 @@
# this tests the entities substitutions with the XmlTextReader interface
#
import sys
import io
import libxml2
# Memory debug specific

View File

@ -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)

View File

@ -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) {
#ifdef DEBUG
fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cobj);
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", 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")) &&