pyexpat code cleanup and minor refactorings:

The handlers array on each parser now has the invariant that None will
never be set as a handler; it will always be NULL or a Python-level
value passed in for the specific handler.

have_handler():  Return true if there is a Python handler for a
    particular event.

get_handler_name():  Return a string object giving the name of a
    particular handler.  This caches the string object so it doesn't
    need to be created more than once.

get_parse_result():  Helper to allow the Parse() and ParseFile()
    methods to share the same logic for determining the return value
    or exception state.

PyUnknownEncodingHandler(), PyModule_AddIntConstant():
    Made these helpers static.  (The later is only defined for older
    versions of Python.)

pyxml_UpdatePairedHandlers(), pyxml_SetStartElementHandler(),
pyxml_SetEndElementHandler(), pyxml_SetStartNamespaceDeclHandler(),
pyxml_SetEndNamespaceDeclHandler(), pyxml_SetStartCdataSection(),
pyxml_SetEndCdataSection(), pyxml_SetStartDoctypeDeclHandler(),
pyxml_SetEndDoctypeDeclHandler():
    Removed.  These are no longer needed with Expat 1.95.x.

handler_info:
    Use the setter functions provided by Expat 1.95.x instead of the
    pyxml_Set*Handler() functions which have been removed.

Minor code formatting changes for consistency.
Trailing whitespace removed.
This commit is contained in:
Fred Drake 2002-06-28 22:29:01 +00:00
parent c9051640f8
commit 71b63ff342

View file

