Commit eb416cf4 authored by Fred Drake's avatar Fred Drake

Convert coding style to be internally consistent and similar to the

rest of the Python C code: space between "if", "for" and "(", no space
between "(", ")" and function call parameters, etc.
parent b21034f4
...@@ -22,22 +22,22 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. ...@@ -22,22 +22,22 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/ */
#define VERSION "1.9" #define VERSION "1.9"
enum HandlerTypes{ enum HandlerTypes {
StartElement, StartElement,
EndElement, EndElement,
ProcessingInstruction, ProcessingInstruction,
CharacterData, CharacterData,
UnparsedEntityDecl, UnparsedEntityDecl,
NotationDecl, NotationDecl,
StartNamespaceDecl, StartNamespaceDecl,
EndNamespaceDecl, EndNamespaceDecl,
Comment, Comment,
StartCdataSection, StartCdataSection,
EndCdataSection, EndCdataSection,
Default, Default,
DefaultHandlerExpand, DefaultHandlerExpand,
NotStandalone, NotStandalone,
ExternalEntityRef ExternalEntityRef
}; };
static PyObject *ErrorObject; static PyObject *ErrorObject;
...@@ -47,12 +47,12 @@ static PyObject *ErrorObject; ...@@ -47,12 +47,12 @@ static PyObject *ErrorObject;
/* Declarations for objects of type xmlparser */ /* Declarations for objects of type xmlparser */
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
XML_Parser itself; XML_Parser itself;
int returns_unicode; /* True if Unicode strings are returned; int returns_unicode; /* True if Unicode strings are returned;
if false, UTF-8 strings are returned */ if false, UTF-8 strings are returned */
PyObject **handlers; PyObject **handlers;
} xmlparseobject; } xmlparseobject;
staticforward PyTypeObject Xmlparsetype; staticforward PyTypeObject Xmlparsetype;
...@@ -61,112 +61,126 @@ typedef void (*xmlhandlersetter)( XML_Parser *self, void *meth ); ...@@ -61,112 +61,126 @@ typedef void (*xmlhandlersetter)( XML_Parser *self, void *meth );
typedef void* xmlhandler; typedef void* xmlhandler;
struct HandlerInfo { struct HandlerInfo {
const char *name; const char *name;
xmlhandlersetter setter; xmlhandlersetter setter;
xmlhandler handler; xmlhandler handler;
}; };
staticforward struct HandlerInfo handler_info[64]; staticforward struct HandlerInfo handler_info[64];
/* Convert an array of attributes and their values into a Python dict */ /* Convert an array of attributes and their values into a Python dict */
static PyObject *conv_atts_using_string(XML_Char **atts) static PyObject *
conv_atts_using_string(XML_Char **atts)
{ {
PyObject *attrs_obj = NULL; PyObject *attrs_obj = NULL;
XML_Char **attrs_p, **attrs_k = NULL; XML_Char **attrs_p, **attrs_k = NULL;
int attrs_len; int attrs_len;
PyObject *rv; PyObject *rv;
if ((attrs_obj = PyDict_New()) == NULL) if ((attrs_obj = PyDict_New()) == NULL)
goto finally; goto finally;
for (attrs_len = 0, attrs_p = atts; for (attrs_len = 0, attrs_p = atts;
*attrs_p; *attrs_p;
attrs_p++, attrs_len++) { attrs_p++, attrs_len++) {
if (attrs_len % 2) { if (attrs_len % 2) {
rv = PyString_FromString(*attrs_p); rv = PyString_FromString(*attrs_p);
if (! rv) { if (!rv) {
Py_DECREF(attrs_obj); Py_DECREF(attrs_obj);
attrs_obj = NULL; attrs_obj = NULL;
goto finally; goto finally;
} }
if (PyDict_SetItemString(attrs_obj, if (PyDict_SetItemString(attrs_obj,
(char*)*attrs_k, rv) < 0) { (char*)*attrs_k, rv) < 0) {
Py_DECREF(attrs_obj); Py_DECREF(attrs_obj);
attrs_obj = NULL; attrs_obj = NULL;
goto finally; goto finally;
} }
Py_DECREF(rv); Py_DECREF(rv);
} }
else else
attrs_k = attrs_p; attrs_k = attrs_p;
} }
finally: finally:
return attrs_obj; return attrs_obj;
} }
#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6) #if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
static PyObject *conv_atts_using_unicode( XML_Char **atts){ static PyObject *
PyObject *attrs_obj=NULL; conv_atts_using_unicode(XML_Char **atts)
XML_Char **attrs_p, **attrs_k = NULL; {
int attrs_len; PyObject *attrs_obj = NULL;
XML_Char **attrs_p, **attrs_k = NULL;
int attrs_len;
if( (attrs_obj = PyDict_New()) == NULL ) if ((attrs_obj = PyDict_New()) == NULL)
goto finally;
for (attrs_len = 0, attrs_p = atts;
*attrs_p;
attrs_p++, attrs_len++) {
if (attrs_len % 2) {
PyObject *attr_str, *value_str;
const char *p = (const char *) (*attrs_k);
attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
if (!attr_str) {
Py_DECREF(attrs_obj);
attrs_obj = NULL;
goto finally;
}
p = (const char *) *attrs_p;
value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
if (!value_str) {
Py_DECREF(attrs_obj);
Py_DECREF(attr_str);
attrs_obj = NULL;
goto finally; goto finally;
for(attrs_len=0, attrs_p = atts; }
*attrs_p; if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
attrs_p++, attrs_len++) { Py_DECREF(attrs_obj);
if (attrs_len%2) { attrs_obj = NULL;
PyObject *attr_str, *value_str; goto finally;
const char *p = (const char *) (*attrs_k); }
attr_str=PyUnicode_DecodeUTF8(p, strlen(p), "strict"); Py_DECREF(attr_str);
if (! attr_str) { Py_DECREF(value_str);
Py_DECREF(attrs_obj);
attrs_obj=NULL;
goto finally;
}
p = (const char *) *attrs_p;
value_str=PyUnicode_DecodeUTF8(p, strlen(p), "strict");
if (! value_str) {
Py_DECREF(attrs_obj);
Py_DECREF(attr_str);
attrs_obj=NULL;
goto finally;
}
if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
Py_DECREF(attrs_obj);
attrs_obj=NULL;
goto finally;
}
Py_DECREF(attr_str);
Py_DECREF(value_str);
}
else attrs_k=attrs_p;
} }
finally: else
return attrs_obj; attrs_k = attrs_p;
}
finally:
return attrs_obj;
} }
/* Convert a string of XML_Chars into a Unicode string. /* Convert a string of XML_Chars into a Unicode string.
Returns None if str is a null pointer. */ Returns None if str is a null pointer. */
static PyObject *conv_string_to_unicode( XML_Char *str ) { static PyObject *
/* XXX currently this code assumes that XML_Char is 8-bit, conv_string_to_unicode(XML_Char *str)
and hence in UTF-8. */ {
/* UTF-8 from Expat, Unicode desired */ /* XXX currently this code assumes that XML_Char is 8-bit,
if (str == NULL) {Py_INCREF(Py_None); return Py_None;} and hence in UTF-8. */
return PyUnicode_DecodeUTF8( (const char *)str, /* UTF-8 from Expat, Unicode desired */
strlen( (const char *)str ), if (str == NULL) {
"strict" ); Py_INCREF(Py_None);
return Py_None;
}
return PyUnicode_DecodeUTF8((const char *)str,
strlen((const char *)str),
"strict");
} }
static PyObject *conv_string_len_to_unicode( const XML_Char *str, int len ) { static PyObject *
/* XXX currently this code assumes that XML_Char is 8-bit, conv_string_len_to_unicode(const XML_Char *str, int len)
and hence in UTF-8. */ {
/* UTF-8 from Expat, Unicode desired */ /* XXX currently this code assumes that XML_Char is 8-bit,
if (str == NULL) {Py_INCREF(Py_None); return Py_None;} and hence in UTF-8. */
return PyUnicode_DecodeUTF8( (const char *)str, /* UTF-8 from Expat, Unicode desired */
len, if (str == NULL) {
"strict" ); Py_INCREF(Py_None);
return Py_None;
}
return PyUnicode_DecodeUTF8((const char *)str,
len,
"strict");
} }
#endif #endif
...@@ -406,198 +420,180 @@ finally: ...@@ -406,198 +420,180 @@ finally:
*/ */
/* ---------------------------------------------------------------- */ /* ---------------------------------------------------------------- */
static char xmlparse_Parse__doc__[] = static char xmlparse_Parse__doc__[] =
"(data [,isfinal]) - Parse XML data" "Parse(data[, isfinal])
; Parse XML data. `isfinal' should be true at end of input.";
static PyObject * static PyObject *
xmlparse_Parse( xmlparseobject *self, PyObject *args ) xmlparse_Parse(xmlparseobject *self, PyObject *args)
{ {
char *s; char *s;
int slen; int slen;
int isFinal = 0; int isFinal = 0;
int rv; int rv;
if (!PyArg_ParseTuple(args, "s#|i", &s, &slen, &isFinal))
return NULL;
rv = XML_Parse(self->itself, s, slen, isFinal);
if( PyErr_Occurred() ){
return NULL;
}
else if (rv == 0) {
PyErr_Format(ErrorObject, "%.200s: line %i, column %i",
XML_ErrorString( XML_GetErrorCode(self->itself) ),
XML_GetErrorLineNumber(self->itself),
XML_GetErrorColumnNumber(self->itself) );
return NULL;
}
return Py_BuildValue("i", rv); if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
return NULL;
rv = XML_Parse(self->itself, s, slen, isFinal);
if (PyErr_Occurred()) {
return NULL;
}
else if (rv == 0) {
PyErr_Format(ErrorObject, "%.200s: line %i, column %i",
XML_ErrorString(XML_GetErrorCode(self->itself)),
XML_GetErrorLineNumber(self->itself),
XML_GetErrorColumnNumber(self->itself));
return NULL;
}
return PyInt_FromLong(rv);
} }
#define BUF_SIZE 2048 #define BUF_SIZE 2048
int readinst(char *buf, int buf_size, PyObject *meth){ static int
PyObject *arg=NULL; readinst(char *buf, int buf_size, PyObject *meth)
PyObject *bytes=NULL; {
PyObject *str=NULL; PyObject *arg = NULL;
int len = -1; PyObject *bytes = NULL;
PyObject *str = NULL;
UNLESS(bytes = PyInt_FromLong(buf_size)) { int len = -1;
if (!PyErr_Occurred())
PyErr_SetNone(PyExc_EOFError);
goto finally;
}
UNLESS(arg) UNLESS(bytes = PyInt_FromLong(buf_size)) {
UNLESS(arg = PyTuple_New(1)) if (!PyErr_Occurred())
goto finally; PyErr_SetNone(PyExc_EOFError);
goto finally;
}
UNLESS(arg)
UNLESS(arg = PyTuple_New(1))
goto finally;
if (PyTuple_SetItem(arg, 0, bytes) < 0) if (PyTuple_SetItem(arg, 0, bytes) < 0)
goto finally; goto finally;
UNLESS(str = PyObject_CallObject(meth, arg)) UNLESS(str = PyObject_CallObject(meth, arg))
goto finally; goto finally;
/* XXX what to do if it returns a Unicode string? */ /* XXX what to do if it returns a Unicode string? */
UNLESS(PyString_Check( str )) { UNLESS(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;
} }
len = PyString_GET_SIZE(str);
len = PyString_GET_SIZE(str); if (len > buf_size) {
if (len > buf_size) { PyErr_Format(PyExc_ValueError,
PyErr_Format(PyExc_ValueError, "read() returned too much data: "
"read() returned too much data: " "%i bytes requested, %i returned",
"%i bytes requested, %i returned", buf_size, len);
buf_size, len); Py_DECREF(str);
Py_DECREF(str); goto finally;
goto finally; }
} memcpy(buf, PyString_AsString(str), len);
memcpy(buf, PyString_AsString(str), len); Py_XDECREF(str);
Py_XDECREF(str);
finally: finally:
Py_XDECREF(arg); Py_XDECREF(arg);
return len; return len;
} }
static char xmlparse_ParseFile__doc__[] = static char xmlparse_ParseFile__doc__[] =
"(file) - Parse XML data" "ParseFile(file)
; Parse XML data from file-like object.";
static PyObject * static PyObject *
xmlparse_ParseFile( xmlparseobject *self, PyObject *args ) xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
{ {
int rv=1; int rv = 1;
PyObject *f; PyObject *f;
FILE *fp; FILE *fp;
PyObject *readmethod=NULL; PyObject *readmethod = NULL;
if (!PyArg_ParseTuple(args, "O", &f)) if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
return NULL; return NULL;
if (PyFile_Check(f)) { if (PyFile_Check(f)) {
fp = PyFile_AsFile(f); fp = PyFile_AsFile(f);
}else{ }
fp = NULL; else{
UNLESS(readmethod = PyObject_GetAttrString(f, "read")) { fp = NULL;
PyErr_Clear(); UNLESS(readmethod = PyObject_GetAttrString(f, "read")) {
PyErr_SetString( PyExc_TypeError, PyErr_Clear();
"argument must have 'read' attribute" ); PyErr_SetString(PyExc_TypeError,
return 0; "argument must have 'read' attribute");
} return 0;
} }
}
for (;;) { for (;;) {
int bytes_read; int bytes_read;
void *buf = XML_GetBuffer(self->itself, BUF_SIZE); void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
if (buf == NULL) { if (buf == NULL)
PyErr_SetString(PyExc_MemoryError, "out of memory"); return PyErr_NoMemory();
return NULL;
} if (fp) {
bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
if( fp ){ if (bytes_read < 0) {
bytes_read=fread( buf, sizeof( char ), BUF_SIZE, fp); PyErr_SetFromErrno(PyExc_IOError);
if (bytes_read < 0) { return NULL;
PyErr_SetFromErrno(PyExc_IOError); }
return NULL; }
} else {
} else { bytes_read = readinst(buf, BUF_SIZE, readmethod);
bytes_read=readinst( buf, BUF_SIZE, readmethod ); if (bytes_read < 0)
if (bytes_read < 0) return NULL;
return NULL; }
} rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
if (PyErr_Occurred())
rv=XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0); return NULL;
if( PyErr_Occurred() ){
return NULL;
}
if (!rv || bytes_read == 0)
break;
}
return Py_BuildValue("i", rv); if (!rv || bytes_read == 0)
break;
}
return Py_BuildValue("i", rv);
} }
static char xmlparse_SetBase__doc__[] = static char xmlparse_SetBase__doc__[] =
"(base_url) - Base URL string" "SetBase(base_url)
; Set the base URL for the parser.";
static PyObject * static PyObject *
xmlparse_SetBase( xmlparseobject *self, PyObject *args ){ xmlparse_SetBase(xmlparseobject *self, PyObject *args)
{
char *base; char *base;
if (!PyArg_ParseTuple(args, "s", &base)) if (!PyArg_ParseTuple(args, "s:SetBase", &base))
return NULL; return NULL;
if( !XML_SetBase( self->itself, base ) ){ if (!XML_SetBase(self->itself, base)) {
PyErr_SetNone(PyExc_MemoryError); return PyErr_NoMemory();
return NULL;
} }
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
static char xmlparse_GetBase__doc__[] = static char xmlparse_GetBase__doc__[] =
"() - returns base URL string " "GetBase() -> url
; Return base URL string for the parser.";
static PyObject * static PyObject *
xmlparse_GetBase( xmlparseobject *self, PyObject *args ){ xmlparse_GetBase(xmlparseobject *self, PyObject *args)
const XML_Char *base; {
PyObject *rc; if (!PyArg_ParseTuple(args, ":GetBase"))
if( PyTuple_Size( args )!=0 ){
PyArg_ParseTuple(args, "()" ); /* get good error reporting */
return NULL; return NULL;
}
base=XML_GetBase( self->itself );
if( base ){
rc=Py_BuildValue("s", base);
}else{
Py_INCREF(Py_None);
rc=Py_None;
}
return rc;
return Py_BuildValue("z", XML_GetBase(self->itself));
} }
static struct PyMethodDef xmlparse_methods[] = { static struct PyMethodDef xmlparse_methods[] = {
{"Parse", (PyCFunction)xmlparse_Parse, {"Parse", (PyCFunction)xmlparse_Parse,
METH_VARARGS, xmlparse_Parse__doc__}, METH_VARARGS, xmlparse_Parse__doc__},
{"ParseFile", (PyCFunction)xmlparse_ParseFile, {"ParseFile", (PyCFunction)xmlparse_ParseFile,
METH_VARARGS, xmlparse_ParseFile__doc__}, METH_VARARGS, xmlparse_ParseFile__doc__},
{"SetBase", (PyCFunction)xmlparse_SetBase, {"SetBase", (PyCFunction)xmlparse_SetBase,
METH_VARARGS, xmlparse_SetBase__doc__}, METH_VARARGS, xmlparse_SetBase__doc__},
{"GetBase", (PyCFunction)xmlparse_GetBase, {"GetBase", (PyCFunction)xmlparse_GetBase,
METH_VARARGS, xmlparse_GetBase__doc__}, METH_VARARGS, xmlparse_GetBase__doc__},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
...@@ -605,136 +601,135 @@ static struct PyMethodDef xmlparse_methods[] = { ...@@ -605,136 +601,135 @@ static struct PyMethodDef xmlparse_methods[] = {
static xmlparseobject * static xmlparseobject *
newxmlparseobject( char *encoding, char *namespace_separator){ newxmlparseobject(char *encoding, char *namespace_separator)
int i; {
xmlparseobject *self; int i;
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)
return NULL; return NULL;
self->returns_unicode = 0; self->returns_unicode = 0;
#else #else
/* Code for versions 1.6 and later */ /* Code for versions 1.6 and later */
self = PyObject_New(xmlparseobject, &Xmlparsetype); self = PyObject_New(xmlparseobject, &Xmlparsetype);
if (self == NULL) if (self == NULL)
return NULL; return NULL;
self->returns_unicode = 1; self->returns_unicode = 1;
#endif #endif
if (namespace_separator) { if (namespace_separator) {
self->itself = XML_ParserCreateNS(encoding, self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
*namespace_separator); }
}else{ else{
self->itself = XML_ParserCreate(encoding); self->itself = XML_ParserCreate(encoding);
} }
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; }
} XML_SetUserData(self->itself, (void *)self);
XML_SetUserData(self->itself, (void *)self);
for( i=0; handler_info[i].name!=NULL;i++);
self->handlers=malloc( sizeof( PyObject *)*i ); for(i = 0; handler_info[i].name != NULL; i++)
/* do nothing */;
clear_handlers( self ); self->handlers = malloc(sizeof(PyObject *)*i);
clear_handlers(self);
return self; return self;
} }
static void static void
xmlparse_dealloc( xmlparseobject *self ) xmlparse_dealloc(xmlparseobject *self)
{ {
int i; int i;
if (self->itself) if (self->itself)
XML_ParserFree(self->itself); XML_ParserFree(self->itself);
self->itself = NULL; self->itself = NULL;
for( i=0; handler_info[i].name!=NULL; i++ ){ for (i=0; handler_info[i].name != NULL; i++) {
Py_XDECREF( self->handlers[i] ); Py_XDECREF(self->handlers[i]);
} }
#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
/* Code for versions before 1.6 */ /* Code for versions before 1.6 */
free(self); free(self);
#else #else
/* Code for versions 1.6 and later */ /* Code for versions 1.6 and later */
PyObject_Del(self); PyObject_Del(self);
#endif #endif
} }
static int handlername2int( const char *name ){ static int
int i; handlername2int(const char *name)
for( i=0;handler_info[i].name!=NULL;i++){ {
if( strcmp( name, handler_info[i].name )==0 ){ int i;
return i; for (i=0; handler_info[i].name != NULL; i++) {
} if (strcmp(name, handler_info[i].name) == 0) {
} return i;
return -1; }
}
return -1;
} }
static PyObject * static PyObject *
xmlparse_getattr(xmlparseobject *self, char *name) xmlparse_getattr(xmlparseobject *self, char *name)
{ {
int handlernum; int handlernum;
if (strcmp(name, "ErrorCode") == 0) if (strcmp(name, "ErrorCode") == 0)
return Py_BuildValue("l", return Py_BuildValue("l",
(long)XML_GetErrorCode(self->itself)); (long)XML_GetErrorCode(self->itself));
if (strcmp(name, "ErrorLineNumber") == 0) if (strcmp(name, "ErrorLineNumber") == 0)
return Py_BuildValue("l", return Py_BuildValue("l",
(long)XML_GetErrorLineNumber(self->itself)); (long)XML_GetErrorLineNumber(self->itself));
if (strcmp(name, "ErrorColumnNumber") == 0) if (strcmp(name, "ErrorColumnNumber") == 0)
return Py_BuildValue("l", return Py_BuildValue("l",
(long)XML_GetErrorColumnNumber(self->itself)); (long)XML_GetErrorColumnNumber(self->itself));
if (strcmp(name, "ErrorByteIndex") == 0) if (strcmp(name, "ErrorByteIndex") == 0)
return Py_BuildValue("l", return Py_BuildValue("l",
XML_GetErrorByteIndex(self->itself)); XML_GetErrorByteIndex(self->itself));
if (strcmp(name, "returns_unicode") == 0) if (strcmp(name, "returns_unicode") == 0)
return Py_BuildValue("i", self->returns_unicode); return Py_BuildValue("i", self->returns_unicode);
handlernum=handlername2int( name ); handlernum = handlername2int(name);
if( handlernum!=-1 && self->handlers[handlernum]!=NULL){ if (handlernum != -1 && self->handlers[handlernum] != NULL) {
Py_INCREF( self->handlers[handlernum] ); Py_INCREF(self->handlers[handlernum]);
return 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,
PyList_Append( rc, PyString_FromString(handler_info[i].name));
PyString_FromString( handler_info[i].name ) ); }
} PyList_Append(rc, PyString_FromString("ErrorCode"));
PyList_Append( rc, PyString_FromString( "ErrorCode" )); PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
PyList_Append( rc, PyString_FromString( "ErrorLineNumber" )); PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
PyList_Append( rc, PyString_FromString( "ErrorColumnNumber")); PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
PyList_Append( rc, PyString_FromString( "ErrorByteIndex" ));
return rc;
}
return Py_FindMethod(xmlparse_methods, (PyObject *)self, name); return rc;
}
return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
} }
static int sethandler( xmlparseobject *self, const char *name, PyObject* v ){ static int sethandler(xmlparseobject *self, const char *name, PyObject* v)
int handlernum = handlername2int( name ); {
if( handlernum!=-1 ){ int handlernum = handlername2int(name);
Py_INCREF( v ); if (handlernum != -1) {
Py_XDECREF( self->handlers[handlernum] ); Py_INCREF(v);
self->handlers[handlernum]=v; Py_XDECREF(self->handlers[handlernum]);
handler_info[handlernum].setter( self->itself, self->handlers[handlernum] = v;
handler_info[handlernum].handler ); handler_info[handlernum].setter(self->itself,
return 1; handler_info[handlernum].handler);
} return 1;
}
return 0; return 0;
} }
static int static int
...@@ -771,8 +766,7 @@ xmlparse_setattr( xmlparseobject *self, char *name, PyObject *v) ...@@ -771,8 +766,7 @@ xmlparse_setattr( xmlparseobject *self, char *name, PyObject *v)
} }
static char Xmlparsetype__doc__[] = static char Xmlparsetype__doc__[] =
"XML parser" "XML parser";
;
static PyTypeObject Xmlparsetype = { static PyTypeObject Xmlparsetype = {
PyObject_HEAD_INIT(NULL) PyObject_HEAD_INIT(NULL)
...@@ -804,247 +798,239 @@ static PyTypeObject Xmlparsetype = { ...@@ -804,247 +798,239 @@ static PyTypeObject Xmlparsetype = {
static char pyexpat_ParserCreate__doc__[] = static char pyexpat_ParserCreate__doc__[] =
"([encoding, namespace_separator]) - Return a new XML parser object" "ParserCreate([encoding[, namespace_separator]]) -> parser\n\
; Return a new XML parser object.";
static PyObject * static PyObject *
pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw) { pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
char *encoding = NULL, *namespace_separator=NULL; {
char *encoding = NULL;
char *namespace_separator = NULL;
static char *kwlist[] = {"encoding", "namespace_separator", NULL}; static char *kwlist[] = {"encoding", "namespace_separator", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
&encoding, &namespace_separator)) &encoding, &namespace_separator))
return NULL; return NULL;
return (PyObject *)newxmlparseobject(encoding, namespace_separator); return (PyObject *)newxmlparseobject(encoding, namespace_separator);
} }
static char pyexpat_ErrorString__doc__[] = static char pyexpat_ErrorString__doc__[] =
"(errno) Returns string error for given number" "ErrorString(errno) -> string\n\
; Returns string error for given number.";
static PyObject * static PyObject *
pyexpat_ErrorString(self, args) pyexpat_ErrorString(PyObject *self, PyObject *args)
PyObject *self; /* Not used */
PyObject *args;
{ {
long code; long code = 0;
if (!PyArg_ParseTuple(args, "l", &code)) if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
return NULL; return NULL;
return Py_BuildValue("z", XML_ErrorString((int)code)); return Py_BuildValue("z", XML_ErrorString((int)code));
} }
/* List of methods defined in the module */ /* List of methods defined in the module */
static struct PyMethodDef pyexpat_methods[] = { static struct PyMethodDef pyexpat_methods[] = {
{"ParserCreate", (PyCFunction)pyexpat_ParserCreate, {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
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 */
}; };
/* Module docstring */ /* Module docstring */
static char pyexpat_module_documentation[] = static char pyexpat_module_documentation[] =
"Python wrapper for Expat parser." "Python wrapper for Expat parser.";
;
/* Initialization function for the module */ /* Initialization function for the module */
void void
initpyexpat(){ initpyexpat(void)
PyObject *m, *d; {
char *rev = "$Revision$"; PyObject *m, *d;
PyObject *errors_module, *errors_dict; char *rev = "$Revision$";
PyObject *sys_modules; PyObject *errors_module, *errors_dict;
PyObject *sys_modules;
Xmlparsetype.ob_type = &PyType_Type;
Xmlparsetype.ob_type = &PyType_Type;
/* Create the module and add the functions */
m = Py_InitModule4("pyexpat", pyexpat_methods, /* Create the module and add the functions */
pyexpat_module_documentation, m = Py_InitModule4("pyexpat", pyexpat_methods,
(PyObject*)NULL, PYTHON_API_VERSION); pyexpat_module_documentation,
(PyObject*)NULL, PYTHON_API_VERSION);
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m); /* Add some symbolic constants to the module */
ErrorObject = PyString_FromString("pyexpat.error"); d = PyModule_GetDict(m);
PyDict_SetItemString(d, "error", ErrorObject); ErrorObject = PyString_FromString("pyexpat.error");
PyDict_SetItemString(d, "error", ErrorObject);
PyDict_SetItemString(d, "__version__",
PyString_FromStringAndSize(rev+11, PyDict_SetItemString(d, "__version__",
strlen(rev+11)-2)); PyString_FromStringAndSize(rev+11,
strlen(rev+11)-2));
sys_modules = PySys_GetObject("modules");
errors_module = PyModule_New("pyexpat.errors"); sys_modules = PySys_GetObject("modules");
PyDict_SetItemString(d, "errors", errors_module); errors_module = PyModule_New("pyexpat.errors");
PyDict_SetItemString(sys_modules, "pyexpat.errors", errors_module); PyDict_SetItemString(d, "errors", errors_module);
PyDict_SetItemString(sys_modules, "pyexpat.errors", errors_module);
/* XXX When Expat supports some way of figuring out how it was
compiled, this should check and set native_encoding /* XXX When Expat supports some way of figuring out how it was
appropriately. compiled, this should check and set native_encoding
*/ appropriately.
PyDict_SetItemString(d, "native_encoding", */
PyString_FromString("UTF-8")); PyDict_SetItemString(d, "native_encoding",
errors_dict = PyModule_GetDict(errors_module); PyString_FromString("UTF-8"));
errors_dict = PyModule_GetDict(errors_module);
#define MYCONST(name) \ #define MYCONST(name) \
PyDict_SetItemString(errors_dict, #name, \ PyDict_SetItemString(errors_dict, #name, \
PyString_FromString(XML_ErrorString(name))) PyString_FromString(XML_ErrorString(name)))
MYCONST(XML_ERROR_NO_MEMORY); MYCONST(XML_ERROR_NO_MEMORY);
MYCONST(XML_ERROR_SYNTAX); MYCONST(XML_ERROR_SYNTAX);
MYCONST(XML_ERROR_NO_ELEMENTS); MYCONST(XML_ERROR_NO_ELEMENTS);
MYCONST(XML_ERROR_INVALID_TOKEN); MYCONST(XML_ERROR_INVALID_TOKEN);
MYCONST(XML_ERROR_UNCLOSED_TOKEN); MYCONST(XML_ERROR_UNCLOSED_TOKEN);
MYCONST(XML_ERROR_PARTIAL_CHAR); MYCONST(XML_ERROR_PARTIAL_CHAR);
MYCONST(XML_ERROR_TAG_MISMATCH); MYCONST(XML_ERROR_TAG_MISMATCH);
MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE); MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT); MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
MYCONST(XML_ERROR_PARAM_ENTITY_REF); MYCONST(XML_ERROR_PARAM_ENTITY_REF);
MYCONST(XML_ERROR_UNDEFINED_ENTITY); MYCONST(XML_ERROR_UNDEFINED_ENTITY);
MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF); MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
MYCONST(XML_ERROR_ASYNC_ENTITY); MYCONST(XML_ERROR_ASYNC_ENTITY);
MYCONST(XML_ERROR_BAD_CHAR_REF); MYCONST(XML_ERROR_BAD_CHAR_REF);
MYCONST(XML_ERROR_BINARY_ENTITY_REF); MYCONST(XML_ERROR_BINARY_ENTITY_REF);
MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF); MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
MYCONST(XML_ERROR_MISPLACED_XML_PI); MYCONST(XML_ERROR_MISPLACED_XML_PI);
MYCONST(XML_ERROR_UNKNOWN_ENCODING); MYCONST(XML_ERROR_UNKNOWN_ENCODING);
MYCONST(XML_ERROR_INCORRECT_ENCODING); MYCONST(XML_ERROR_INCORRECT_ENCODING);
/* Check for errors */ /* Check for errors */
if (PyErr_Occurred()) if (PyErr_Occurred())
Py_FatalError("can't initialize module pyexpat"); Py_FatalError("can't initialize module pyexpat");
} }
void clear_handlers( xmlparseobject *self ){ void clear_handlers(xmlparseobject *self)
int i=0; {
int i = 0;
for( i=0;handler_info[i].name!=NULL;i++ ){ for (; handler_info[i].name!=NULL; i++) {
self->handlers[i]=NULL; self->handlers[i]=NULL;
handler_info[i].setter( self->itself, NULL ); handler_info[i].setter( self->itself, NULL );
} }
} }
typedef void (*pairsetter)( XML_Parser, void *handler1, void *handler2 ); typedef void (*pairsetter)( XML_Parser, void *handler1, void *handler2 );
void pyxml_UpdatePairedHandlers( xmlparseobject *self, void pyxml_UpdatePairedHandlers(xmlparseobject *self,
int startHandler, int startHandler,
int endHandler, int endHandler,
pairsetter setter){ pairsetter setter)
void *start_handler=NULL; {
void *end_handler=NULL; void *start_handler=NULL;
void *end_handler=NULL;
if( self->handlers[startHandler] && if (self->handlers[startHandler]
self->handlers[endHandler]!=Py_None ){ && self->handlers[endHandler]!=Py_None) {
start_handler=handler_info[startHandler].handler; start_handler=handler_info[startHandler].handler;
} }
if( self->handlers[EndElement] && if (self->handlers[EndElement]
self->handlers[EndElement] !=Py_None ){ && self->handlers[EndElement] !=Py_None) {
end_handler=handler_info[endHandler].handler; end_handler=handler_info[endHandler].handler;
} }
setter(self->itself, start_handler, end_handler);
setter(self->itself,
start_handler,
end_handler);
} }
void pyxml_SetStartElementHandler( XML_Parser *parser, void pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
void *junk){ {
pyxml_UpdatePairedHandlers( pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
(xmlparseobject *)XML_GetUserData( parser ), StartElement, EndElement,
StartElement, EndElement, (pairsetter)XML_SetElementHandler);
(pairsetter)XML_SetElementHandler);
} }
void pyxml_SetEndElementHandler( XML_Parser *parser, void pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
void *junk){ {
pyxml_UpdatePairedHandlers( pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
(xmlparseobject *)XML_GetUserData( parser ), StartElement, EndElement,
StartElement, EndElement, (pairsetter)XML_SetElementHandler);
(pairsetter)XML_SetElementHandler);
} }
void pyxml_SetStartNamespaceDeclHandler( XML_Parser *parser, void pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
void *junk){ {
pyxml_UpdatePairedHandlers( pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
(xmlparseobject *)XML_GetUserData( parser ), StartNamespaceDecl, EndNamespaceDecl,
StartNamespaceDecl, EndNamespaceDecl, (pairsetter)XML_SetNamespaceDeclHandler);
(pairsetter)XML_SetNamespaceDeclHandler);
} }
void pyxml_SetEndNamespaceDeclHandler( XML_Parser *parser, void pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
void *junk){ {
pyxml_UpdatePairedHandlers( pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
(xmlparseobject *)XML_GetUserData( parser ), StartNamespaceDecl, EndNamespaceDecl,
StartNamespaceDecl, EndNamespaceDecl, (pairsetter)XML_SetNamespaceDeclHandler);
(pairsetter)XML_SetNamespaceDeclHandler);
} }
void pyxml_SetStartCdataSection( XML_Parser *parser, void pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
void *junk){ {
pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
pyxml_UpdatePairedHandlers( StartCdataSection, EndCdataSection,
(xmlparseobject *)XML_GetUserData( parser ), (pairsetter)XML_SetCdataSectionHandler);
StartCdataSection, EndCdataSection,
(pairsetter)XML_SetCdataSectionHandler);
} }
void pyxml_SetEndCdataSection( XML_Parser *parser, void pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
void *junk){ {
pyxml_UpdatePairedHandlers( pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
(xmlparseobject *)XML_GetUserData( parser ), StartCdataSection, EndCdataSection,
StartCdataSection, EndCdataSection, (pairsetter)XML_SetCdataSectionHandler);
(pairsetter)XML_SetCdataSectionHandler);
} }
statichere struct HandlerInfo handler_info[]= statichere struct HandlerInfo handler_info[] = {
{{"StartElementHandler", {"StartElementHandler",
pyxml_SetStartElementHandler, pyxml_SetStartElementHandler,
(xmlhandler)my_StartElementHandler}, (xmlhandler)my_StartElementHandler},
{"EndElementHandler", {"EndElementHandler",
pyxml_SetEndElementHandler, pyxml_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, pyxml_SetStartNamespaceDeclHandler,
(xmlhandler)my_StartNamespaceDeclHandler }, (xmlhandler)my_StartNamespaceDeclHandler },
{"EndNamespaceDeclHandler", {"EndNamespaceDeclHandler",
pyxml_SetEndNamespaceDeclHandler, pyxml_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, pyxml_SetStartCdataSection,
(xmlhandler)my_StartCdataSectionHandler}, (xmlhandler)my_StartCdataSectionHandler},
{"EndCdataSectionHandler", {"EndCdataSectionHandler",
pyxml_SetEndCdataSection, pyxml_SetEndCdataSection,
(xmlhandler)my_EndCdataSectionHandler}, (xmlhandler)my_EndCdataSectionHandler},
{"DefaultHandler", {"DefaultHandler",
(xmlhandlersetter)XML_SetDefaultHandler, (xmlhandlersetter)XML_SetDefaultHandler,
(xmlhandler)my_DefaultHandler}, (xmlhandler)my_DefaultHandler},
{"DefaultHandlerExpand", {"DefaultHandlerExpand",
(xmlhandlersetter)XML_SetDefaultHandlerExpand, (xmlhandlersetter)XML_SetDefaultHandlerExpand,
(xmlhandler)my_DefaultHandlerExpandHandler}, (xmlhandler)my_DefaultHandlerExpandHandler},
{"NotStandaloneHandler", {"NotStandaloneHandler",
(xmlhandlersetter)XML_SetNotStandaloneHandler, (xmlhandlersetter)XML_SetNotStandaloneHandler,
(xmlhandler)my_NotStandaloneHandler}, (xmlhandler)my_NotStandaloneHandler},
{"ExternalEntityRefHandler", {"ExternalEntityRefHandler",
(xmlhandlersetter)XML_SetExternalEntityRefHandler, (xmlhandlersetter)XML_SetExternalEntityRefHandler,
(xmlhandler)my_ExternalEntityRefHandler }, (xmlhandler)my_ExternalEntityRefHandler },
{NULL, NULL, NULL } /* sentinel */ {NULL, NULL, NULL} /* sentinel */
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment