diff --git a/python/libxml.py b/python/libxml.py index 117de824..e507e0fa 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 70248d76..a9b97390 100644 --- a/python/libxml_wrap.h +++ b/python/libxml_wrap.h @@ -23,6 +23,25 @@ #include #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 f8d08d5f..495ab62f 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 a24ee11a..c88e3702 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 d7064eb5..de2dcd6f 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 5c6f06d4..47f0557c 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 d2196fad..33ab61c0 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 c865b65f..31c909a1 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) { -#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")) &&