@ -74,6 +74,7 @@ struct HandlerInfo {
xmlhandlersetter setter; xmlhandlersetter setter;
xmlhandler handler; xmlhandler handler;
PyCodeObject *tb_code; PyCodeObject *tb_code;
PyObject *nameobj;
}; };
staticforward struct HandlerInfo handler_info[64]; staticforward struct HandlerInfo handler_info[64];
@ -118,6 +119,25 @@ set_error(xmlparseobject *self)
return NULL; return NULL;
} }
static int
have_handler(xmlparseobject *self, int type)
{
PyObject *handler = self->handlers[type];
return handler != NULL;
}
static PyObject *
get_handler_name(struct HandlerInfo *hinfo)
{
PyObject *name = hinfo->nameobj;
if (name == NULL) {
name = PyString_FromString(hinfo->name);
hinfo->nameobj = name;
}
Py_XINCREF(name);
return name;
}
#ifdef Py_USING_UNICODE #ifdef Py_USING_UNICODE
/* Convert a string of XML_Chars into a Unicode string. /* Convert a string of XML_Chars into a Unicode string.
@ -133,8 +153,7 @@ conv_string_to_unicode(const XML_Char *str)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
return PyUnicode_DecodeUTF8(str, strlen(str), return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
"strict");
} }
static PyObject * static PyObject *
@ -296,12 +315,11 @@ string_intern(xmlparseobject *self, const char* str)
static void static void
my_StartElementHandler(void *userData, my_StartElementHandler(void *userData,
const XML_Char *name, const XML_Char **atts) const XML_Char *name, const XML_Char *atts[])
{ {
xmlparseobject *self = (xmlparseobject *)userData; xmlparseobject *self = (xmlparseobject *)userData;
if (self->handlers[StartElement] if (have_handler(self, StartElement)) {
&& self->handlers[StartElement] != Py_None) {
PyObject *container, *rv, *args; PyObject *container, *rv, *args;
int i, max; int i, max;
@ -383,8 +401,7 @@ my_##NAME##Handler PARAMS {\
PyObject *rv = NULL; \ PyObject *rv = NULL; \
INIT \ INIT \
\ \
if (self->handlers[NAME] \ if (have_handler(self, NAME)) { \
&& self->handlers[NAME] != Py_None) { \
args = Py_BuildValue PARAM_FORMAT ;\ args = Py_BuildValue PARAM_FORMAT ;\
if (!args) { flag_error(self); return RETURN;} \ if (!args) { flag_error(self); return RETURN;} \
self->in_callback = 1; \ self->in_callback = 1; \
@ -647,6 +664,18 @@ VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
static PyObject *
get_parse_result(xmlparseobject *self, int rv)
{
if (PyErr_Occurred()) {
return NULL;
}
if (rv == 0) {
return set_error(self);
}
return PyInt_FromLong(rv);
}
PyDoc_STRVAR(xmlparse_Parse__doc__, PyDoc_STRVAR(xmlparse_Parse__doc__,
"Parse(data[, isfinal])\n\ "Parse(data[, isfinal])\n\
Parse XML data. `isfinal' should be true at end of input."); Parse XML data. `isfinal' should be true at end of input.");
@ -657,18 +686,11 @@ xmlparse_Parse(xmlparseobject *self, PyObject *args)
char *s; char *s;
int slen; int slen;
int isFinal = 0; int isFinal = 0;
int rv;
if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal)) if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
return NULL; return NULL;
rv = XML_Parse(self->itself, s, slen, isFinal);
if (PyErr_Occurred()) { return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
return NULL;
}
else if (rv == 0) {
return set_error(self);
}
return PyInt_FromLong(rv);
} }
/* File reading copied from cPickle */ /* File reading copied from cPickle */
@ -770,10 +792,7 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
if (!rv || bytes_read == 0) if (!rv || bytes_read == 0)
break; break;
} }
if (rv == 0) { return get_parse_result(self, rv);
return set_error(self);
}
return Py_BuildValue("i", rv);
} }
PyDoc_STRVAR(xmlparse_SetBase__doc__, PyDoc_STRVAR(xmlparse_SetBase__doc__,
@ -906,9 +925,10 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
/* then copy handlers from self */ /* then copy handlers from self */
for (i = 0; handler_info[i].name != NULL; i++) { for (i = 0; handler_info[i].name != NULL; i++) {
if (self->handlers[i]) { PyObject *handler = self->handlers[i];
Py_INCREF(self->handlers[i]); if (handler != NULL) {
new_parser->handlers[i] = self->handlers[i]; Py_INCREF(handler);
new_parser->handlers[i] = handler;
handler_info[i].setter(new_parser->itself, handler_info[i].setter(new_parser->itself,
handler_info[i].handler); handler_info[i].handler);
} }
@ -957,8 +977,7 @@ static struct PyMethodDef xmlparse_methods[] = {
#ifdef Py_USING_UNICODE #ifdef Py_USING_UNICODE
/* /* pyexpat international encoding support.
pyexpat international encoding support.
Make it as simple as possible. Make it as simple as possible.
*/ */
@ -975,10 +994,10 @@ init_template_buffer(void)
template_buffer[256] = 0; template_buffer[256] = 0;
} }
int static int
PyUnknownEncodingHandler(void *encodingHandlerData, PyUnknownEncodingHandler(void *encodingHandlerData,
const XML_Char *name, const XML_Char *name,
XML_Encoding * info) XML_Encoding *info)
{ {
PyUnicodeObject *_u_string = NULL; PyUnicodeObject *_u_string = NULL;
int result = 0; int result = 0;
@ -999,12 +1018,10 @@ XML_Encoding * info)
else else
info->map[i] = c; info->map[i] = c;
} }
info->data = NULL; info->data = NULL;
info->convert = NULL; info->convert = NULL;
info->release = NULL; info->release = NULL;
result=1; result = 1;
Py_DECREF(_u_string); Py_DECREF(_u_string);
return result; return result;
} }
@ -1099,6 +1116,7 @@ xmlparse_dealloc(xmlparseobject *self)
Py_XDECREF(temp); Py_XDECREF(temp);
} }
free(self->handlers); free(self->handlers);
self->handlers = NULL;
} }
Py_XDECREF(self->intern); Py_XDECREF(self->intern);
#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
@ -1119,7 +1137,7 @@ static int
handlername2int(const char *name) handlername2int(const char *name)
{ {
int i; int i;
for (i=0; handler_info[i].name != NULL; i++) { for (i = 0; handler_info[i].name != NULL; i++) {
if (strcmp(name, handler_info[i].name) == 0) { if (strcmp(name, handler_info[i].name) == 0) {
return i; return i;
} }
@ -1127,24 +1145,46 @@ handlername2int(const char *name)
return -1; return -1;
} }
static PyObject *
get_pybool(int istrue)
{
PyObject *result = istrue ? Py_True : Py_False;
Py_INCREF(result);
return result;
}
static PyObject * static PyObject *
xmlparse_getattr(xmlparseobject *self, char *name) xmlparse_getattr(xmlparseobject *self, char *name)
{ {
int handlernum; int handlernum = handlername2int(name);
if (handlernum != -1) {
PyObject *result = self->handlers[handlernum];
if (result == NULL)
result = Py_None;
Py_INCREF(result);
return result;
}
if (name[0] == 'E') {
if (strcmp(name, "ErrorCode") == 0) if (strcmp(name, "ErrorCode") == 0)
return PyInt_FromLong((long) XML_GetErrorCode(self->itself)); return PyInt_FromLong((long)
XML_GetErrorCode(self->itself));
if (strcmp(name, "ErrorLineNumber") == 0) if (strcmp(name, "ErrorLineNumber") == 0)
return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself)); return PyInt_FromLong((long)
XML_GetErrorLineNumber(self->itself));
if (strcmp(name, "ErrorColumnNumber") == 0) if (strcmp(name, "ErrorColumnNumber") == 0)
return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself)); return PyInt_FromLong((long)
XML_GetErrorColumnNumber(self->itself));
if (strcmp(name, "ErrorByteIndex") == 0) if (strcmp(name, "ErrorByteIndex") == 0)
return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself)); return PyInt_FromLong((long)
XML_GetErrorByteIndex(self->itself));
}
if (strcmp(name, "ordered_attributes") == 0) if (strcmp(name, "ordered_attributes") == 0)
return PyInt_FromLong((long) self->ordered_attributes); return get_pybool(self->ordered_attributes);
if (strcmp(name, "returns_unicode") == 0) if (strcmp(name, "returns_unicode") == 0)
return PyInt_FromLong((long) self->returns_unicode); return get_pybool((long) self->returns_unicode);
if (strcmp(name, "specified_attributes") == 0) if (strcmp(name, "specified_attributes") == 0)
return PyInt_FromLong((long) self->specified_attributes); return get_pybool((long) self->specified_attributes);
if (strcmp(name, "intern") == 0) { if (strcmp(name, "intern") == 0) {
if (self->intern == NULL) { if (self->intern == NULL) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
@ -1156,17 +1196,11 @@ xmlparse_getattr(xmlparseobject *self, char *name)
} }
} }
handlernum = handlername2int(name);
if (handlernum != -1 && self->handlers[handlernum] != NULL) {
Py_INCREF(self->handlers[handlernum]);
return self->handlers[handlernum];
}
if (strcmp(name, "__members__") == 0) { if (strcmp(name, "__members__") == 0) {
int i; int i;
PyObject *rc = PyList_New(0); PyObject *rc = PyList_New(0);
for(i = 0; handler_info[i].name != NULL; i++) { for (i = 0; handler_info[i].name != NULL; i++) {
PyList_Append(rc, PyString_FromString(handler_info[i].name)); PyList_Append(rc, get_handler_name(&handler_info[i]));
} }
PyList_Append(rc, PyString_FromString("ErrorCode")); PyList_Append(rc, PyString_FromString("ErrorCode"));
PyList_Append(rc, PyString_FromString("ErrorLineNumber")); PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
@ -1186,12 +1220,19 @@ static int
sethandler(xmlparseobject *self, const char *name, PyObject* v) sethandler(xmlparseobject *self, const char *name, PyObject* v)
{ {
int handlernum = handlername2int(name); int handlernum = handlername2int(name);
if (handlernum != -1) { if (handlernum >= 0) {
xmlhandler c_handler = NULL;
PyObject *temp = self->handlers[handlernum];
if (v == Py_None)
v = NULL;
else if (v != NULL) {
Py_INCREF(v); Py_INCREF(v);
Py_XDECREF(self->handlers[handlernum]); c_handler = handler_info[handlernum].handler;
}
self->handlers[handlernum] = v; self->handlers[handlernum] = v;
handler_info[handlernum].setter(self->itself, Py_XDECREF(temp);
handler_info[handlernum].handler); handler_info[handlernum].setter(self->itself, c_handler);
return 1; return 1;
} }
return 0; return 0;
@ -1216,7 +1257,7 @@ xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
if (PyObject_IsTrue(v)) { if (PyObject_IsTrue(v)) {
#ifndef Py_USING_UNICODE #ifndef Py_USING_UNICODE
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Cannot return Unicode strings in Python 1.5"); "Unicode support not available");
return -1; return -1;
#else #else
self->returns_unicode = 1; self->returns_unicode = 1;
@ -1401,7 +1442,7 @@ PyModule_AddObject(PyObject *m, char *name, PyObject *o)
return 0; return 0;
} }
int static int
PyModule_AddIntConstant(PyObject *m, char *name, long value) PyModule_AddIntConstant(PyObject *m, char *name, long value)
{ {
return PyModule_AddObject(m, name, PyInt_FromLong(value)); return PyModule_AddObject(m, name, PyInt_FromLong(value));
@ -1590,9 +1631,9 @@ clear_handlers(xmlparseobject *self, int initial)
int i = 0; int i = 0;
PyObject *temp; PyObject *temp;
for (; handler_info[i].name!=NULL; i++) { for (; handler_info[i].name != NULL; i++) {
if (initial) if (initial)
self->handlers[i]=NULL; self->handlers[i] = NULL;
else { else {
temp = self->handlers[i]; temp = self->handlers[i];
self->handlers[i] = NULL; self->handlers[i] = NULL;
@ -1602,98 +1643,12 @@ clear_handlers(xmlparseobject *self, int initial)
} }
} }
typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
static void
pyxml_UpdatePairedHandlers(xmlparseobject *self,
int startHandler,
int endHandler,
pairsetter setter)
{
void *start_handler = NULL;
void *end_handler = NULL;
if (self->handlers[startHandler]
&& self->handlers[startHandler] != Py_None) {
start_handler = handler_info[startHandler].handler;
}
if (self->handlers[endHandler]
&& self->handlers[endHandler] != Py_None) {
end_handler = handler_info[endHandler].handler;
}
setter(self->itself, start_handler, end_handler);
}
static void
pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
{
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
StartElement, EndElement,
(pairsetter)XML_SetElementHandler);
}
static void
pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
{
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
StartElement, EndElement,
(pairsetter)XML_SetElementHandler);
}
static void
pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
{
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
StartNamespaceDecl, EndNamespaceDecl,
(pairsetter)XML_SetNamespaceDeclHandler);
}
static void
pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
{
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
StartNamespaceDecl, EndNamespaceDecl,
(pairsetter)XML_SetNamespaceDeclHandler);
}
static void
pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
{
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
StartCdataSection, EndCdataSection,
(pairsetter)XML_SetCdataSectionHandler);
}
static void
pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
{
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
StartCdataSection, EndCdataSection,
(pairsetter)XML_SetCdataSectionHandler);
}
static void
pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
{
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
StartDoctypeDecl, EndDoctypeDecl,
(pairsetter)XML_SetDoctypeDeclHandler);
}
static void
pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
{
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
StartDoctypeDecl, EndDoctypeDecl,
(pairsetter)XML_SetDoctypeDeclHandler);
}
statichere struct HandlerInfo handler_info[] = { statichere struct HandlerInfo handler_info[] = {
{"StartElementHandler", {"StartElementHandler",
pyxml_SetStartElementHandler, (xmlhandlersetter)XML_SetStartElementHandler,
(xmlhandler)my_StartElementHandler}, (xmlhandler)my_StartElementHandler},
{"EndElementHandler", {"EndElementHandler",
pyxml_SetEndElementHandler, (xmlhandlersetter)XML_SetEndElementHandler,
(xmlhandler)my_EndElementHandler}, (xmlhandler)my_EndElementHandler},
{"ProcessingInstructionHandler", {"ProcessingInstructionHandler",
(xmlhandlersetter)XML_SetProcessingInstructionHandler, (xmlhandlersetter)XML_SetProcessingInstructionHandler,
@ -1708,19 +1663,19 @@ statichere struct HandlerInfo handler_info[] = {
(xmlhandlersetter)XML_SetNotationDeclHandler, (xmlhandlersetter)XML_SetNotationDeclHandler,
(xmlhandler)my_NotationDeclHandler }, (xmlhandler)my_NotationDeclHandler },
{"StartNamespaceDeclHandler", {"StartNamespaceDeclHandler",
pyxml_SetStartNamespaceDeclHandler, (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
(xmlhandler)my_StartNamespaceDeclHandler }, (xmlhandler)my_StartNamespaceDeclHandler },
{"EndNamespaceDeclHandler", {"EndNamespaceDeclHandler",
pyxml_SetEndNamespaceDeclHandler, (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
(xmlhandler)my_EndNamespaceDeclHandler }, (xmlhandler)my_EndNamespaceDeclHandler },
{"CommentHandler", {"CommentHandler",
(xmlhandlersetter)XML_SetCommentHandler, (xmlhandlersetter)XML_SetCommentHandler,
(xmlhandler)my_CommentHandler}, (xmlhandler)my_CommentHandler},
{"StartCdataSectionHandler", {"StartCdataSectionHandler",
pyxml_SetStartCdataSection, (xmlhandlersetter)XML_SetStartCdataSectionHandler,
(xmlhandler)my_StartCdataSectionHandler}, (xmlhandler)my_StartCdataSectionHandler},
{"EndCdataSectionHandler", {"EndCdataSectionHandler",
pyxml_SetEndCdataSection, (xmlhandlersetter)XML_SetEndCdataSectionHandler,
(xmlhandler)my_EndCdataSectionHandler}, (xmlhandler)my_EndCdataSectionHandler},
{"DefaultHandler", {"DefaultHandler",
(xmlhandlersetter)XML_SetDefaultHandler, (xmlhandlersetter)XML_SetDefaultHandler,
@ -1735,10 +1690,10 @@ statichere struct HandlerInfo handler_info[] = {
(xmlhandlersetter)XML_SetExternalEntityRefHandler, (xmlhandlersetter)XML_SetExternalEntityRefHandler,
(xmlhandler)my_ExternalEntityRefHandler }, (xmlhandler)my_ExternalEntityRefHandler },
{"StartDoctypeDeclHandler", {"StartDoctypeDeclHandler",
pyxml_SetStartDoctypeDeclHandler, (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
(xmlhandler)my_StartDoctypeDeclHandler}, (xmlhandler)my_StartDoctypeDeclHandler},
{"EndDoctypeDeclHandler", {"EndDoctypeDeclHandler",
pyxml_SetEndDoctypeDeclHandler, (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
(xmlhandler)my_EndDoctypeDeclHandler}, (xmlhandler)my_EndDoctypeDeclHandler},
{"EntityDeclHandler", {"EntityDeclHandler",
(xmlhandlersetter)XML_SetEntityDeclHandler, (xmlhandlersetter)XML_SetEntityDeclHandler,