1
0
mirror of https://gitlab.gnome.org/GNOME/libxslt synced 2025-07-29 15:41:13 +03:00

Finish and clean up Python 3 support

- Handle Python 3 types similar to libxml2
- Copy new versions of libxml_xmlXPathDestructNsNode and
  libxml_xmlXPathObjectPtrConvert from libxml2
- Fix compiler warnings
- Fix whitespace
- Remove unneeded imports from __future__
- Remove test in extelem.py (StringIO can't be converted to FILE under
  Python 3)
- string.lower() works in both Python 2 and 3

Closes #25.
This commit is contained in:
Nick Wellnhofer
2020-11-19 16:08:03 +01:00
parent a2db8da1ac
commit b3076bccdb
10 changed files with 230 additions and 284 deletions

View File

@ -204,7 +204,7 @@ dnl
if test "${NEED_TRIO}" = "1" ; then if test "${NEED_TRIO}" = "1" ; then
echo Reusing trio library for string functions echo Reusing trio library for string functions
WITH_TRIO=1 WITH_TRIO=1
else else
WITH_TRIO=0 WITH_TRIO=0
fi fi
AC_SUBST(WITH_TRIO) AC_SUBST(WITH_TRIO)
@ -405,7 +405,7 @@ AC_ARG_WITH(debug, [ --with-debug Add the debugging code (on)])
if test "$with_debug" = "no" ; then if test "$with_debug" = "no" ; then
echo Disabling debug support echo Disabling debug support
WITH_XSLT_DEBUG=0 WITH_XSLT_DEBUG=0
else else
WITH_XSLT_DEBUG=1 WITH_XSLT_DEBUG=1
fi fi
AC_SUBST(WITH_XSLT_DEBUG) AC_SUBST(WITH_XSLT_DEBUG)
@ -414,12 +414,12 @@ AC_ARG_WITH(mem_debug, [ --with-mem-debug Add the memory debugging modul
if test "$with_mem_debug" = "yes" ; then if test "$with_mem_debug" = "yes" ; then
echo Enabling memory debug support echo Enabling memory debug support
WITH_MEM_DEBUG=1 WITH_MEM_DEBUG=1
else else
WITH_MEM_DEBUG=0 WITH_MEM_DEBUG=0
fi fi
AC_SUBST(WITH_MEM_DEBUG) AC_SUBST(WITH_MEM_DEBUG)
dnl dnl
dnl Is debugger support requested dnl Is debugger support requested
dnl dnl
AC_ARG_WITH(debugger, [ --with-debugger Add the debugging support (on)]) AC_ARG_WITH(debugger, [ --with-debugger Add the debugging support (on)])
@ -463,7 +463,7 @@ AC_ARG_WITH(libxml-prefix,
[ --with-libxml-prefix=[PFX] Specify location of libxml config], [ --with-libxml-prefix=[PFX] Specify location of libxml config],
LIBXML_CONFIG_PREFIX=$withval LIBXML_CONFIG_PREFIX=$withval
) )
AC_ARG_WITH(libxml-include-prefix, AC_ARG_WITH(libxml-include-prefix,
[ --with-libxml-include-prefix=[PFX] Specify location of libxml headers], [ --with-libxml-include-prefix=[PFX] Specify location of libxml headers],
LIBXML_CFLAGS="-I$withval" LIBXML_CFLAGS="-I$withval"
@ -493,7 +493,7 @@ else
fi fi
dnl dnl
dnl imported from libxml2, c.f. #77827 dnl imported from libxml2, c.f. #77827
dnl dnl
if test "${GCC}" != "yes" ; then if test "${GCC}" != "yes" ; then
case "${host}" in case "${host}" in

View File

@ -2,15 +2,11 @@
# #
# generate python wrappers from the XML API description # generate python wrappers from the XML API description
# #
from __future__ import print_function
functions = {} functions = {}
enums = {} # { enumType: { enumConstant: enumValue } } enums = {} # { enumType: { enumConstant: enumValue } }
import sys
if sys.version_info < (3, 0): import string
import string as re_str
else:
re_str=str
####################################################################### #######################################################################
# #
@ -461,7 +457,7 @@ def buildStubs():
failed, skipped)) failed, skipped))
print("Missing type converters:") print("Missing type converters:")
for type in list(unknown_types.keys()): for type in list(unknown_types.keys()):
print("%s:%d " % (type, len(unknown_types[type])), end=' ') print("%s:%d " % (type, len(unknown_types[type])))
print() print()
####################################################################### #######################################################################
@ -537,55 +533,55 @@ def nameFixup(name, classe, type, file):
l = len(classe) l = len(classe)
if name[0:l] == listname: if name[0:l] == listname:
func = name[l:] func = name[l:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:12] == "xmlParserGet" and file == "python_accessor": elif name[0:12] == "xmlParserGet" and file == "python_accessor":
func = name[12:] func = name[12:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:12] == "xmlParserSet" and file == "python_accessor": elif name[0:12] == "xmlParserSet" and file == "python_accessor":
func = name[12:] func = name[12:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:10] == "xmlNodeGet" and file == "python_accessor": elif name[0:10] == "xmlNodeGet" and file == "python_accessor":
func = name[10:] func = name[10:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:18] == "xsltXPathParserGet" and file == "python_accessor": elif name[0:18] == "xsltXPathParserGet" and file == "python_accessor":
func = name[18:] func = name[18:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:12] == "xsltXPathGet" and file == "python_accessor": elif name[0:12] == "xsltXPathGet" and file == "python_accessor":
func = name[12:] func = name[12:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:16] == "xsltTransformGet" and file == "python_accessor": elif name[0:16] == "xsltTransformGet" and file == "python_accessor":
func = name[16:] func = name[16:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:16] == "xsltTransformSet" and file == "python_accessor": elif name[0:16] == "xsltTransformSet" and file == "python_accessor":
func = name[13:] func = name[13:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:17] == "xsltStylesheetGet" and file == "python_accessor": elif name[0:17] == "xsltStylesheetGet" and file == "python_accessor":
func = name[17:] func = name[17:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:17] == "xsltStylesheetSet" and file == "python_accessor": elif name[0:17] == "xsltStylesheetSet" and file == "python_accessor":
func = name[14:] func = name[14:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:l] == classe: elif name[0:l] == classe:
func = name[l:] func = name[l:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:7] == "libxml_": elif name[0:7] == "libxml_":
func = name[7:] func = name[7:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:8] == "libxslt_": elif name[0:8] == "libxslt_":
func = name[8:] func = name[8:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:6] == "xmlGet": elif name[0:6] == "xmlGet":
func = name[6:] func = name[6:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:3] == "xml": elif name[0:3] == "xml":
func = name[3:] func = name[3:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:7] == "xsltGet": elif name[0:7] == "xsltGet":
func = name[7:] func = name[7:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
elif name[0:4] == "xslt": elif name[0:4] == "xslt":
func = name[4:] func = name[4:]
func = re_str.lower(func[0:1]) + func[1:] func = func[0:1].lower() + func[1:]
else: else:
func = name func = name
if func[0:5] == "xPath": if func[0:5] == "xPath":

View File

@ -15,15 +15,24 @@
#include <libxml/xinclude.h> #include <libxml/xinclude.h>
#include <libxml/xpointer.h> #include <libxml/xpointer.h>
PyObject* PY_IMPORT_INT(long ival); /*
PyObject* PY_IMPORT_STRING(const char *u); * for older versions of Python, we don't use PyBytes, but keep PyString
PyObject* PY_IMPORT_CPTRD(void *po, const char *na, void *de); * and don't use Capsule but CObjects
int PY_IMPORT_LONG_CHECK(PyObject *o); */
int PY_IMPORT_STRING_CHECK(PyObject *o); #if PY_VERSION_HEX < 0x02070000
int PY_IMPORT_CPTR_CHECK(PyObject *o); #ifndef PyBytes_Check
Py_ssize_t PY_IMPORT_STRING_GET_SIZE(PyObject *o); #define PyBytes_Check PyString_Check
char* PY_IMPORT_AS_STRING(PyObject *o); #define PyBytes_Size PyString_Size
long PY_IMPORT_AS_LONG(PyObject *io); #define PyBytes_AsString PyString_AsString
#define PyBytes_AS_STRING PyString_AS_STRING
#define PyBytes_GET_SIZE PyString_GET_SIZE
#endif
#ifndef PyCapsule_New
#define PyCapsule_New PyCObject_FromVoidPtrAndDesc
#define PyCapsule_CheckExact PyCObject_Check
#define PyCapsule_GetPointer(o, n) PyCObject_GetDesc((o))
#endif
#endif
#define PyxmlNode_Get(v) (((v) == Py_None) ? NULL : \ #define PyxmlNode_Get(v) (((v) == Py_None) ? NULL : \
(((PyxmlNode_Object *)(v))->obj)) (((PyxmlNode_Object *)(v))->obj))
@ -65,8 +74,16 @@ typedef struct {
xmlCatalogPtr obj; xmlCatalogPtr obj;
} Pycatalog_Object; } Pycatalog_Object;
#if PY_MAJOR_VERSION >= 3
FILE *libxml_PyFileGet(PyObject *f);
void libxml_PyFileRelease(FILE *f);
#define PyFile_Get(v) (((v) == Py_None) ? NULL : libxml_PyFileGet(v))
#define PyFile_Release(f) libxml_PyFileRelease(f)
#else
#define PyFile_Get(v) (((v) == Py_None) ? NULL : \ #define PyFile_Get(v) (((v) == Py_None) ? NULL : \
(PyFile_Check(v) ? (PyFile_AsFile(v)) : stdout)) (PyFile_Check(v) ? (PyFile_AsFile(v)) : stdout))
#define PyFile_Release(f)
#endif
PyObject * libxml_intWrap(int val); PyObject * libxml_intWrap(int val);
PyObject * libxml_longWrap(long val); PyObject * libxml_longWrap(long val);

