1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-07-29 11:41:22 +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() ret = self.__io.read()
else: else:
ret = self.__io.read(len) 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("failed to read from Python:", type(e))
print("on IO:", self.__io) print("on IO:", self.__io)
self.__io == None self.__io == None

View File

@ -23,6 +23,25 @@
#include <libxml/xmlschemas.h> #include <libxml/xmlschemas.h>
#endif #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: * ATTRIBUTE_UNUSED:
* *

View File

@ -133,7 +133,7 @@ run_test(desc="Loading using standard i/o after unregistering callback",
try: try:
while True: while True:
libxml2.popInputCallbacks() libxml2.popInputCallbacks()
except IndexError as e: except IndexError:
pass pass
run_test(desc="Loading using standard i/o after unregistering all callbacks", 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 # this tests the entities substitutions with the XmlTextReader interface
# #
import sys import sys
import io
import libxml2 import libxml2
# Memory debug specific # Memory debug specific

View File

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

View File

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

View File

@ -42,7 +42,7 @@ badexprs = (
for expr in badexprs: for expr in badexprs:
try: try:
ctxt.xpathEval(expr) ctxt.xpathEval(expr)
except libxml2.xpathError as e: except libxml2.xpathError:
pass pass
else: else:
print("Unexpectedly legal expression:", expr) print("Unexpectedly legal expression:", expr)

View File

@ -384,19 +384,29 @@ libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
/** /**
* libxml_xmlXPathDestructNsNode: * 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 * 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 * 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 * object returned in node set not directly linked to the original
* xmlDoc document, see xmlXPathNodeSetDupNs. * xmlDoc document, see xmlXPathNodeSetDupNs.
*/ */
#if PY_VERSION_HEX < 0x02070000
static void static void
libxml_xmlXPathDestructNsNode(PyObject *cap) { libxml_xmlXPathDestructNsNode(void *cap, void *desc ATTRIBUTE_UNUSED)
#ifdef DEBUG #else
fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cobj); static void
libxml_xmlXPathDestructNsNode(PyObject *cap)
#endif #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")); xmlXPathNodeSetFreeNs((xmlNsPtr) PyCapsule_GetPointer(cap, "xmlNsPtr"));
#endif
} }
PyObject * PyObject *
@ -658,7 +668,7 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj)
cur = NULL; cur = NULL;
if (PyCapsule_CheckExact(node)) { if (PyCapsule_CheckExact(node)) {
#ifdef DEBUG #ifdef DEBUG
printf("Got a CObject\n"); printf("Got a Capsule\n");
#endif #endif
cur = PyxmlNode_Get(node); cur = PyxmlNode_Get(node);
} else if ((PyObject_HasAttrString(node, (char *) "_o")) && } else if ((PyObject_HasAttrString(node, (char *) "_o")) &&