1 diff --git i/python/libxml.c w/python/libxml.c
2 index ef630254..65a51af0 100644
5 @@ -1621,28 +1621,37 @@ libxml_xmlErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, const char *msg,
9 + unsigned char *ptr = (unsigned char *)str;
12 printf("libxml_xmlErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
16 if (libxml_xmlPythonErrorFuncHandler == NULL) {
18 vfprintf(stderr, msg, ap);
22 if (vsnprintf(str, 999, msg, ap) >= 998)
26 +#if PY_MAJOR_VERSION >= 3
27 + /* Ensure the error string doesn't start at UTF8 continuation. */
28 + while (*ptr && (*ptr & 0xc0) == 0x80)
32 list = PyTuple_New(2);
33 PyTuple_SetItem(list, 0, libxml_xmlPythonErrorFuncCtxt);
34 Py_XINCREF(libxml_xmlPythonErrorFuncCtxt);
35 - message = libxml_charPtrConstWrap(str);
36 + message = libxml_charPtrConstWrap(ptr);
37 PyTuple_SetItem(list, 1, message);
38 result = PyEval_CallObject(libxml_xmlPythonErrorFuncHandler, list);
39 + /* Forget any errors caused in the error handler. */