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
1 changed files with 176 additions and 221 deletions

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.
@ -126,21 +146,20 @@ set_error(xmlparseobject *self)
static PyObject * static PyObject *
conv_string_to_unicode(const XML_Char *str) conv_string_to_unicode(const XML_Char *str)
{ {
/* XXX currently this code assumes that XML_Char is 8-bit, /* XXX currently this code assumes that XML_Char is 8-bit,
and hence in UTF-8. */ and hence in UTF-8. */
/* UTF-8 from Expat, Unicode desired */ /* UTF-8 from Expat, Unicode desired */
if (str == NULL) { if (str == NULL) {
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 *
conv_string_len_to_unicode(const XML_Char *str, int len) conv_string_len_to_unicode(const XML_Char *str, int len)
{ {
/* XXX currently this code assumes that XML_Char is 8-bit, /* XXX currently this code assumes that XML_Char is 8-bit,
and hence in UTF-8. */ and hence in UTF-8. */
/* UTF-8 from Expat, Unicode desired */ /* UTF-8 from Expat, Unicode desired */
if (str == NULL) { if (str == NULL) {
@ -157,7 +176,7 @@ conv_string_len_to_unicode(const XML_Char *str, int len)
static PyObject * static PyObject *
conv_string_to_utf8(const XML_Char *str) conv_string_to_utf8(const XML_Char *str)
{ {
/* XXX currently this code assumes that XML_Char is 8-bit, /* XXX currently this code assumes that XML_Char is 8-bit,
and hence in UTF-8. */ and hence in UTF-8. */
/* UTF-8 from Expat, UTF-8 desired */ /* UTF-8 from Expat, UTF-8 desired */
if (str == NULL) { if (str == NULL) {
@ -168,9 +187,9 @@ conv_string_to_utf8(const XML_Char *str)
} }
static PyObject * static PyObject *
conv_string_len_to_utf8(const XML_Char *str, int len) conv_string_len_to_utf8(const XML_Char *str, int len)
{ {
/* XXX currently this code assumes that XML_Char is 8-bit, /* XXX currently this code assumes that XML_Char is 8-bit,
and hence in UTF-8. */ and hence in UTF-8. */
/* UTF-8 from Expat, UTF-8 desired */ /* UTF-8 from Expat, UTF-8 desired */
if (str == NULL) { if (str == NULL) {
@ -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; \
@ -411,38 +428,38 @@ my_##NAME##Handler PARAMS {\
rc = PyInt_AsLong(rv);, rc, \ rc = PyInt_AsLong(rv);, rc, \
(xmlparseobject *)userData) (xmlparseobject *)userData)
VOID_HANDLER(EndElement, VOID_HANDLER(EndElement,
(void *userData, const XML_Char *name), (void *userData, const XML_Char *name),
("(N)", string_intern(self, name))) ("(N)", string_intern(self, name)))
VOID_HANDLER(ProcessingInstruction, VOID_HANDLER(ProcessingInstruction,
(void *userData, (void *userData,
const XML_Char *target, const XML_Char *target,
const XML_Char *data), const XML_Char *data),
("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data)) ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
#ifndef Py_USING_UNICODE #ifndef Py_USING_UNICODE
VOID_HANDLER(CharacterData, VOID_HANDLER(CharacterData,
(void *userData, const XML_Char *data, int len), (void *userData, const XML_Char *data, int len),
("(N)", conv_string_len_to_utf8(data,len))) ("(N)", conv_string_len_to_utf8(data,len)))
#else #else
VOID_HANDLER(CharacterData, VOID_HANDLER(CharacterData,
(void *userData, const XML_Char *data, int len), (void *userData, const XML_Char *data, int len),
("(N)", (self->returns_unicode ("(N)", (self->returns_unicode
? conv_string_len_to_unicode(data,len) ? conv_string_len_to_unicode(data,len)
: conv_string_len_to_utf8(data,len)))) : conv_string_len_to_utf8(data,len))))
#endif #endif
VOID_HANDLER(UnparsedEntityDecl, VOID_HANDLER(UnparsedEntityDecl,
(void *userData, (void *userData,
const XML_Char *entityName, const XML_Char *entityName,
const XML_Char *base, const XML_Char *base,
const XML_Char *systemId, const XML_Char *systemId,
const XML_Char *publicId, const XML_Char *publicId,
const XML_Char *notationName), const XML_Char *notationName),
("(NNNNN)", ("(NNNNN)",
string_intern(self, entityName), string_intern(self, base), string_intern(self, entityName), string_intern(self, base),
string_intern(self, systemId), string_intern(self, publicId), string_intern(self, systemId), string_intern(self, publicId),
string_intern(self, notationName))) string_intern(self, notationName)))
#ifndef Py_USING_UNICODE #ifndef Py_USING_UNICODE
@ -475,8 +492,8 @@ VOID_HANDLER(EntityDecl,
const XML_Char *notationName), const XML_Char *notationName),
("NiNNNNN", ("NiNNNNN",
string_intern(self, entityName), is_parameter_entity, string_intern(self, entityName), is_parameter_entity,
(self->returns_unicode (self->returns_unicode
? conv_string_len_to_unicode(value, value_length) ? conv_string_len_to_unicode(value, value_length)
: conv_string_len_to_utf8(value, value_length)), : conv_string_len_to_utf8(value, value_length)),
string_intern(self, base), string_intern(self, systemId), string_intern(self, base), string_intern(self, systemId),
string_intern(self, publicId), string_intern(self, publicId),
@ -489,7 +506,7 @@ VOID_HANDLER(XmlDecl,
const XML_Char *encoding, const XML_Char *encoding,
int standalone), int standalone),
("(O&O&i)", ("(O&O&i)",
STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding, STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
standalone)) standalone))
static PyObject * static PyObject *
@ -560,14 +577,14 @@ VOID_HANDLER(AttlistDecl,
STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt, STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
isrequired)) isrequired))
VOID_HANDLER(NotationDecl, VOID_HANDLER(NotationDecl,
(void *userData, (void *userData,
const XML_Char *notationName, const XML_Char *notationName,
const XML_Char *base, const XML_Char *base,
const XML_Char *systemId, const XML_Char *systemId,
const XML_Char *publicId), const XML_Char *publicId),
("(NNNN)", ("(NNNN)",
string_intern(self, notationName), string_intern(self, base), string_intern(self, notationName), string_intern(self, base),
string_intern(self, systemId), string_intern(self, publicId))) string_intern(self, systemId), string_intern(self, publicId)))
VOID_HANDLER(StartNamespaceDecl, VOID_HANDLER(StartNamespaceDecl,
@ -589,35 +606,35 @@ VOID_HANDLER(Comment,
VOID_HANDLER(StartCdataSection, VOID_HANDLER(StartCdataSection,
(void *userData), (void *userData),
("()")) ("()"))
VOID_HANDLER(EndCdataSection, VOID_HANDLER(EndCdataSection,
(void *userData), (void *userData),
("()")) ("()"))
#ifndef Py_USING_UNICODE #ifndef Py_USING_UNICODE
VOID_HANDLER(Default, VOID_HANDLER(Default,
(void *userData, const XML_Char *s, int len), (void *userData, const XML_Char *s, int len),
("(N)", conv_string_len_to_utf8(s,len))) ("(N)", conv_string_len_to_utf8(s,len)))
VOID_HANDLER(DefaultHandlerExpand, VOID_HANDLER(DefaultHandlerExpand,
(void *userData, const XML_Char *s, int len), (void *userData, const XML_Char *s, int len),
("(N)", conv_string_len_to_utf8(s,len))) ("(N)", conv_string_len_to_utf8(s,len)))
#else #else
VOID_HANDLER(Default, VOID_HANDLER(Default,
(void *userData, const XML_Char *s, int len), (void *userData, const XML_Char *s, int len),
("(N)", (self->returns_unicode ("(N)", (self->returns_unicode
? conv_string_len_to_unicode(s,len) ? conv_string_len_to_unicode(s,len)
: conv_string_len_to_utf8(s,len)))) : conv_string_len_to_utf8(s,len))))
VOID_HANDLER(DefaultHandlerExpand, VOID_HANDLER(DefaultHandlerExpand,
(void *userData, const XML_Char *s, int len), (void *userData, const XML_Char *s, int len),
("(N)", (self->returns_unicode ("(N)", (self->returns_unicode
? conv_string_len_to_unicode(s,len) ? conv_string_len_to_unicode(s,len)
: conv_string_len_to_utf8(s,len)))) : conv_string_len_to_utf8(s,len))))
#endif #endif
INT_HANDLER(NotStandalone, INT_HANDLER(NotStandalone,
(void *userData), (void *userData),
("()")) ("()"))
RC_HANDLER(int, ExternalEntityRef, RC_HANDLER(int, ExternalEntityRef,
@ -628,7 +645,7 @@ RC_HANDLER(int, ExternalEntityRef,
const XML_Char *publicId), const XML_Char *publicId),
int rc=0;, int rc=0;,
("(O&NNN)", ("(O&NNN)",
STRING_CONV_FUNC,context, string_intern(self, base), STRING_CONV_FUNC,context, string_intern(self, base),
string_intern(self, systemId), string_intern(self, publicId)), string_intern(self, systemId), string_intern(self, publicId)),
rc = PyInt_AsLong(rv);, rc, rc = PyInt_AsLong(rv);, rc,
XML_GetUserData(parser)) XML_GetUserData(parser))
@ -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 */
@ -696,7 +718,7 @@ readinst(char *buf, int buf_size, PyObject *meth)
/* XXX what to do if it returns a Unicode string? */ /* XXX what to do if it returns a Unicode string? */
if (!PyString_Check(str)) { if (!PyString_Check(str)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"read() did not return a string object (type=%.400s)", "read() did not return a string object (type=%.400s)",
str->ob_type->tp_name); str->ob_type->tp_name);
goto finally; goto finally;
@ -740,7 +762,7 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
readmethod = PyObject_GetAttrString(f, "read"); readmethod = PyObject_GetAttrString(f, "read");
if (readmethod == NULL) { if (readmethod == NULL) {
PyErr_Clear(); PyErr_Clear();
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"argument must have 'read' attribute"); "argument must have 'read' attribute");
return 0; return 0;
} }
@ -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,14 +925,15 @@ 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);
handler_info[i].setter(new_parser->itself, new_parser->handlers[i] = handler;
handler_info[i].setter(new_parser->itself,
handler_info[i].handler); handler_info[i].handler);
} }
} }
return (PyObject *)new_parser; return (PyObject *)new_parser;
} }
PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__, PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
@ -957,15 +977,14 @@ 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.
*/ */
static char template_buffer[257]; static char template_buffer[257];
PyObject *template_string = NULL; PyObject *template_string = NULL;
static void static void
init_template_buffer(void) init_template_buffer(void)
{ {
int i; int i;
@ -975,22 +994,22 @@ 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;
int i; int i;
/* Yes, supports only 8bit encodings */ /* Yes, supports only 8bit encodings */
_u_string = (PyUnicodeObject *) _u_string = (PyUnicodeObject *)
PyUnicode_Decode(template_buffer, 256, name, "replace"); PyUnicode_Decode(template_buffer, 256, name, "replace");
if (_u_string == NULL) if (_u_string == NULL)
return result; return result;
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
/* Stupid to access directly, but fast */ /* Stupid to access directly, but fast */
Py_UNICODE c = _u_string->str[i]; Py_UNICODE c = _u_string->str[i];
@ -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;
} }
@ -1016,7 +1033,7 @@ newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
{ {
int i; int i;
xmlparseobject *self; xmlparseobject *self;
#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
self = PyObject_NEW(xmlparseobject, &Xmlparsetype); self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
if (self == NULL) if (self == NULL)
@ -1054,7 +1071,7 @@ newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
PyObject_GC_Init(self); PyObject_GC_Init(self);
#endif #endif
if (self->itself == NULL) { if (self->itself == NULL) {
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"XML_ParserCreate failed"); "XML_ParserCreate failed");
Py_DECREF(self); Py_DECREF(self);
return NULL; return NULL;
@ -1069,8 +1086,8 @@ newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
self->handlers = malloc(sizeof(PyObject *)*i); self->handlers = malloc(sizeof(PyObject *)*i);
if (!self->handlers){ if (!self->handlers){
Py_DECREF(self); Py_DECREF(self);
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
clear_handlers(self, 1); clear_handlers(self, 1);
@ -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 (strcmp(name, "ErrorCode") == 0)
return PyInt_FromLong((long) XML_GetErrorCode(self->itself)); if (handlernum != -1) {
if (strcmp(name, "ErrorLineNumber") == 0) PyObject *result = self->handlers[handlernum];
return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself)); if (result == NULL)
if (strcmp(name, "ErrorColumnNumber") == 0) result = Py_None;
return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself)); Py_INCREF(result);
if (strcmp(name, "ErrorByteIndex") == 0) return result;
return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself)); }
if (name[0] == 'E') {
if (strcmp(name, "ErrorCode") == 0)
return PyInt_FromLong((long)
XML_GetErrorCode(self->itself));
if (strcmp(name, "ErrorLineNumber") == 0)
return PyInt_FromLong((long)
XML_GetErrorLineNumber(self->itself));
if (strcmp(name, "ErrorColumnNumber") == 0)
return PyInt_FromLong((long)
XML_GetErrorColumnNumber(self->itself));
if (strcmp(name, "ErrorByteIndex") == 0)
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) {
Py_INCREF(v); xmlhandler c_handler = NULL;
Py_XDECREF(self->handlers[handlernum]); PyObject *temp = self->handlers[handlernum];
if (v == Py_None)
v = NULL;
else if (v != NULL) {
Py_INCREF(v);
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;
@ -1215,8 +1256,8 @@ xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
if (strcmp(name, "returns_unicode") == 0) { if (strcmp(name, "returns_unicode") == 0) {
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;
@ -1290,9 +1331,9 @@ static PyTypeObject Xmlparsetype = {
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
#ifdef Py_TPFLAGS_HAVE_GC #ifdef Py_TPFLAGS_HAVE_GC
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
#else #else
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
#endif #endif
Xmlparsetype__doc__, /* Documentation string */ Xmlparsetype__doc__, /* Documentation string */
#ifdef WITH_CYCLE_GC #ifdef WITH_CYCLE_GC
@ -1318,7 +1359,7 @@ pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
PyObject *intern = NULL; PyObject *intern = NULL;
PyObject *result; PyObject *result;
int intern_decref = 0; int intern_decref = 0;
static char *kwlist[] = {"encoding", "namespace_separator", static char *kwlist[] = {"encoding", "namespace_separator",
"intern", NULL}; "intern", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
@ -1340,7 +1381,7 @@ pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
if (!intern) if (!intern)
return NULL; return NULL;
intern_decref = 1; intern_decref = 1;
} }
else if (!PyDict_Check(intern)) { else if (!PyDict_Check(intern)) {
PyErr_SetString(PyExc_TypeError, "intern must be a dictionary"); PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
return NULL; return NULL;
@ -1374,7 +1415,7 @@ static struct PyMethodDef pyexpat_methods[] = {
METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__}, METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
{"ErrorString", (PyCFunction)pyexpat_ErrorString, {"ErrorString", (PyCFunction)pyexpat_ErrorString,
METH_VARARGS, pyexpat_ErrorString__doc__}, METH_VARARGS, pyexpat_ErrorString__doc__},
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */ {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
}; };
@ -1401,13 +1442,13 @@ 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));
} }
static int static int
PyModule_AddStringConstant(PyObject *m, char *name, char *value) PyModule_AddStringConstant(PyObject *m, char *name, char *value)
{ {
return PyModule_AddObject(m, name, PyString_FromString(value)); return PyModule_AddObject(m, name, PyString_FromString(value));
@ -1497,8 +1538,8 @@ MODULE_INITFUNC(void)
init_template_buffer(); init_template_buffer();
#endif #endif
/* XXX When Expat supports some way of figuring out how it was /* XXX When Expat supports some way of figuring out how it was
compiled, this should check and set native_encoding compiled, this should check and set native_encoding
appropriately. appropriately.
*/ */
PyModule_AddStringConstant(m, "native_encoding", "UTF-8"); PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
@ -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,125 +1643,39 @@ 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,
(xmlhandler)my_ProcessingInstructionHandler}, (xmlhandler)my_ProcessingInstructionHandler},
{"CharacterDataHandler", {"CharacterDataHandler",
(xmlhandlersetter)XML_SetCharacterDataHandler, (xmlhandlersetter)XML_SetCharacterDataHandler,
(xmlhandler)my_CharacterDataHandler}, (xmlhandler)my_CharacterDataHandler},
{"UnparsedEntityDeclHandler", {"UnparsedEntityDeclHandler",
(xmlhandlersetter)XML_SetUnparsedEntityDeclHandler, (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
(xmlhandler)my_UnparsedEntityDeclHandler }, (xmlhandler)my_UnparsedEntityDeclHandler },
{"NotationDeclHandler", {"NotationDeclHandler",
(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,