View File

@ -28,7 +28,6 @@
/* snprintf emulation taken from http://stackoverflow.com/a/8712996/1956010 */ /* snprintf emulation taken from http://stackoverflow.com/a/8712996/1956010 */
#if _MSC_VER < 1900 #if _MSC_VER < 1900
#include <stdarg.h> #include <stdarg.h>
#define vsnprintf c99_vsnprintf #define vsnprintf c99_vsnprintf
@ -78,8 +77,8 @@ libxslt_xsltStylesheetPtrWrap(xsltStylesheetPtr style) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return(Py_None); return(Py_None);
} }
ret = PY_IMPORT_CPTRD((void *) style, ret = PyCapsule_New((void *) style, (char *)"xsltStylesheetPtr", NULL);
(char *)"xsltStylesheetPtr", NULL);
return(ret); return(ret);
} }
@ -94,9 +93,8 @@ libxslt_xsltTransformContextPtrWrap(xsltTransformContextPtr ctxt) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return(Py_None); return(Py_None);
} }
ret = PY_IMPORT_CPTRD((void *) ctxt, ret = PyCapsule_New((void *) ctxt, (char *)"xsltTransformContextPtr",
(char *)"xsltTransformContextPtr", NULL); NULL);
return(ret); return(ret);
} }
@ -111,8 +109,7 @@ libxslt_xsltElemPreCompPtrWrap(xsltElemPreCompPtr ctxt) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
return(Py_None); return(Py_None);
} }
ret = PY_IMPORT_CPTRD((void *) ctxt, ret = PyCapsule_New((void *) ctxt, (char *)"xsltElemPreCompPtr", NULL);
(char *)"xsltElemPreCompPtr", NULL);
return(ret); return(ret);
} }
@ -129,7 +126,8 @@ libxslt_xsltGetTransformContextHashCode(PyObject *self ATTRIBUTE_UNUSED, PyObjec
tctxt = (xsltTransformContextPtr) PytransformCtxt_Get(py_tctxt); tctxt = (xsltTransformContextPtr) PytransformCtxt_Get(py_tctxt);
hash_code = (ptrdiff_t) tctxt; hash_code = (ptrdiff_t) tctxt;
ret = PY_IMPORT_INT(hash_code);
ret = PyLong_FromLong(hash_code);
return ret; return ret;
} }
@ -166,8 +164,7 @@ libxslt_xsltGetStylesheetHashCode(PyObject *self ATTRIBUTE_UNUSED, PyObject *arg
style = (xsltStylesheetPtr) Pystylesheet_Get(py_style); style = (xsltStylesheetPtr) Pystylesheet_Get(py_style);
hash_code = (ptrdiff_t) style; hash_code = (ptrdiff_t) style;
ret = PY_IMPORT_INT(hash_code); ret = PyLong_FromLong(hash_code);
return ret; return ret;
} }
@ -621,8 +618,7 @@ libxslt_xsltSetLoaderFunc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
pythonDocLoaderObject = loader; pythonDocLoaderObject = loader;
xsltSetLoaderFunc(pythonDocLoaderFuncWrapper); xsltSetLoaderFunc(pythonDocLoaderFuncWrapper);
py_retval = PY_IMPORT_INT(0); py_retval = PyLong_FromLong(0);
return(py_retval); return(py_retval);
} }
@ -713,11 +709,13 @@ libxslt_xsltApplyStylesheetUser(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
j = 0; j = 0;
while (PyDict_Next(pyobj_params, &ppos, &name, &value)) { while (PyDict_Next(pyobj_params, &ppos, &name, &value)) {
const char *tmp; const char *tmp;
int size;
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
tmp = PyUnicode_AsUTF8AndSize(name, &size); tmp = PyUnicode_AsUTF8AndSize(name, &size);
#else #else
int size;
tmp = PyString_AS_STRING(name); tmp = PyString_AS_STRING(name);
size = PyString_GET_SIZE(name); size = PyString_GET_SIZE(name);
@ -800,11 +798,13 @@ libxslt_xsltApplyStylesheet(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
j = 0; j = 0;
while (PyDict_Next(pyobj_params, &ppos, &name, &value)) { while (PyDict_Next(pyobj_params, &ppos, &name, &value)) {
const char *tmp; const char *tmp;
int size;
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
tmp = PyUnicode_AsUTF8AndSize(name, &size); tmp = PyUnicode_AsUTF8AndSize(name, &size);
#else #else
int size;
tmp = PyString_AS_STRING(name); tmp = PyString_AS_STRING(name);
size = PyString_GET_SIZE(name); size = PyString_GET_SIZE(name);
#endif #endif
@ -1236,6 +1236,8 @@ extern void initlibxml2mod(void);
#endif #endif
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
#define INITERROR return NULL
static struct PyModuleDef moduledef = { static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
"libxsltmod", "libxsltmod",
@ -1247,6 +1249,8 @@ static struct PyModuleDef moduledef = {
NULL, NULL,
NULL NULL
}; };
#else
#define INITERROR return
#endif #endif
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
@ -1254,7 +1258,6 @@ PyObject* PyInit_libxsltmod(void){
#else #else
void initlibxsltmod(void) { void initlibxsltmod(void) {
#endif #endif
static int initialized = 0;
PyObject *m; PyObject *m;
#ifdef MERGED_MODULES #ifdef MERGED_MODULES
@ -1265,16 +1268,13 @@ void initlibxsltmod(void) {
#endif #endif
#endif #endif
if (initialized != 0)
return;
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&moduledef); m = PyModule_Create(&moduledef);
#else #else
m = Py_InitModule((char *)"libxsltmod", libxsltMethods); m = Py_InitModule((char *)"libxsltmod", libxsltMethods);
#endif #endif
if (!m) if (!m)
return; INITERROR;
initialized = 1;
/* /*
* Specific XSLT initializations * Specific XSLT initializations
*/ */

View File

@ -1,6 +1,4 @@
#!/usr/bin/python -u #!/usr/bin/python -u
from __future__ import print_function
import sys import sys
import libxml2 import libxml2
# Memory debug specific # Memory debug specific

View File

@ -1,6 +1,4 @@
#!/usr/bin/python -u #!/usr/bin/python -u
from __future__ import print_function
import sys import sys
import libxml2 import libxml2
# Memory debug specific # Memory debug specific

View File

@ -1,15 +1,6 @@
#!/usr/bin/python -u #!/usr/bin/python -u
from __future__ import print_function
import sys import sys
import string import string
import sys
if sys.version_info < (3, 0):
import StringIO as io
else:
import io
import libxml2 import libxml2
# Memory debug specific # Memory debug specific
libxml2.debugMemory(1) libxml2.debugMemory(1)
@ -42,7 +33,7 @@ def transform_test(ctx, node, inst, comp):
pass pass
tctxt.insertNode().addContent('SUCCESS') tctxt.insertNode().addContent('SUCCESS')
styledoc = libxml2.parseDoc(""" styledoc = libxml2.parseDoc("""
@ -65,14 +56,6 @@ result = style.applyStylesheet(doc, None)
style.freeStylesheet() style.freeStylesheet()
doc.freeDoc() doc.freeDoc()
extensions = io.StringIO()
libxslt.debugDumpExtensions(extensions)
if 0 and extensions.buf.find(EXT_URL) < 0:
print("Element extension not registered (or dumping broken)")
sys.exit(1)
root = result.children root = result.children
if root.name != "article": if root.name != "article":

View File

@ -1,6 +1,4 @@
#!/usr/bin/python -u #!/usr/bin/python -u
from __future__ import print_function
import sys import sys
import string import string
import libxml2 import libxml2
@ -24,7 +22,7 @@ def f(ctx, str):
except: except:
pass pass
return string.upper(str) return str.upper()
libxslt.registerExtModuleFunction("foo", "http://example.com/foo", f) libxslt.registerExtModuleFunction("foo", "http://example.com/foo", f)

View File

@ -4,8 +4,6 @@
# bindings, not complete yet and shows up the things missing # bindings, not complete yet and shows up the things missing
# from the existing python interfaces # from the existing python interfaces
# #
from __future__ import print_function
import sys import sys
import time import time
import os import os
@ -150,7 +148,7 @@ def main(args = None):
args = sys.argv[1:] args = sys.argv[1:]
if len(args) <= 0: if len(args) <= 0:
usage(sys.argv[0]) usage(sys.argv[0])
i = 0 i = 0
while i < len(args): while i < len(args):
@ -211,12 +209,12 @@ def main(args = None):
print("Unknown option %s" % (args[i])) print("Unknown option %s" % (args[i]))
usage() usage()
return(3) return(3)
i = i + 1 i = i + 1
libxml2.lineNumbersDefault(1) libxml2.lineNumbersDefault(1)
libxml2.substituteEntitiesDefault(1) libxml2.substituteEntitiesDefault(1)
# TODO: xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS # TODO: xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS

View File

@ -13,101 +13,80 @@ xmlParserInputPtr xmlNoNetExternalEntityLoader(const char *URL,
#include "libxml_wrap.h" #include "libxml_wrap.h"
#include <libxml/xpathInternals.h> #include <libxml/xpathInternals.h>
/* Helper functions */ #if PY_MAJOR_VERSION >= 3
#define PY_IMPORT_STRING_SIZE PyUnicode_FromStringAndSize
#define PY_IMPORT_STRING PyUnicode_FromString
#define PY_IMPORT_INT PyLong_FromLong
#else
#define PY_IMPORT_STRING_SIZE PyString_FromStringAndSize
#define PY_IMPORT_STRING PyString_FromString
#define PY_IMPORT_INT PyInt_FromLong
#endif
PyObject* PY_IMPORT_INT(long ival){ #if PY_MAJOR_VERSION >= 3
PyObject *ret; #include <stdio.h>
#if PY_MAJOR_VERSION >= 3 #include <unistd.h>
ret=PyLong_FromLong(ival); #include <fcntl.h>
#else
ret=PyInt_FromLong(ival); FILE *
#endif libxml_PyFileGet(PyObject *f) {
return ret; int fd, flags;
FILE *res;
const char *mode;
fd = PyObject_AsFileDescriptor(f);
/*
* Get the flags on the fd to understand how it was opened
*/
flags = fcntl(fd, F_GETFL, 0);
switch (flags & O_ACCMODE) {
case O_RDWR:
if (flags & O_APPEND)
mode = "a+";
else
mode = "rw";
break;
case O_RDONLY:
if (flags & O_APPEND)
mode = "r+";
else
mode = "r";
break;
case O_WRONLY:
if (flags & O_APPEND)
mode = "a";
else
mode = "w";
break;
default:
return(NULL);
}
/*
* the FILE struct gets a new fd, so that it can be closed
* independently of the file descriptor given. The risk though is
* lack of sync. So at the python level sync must be implemented
* before and after a conversion took place. No way around it
* in the Python3 infrastructure !
* The duplicated fd and FILE * will be released in the subsequent
* call to libxml_PyFileRelease() which must be generated accordingly
*/
fd = dup(fd);
if (fd == -1)
return(NULL);
res = fdopen(fd, mode);
if (res == NULL) {
close(fd);
return(NULL);
}
return(res);
} }
PyObject* PY_IMPORT_STRING(const char *u){ void libxml_PyFileRelease(FILE *f) {
PyObject *ret; if (f != NULL)
#if PY_MAJOR_VERSION >= 3 fclose(f);
ret=PyUnicode_FromString(u);
#else
ret=PyString_FromString(u);
#endif
return ret;
} }
#endif
PyObject* PY_IMPORT_CPTRD(void *po,
const char *na,
void *de){
PyObject *ret;
#if PY_MAJOR_VERSION >= 3
ret=PyCapsule_New(po,na,de);
#else
ret=PyCObject_FromVoidPtrAndDesc(po,(void *)na,de);
#endif
return ret;
}
int PY_IMPORT_LONG_CHECK(PyObject *o){
int ret;
#if PY_MAJOR_VERSION >= 3
ret=PyLong_Check(o);
#else
ret=PyInt_Check(o);
#endif
return ret;
}
int PY_IMPORT_STRING_CHECK(PyObject *o){
int ret;
#if PY_MAJOR_VERSION >= 3
ret=PyUnicode_Check(o);
#else
ret=PyString_Check(o);
#endif
return ret;
}
int PY_IMPORT_CPTR_CHECK(PyObject *o){
int ret;
#if PY_MAJOR_VERSION >= 3
ret=PyCapsule_CheckExact(o);
#else
ret=PyCObject_Check(o);
#endif
return ret;
}
Py_ssize_t PY_IMPORT_STRING_GET_SIZE(PyObject *o){
Py_ssize_t ret;
#if PY_MAJOR_VERSION >= 3
ret=PyUnicode_GET_SIZE(o);
#else
ret=PyString_GET_SIZE(o);
#endif
return ret;
}
char* PY_IMPORT_AS_STRING(PyObject *o){
char *ret;
#if PY_MAJOR_VERSION >= 3
ret=PyUnicode_AS_DATA(o);
#else
ret=PyString_AS_STRING(o);
#endif
return ret;
}
long PY_IMPORT_AS_LONG(PyObject *io){
long ret;
#if PY_MAJOR_VERSION >= 3
ret=PyLong_AS_LONG(io);
#else
ret=PyInt_AS_LONG(io);
#endif
return ret;
}
/* */
PyObject * PyObject *
libxml_intWrap(int val) libxml_intWrap(int val)
@ -129,9 +108,7 @@ libxml_longWrap(long val)
#ifdef DEBUG #ifdef DEBUG
printf("libxml_longWrap: val = %ld\n", val); printf("libxml_longWrap: val = %ld\n", val);
#endif #endif
ret = PY_IMPORT_INT(val); ret = PY_IMPORT_INT(val);
return (ret); return (ret);
} }
@ -177,6 +154,7 @@ libxml_charPtrConstWrap(const char *str)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
/* TODO: look at deallocation */
ret = PY_IMPORT_STRING(str); ret = PY_IMPORT_STRING(str);
return (ret); return (ret);
} }
@ -229,7 +207,7 @@ libxml_constcharPtrWrap(const char *str)
return (Py_None); return (Py_None);
} }
/* TODO: look at deallocation */ /* TODO: look at deallocation */
ret = PY_IMPORT_STRING((char *) str); ret = PY_IMPORT_STRING(str);
return (ret); return (ret);
} }
@ -263,8 +241,7 @@ libxml_xmlDocPtrWrap(xmlDocPtr doc)
return (Py_None); return (Py_None);
} }
/* TODO: look at deallocation */ /* TODO: look at deallocation */
ret = PY_IMPORT_CPTRD((void *) doc, (char *) "xmlDocPtr", ret = PyCapsule_New((void *) doc, (char *) "xmlDocPtr", NULL);
NULL);
return (ret); return (ret);
} }
@ -280,10 +257,7 @@ libxml_xmlNodePtrWrap(xmlNodePtr node)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) node, (char *) "xmlNodePtr", NULL);
PY_IMPORT_CPTRD((void *) node, (char *) "xmlNodePtr",
NULL);
return (ret); return (ret);
} }
@ -299,10 +273,7 @@ libxml_xmlURIPtrWrap(xmlURIPtr uri)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) uri, (char *) "xmlURIPtr", NULL);
PY_IMPORT_CPTRD((void *) uri, (char *) "xmlURIPtr",
NULL);
return (ret); return (ret);
} }
@ -318,9 +289,7 @@ libxml_xmlNsPtrWrap(xmlNsPtr ns)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) ns, (char *) "xmlNsPtr", NULL);
PY_IMPORT_CPTRD((void *) ns, (char *) "xmlNsPtr",
NULL);
return (ret); return (ret);
} }
@ -336,10 +305,7 @@ libxml_xmlAttrPtrWrap(xmlAttrPtr attr)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) attr, (char *) "xmlAttrPtr", NULL);
PY_IMPORT_CPTRD((void *) attr, (char *) "xmlAttrPtr",
NULL);
return (ret); return (ret);
} }
@ -355,10 +321,7 @@ libxml_xmlAttributePtrWrap(xmlAttributePtr attr)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) attr, (char *) "xmlAttributePtr", NULL);
PY_IMPORT_CPTRD((void *) attr,
(char *) "xmlAttributePtr", NULL);
return (ret); return (ret);
} }
@ -374,9 +337,7 @@ libxml_xmlElementPtrWrap(xmlElementPtr elem)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) elem, (char *) "xmlElementPtr", NULL);
PY_IMPORT_CPTRD((void *) elem,
(char *) "xmlElementPtr", NULL);
return (ret); return (ret);
} }
@ -392,11 +353,7 @@ libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = PyCapsule_New((void *) ctxt, (char *) "xmlXPathContextPtr", NULL);
ret =
PY_IMPORT_CPTRD((void *) ctxt,
(char *) "xmlXPathContextPtr", NULL);
return (ret); return (ret);
} }
@ -412,9 +369,8 @@ libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = PY_IMPORT_CPTRD((void *) ctxt, ret = PyCapsule_New((void *) ctxt, (char *) "xmlXPathParserContextPtr",
(char *) "xmlXPathParserContextPtr", NULL);
NULL);
return (ret); return (ret);
} }
@ -430,9 +386,7 @@ libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) ctxt, (char *) "xmlParserCtxtPtr", NULL);
PY_IMPORT_CPTRD((void *) ctxt,
(char *) "xmlParserCtxtPtr", NULL);
return (ret); return (ret);
} }
@ -446,12 +400,22 @@ libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
* 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(void *cobj, void *desc ATTRIBUTE_UNUSED) { libxml_xmlXPathDestructNsNode(void *cap, void *desc ATTRIBUTE_UNUSED)
#else
static void
libxml_xmlXPathDestructNsNode(PyObject *cap)
#endif
{
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cobj); fprintf(stderr, "libxml_xmlXPathDestructNsNode called %p\n", cobj);
#endif #endif
xmlXPathNodeSetFreeNs((xmlNsPtr) cobj); #if PY_VERSION_HEX < 0x02070000
xmlXPathNodeSetFreeNs((xmlNsPtr) cap);
#else
xmlXPathNodeSetFreeNs((xmlNsPtr) PyCapsule_GetPointer(cap, "xmlNsPtr"));
#endif
} }
PyObject * PyObject *
@ -506,9 +470,9 @@ libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
node = obj->nodesetval->nodeTab[i]; node = obj->nodesetval->nodeTab[i];
if (node->type == XML_NAMESPACE_DECL) { if (node->type == XML_NAMESPACE_DECL) {
PyObject *ns = PyObject *ns =
PY_IMPORT_CPTRD((void *) node, PyCapsule_New((void *) node,
(char *) "xmlNsPtr", (char *) "xmlNsPtr",
libxml_xmlXPathDestructNsNode); libxml_xmlXPathDestructNsNode);
PyList_SetItem(ret, i, ns); PyList_SetItem(ret, i, ns);
/* make sure the xmlNsPtr is not destroyed now */ /* make sure the xmlNsPtr is not destroyed now */
obj->nodesetval->nodeTab[i] = NULL; obj->nodesetval->nodeTab[i] = NULL;
@ -542,7 +506,7 @@ libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
} }
xmlXPathObjectPtr xmlXPathObjectPtr
libxml_xmlXPathObjectPtrConvert(PyObject * obj) libxml_xmlXPathObjectPtrConvert(PyObject *obj)
{ {
xmlXPathObjectPtr ret = NULL; xmlXPathObjectPtr ret = NULL;
@ -552,11 +516,15 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj)
if (obj == NULL) { if (obj == NULL) {
return (NULL); return (NULL);
} }
if PyFloat_Check if (PyFloat_Check (obj)) {
(obj) {
ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj)); ret = xmlXPathNewFloat((double) PyFloat_AS_DOUBLE(obj));
} else if (PY_IMPORT_LONG_CHECK (obj)) { } else if (PyLong_Check(obj)) {
ret = xmlXPathNewFloat((double) PY_IMPORT_AS_LONG(obj)); #ifdef PyLong_AS_LONG
ret = xmlXPathNewFloat((double) PyLong_AS_LONG(obj));
#else
ret = xmlXPathNewFloat((double) PyInt_AS_LONG(obj));
#endif
#ifdef PyBool_Check
} else if (PyBool_Check (obj)) { } else if (PyBool_Check (obj)) {
if (obj == Py_True) { if (obj == Py_True) {
@ -565,13 +533,38 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj)
else { else {
ret = xmlXPathNewBoolean(0); ret = xmlXPathNewBoolean(0);
} }
} else if (PY_IMPORT_STRING_CHECK(obj)) { #endif
} else if (PyBytes_Check (obj)) {
xmlChar *str; xmlChar *str;
str = xmlStrndup((const xmlChar *) PY_IMPORT_AS_STRING(obj), str = xmlStrndup((const xmlChar *) PyBytes_AS_STRING(obj),
PY_IMPORT_STRING_GET_SIZE(obj)); PyBytes_GET_SIZE(obj));
ret = xmlXPathWrapString(str); ret = xmlXPathWrapString(str);
} else if (PyList_Check(obj)) { #ifdef PyUnicode_Check
} else if (PyUnicode_Check (obj)) {
#if PY_VERSION_HEX >= 0x03030000
xmlChar *str;
const char *tmp;
Py_ssize_t size;
/* tmp doesn't need to be deallocated */
tmp = PyUnicode_AsUTF8AndSize(obj, &size);
str = xmlStrndup((const xmlChar *) tmp, (int) size);
ret = xmlXPathWrapString(str);
#else
xmlChar *str = NULL;
PyObject *b;
b = PyUnicode_AsUTF8String(obj);
if (b != NULL) {
str = xmlStrndup((const xmlChar *) PyBytes_AS_STRING(b),
PyBytes_GET_SIZE(b));
Py_DECREF(b);
}
ret = xmlXPathWrapString(str);
#endif
#endif
} else if (PyList_Check (obj)) {
int i; int i;
PyObject *node; PyObject *node;
xmlNodePtr cur; xmlNodePtr cur;
@ -585,43 +578,19 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj)
continue; continue;
cur = NULL; cur = NULL;
if (PY_IMPORT_CPTR_CHECK(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")) &&
#if PY_MAJOR_VERSION >= 3 (PyObject_HasAttrString(node, (char *) "get_doc"))) {
else if ((PyObject_HasAttrString(node, (char *) "_o")) && PyObject *wrapper;
(PyObject_HasAttrString(node, (char *) "get_doc"))) {
PyObject *wrapper;
wrapper = PyObject_GetAttrString(node, (char *) "_o"); wrapper = PyObject_GetAttrString(node, (char *) "_o");
if (wrapper != NULL) if (wrapper != NULL)
cur = PyxmlNode_Get(wrapper); cur = PyxmlNode_Get(wrapper);
} } else {
#else
else if (PyInstance_Check(node)) {
PyInstanceObject *inst = (PyInstanceObject *) node;
PyObject *name = inst->in_class->cl_name;
if PyString_Check
(name) {
char *type = PyString_AS_STRING(name);
PyObject *wrapper;
if (!strcmp(type, "xmlNode")) {
wrapper =
PyObject_GetAttrString(node, (char *) "_o");
if (wrapper != NULL) {
cur = PyxmlNode_Get(wrapper);
}
}
}
}
#endif
else {
#ifdef DEBUG #ifdef DEBUG
printf("Unknown object in Python return list\n"); printf("Unknown object in Python return list\n");
#endif #endif
@ -636,7 +605,6 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj)
printf("Unable to convert Python Object to XPath"); printf("Unable to convert Python Object to XPath");
#endif #endif
} }
Py_DECREF(obj);
return (ret); return (ret);
} }
@ -652,9 +620,7 @@ libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) catal, (char *) "xmlCatalogPtr", NULL);
PY_IMPORT_CPTRD((void *) catal,
(char *) "xmlCatalogPtr", NULL);
return (ret); return (ret);
} }
@ -670,10 +636,7 @@ libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) buffer, (char *) "xmlOutputBufferPtr", NULL);
PY_IMPORT_CPTRD((void *) buffer,
(char *) "xmlOutputBufferPtr", NULL);
return (ret); return (ret);
} }
@ -689,10 +652,8 @@ libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) buffer, (char *) "xmlParserInputBufferPtr",
PY_IMPORT_CPTRD((void *) buffer, NULL);
(char *) "xmlParserInputBufferPtr", NULL);
return (ret); return (ret);
} }
@ -708,9 +669,6 @@ libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return (Py_None); return (Py_None);
} }
ret = ret = PyCapsule_New((void *) regexp, (char *) "xmlRegexpPtr", NULL);
PY_IMPORT_CPTRD((void *) regexp,
(char *) "xmlRegexpPtr", NULL);
return (ret); return (ret);
} }