mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-12-24 17:41:03 +03:00
adding error redirections and preformat to a python handler cleanup made
* python/Makefile.am python/libxml.c python/libxml2-python-api.xml python/libxml2class.txt: adding error redirections and preformat to a python handler * python/tests/Makefile.am python/tests/*.py: cleanup made all tests self checking Daniel
This commit is contained in:
@@ -1,3 +1,11 @@
|
|||||||
|
Sat Feb 2 22:47:10 CET 2002 Daniel Veillard <daniel@veillard.com>
|
||||||
|
|
||||||
|
* python/Makefile.am python/libxml.c python/libxml2-python-api.xml
|
||||||
|
python/libxml2class.txt: adding error redirections and preformat
|
||||||
|
to a python handler
|
||||||
|
* python/tests/Makefile.am python/tests/*.py: cleanup made all
|
||||||
|
tests self checking
|
||||||
|
|
||||||
Sat Feb 2 13:18:54 CET 2002 Daniel Veillard <daniel@veillard.com>
|
Sat Feb 2 13:18:54 CET 2002 Daniel Veillard <daniel@veillard.com>
|
||||||
|
|
||||||
* python/libxml.c python/libxml.py: fixed a stupid bug when renaming
|
* python/libxml.c python/libxml.py: fixed a stupid bug when renaming
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ SUBDIRS= . tests
|
|||||||
|
|
||||||
LIBS=-L../.libs -L.. $(XML_LIBS)
|
LIBS=-L../.libs -L.. $(XML_LIBS)
|
||||||
INCLUDES=-I/usr/include/python$(PYTHON_VERSION) -I$(PYTHON_INCLUDES) -I$(top_srcdir)/include
|
INCLUDES=-I/usr/include/python$(PYTHON_VERSION) -I$(PYTHON_INCLUDES) -I$(top_srcdir)/include
|
||||||
SHCFLAGS=$(INCLUDES) -Wall -fPIC
|
SHCFLAGS=$(INCLUDES) -Wall -fPIC -g
|
||||||
LINK_FLAGS= -shared
|
LINK_FLAGS= -shared
|
||||||
DOCS_DIR = $(prefix)/share/doc/libxml2-python-$(LIBXML_VERSION)
|
DOCS_DIR = $(prefix)/share/doc/libxml2-python-$(LIBXML_VERSION)
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ Things to do:
|
|||||||
|
|
||||||
- handling of node.content
|
- handling of node.content
|
||||||
- SAX interfaces
|
- SAX interfaces
|
||||||
- error redirections and preformat
|
|
||||||
- memory debug interfaces
|
- memory debug interfaces
|
||||||
- enums -> libxml.py
|
- enums -> libxml.py
|
||||||
- access to XPath variables
|
- access to XPath variables
|
||||||
@@ -41,5 +40,6 @@ Done:
|
|||||||
- spec file: automatically generate for pythonX.Y if found
|
- spec file: automatically generate for pythonX.Y if found
|
||||||
Done, a bit ugly by running new makes in %install for each level
|
Done, a bit ugly by running new makes in %install for each level
|
||||||
found.
|
found.
|
||||||
|
- error redirections and preformat
|
||||||
|
|
||||||
Daniel Veillard
|
Daniel Veillard
|
||||||
|
|||||||
107
python/libxml.c
107
python/libxml.c
@@ -4,12 +4,14 @@
|
|||||||
#include <libxml/parser.h>
|
#include <libxml/parser.h>
|
||||||
#include <libxml/tree.h>
|
#include <libxml/tree.h>
|
||||||
#include <libxml/xpath.h>
|
#include <libxml/xpath.h>
|
||||||
|
#include <libxml/xmlerror.h>
|
||||||
#include <libxml/xpathInternals.h>
|
#include <libxml/xpathInternals.h>
|
||||||
#include "libxml_wrap.h"
|
#include "libxml_wrap.h"
|
||||||
#include "libxml2-py.h"
|
#include "libxml2-py.h"
|
||||||
|
|
||||||
/* #define DEBUG */
|
/* #define DEBUG */
|
||||||
/* #define DEBUG_XPATH */
|
/* #define DEBUG_XPATH */
|
||||||
|
/* #define DEBUG_ERROR */
|
||||||
|
|
||||||
/************************************************************************
|
/************************************************************************
|
||||||
* *
|
* *
|
||||||
@@ -285,6 +287,110 @@ libxml_xmlXPathObjectPtrConvert(PyObject * obj) {
|
|||||||
Py_DECREF(obj);
|
Py_DECREF(obj);
|
||||||
return(ret);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/************************************************************************
|
||||||
|
* *
|
||||||
|
* Error message callback *
|
||||||
|
* *
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
static PyObject *libxml_xmlPythonErrorFuncHandler = NULL;
|
||||||
|
static PyObject *libxml_xmlPythonErrorFuncCtxt = NULL;
|
||||||
|
|
||||||
|
static void
|
||||||
|
libxml_xmlErrorFuncHandler(void *ctx, const char *msg, ...) {
|
||||||
|
int size;
|
||||||
|
int chars;
|
||||||
|
char *larger;
|
||||||
|
va_list ap;
|
||||||
|
char *str;
|
||||||
|
PyObject *list;
|
||||||
|
PyObject *message;
|
||||||
|
PyObject *result;
|
||||||
|
|
||||||
|
#ifdef DEBUG_ERROR
|
||||||
|
printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
if (libxml_xmlPythonErrorFuncHandler == NULL) {
|
||||||
|
va_start(ap, msg);
|
||||||
|
vfprintf(stdout, msg, ap);
|
||||||
|
va_end(ap);
|
||||||
|
} else {
|
||||||
|
str = (char *) xmlMalloc(150);
|
||||||
|
if (str == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
size = 150;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
va_start(ap, msg);
|
||||||
|
chars = vsnprintf(str, size, msg, ap);
|
||||||
|
va_end(ap);
|
||||||
|
if ((chars > -1) && (chars < size))
|
||||||
|
break;
|
||||||
|
if (chars > -1)
|
||||||
|
size += chars + 1;
|
||||||
|
else
|
||||||
|
size += 100;
|
||||||
|
if ((larger = (char *) xmlRealloc(str, size)) == NULL) {
|
||||||
|
xmlFree(str);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
str = larger;
|
||||||
|
}
|
||||||
|
|
||||||
|
list = PyTuple_New(2);
|
||||||
|
PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
|
||||||
|
Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
|
||||||
|
message = libxml_charPtrWrap(str);
|
||||||
|
PyTuple_SetItem(list, 1, message);
|
||||||
|
result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
|
||||||
|
Py_XDECREF(list);
|
||||||
|
Py_XDECREF(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
libxml_xmlErrorInitialize(void) {
|
||||||
|
#ifdef DEBUG_ERROR
|
||||||
|
printf("libxml_xmlErrorInitialize() called\n");
|
||||||
|
#endif
|
||||||
|
xmlSetGenericErrorFunc(NULL, libxml_xmlErrorFuncHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
PyObject *
|
||||||
|
libxml_xmlRegisterErrorHandler(PyObject *self, PyObject *args) {
|
||||||
|
PyObject *py_retval;
|
||||||
|
PyObject *pyobj_f;
|
||||||
|
PyObject *pyobj_ctx;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "OO:xmlRegisterErrorHandler", &pyobj_f,
|
||||||
|
&pyobj_ctx))
|
||||||
|
return(NULL);
|
||||||
|
|
||||||
|
#ifdef DEBUG_ERROR
|
||||||
|
printf("libxml_registerXPathFunction(%p, %p) called\n", pyobj_ctx, pyobj_f);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (libxml_xmlPythonErrorFuncHandler != NULL) {
|
||||||
|
Py_XDECREF(libxml_xmlPythonErrorFuncHandler);
|
||||||
|
}
|
||||||
|
if (libxml_xmlPythonErrorFuncCtxt != NULL) {
|
||||||
|
Py_XDECREF(libxml_xmlPythonErrorFuncCtxt);
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_XINCREF(pyobj_ctx);
|
||||||
|
Py_XINCREF(pyobj_f);
|
||||||
|
|
||||||
|
/* TODO: check f is a function ! */
|
||||||
|
libxml_xmlPythonErrorFuncHandler = pyobj_f;
|
||||||
|
libxml_xmlPythonErrorFuncCtxt = pyobj_ctx;
|
||||||
|
|
||||||
|
py_retval = libxml_intWrap(1);
|
||||||
|
return(py_retval);
|
||||||
|
}
|
||||||
/************************************************************************
|
/************************************************************************
|
||||||
* *
|
* *
|
||||||
* XPath extensions *
|
* XPath extensions *
|
||||||
@@ -954,5 +1060,6 @@ void init_libxml(void) {
|
|||||||
d = PyModule_GetDict(m);
|
d = PyModule_GetDict(m);
|
||||||
PyDict_SetItemString(d, "xmlNodeType", (PyObject *)&PyxmlNode_Type);
|
PyDict_SetItemString(d, "xmlNodeType", (PyObject *)&PyxmlNode_Type);
|
||||||
PyDict_SetItemString(d, "xmlXPathContextType", (PyObject *)&PyxmlXPathContext_Type);
|
PyDict_SetItemString(d, "xmlXPathContextType", (PyObject *)&PyxmlXPathContext_Type);
|
||||||
|
libxml_xmlErrorInitialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,5 +14,11 @@
|
|||||||
<arg name='ns_uri' type='xmlChar *' info='the namespace or NULL'/>
|
<arg name='ns_uri' type='xmlChar *' info='the namespace or NULL'/>
|
||||||
<arg name='f' type='pythonObject' info='the python function'/>
|
<arg name='f' type='pythonObject' info='the python function'/>
|
||||||
</function>
|
</function>
|
||||||
|
<function name='xmlRegisterErrorHandler' file='python'>
|
||||||
|
<info>Register a Python written function to for error reporting. The function is called back as f(ctx, error).</info>
|
||||||
|
<return type='int' info="1 in case of success, 0 or -1 in case of error"/>
|
||||||
|
<arg name='f' type='pythonObject' info='the python function'/>
|
||||||
|
<arg name='ctx' type='pythonObject' info='a context for the callback'/>
|
||||||
|
</function>
|
||||||
</symbols>
|
</symbols>
|
||||||
</api>
|
</api>
|
||||||
|
|||||||
@@ -99,6 +99,9 @@ isIdeographic()
|
|||||||
isLetter()
|
isLetter()
|
||||||
isPubidChar()
|
isPubidChar()
|
||||||
|
|
||||||
|
# functions from module python
|
||||||
|
registerErrorHandler()
|
||||||
|
|
||||||
# functions from module tree
|
# functions from module tree
|
||||||
compressMode()
|
compressMode()
|
||||||
newComment()
|
newComment()
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ TESTS= \
|
|||||||
tst.py \
|
tst.py \
|
||||||
tstxpath.py \
|
tstxpath.py \
|
||||||
xpathext.py \
|
xpathext.py \
|
||||||
|
error.py \
|
||||||
xpath.py
|
xpath.py
|
||||||
|
|
||||||
XMLS= \
|
XMLS= \
|
||||||
@@ -13,7 +14,7 @@ EXTRA_DIST = $(TESTS) $(XMLS)
|
|||||||
|
|
||||||
if WITH_PYTHON
|
if WITH_PYTHON
|
||||||
tests: $(TESTS)
|
tests: $(TESTS)
|
||||||
-@(CLASSPATH=".." ; export CLASSPATH; \
|
-@(PYTHONPATH=".." ; export PYTHONPATH; \
|
||||||
for test in $(TESTS) ; do echo "-- $$test" ; $(PYTHON) $$test ; done)
|
for test in $(TESTS) ; do echo "-- $$test" ; $(PYTHON) $$test ; done)
|
||||||
else
|
else
|
||||||
tests:
|
tests:
|
||||||
|
|||||||
@@ -1,10 +1,18 @@
|
|||||||
#!/usr/bin/python -u
|
#!/usr/bin/python -u
|
||||||
|
import sys
|
||||||
import libxml2
|
import libxml2
|
||||||
|
|
||||||
doc = libxml2.parseFile("tst.xml")
|
doc = libxml2.parseFile("tst.xml")
|
||||||
print doc.name
|
if doc.name != "tst.xml":
|
||||||
|
print "doc.name failed"
|
||||||
|
sys.exit(1)
|
||||||
root = doc.children
|
root = doc.children
|
||||||
print root.name
|
if root.name != "doc":
|
||||||
|
print "root.name failed"
|
||||||
|
sys.exit(1)
|
||||||
child = root.children
|
child = root.children
|
||||||
print child.name
|
if child.name != "foo":
|
||||||
|
print "child.name failed"
|
||||||
|
sys.exit(1)
|
||||||
doc.freeDoc()
|
doc.freeDoc()
|
||||||
|
print "OK"
|
||||||
|
|||||||
@@ -1,29 +1,38 @@
|
|||||||
#!/usr/bin/python -u
|
#!/usr/bin/python -u
|
||||||
|
import sys
|
||||||
import libxml2
|
import libxml2
|
||||||
|
|
||||||
def foo(x):
|
def foo(x):
|
||||||
# print "foo called %s" % (x)
|
|
||||||
return x + 1
|
return x + 1
|
||||||
|
|
||||||
def bar(x):
|
def bar(x):
|
||||||
# print "foo called %s" % (x)
|
return "%d" % (x + 2)
|
||||||
return "%s" % (x + 1)
|
|
||||||
|
|
||||||
doc = libxml2.parseFile("tst.xml")
|
doc = libxml2.parseFile("tst.xml")
|
||||||
ctxt = doc.xpathNewContext()
|
ctxt = doc.xpathNewContext()
|
||||||
res = ctxt.xpathEval("//*")
|
res = ctxt.xpathEval("//*")
|
||||||
print res
|
if len(res) != 2:
|
||||||
|
print "xpath query: wrong node set size"
|
||||||
|
sys.exit(1)
|
||||||
|
if res[0].name != "doc" or res[1].name != "foo":
|
||||||
|
print "xpath query: wrong node set value"
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
|
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
|
||||||
libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
|
libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
|
||||||
i = 10000
|
i = 10000
|
||||||
while i > 0:
|
while i > 0:
|
||||||
res = ctxt.xpathEval("foo(1)")
|
res = ctxt.xpathEval("foo(1)")
|
||||||
|
if res != 2:
|
||||||
|
print "xpath extension failure"
|
||||||
|
sys.exit(1)
|
||||||
i = i - 1
|
i = i - 1
|
||||||
print res
|
|
||||||
i = 10000
|
i = 10000
|
||||||
while i > 0:
|
while i > 0:
|
||||||
res = ctxt.xpathEval("bar(1)")
|
res = ctxt.xpathEval("bar(1)")
|
||||||
|
if res != "3":
|
||||||
|
print "xpath extension failure got %s expecting '3'"
|
||||||
|
sys.exit(1)
|
||||||
i = i - 1
|
i = i - 1
|
||||||
print res
|
|
||||||
doc.freeDoc()
|
doc.freeDoc()
|
||||||
|
print "OK"
|
||||||
|
|||||||
@@ -3,10 +3,23 @@
|
|||||||
# this test exercise the XPath basic engine, parser, etc, and
|
# this test exercise the XPath basic engine, parser, etc, and
|
||||||
# allows to detect memory leaks
|
# allows to detect memory leaks
|
||||||
#
|
#
|
||||||
|
import sys
|
||||||
import libxml2
|
import libxml2
|
||||||
|
|
||||||
doc = libxml2.parseFile("tst.xml")
|
doc = libxml2.parseFile("tst.xml")
|
||||||
print doc
|
if doc.name != "tst.xml":
|
||||||
|
print "doc.name error"
|
||||||
|
sys.exit(1);
|
||||||
|
|
||||||
|
ctxt = doc.xpathNewContext()
|
||||||
|
res = ctxt.xpathEval("//*")
|
||||||
|
if len(res) != 2:
|
||||||
|
print "xpath query: wrong node set size"
|
||||||
|
sys.exit(1)
|
||||||
|
if res[0].name != "doc" or res[1].name != "foo":
|
||||||
|
print "xpath query: wrong node set value"
|
||||||
|
sys.exit(1)
|
||||||
|
doc.freeDoc()
|
||||||
i = 1000
|
i = 1000
|
||||||
while i > 0:
|
while i > 0:
|
||||||
doc = libxml2.parseFile("tst.xml")
|
doc = libxml2.parseFile("tst.xml")
|
||||||
@@ -14,8 +27,4 @@ while i > 0:
|
|||||||
res = ctxt.xpathEval("//*")
|
res = ctxt.xpathEval("//*")
|
||||||
doc.freeDoc()
|
doc.freeDoc()
|
||||||
i = i -1
|
i = i -1
|
||||||
doc = libxml2.parseFile("tst.xml")
|
print "OK"
|
||||||
ctxt = doc.xpathNewContext()
|
|
||||||
res = ctxt.xpathEval("//*")
|
|
||||||
print res
|
|
||||||
doc.freeDoc()
|
|
||||||
|
|||||||
@@ -1,33 +1,38 @@
|
|||||||
#!/usr/bin/python -u
|
#!/usr/bin/python -u
|
||||||
#
|
import sys
|
||||||
# This test exercise the extension of the XPath engine with
|
|
||||||
# functions defined in Python.
|
|
||||||
#
|
|
||||||
import libxml2
|
import libxml2
|
||||||
|
|
||||||
def foo(x):
|
def foo(x):
|
||||||
# print "foo called %s" % (x)
|
|
||||||
return x + 1
|
return x + 1
|
||||||
|
|
||||||
def bar(x):
|
def bar(x):
|
||||||
# print "foo called %s" % (x)
|
return "%d" % (x + 2)
|
||||||
return "%s" % (x + 1)
|
|
||||||
|
|
||||||
doc = libxml2.parseFile("tst.xml")
|
doc = libxml2.parseFile("tst.xml")
|
||||||
ctxt = doc.xpathNewContext()
|
ctxt = doc.xpathNewContext()
|
||||||
res = ctxt.xpathEval("//*")
|
res = ctxt.xpathEval("//*")
|
||||||
print res
|
if len(res) != 2:
|
||||||
|
print "xpath query: wrong node set size"
|
||||||
|
sys.exit(1)
|
||||||
|
if res[0].name != "doc" or res[1].name != "foo":
|
||||||
|
print "xpath query: wrong node set value"
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
|
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
|
||||||
libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
|
libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
|
||||||
i = 10000
|
i = 10000
|
||||||
while i > 0:
|
while i > 0:
|
||||||
res = ctxt.xpathEval("foo(1)")
|
res = ctxt.xpathEval("foo(1)")
|
||||||
|
if res != 2:
|
||||||
|
print "xpath extension failure"
|
||||||
|
sys.exit(1)
|
||||||
i = i - 1
|
i = i - 1
|
||||||
print res
|
|
||||||
i = 10000
|
i = 10000
|
||||||
while i > 0:
|
while i > 0:
|
||||||
res = ctxt.xpathEval("bar(1)")
|
res = ctxt.xpathEval("bar(1)")
|
||||||
|
if res != "3":
|
||||||
|
print "xpath extension failure got %s expecting '3'"
|
||||||
|
sys.exit(1)
|
||||||
i = i - 1
|
i = i - 1
|
||||||
print res
|
|
||||||
doc.freeDoc()
|
doc.freeDoc()
|
||||||
|
print "OK"
|
||||||
|
|||||||
Reference in New Issue
Block a user