mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-07-29 11:41:22 +03:00
Fix compilation on Python3
while still compiling on recent Python2: - change the handling of files, tweak the generator, get the fd instead of the FILE *, dup it and fdopen based on mode, add a Release function on Python3 and call to flush from the generated python stubs - switch to using Capsules instead of CObjects - fix PyString to PyBytes - fix PyInt to PyLong - tweak the module registration to compile on both versions - drop PyInstance check for passed xmlNodes and instead check attributes presence Daniel
This commit is contained in:
@ -133,8 +133,8 @@ class docParser(xml.sax.handler.ContentHandler):
|
|||||||
str = str + c
|
str = str + c
|
||||||
if self.in_function == 1:
|
if self.in_function == 1:
|
||||||
self.function_cond = str
|
self.function_cond = str
|
||||||
|
|
||||||
|
|
||||||
def function(name, desc, ret, args, file, cond):
|
def function(name, desc, ret, args, file, cond):
|
||||||
functions[name] = (desc, ret, args, file, cond)
|
functions[name] = (desc, ret, args, file, cond)
|
||||||
|
|
||||||
@ -370,6 +370,7 @@ def print_function_wrapper(name, output, export, include):
|
|||||||
c_args=""
|
c_args=""
|
||||||
c_return=""
|
c_return=""
|
||||||
c_convert=""
|
c_convert=""
|
||||||
|
c_release=""
|
||||||
num_bufs=0
|
num_bufs=0
|
||||||
for arg in args:
|
for arg in args:
|
||||||
# This should be correct
|
# This should be correct
|
||||||
@ -379,7 +380,7 @@ def print_function_wrapper(name, output, export, include):
|
|||||||
if arg[1] in py_types:
|
if arg[1] in py_types:
|
||||||
(f, t, n, c) = py_types[arg[1]]
|
(f, t, n, c) = py_types[arg[1]]
|
||||||
if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
|
if (f == 'z') and (name in foreign_encoding_args) and (num_bufs == 0):
|
||||||
f = 't#'
|
f = 's#'
|
||||||
if f != None:
|
if f != None:
|
||||||
format = format + f
|
format = format + f
|
||||||
if t != None:
|
if t != None:
|
||||||
@ -390,13 +391,16 @@ def print_function_wrapper(name, output, export, include):
|
|||||||
arg[1], t, arg[0])
|
arg[1], t, arg[0])
|
||||||
else:
|
else:
|
||||||
format_args = format_args + ", &%s" % (arg[0])
|
format_args = format_args + ", &%s" % (arg[0])
|
||||||
if f == 't#':
|
if f == 's#':
|
||||||
format_args = format_args + ", &py_buffsize%d" % num_bufs
|
format_args = format_args + ", &py_buffsize%d" % num_bufs
|
||||||
c_args = c_args + " int py_buffsize%d;\n" % num_bufs
|
c_args = c_args + " int py_buffsize%d;\n" % num_bufs
|
||||||
num_bufs = num_bufs + 1
|
num_bufs = num_bufs + 1
|
||||||
if c_call != "":
|
if c_call != "":
|
||||||
c_call = c_call + ", "
|
c_call = c_call + ", "
|
||||||
c_call = c_call + "%s" % (arg[0])
|
c_call = c_call + "%s" % (arg[0])
|
||||||
|
if t == "File":
|
||||||
|
c_release = c_release + \
|
||||||
|
" PyFile_Release(%s);\n" % (arg[0])
|
||||||
else:
|
else:
|
||||||
if arg[1] in skipped_types:
|
if arg[1] in skipped_types:
|
||||||
return 0
|
return 0
|
||||||
@ -424,7 +428,7 @@ def print_function_wrapper(name, output, export, include):
|
|||||||
ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
|
ret_convert = " Py_INCREF(Py_None);\n return(Py_None);\n"
|
||||||
elif ret[0] in py_types:
|
elif ret[0] in py_types:
|
||||||
(f, t, n, c) = py_types[ret[0]]
|
(f, t, n, c) = py_types[ret[0]]
|
||||||
c_return = " %s c_retval;\n" % (ret[0])
|
c_return = c_return + " %s c_retval;\n" % (ret[0])
|
||||||
if file == "python_accessor" and ret[2] != None:
|
if file == "python_accessor" and ret[2] != None:
|
||||||
c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
|
c_call = "\n c_retval = %s->%s;\n" % (args[0][0], ret[2])
|
||||||
else:
|
else:
|
||||||
@ -433,7 +437,7 @@ def print_function_wrapper(name, output, export, include):
|
|||||||
ret_convert = ret_convert + " return(py_retval);\n"
|
ret_convert = ret_convert + " return(py_retval);\n"
|
||||||
elif ret[0] in py_return_types:
|
elif ret[0] in py_return_types:
|
||||||
(f, t, n, c) = py_return_types[ret[0]]
|
(f, t, n, c) = py_return_types[ret[0]]
|
||||||
c_return = " %s c_retval;\n" % (ret[0])
|
c_return = c_return + " %s c_retval;\n" % (ret[0])
|
||||||
c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
|
c_call = "\n c_retval = %s(%s);\n" % (name, c_call)
|
||||||
ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
|
ret_convert = " py_retval = libxml_%sWrap((%s) c_retval);\n" % (n,c)
|
||||||
ret_convert = ret_convert + " return(py_retval);\n"
|
ret_convert = ret_convert + " return(py_retval);\n"
|
||||||
@ -491,8 +495,10 @@ def print_function_wrapper(name, output, export, include):
|
|||||||
output.write(" return(NULL);\n")
|
output.write(" return(NULL);\n")
|
||||||
if c_convert != "":
|
if c_convert != "":
|
||||||
output.write(c_convert)
|
output.write(c_convert)
|
||||||
|
|
||||||
output.write(c_call)
|
output.write(c_call)
|
||||||
|
if c_release != "":
|
||||||
|
output.write(c_release)
|
||||||
output.write(ret_convert)
|
output.write(ret_convert)
|
||||||
output.write("}\n\n")
|
output.write("}\n\n")
|
||||||
if cond != None and cond != "":
|
if cond != None and cond != "":
|
||||||
@ -959,6 +965,12 @@ def buildWrappers():
|
|||||||
(arg[0], arg[0]))
|
(arg[0], arg[0]))
|
||||||
classes.write(" else: %s__o = %s%s\n" %
|
classes.write(" else: %s__o = %s%s\n" %
|
||||||
(arg[0], arg[0], classes_type[arg[1]][0]))
|
(arg[0], arg[0], classes_type[arg[1]][0]))
|
||||||
|
if arg[1] in py_types:
|
||||||
|
(f, t, n, c) = py_types[arg[1]]
|
||||||
|
if t == "File":
|
||||||
|
classes.write(" if %s is not None: %s.flush()\n" % (
|
||||||
|
arg[0], arg[0]))
|
||||||
|
|
||||||
if ret[0] != "void":
|
if ret[0] != "void":
|
||||||
classes.write(" ret = ")
|
classes.write(" ret = ")
|
||||||
else:
|
else:
|
||||||
@ -973,6 +985,16 @@ def buildWrappers():
|
|||||||
classes.write("__o")
|
classes.write("__o")
|
||||||
n = n + 1
|
n = n + 1
|
||||||
classes.write(")\n")
|
classes.write(")\n")
|
||||||
|
|
||||||
|
# This may be needed to reposition the I/O, but likely to cause more harm
|
||||||
|
# than good. Those changes in Python3 really break the model.
|
||||||
|
# for arg in args:
|
||||||
|
# if arg[1] in py_types:
|
||||||
|
# (f, t, n, c) = py_types[arg[1]]
|
||||||
|
# if t == "File":
|
||||||
|
# classes.write(" if %s is not None: %s.seek(0,0)\n"%(
|
||||||
|
# arg[0], arg[0]))
|
||||||
|
|
||||||
if ret[0] != "void":
|
if ret[0] != "void":
|
||||||
if ret[0] in classes_type:
|
if ret[0] in classes_type:
|
||||||
#
|
#
|
||||||
|
193
python/libxml.c
193
python/libxml.c
@ -41,7 +41,17 @@
|
|||||||
/* #define DEBUG_FILES */
|
/* #define DEBUG_FILES */
|
||||||
/* #define DEBUG_LOADER */
|
/* #define DEBUG_LOADER */
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
PyObject *PyInit_libxml2mod(void);
|
||||||
|
|
||||||
|
#define PY_IMPORT_STRING_SIZE PyUnicode_FromStringAndSize
|
||||||
|
#define PY_IMPORT_STRING PyUnicode_FromString
|
||||||
|
#else
|
||||||
void initlibxml2mod(void);
|
void initlibxml2mod(void);
|
||||||
|
#define PY_IMPORT_STRING_SIZE PyString_FromStringAndSize
|
||||||
|
#define PY_IMPORT_STRING PyString_FromString
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO:
|
* TODO:
|
||||||
@ -280,18 +290,42 @@ xmlPythonFileReadRaw (void * context, char * buffer, int len) {
|
|||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
printf("xmlPythonFileReadRaw: result is NULL\n");
|
printf("xmlPythonFileReadRaw: result is NULL\n");
|
||||||
return(-1);
|
return(-1);
|
||||||
} else if (PyString_Check(ret)) {
|
} else if (PyBytes_Check(ret)) {
|
||||||
lenread = PyString_Size(ret);
|
lenread = PyBytes_Size(ret);
|
||||||
data = PyString_AsString(ret);
|
data = PyBytes_AsString(ret);
|
||||||
if (lenread > len)
|
#ifdef PyUnicode_Check
|
||||||
memcpy(buffer, data, len);
|
} else if PyUnicode_Check (ret) {
|
||||||
else
|
#if PY_VERSION_HEX >= 0x03030000
|
||||||
memcpy(buffer, data, lenread);
|
size_t size;
|
||||||
Py_DECREF(ret);
|
const char *tmp;
|
||||||
|
|
||||||
|
/* tmp doesn't need to be deallocated */
|
||||||
|
tmp = PyUnicode_AsUTF8AndSize(ret, &size);
|
||||||
|
|
||||||
|
lenread = (int) size;
|
||||||
|
data = (char *) tmp;
|
||||||
|
#else
|
||||||
|
PyObject *b;
|
||||||
|
b = PyUnicode_AsUTF8String(ret);
|
||||||
|
if (b == NULL) {
|
||||||
|
printf("xmlPythonFileReadRaw: failed to convert to UTF-8\n");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
lenread = PyBytes_Size(b);
|
||||||
|
data = PyBytes_AsString(b);
|
||||||
|
Py_DECREF(b);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
printf("xmlPythonFileReadRaw: result is not a String\n");
|
printf("xmlPythonFileReadRaw: result is not a String\n");
|
||||||
Py_DECREF(ret);
|
Py_DECREF(ret);
|
||||||
|
return(-1);
|
||||||
}
|
}
|
||||||
|
if (lenread > len)
|
||||||
|
memcpy(buffer, data, len);
|
||||||
|
else
|
||||||
|
memcpy(buffer, data, lenread);
|
||||||
|
Py_DECREF(ret);
|
||||||
return(lenread);
|
return(lenread);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -321,18 +355,42 @@ xmlPythonFileRead (void * context, char * buffer, int len) {
|
|||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
printf("xmlPythonFileRead: result is NULL\n");
|
printf("xmlPythonFileRead: result is NULL\n");
|
||||||
return(-1);
|
return(-1);
|
||||||
} else if (PyString_Check(ret)) {
|
} else if (PyBytes_Check(ret)) {
|
||||||
lenread = PyString_Size(ret);
|
lenread = PyBytes_Size(ret);
|
||||||
data = PyString_AsString(ret);
|
data = PyBytes_AsString(ret);
|
||||||
if (lenread > len)
|
#ifdef PyUnicode_Check
|
||||||
memcpy(buffer, data, len);
|
} else if PyUnicode_Check (ret) {
|
||||||
else
|
#if PY_VERSION_HEX >= 0x03030000
|
||||||
memcpy(buffer, data, lenread);
|
size_t size;
|
||||||
Py_DECREF(ret);
|
const char *tmp;
|
||||||
|
|
||||||
|
/* tmp doesn't need to be deallocated */
|
||||||
|
tmp = PyUnicode_AsUTF8AndSize(ret, &size);
|
||||||
|
|
||||||
|
lenread = (int) size;
|
||||||
|
data = (char *) tmp;
|
||||||
|
#else
|
||||||
|
PyObject *b;
|
||||||
|
b = PyUnicode_AsUTF8String(ret);
|
||||||
|
if (b == NULL) {
|
||||||
|
printf("xmlPythonFileRead: failed to convert to UTF-8\n");
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
lenread = PyBytes_Size(b);
|
||||||
|
data = PyBytes_AsString(b);
|
||||||
|
Py_DECREF(b);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
printf("xmlPythonFileRead: result is not a String\n");
|
printf("xmlPythonFileRead: result is not a String\n");
|
||||||
Py_DECREF(ret);
|
Py_DECREF(ret);
|
||||||
|
return(-1);
|
||||||
}
|
}
|
||||||
|
if (lenread > len)
|
||||||
|
memcpy(buffer, data, len);
|
||||||
|
else
|
||||||
|
memcpy(buffer, data, lenread);
|
||||||
|
Py_DECREF(ret);
|
||||||
return(lenread);
|
return(lenread);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,7 +416,7 @@ xmlPythonFileWrite (void * context, const char * buffer, int len) {
|
|||||||
#endif
|
#endif
|
||||||
file = (PyObject *) context;
|
file = (PyObject *) context;
|
||||||
if (file == NULL) return(-1);
|
if (file == NULL) return(-1);
|
||||||
string = PyString_FromStringAndSize(buffer, len);
|
string = PY_IMPORT_STRING_SIZE(buffer, len);
|
||||||
if (string == NULL) return(-1);
|
if (string == NULL) return(-1);
|
||||||
if (PyObject_HasAttrString(file, (char *) "io_write")) {
|
if (PyObject_HasAttrString(file, (char *) "io_write")) {
|
||||||
ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
|
ret = PyEval_CallMethod(file, (char *) "io_write", (char *) "(O)",
|
||||||
@ -371,8 +429,8 @@ xmlPythonFileWrite (void * context, const char * buffer, int len) {
|
|||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
printf("xmlPythonFileWrite: result is NULL\n");
|
printf("xmlPythonFileWrite: result is NULL\n");
|
||||||
return(-1);
|
return(-1);
|
||||||
} else if (PyInt_Check(ret)) {
|
} else if (PyLong_Check(ret)) {
|
||||||
written = (int) PyInt_AsLong(ret);
|
written = (int) PyLong_AsLong(ret);
|
||||||
Py_DECREF(ret);
|
Py_DECREF(ret);
|
||||||
} else if (ret == Py_None) {
|
} else if (ret == Py_None) {
|
||||||
written = len;
|
written = len;
|
||||||
@ -727,7 +785,7 @@ libxml_xmlSetEntityLoader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
|
|||||||
Py_XINCREF(pythonExternalEntityLoaderObjext);
|
Py_XINCREF(pythonExternalEntityLoaderObjext);
|
||||||
xmlSetExternalEntityLoader(pythonExternalEntityLoader);
|
xmlSetExternalEntityLoader(pythonExternalEntityLoader);
|
||||||
|
|
||||||
py_retval = PyInt_FromLong(0);
|
py_retval = PyLong_FromLong(0);
|
||||||
return(py_retval);
|
return(py_retval);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -859,10 +917,10 @@ pythonStartElement(void *user_data, const xmlChar * name,
|
|||||||
} else {
|
} else {
|
||||||
dict = PyDict_New();
|
dict = PyDict_New();
|
||||||
for (i = 0; attrs[i] != NULL; i++) {
|
for (i = 0; attrs[i] != NULL; i++) {
|
||||||
attrname = PyString_FromString((char *) attrs[i]);
|
attrname = PY_IMPORT_STRING((char *) attrs[i]);
|
||||||
i++;
|
i++;
|
||||||
if (attrs[i] != NULL) {
|
if (attrs[i] != NULL) {
|
||||||
attrvalue = PyString_FromString((char *) attrs[i]);
|
attrvalue = PY_IMPORT_STRING((char *) attrs[i]);
|
||||||
} else {
|
} else {
|
||||||
Py_XINCREF(Py_None);
|
Py_XINCREF(Py_None);
|
||||||
attrvalue = Py_None;
|
attrvalue = Py_None;
|
||||||
@ -1265,7 +1323,7 @@ pythonAttributeDecl(void *user_data,
|
|||||||
nameList = PyList_New(count);
|
nameList = PyList_New(count);
|
||||||
count = 0;
|
count = 0;
|
||||||
for (node = tree; node != NULL; node = node->next) {
|
for (node = tree; node != NULL; node = node->next) {
|
||||||
newName = PyString_FromString((char *) node->name);
|
newName = PY_IMPORT_STRING((char *) node->name);
|
||||||
PyList_SetItem(nameList, count, newName);
|
PyList_SetItem(nameList, count, newName);
|
||||||
Py_DECREF(newName);
|
Py_DECREF(newName);
|
||||||
count++;
|
count++;
|
||||||
@ -2128,7 +2186,7 @@ libxml_xmlFreeTextReader(ATTRIBUTE_UNUSED PyObject *self, PyObject *args) {
|
|||||||
|
|
||||||
if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
|
if (!PyArg_ParseTuple(args, (char *)"O:xmlFreeTextReader", &pyobj_reader))
|
||||||
return(NULL);
|
return(NULL);
|
||||||
if (!PyCObject_Check(pyobj_reader)) {
|
if (!PyCapsule_CheckExact(pyobj_reader)) {
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return(Py_None);
|
return(Py_None);
|
||||||
}
|
}
|
||||||
@ -2687,6 +2745,10 @@ libxml_type(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
|
|||||||
if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
|
if (!PyArg_ParseTuple(args, (char *) "O:last", &obj))
|
||||||
return NULL;
|
return NULL;
|
||||||
cur = PyxmlNode_Get(obj);
|
cur = PyxmlNode_Get(obj);
|
||||||
|
if (cur == NULL) {
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return (Py_None);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("libxml_type: cur = %p\n", cur);
|
printf("libxml_type: cur = %p\n", cur);
|
||||||
@ -2803,7 +2865,7 @@ libxml_xmlNodeRemoveNsDef(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
|
|||||||
PyObject *pyobj_node;
|
PyObject *pyobj_node;
|
||||||
xmlChar *href;
|
xmlChar *href;
|
||||||
xmlNsPtr c_retval;
|
xmlNsPtr c_retval;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple
|
if (!PyArg_ParseTuple
|
||||||
(args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
|
(args, (char *) "Oz:xmlNodeRemoveNsDef", &pyobj_node, &href))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
@ -2965,16 +3027,12 @@ libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
|
|||||||
&py_file, &encoding, &format))
|
&py_file, &encoding, &format))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
|
node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
|
||||||
|
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
return (PyInt_FromLong((long) -1));
|
return (PyLong_FromLong((long) -1));
|
||||||
}
|
}
|
||||||
if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
|
output = PyFile_Get(py_file);
|
||||||
return (PyInt_FromLong((long) -1));
|
|
||||||
}
|
|
||||||
output = PyFile_AsFile(py_file);
|
|
||||||
if (output == NULL) {
|
if (output == NULL) {
|
||||||
return (PyInt_FromLong((long) -1));
|
return (PyLong_FromLong((long) -1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node->type == XML_DOCUMENT_NODE) {
|
if (node->type == XML_DOCUMENT_NODE) {
|
||||||
@ -2993,7 +3051,7 @@ libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
|
|||||||
if (encoding != NULL) {
|
if (encoding != NULL) {
|
||||||
handler = xmlFindCharEncodingHandler(encoding);
|
handler = xmlFindCharEncodingHandler(encoding);
|
||||||
if (handler == NULL) {
|
if (handler == NULL) {
|
||||||
return (PyInt_FromLong((long) -1));
|
return (PyLong_FromLong((long) -1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (doc->type == XML_HTML_DOCUMENT_NODE) {
|
if (doc->type == XML_HTML_DOCUMENT_NODE) {
|
||||||
@ -3018,7 +3076,8 @@ libxml_saveNodeTo(ATTRIBUTE_UNUSED PyObject * self, PyObject * args)
|
|||||||
xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
|
xmlNodeDumpOutput(buf, doc, node, 0, format, encoding);
|
||||||
len = xmlOutputBufferClose(buf);
|
len = xmlOutputBufferClose(buf);
|
||||||
}
|
}
|
||||||
return (PyInt_FromLong((long) len));
|
PyFile_Release(output);
|
||||||
|
return (PyLong_FromLong((long) len));
|
||||||
}
|
}
|
||||||
#endif /* LIBXML_OUTPUT_ENABLED */
|
#endif /* LIBXML_OUTPUT_ENABLED */
|
||||||
|
|
||||||
@ -3524,7 +3583,7 @@ PystringSet_Convert(PyObject *py_strings, xmlChar *** result)
|
|||||||
{
|
{
|
||||||
int idx;
|
int idx;
|
||||||
for (idx=0; idx < count; ++idx) {
|
for (idx=0; idx < count; ++idx) {
|
||||||
char* s = PyString_AsString
|
char* s = PyBytes_AsString
|
||||||
(is_tuple
|
(is_tuple
|
||||||
? PyTuple_GET_ITEM(py_strings, idx)
|
? PyTuple_GET_ITEM(py_strings, idx)
|
||||||
: PyList_GET_ITEM(py_strings, idx));
|
: PyList_GET_ITEM(py_strings, idx));
|
||||||
@ -3613,8 +3672,8 @@ libxml_C14NDocDumpMemory(ATTRIBUTE_UNUSED PyObject * self,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
py_retval = PyString_FromStringAndSize((const char *) doc_txt,
|
py_retval = PY_IMPORT_STRING_SIZE((const char *) doc_txt,
|
||||||
result);
|
result);
|
||||||
xmlFree(doc_txt);
|
xmlFree(doc_txt);
|
||||||
return py_retval;
|
return py_retval;
|
||||||
}
|
}
|
||||||
@ -3655,11 +3714,7 @@ libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((py_file == NULL) || (!(PyFile_Check(py_file)))) {
|
output = PyFile_Get(py_file);
|
||||||
PyErr_SetString(PyExc_TypeError, "bad file.");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
output = PyFile_AsFile(py_file);
|
|
||||||
if (output == NULL) {
|
if (output == NULL) {
|
||||||
PyErr_SetString(PyExc_TypeError, "bad file.");
|
PyErr_SetString(PyExc_TypeError, "bad file.");
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -3697,6 +3752,7 @@ libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
|
|||||||
xmlFree(prefixes);
|
xmlFree(prefixes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyFile_Release(output);
|
||||||
len = xmlOutputBufferClose(buf);
|
len = xmlOutputBufferClose(buf);
|
||||||
|
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
@ -3705,7 +3761,7 @@ libxml_C14NDocSaveTo(ATTRIBUTE_UNUSED PyObject * self,
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return PyInt_FromLong((long) len);
|
return PyLong_FromLong((long) len);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -3719,7 +3775,7 @@ libxml_getObjDesc(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
|
|||||||
|
|
||||||
if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
|
if (!PyArg_ParseTuple(args, (char *)"O:getObjDesc", &obj))
|
||||||
return NULL;
|
return NULL;
|
||||||
str = PyCObject_GetDesc(obj);
|
str = PyCapsule_GetPointer(obj, PyCapsule_GetName(obj));
|
||||||
return Py_BuildValue((char *)"s", str);
|
return Py_BuildValue((char *)"s", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3819,28 +3875,59 @@ static PyMethodDef libxmlMethods[] = {
|
|||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
#define INITERROR return NULL
|
||||||
|
|
||||||
|
static struct PyModuleDef moduledef = {
|
||||||
|
PyModuleDef_HEAD_INIT,
|
||||||
|
"libxml2mod",
|
||||||
|
NULL,
|
||||||
|
-1,
|
||||||
|
libxmlMethods,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
#else
|
||||||
|
#define INITERROR return
|
||||||
|
|
||||||
#ifdef MERGED_MODULES
|
#ifdef MERGED_MODULES
|
||||||
extern void initlibxsltmod(void);
|
extern void initlibxsltmod(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void
|
#endif
|
||||||
initlibxml2mod(void)
|
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
PyObject *PyInit_libxml2mod(void)
|
||||||
|
#else
|
||||||
|
void initlibxml2mod(void)
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
static int initialized = 0;
|
PyObject *module;
|
||||||
|
|
||||||
if (initialized != 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
module = PyModule_Create(&moduledef);
|
||||||
|
#else
|
||||||
/* intialize the python extension module */
|
/* intialize the python extension module */
|
||||||
Py_InitModule((char *) "libxml2mod", libxmlMethods);
|
module = Py_InitModule((char *) "libxml2mod", libxmlMethods);
|
||||||
|
#endif
|
||||||
|
if (module == NULL)
|
||||||
|
INITERROR;
|
||||||
|
|
||||||
/* initialize libxml2 */
|
/* initialize libxml2 */
|
||||||
xmlInitParser();
|
xmlInitParser();
|
||||||
|
/* TODO this probably need to be revamped for Python3 */
|
||||||
libxml_xmlErrorInitialize();
|
libxml_xmlErrorInitialize();
|
||||||
|
|
||||||
initialized = 1;
|
#if PY_MAJOR_VERSION < 3
|
||||||
|
|
||||||
#ifdef MERGED_MODULES
|
#ifdef MERGED_MODULES
|
||||||
initlibxsltmod();
|
initlibxsltmod();
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
return module;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -150,8 +150,16 @@ typedef struct {
|
|||||||
} PyURI_Object;
|
} PyURI_Object;
|
||||||
|
|
||||||
/* FILE * have their own internal representation */
|
/* FILE * have their own internal representation */
|
||||||
|
#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
|
||||||
|
|
||||||
#ifdef LIBXML_SCHEMAS_ENABLED
|
#ifdef LIBXML_SCHEMAS_ENABLED
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -249,3 +257,4 @@ PyObject * libxml_xmlErrorPtrWrap(xmlErrorPtr error);
|
|||||||
PyObject * libxml_xmlSchemaSetValidErrors(PyObject * self, PyObject * args);
|
PyObject * libxml_xmlSchemaSetValidErrors(PyObject * self, PyObject * args);
|
||||||
PyObject * libxml_xmlRegisterInputCallback(PyObject *self, PyObject *args);
|
PyObject * libxml_xmlRegisterInputCallback(PyObject *self, PyObject *args);
|
||||||
PyObject * libxml_xmlUnregisterInputCallback(PyObject *self, PyObject *args);
|
PyObject * libxml_xmlUnregisterInputCallback(PyObject *self, PyObject *args);
|
||||||
|
PyObject * libxml_xmlNodeRemoveNsDef(PyObject * self, PyObject * args);
|
||||||
|
263
python/types.c
263
python/types.c
@ -9,6 +9,83 @@
|
|||||||
#include "libxml_wrap.h"
|
#include "libxml_wrap.h"
|
||||||
#include <libxml/xpathInternals.h>
|
#include <libxml/xpathInternals.h>
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
FILE *
|
||||||
|
libxml_PyFileGet(PyObject *f) {
|
||||||
|
int fd, flags;
|
||||||
|
FILE *res;
|
||||||
|
const char *mode;
|
||||||
|
|
||||||
|
fd = PyObject_AsFileDescriptor(f);
|
||||||
|
if (!_PyVerify_fd(fd))
|
||||||
|
return(NULL);
|
||||||
|
/*
|
||||||
|
* 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 genrated accodingly
|
||||||
|
*/
|
||||||
|
fd = dup(fd);
|
||||||
|
if (fd == -1)
|
||||||
|
return(NULL);
|
||||||
|
res = fdopen(fd, mode);
|
||||||
|
if (res == NULL) {
|
||||||
|
close(fd);
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
return(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
void libxml_PyFileRelease(FILE *f) {
|
||||||
|
if (f != NULL)
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
libxml_intWrap(int val)
|
libxml_intWrap(int val)
|
||||||
{
|
{
|
||||||
@ -17,7 +94,7 @@ libxml_intWrap(int val)
|
|||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("libxml_intWrap: val = %d\n", val);
|
printf("libxml_intWrap: val = %d\n", val);
|
||||||
#endif
|
#endif
|
||||||
ret = PyInt_FromLong((long) val);
|
ret = PY_IMPORT_INT((long) val);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +106,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 = PyInt_FromLong(val);
|
ret = PyLong_FromLong(val);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,8 +134,7 @@ libxml_charPtrWrap(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 = PyString_FromString(str);
|
|
||||||
xmlFree(str);
|
xmlFree(str);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -75,8 +151,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 = PyString_FromString(str);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,8 +167,7 @@ libxml_xmlCharPtrWrap(xmlChar * str)
|
|||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
/* TODO: look at deallocation */
|
ret = PY_IMPORT_STRING((char *) str);
|
||||||
ret = PyString_FromString((char *) str);
|
|
||||||
xmlFree(str);
|
xmlFree(str);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -110,8 +184,7 @@ libxml_xmlCharPtrConstWrap(const xmlChar * str)
|
|||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
/* TODO: look at deallocation */
|
ret = PY_IMPORT_STRING((char *) str);
|
||||||
ret = PyString_FromString((char *) str);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,8 +200,7 @@ libxml_constcharPtrWrap(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 = PyString_FromString(str);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,8 +216,7 @@ libxml_constxmlCharPtrWrap(const xmlChar * str)
|
|||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
/* TODO: look at deallocation */
|
ret = PY_IMPORT_STRING((char *) str);
|
||||||
ret = PyString_FromString((char *) str);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,9 +233,7 @@ libxml_xmlDocPtrWrap(xmlDocPtr doc)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
/* TODO: look at deallocation */
|
/* TODO: look at deallocation */
|
||||||
ret =
|
ret = PyCapsule_New((void *) doc, (char *) "xmlDocPtr", NULL);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) doc, (char *) "xmlDocPtr",
|
|
||||||
NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,9 +249,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);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "xmlNodePtr",
|
|
||||||
NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,9 +265,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);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) uri, (char *) "xmlURIPtr",
|
|
||||||
NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,9 +281,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);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) ns, (char *) "xmlNsPtr",
|
|
||||||
NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,9 +297,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);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) attr, (char *) "xmlAttrPtr",
|
|
||||||
NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,9 +313,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);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) attr,
|
|
||||||
(char *) "xmlAttributePtr", NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,9 +329,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);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) elem,
|
|
||||||
(char *) "xmlElementPtr", NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -288,9 +345,7 @@ libxml_xmlXPathContextPtrWrap(xmlXPathContextPtr ctxt)
|
|||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret = PyCapsule_New((void *) ctxt, (char *) "xmlXPathContextPtr", NULL);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
|
|
||||||
(char *) "xmlXPathContextPtr", NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -306,9 +361,7 @@ libxml_xmlXPathParserContextPtrWrap(xmlXPathParserContextPtr ctxt)
|
|||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret = PyCObject_FromVoidPtrAndDesc((void *) ctxt,
|
ret = PyCapsule_New((void *)ctxt, (char *)"xmlXPathParserContextPtr", NULL);
|
||||||
(char *) "xmlXPathParserContextPtr",
|
|
||||||
NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,16 +378,13 @@ libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret =
|
ret = PyCapsule_New((void *) ctxt, (char *) "xmlParserCtxtPtr", NULL);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
|
|
||||||
(char *) "xmlParserCtxtPtr", NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* libxml_xmlXPathDestructNsNode:
|
* libxml_xmlXPathDestructNsNode:
|
||||||
* cobj: xmlNsPtr namespace node
|
* cobj: xmlNsPtr namespace node capsule object
|
||||||
* desc: ignored string
|
|
||||||
*
|
*
|
||||||
* 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
|
||||||
@ -342,11 +392,11 @@ libxml_xmlParserCtxtPtrWrap(xmlParserCtxtPtr ctxt)
|
|||||||
* xmlDoc document, see xmlXPathNodeSetDupNs.
|
* xmlDoc document, see xmlXPathNodeSetDupNs.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
libxml_xmlXPathDestructNsNode(void *cobj, void *desc ATTRIBUTE_UNUSED) {
|
libxml_xmlXPathDestructNsNode(PyObject *cap) {
|
||||||
#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);
|
xmlXPathNodeSetFreeNs((xmlNsPtr) PyCapsule_GetPointer(cap, "xmlNsPtr"));
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
@ -400,8 +450,7 @@ libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
|
|||||||
for (i = 0; i < obj->nodesetval->nodeNr; i++) {
|
for (i = 0; i < obj->nodesetval->nodeNr; i++) {
|
||||||
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 = PyCapsule_New((void *) node,
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) node,
|
|
||||||
(char *) "xmlNsPtr",
|
(char *) "xmlNsPtr",
|
||||||
libxml_xmlXPathDestructNsNode);
|
libxml_xmlXPathDestructNsNode);
|
||||||
PyList_SetItem(ret, i, ns);
|
PyList_SetItem(ret, i, ns);
|
||||||
@ -414,13 +463,13 @@ libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case XPATH_BOOLEAN:
|
case XPATH_BOOLEAN:
|
||||||
ret = PyInt_FromLong((long) obj->boolval);
|
ret = PY_IMPORT_INT((long) obj->boolval);
|
||||||
break;
|
break;
|
||||||
case XPATH_NUMBER:
|
case XPATH_NUMBER:
|
||||||
ret = PyFloat_FromDouble(obj->floatval);
|
ret = PyFloat_FromDouble(obj->floatval);
|
||||||
break;
|
break;
|
||||||
case XPATH_STRING:
|
case XPATH_STRING:
|
||||||
ret = PyString_FromString((char *) obj->stringval);
|
ret = PY_IMPORT_STRING((char *) obj->stringval);
|
||||||
break;
|
break;
|
||||||
case XPATH_POINT:
|
case XPATH_POINT:
|
||||||
{
|
{
|
||||||
@ -429,7 +478,7 @@ libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
|
|||||||
PyObject *tuple;
|
PyObject *tuple;
|
||||||
|
|
||||||
node = libxml_xmlNodePtrWrap(obj->user);
|
node = libxml_xmlNodePtrWrap(obj->user);
|
||||||
indexIntoNode = PyInt_FromLong((long) obj->index);
|
indexIntoNode = PY_IMPORT_INT((long) obj->index);
|
||||||
|
|
||||||
tuple = PyTuple_New(2);
|
tuple = PyTuple_New(2);
|
||||||
PyTuple_SetItem(tuple, 0, node);
|
PyTuple_SetItem(tuple, 0, node);
|
||||||
@ -453,7 +502,7 @@ libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
|
|||||||
list = PyList_New(1);
|
list = PyList_New(1);
|
||||||
|
|
||||||
node = libxml_xmlNodePtrWrap(obj->user);
|
node = libxml_xmlNodePtrWrap(obj->user);
|
||||||
indexIntoNode = PyInt_FromLong((long) obj->index);
|
indexIntoNode = PY_IMPORT_INT((long) obj->index);
|
||||||
|
|
||||||
tuple = PyTuple_New(2);
|
tuple = PyTuple_New(2);
|
||||||
PyTuple_SetItem(tuple, 0, node);
|
PyTuple_SetItem(tuple, 0, node);
|
||||||
@ -471,7 +520,7 @@ libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
|
|||||||
list = PyList_New(2);
|
list = PyList_New(2);
|
||||||
|
|
||||||
node = libxml_xmlNodePtrWrap(obj->user);
|
node = libxml_xmlNodePtrWrap(obj->user);
|
||||||
indexIntoNode = PyInt_FromLong((long) obj->index);
|
indexIntoNode = PY_IMPORT_INT((long) obj->index);
|
||||||
|
|
||||||
tuple = PyTuple_New(2);
|
tuple = PyTuple_New(2);
|
||||||
PyTuple_SetItem(tuple, 0, node);
|
PyTuple_SetItem(tuple, 0, node);
|
||||||
@ -480,7 +529,7 @@ libxml_xmlXPathObjectPtrWrap(xmlXPathObjectPtr obj)
|
|||||||
PyList_SetItem(list, 0, tuple);
|
PyList_SetItem(list, 0, tuple);
|
||||||
|
|
||||||
node = libxml_xmlNodePtrWrap(obj->user2);
|
node = libxml_xmlNodePtrWrap(obj->user2);
|
||||||
indexIntoNode = PyInt_FromLong((long) obj->index2);
|
indexIntoNode = PY_IMPORT_INT((long) obj->index2);
|
||||||
|
|
||||||
tuple = PyTuple_New(2);
|
tuple = PyTuple_New(2);
|
||||||
PyTuple_SetItem(tuple, 0, node);
|
PyTuple_SetItem(tuple, 0, node);
|
||||||
@ -545,14 +594,14 @@ 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 PyLong_Check(obj) {
|
||||||
} else if PyInt_Check(obj) {
|
#ifdef PyLong_AS_LONG
|
||||||
|
ret = xmlXPathNewFloat((double) PyLong_AS_LONG(obj));
|
||||||
|
#else
|
||||||
ret = xmlXPathNewFloat((double) PyInt_AS_LONG(obj));
|
ret = xmlXPathNewFloat((double) PyInt_AS_LONG(obj));
|
||||||
|
#endif
|
||||||
#ifdef PyBool_Check
|
#ifdef PyBool_Check
|
||||||
} else if PyBool_Check (obj) {
|
} else if PyBool_Check (obj) {
|
||||||
|
|
||||||
@ -563,15 +612,37 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj)
|
|||||||
ret = xmlXPathNewBoolean(0);
|
ret = xmlXPathNewBoolean(0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
} else if PyString_Check
|
} else if PyBytes_Check (obj) {
|
||||||
(obj) {
|
|
||||||
xmlChar *str;
|
xmlChar *str;
|
||||||
|
|
||||||
str = xmlStrndup((const xmlChar *) PyString_AS_STRING(obj),
|
str = xmlStrndup((const xmlChar *) PyBytes_AS_STRING(obj),
|
||||||
PyString_GET_SIZE(obj));
|
PyBytes_GET_SIZE(obj));
|
||||||
ret = xmlXPathWrapString(str);
|
ret = xmlXPathWrapString(str);
|
||||||
} else if PyList_Check
|
#ifdef PyUnicode_Check
|
||||||
(obj) {
|
} else if PyUnicode_Check (obj) {
|
||||||
|
#if PY_VERSION_HEX >= 0x03030000
|
||||||
|
xmlChar *str;
|
||||||
|
const char *tmp;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
/* tmp doesn't need to be deallocated */
|
||||||
|
tmp = PyUnicode_AsUTF8AndSize(obj, &size);
|
||||||
|
str = xmlStrndup(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,28 +656,18 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
cur = NULL;
|
cur = NULL;
|
||||||
if (PyCObject_Check(node)) {
|
if (PyCapsule_CheckExact(node)) {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("Got a CObject\n");
|
printf("Got a CObject\n");
|
||||||
#endif
|
#endif
|
||||||
cur = PyxmlNode_Get(node);
|
cur = PyxmlNode_Get(node);
|
||||||
} else if (PyInstance_Check(node)) {
|
} else if ((PyObject_HasAttrString(node, (char *) "_o")) &&
|
||||||
PyInstanceObject *inst = (PyInstanceObject *) node;
|
(PyObject_HasAttrString(node, (char *) "get_doc"))) {
|
||||||
PyObject *name = inst->in_class->cl_name;
|
PyObject *wrapper;
|
||||||
|
|
||||||
if PyString_Check
|
wrapper = PyObject_GetAttrString(node, (char *) "_o");
|
||||||
(name) {
|
if (wrapper != NULL)
|
||||||
char *type = PyString_AS_STRING(name);
|
cur = PyxmlNode_Get(wrapper);
|
||||||
PyObject *wrapper;
|
|
||||||
|
|
||||||
if (!strcmp(type, "xmlNode")) {
|
|
||||||
wrapper =
|
|
||||||
PyObject_GetAttrString(node, (char *) "_o");
|
|
||||||
if (wrapper != NULL) {
|
|
||||||
cur = PyxmlNode_Get(wrapper);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("Unknown object in Python return list\n");
|
printf("Unknown object in Python return list\n");
|
||||||
@ -629,7 +690,7 @@ PyObject *
|
|||||||
libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid)
|
libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid)
|
||||||
{
|
{
|
||||||
PyObject *ret;
|
PyObject *ret;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("libxml_xmlValidCtxtPtrWrap: valid = %p\n", valid);
|
printf("libxml_xmlValidCtxtPtrWrap: valid = %p\n", valid);
|
||||||
#endif
|
#endif
|
||||||
@ -639,7 +700,7 @@ libxml_xmlValidCtxtPtrWrap(xmlValidCtxtPtr valid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) valid,
|
PyCapsule_New((void *) valid,
|
||||||
(char *) "xmlValidCtxtPtr", NULL);
|
(char *) "xmlValidCtxtPtr", NULL);
|
||||||
|
|
||||||
return (ret);
|
return (ret);
|
||||||
@ -658,7 +719,7 @@ libxml_xmlCatalogPtrWrap(xmlCatalogPtr catal)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) catal,
|
PyCapsule_New((void *) catal,
|
||||||
(char *) "xmlCatalogPtr", NULL);
|
(char *) "xmlCatalogPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -676,7 +737,7 @@ libxml_xmlOutputBufferPtrWrap(xmlOutputBufferPtr buffer)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) buffer,
|
PyCapsule_New((void *) buffer,
|
||||||
(char *) "xmlOutputBufferPtr", NULL);
|
(char *) "xmlOutputBufferPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -694,7 +755,7 @@ libxml_xmlParserInputBufferPtrWrap(xmlParserInputBufferPtr buffer)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) buffer,
|
PyCapsule_New((void *) buffer,
|
||||||
(char *) "xmlParserInputBufferPtr", NULL);
|
(char *) "xmlParserInputBufferPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -713,7 +774,7 @@ libxml_xmlRegexpPtrWrap(xmlRegexpPtr regexp)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) regexp,
|
PyCapsule_New((void *) regexp,
|
||||||
(char *) "xmlRegexpPtr", NULL);
|
(char *) "xmlRegexpPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -733,7 +794,7 @@ libxml_xmlTextReaderPtrWrap(xmlTextReaderPtr reader)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) reader,
|
PyCapsule_New((void *) reader,
|
||||||
(char *) "xmlTextReaderPtr", NULL);
|
(char *) "xmlTextReaderPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -751,7 +812,7 @@ libxml_xmlTextReaderLocatorPtrWrap(xmlTextReaderLocatorPtr locator)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) locator,
|
PyCapsule_New((void *) locator,
|
||||||
(char *) "xmlTextReaderLocatorPtr", NULL);
|
(char *) "xmlTextReaderLocatorPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -771,7 +832,7 @@ libxml_xmlRelaxNGPtrWrap(xmlRelaxNGPtr ctxt)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
|
PyCapsule_New((void *) ctxt,
|
||||||
(char *) "xmlRelaxNGPtr", NULL);
|
(char *) "xmlRelaxNGPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -789,7 +850,7 @@ libxml_xmlRelaxNGParserCtxtPtrWrap(xmlRelaxNGParserCtxtPtr ctxt)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
|
PyCapsule_New((void *) ctxt,
|
||||||
(char *) "xmlRelaxNGParserCtxtPtr", NULL);
|
(char *) "xmlRelaxNGParserCtxtPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -806,7 +867,7 @@ libxml_xmlRelaxNGValidCtxtPtrWrap(xmlRelaxNGValidCtxtPtr valid)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) valid,
|
PyCapsule_New((void *) valid,
|
||||||
(char *) "xmlRelaxNGValidCtxtPtr", NULL);
|
(char *) "xmlRelaxNGValidCtxtPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -824,7 +885,7 @@ libxml_xmlSchemaPtrWrap(xmlSchemaPtr ctxt)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
|
PyCapsule_New((void *) ctxt,
|
||||||
(char *) "xmlSchemaPtr", NULL);
|
(char *) "xmlSchemaPtr", NULL);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
@ -842,7 +903,7 @@ libxml_xmlSchemaParserCtxtPtrWrap(xmlSchemaParserCtxtPtr ctxt)
|
|||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) ctxt,
|
PyCapsule_New((void *) ctxt,
|
||||||
(char *) "xmlSchemaParserCtxtPtr", NULL);
|
(char *) "xmlSchemaParserCtxtPtr", NULL);
|
||||||
|
|
||||||
return (ret);
|
return (ret);
|
||||||
@ -862,7 +923,7 @@ libxml_xmlSchemaValidCtxtPtrWrap(xmlSchemaValidCtxtPtr valid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ret =
|
ret =
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) valid,
|
PyCapsule_New((void *) valid,
|
||||||
(char *) "xmlSchemaValidCtxtPtr", NULL);
|
(char *) "xmlSchemaValidCtxtPtr", NULL);
|
||||||
|
|
||||||
return (ret);
|
return (ret);
|
||||||
@ -881,8 +942,6 @@ libxml_xmlErrorPtrWrap(xmlErrorPtr error)
|
|||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return (Py_None);
|
return (Py_None);
|
||||||
}
|
}
|
||||||
ret =
|
ret = PyCapsule_New((void *) error, (char *) "xmlErrorPtr", NULL);
|
||||||
PyCObject_FromVoidPtrAndDesc((void *) error,
|
|
||||||
(char *) "xmlErrorPtr", NULL);
|
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user