pyexpat.c
来自「python s60 1.4.5版本的源代码」· C语言 代码 · 共 1,906 行 · 第 1/4 页
C
1,906 行
Return base URL string for the parser.";
static PyObject *
xmlparse_GetBase(xmlparseobject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":GetBase"))
return NULL;
return Py_BuildValue("z", XML_GetBase(self->itself));
}
#if EXPAT_VERSION >= 0x015f00
static char xmlparse_GetInputContext__doc__[] =
"GetInputContext() -> string\n\
Return the untranslated text of the input that caused the current event.\n\
If the event was generated by a large amount of text (such as a start tag\n\
for an element with many attributes), not all of the text may be available.";
static PyObject *
xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
{
PyObject *result = NULL;
if (PyArg_ParseTuple(args, ":GetInputContext")) {
if (self->in_callback) {
int offset, size;
const char *buffer
= XML_GetInputContext(self->itself, &offset, &size);
if (buffer != NULL)
result = PyString_FromStringAndSize(buffer + offset, size);
else {
result = Py_None;
Py_INCREF(result);
}
}
else {
result = Py_None;
Py_INCREF(result);
}
}
return result;
}
#endif
static char xmlparse_ExternalEntityParserCreate__doc__[] =
"ExternalEntityParserCreate(context[, encoding])\n\
Create a parser for parsing an external entity based on the\n\
information passed to the ExternalEntityRefHandler.";
static PyObject *
xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
{
char *context;
char *encoding = NULL;
xmlparseobject *new_parser;
int i;
if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
&context, &encoding)) {
return NULL;
}
#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
#else
#ifndef Py_TPFLAGS_HAVE_GC
/* Python versions 1.6 to 2.1 */
new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
#else
/* Python versions 2.2 and later */
new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
#endif
#endif
if (new_parser == NULL)
return NULL;
new_parser->returns_unicode = self->returns_unicode;
new_parser->ordered_attributes = self->ordered_attributes;
new_parser->specified_attributes = self->specified_attributes;
new_parser->in_callback = 0;
new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
encoding);
new_parser->handlers = 0;
#ifdef Py_TPFLAGS_HAVE_GC
PyObject_GC_Track(new_parser);
#else
PyObject_GC_Init(new_parser);
#endif
if (!new_parser->itself) {
Py_DECREF(new_parser);
return PyErr_NoMemory();
}
XML_SetUserData(new_parser->itself, (void *)new_parser);
/* allocate and clear handlers first */
for(i = 0; handler_info[i].name != NULL; i++)
/* do nothing */;
new_parser->handlers = malloc(sizeof(PyObject *)*i);
if (!new_parser->handlers) {
Py_DECREF(new_parser);
return PyErr_NoMemory();
}
clear_handlers(new_parser, 1);
/* then copy handlers from self */
for (i = 0; handler_info[i].name != NULL; i++) {
if (self->handlers[i]) {
Py_INCREF(self->handlers[i]);
new_parser->handlers[i] = self->handlers[i];
handler_info[i].setter(new_parser->itself,
handler_info[i].handler);
}
}
return (PyObject *)new_parser;
}
#if EXPAT_VERSION >= 0x010200
static char xmlparse_SetParamEntityParsing__doc__[] =
"SetParamEntityParsing(flag) -> success\n\
Controls parsing of parameter entities (including the external DTD\n\
subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
was successful.";
static PyObject*
xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
{
int flag;
if (!PyArg_ParseTuple(args, "i", &flag))
return NULL;
flag = XML_SetParamEntityParsing(p->itself, flag);
return PyInt_FromLong(flag);
}
#endif /* Expat version 1.2 or better */
static struct PyMethodDef xmlparse_methods[] = {
{"Parse", (PyCFunction)xmlparse_Parse,
METH_VARARGS, xmlparse_Parse__doc__},
{"ParseFile", (PyCFunction)xmlparse_ParseFile,
METH_VARARGS, xmlparse_ParseFile__doc__},
{"SetBase", (PyCFunction)xmlparse_SetBase,
METH_VARARGS, xmlparse_SetBase__doc__},
{"GetBase", (PyCFunction)xmlparse_GetBase,
METH_VARARGS, xmlparse_GetBase__doc__},
{"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
#if EXPAT_VERSION >= 0x010200
{"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
#endif
#if EXPAT_VERSION >= 0x015f00
{"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
METH_VARARGS, xmlparse_GetInputContext__doc__},
#endif
{NULL, NULL} /* sentinel */
};
/* ---------- */
#ifdef Py_USING_UNICODE
/*
pyexpat international encoding support.
Make it as simple as possible.
*/
static char template_buffer[257];
PyObject *template_string = NULL;
static void
init_template_buffer(void)
{
int i;
for (i = 0; i < 256; i++) {
template_buffer[i] = i;
}
template_buffer[256] = 0;
}
int
PyUnknownEncodingHandler(void *encodingHandlerData,
const XML_Char *name,
XML_Encoding * info)
{
PyUnicodeObject *_u_string = NULL;
int result = 0;
int i;
/* Yes, supports only 8bit encodings */
_u_string = (PyUnicodeObject *)
PyUnicode_Decode(template_buffer, 256, name, "replace");
if (_u_string == NULL)
return result;
for (i = 0; i < 256; i++) {
/* Stupid to access directly, but fast */
Py_UNICODE c = _u_string->str[i];
if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
info->map[i] = -1;
else
info->map[i] = c;
}
info->data = NULL;
info->convert = NULL;
info->release = NULL;
result=1;
Py_DECREF(_u_string);
return result;
}
#endif
static PyObject *
newxmlparseobject(char *encoding, char *namespace_separator)
{
int i;
xmlparseobject *self;
#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
if (self == NULL)
return NULL;
self->returns_unicode = 0;
#else
/* Code for versions 1.6 and later */
#ifdef Py_TPFLAGS_HAVE_GC
/* Code for versions 2.2 and later */
self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
#else
self = PyObject_New(xmlparseobject, &Xmlparsetype);
#endif
if (self == NULL)
return NULL;
self->returns_unicode = 1;
#endif
self->ordered_attributes = 0;
self->specified_attributes = 0;
self->in_callback = 0;
self->handlers = NULL;
if (namespace_separator != NULL) {
self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
}
else {
self->itself = XML_ParserCreate(encoding);
}
#ifdef Py_TPFLAGS_HAVE_GC
PyObject_GC_Track(self);
#else
PyObject_GC_Init(self);
#endif
if (self->itself == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"XML_ParserCreate failed");
Py_DECREF(self);
return NULL;
}
XML_SetUserData(self->itself, (void *)self);
#ifdef Py_USING_UNICODE
XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
#endif
for(i = 0; handler_info[i].name != NULL; i++)
/* do nothing */;
self->handlers = malloc(sizeof(PyObject *)*i);
if (!self->handlers){
Py_DECREF(self);
return PyErr_NoMemory();
}
clear_handlers(self, 1);
return (PyObject*)self;
}
static void
xmlparse_dealloc(xmlparseobject *self)
{
int i;
#ifdef Py_TPFLAGS_HAVE_GC
PyObject_GC_UnTrack(self);
#else
PyObject_GC_Fini(self);
#endif
if (self->itself != NULL)
XML_ParserFree(self->itself);
self->itself = NULL;
if (self->handlers != NULL) {
PyObject *temp;
for (i = 0; handler_info[i].name != NULL; i++) {
temp = self->handlers[i];
self->handlers[i] = NULL;
Py_XDECREF(temp);
}
free(self->handlers);
}
#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
/* Code for versions before 1.6 */
free(self);
#else
#ifndef Py_TPFLAGS_HAVE_GC
/* Code for versions 1.6 to 2.1 */
PyObject_Del(self);
#else
/* Code for versions 2.2 and later. */
PyObject_GC_Del(self);
#endif
#endif
}
static int
handlername2int(const char *name)
{
int i;
for (i=0; handler_info[i].name != NULL; i++) {
if (strcmp(name, handler_info[i].name) == 0) {
return i;
}
}
return -1;
}
static PyObject *
xmlparse_getattr(xmlparseobject *self, char *name)
{
int handlernum;
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)
return PyInt_FromLong((long) self->ordered_attributes);
if (strcmp(name, "returns_unicode") == 0)
return PyInt_FromLong((long) self->returns_unicode);
if (strcmp(name, "specified_attributes") == 0)
return PyInt_FromLong((long) self->specified_attributes);
handlernum = handlername2int(name);
if (handlernum != -1 && self->handlers[handlernum] != NULL) {
Py_INCREF(self->handlers[handlernum]);
return self->handlers[handlernum];
}
if (strcmp(name, "__members__") == 0) {
int i;
PyObject *rc = PyList_New(0);
for(i = 0; handler_info[i].name != NULL; i++) {
PyList_Append(rc, PyString_FromString(handler_info[i].name));
}
PyList_Append(rc, PyString_FromString("ErrorCode"));
PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
PyList_Append(rc, PyString_FromString("ordered_attributes"));
PyList_Append(rc, PyString_FromString("returns_unicode"));
PyList_Append(rc, PyString_FromString("specified_attributes"));
return rc;
}
return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
}
static int
sethandler(xmlparseobject *self, const char *name, PyObject* v)
{
int handlernum = handlername2int(name);
if (handlernum != -1) {
Py_INCREF(v);
Py_XDECREF(self->handlers[handlernum]);
self->handlers[handlernum] = v;
handler_info[handlernum].setter(self->itself,
handler_info[handlernum].handler);
return 1;
}
return 0;
}
static int
xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
{
/* Set attribute 'name' to value 'v'. v==NULL means delete */
if (v == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
return -1;
}
if (strcmp(name, "ordered_attributes") == 0) {
if (PyObject_IsTrue(v))
self->ordered_attributes = 1;
else
self->ordered_attributes = 0;
return 0;
}
if (strcmp(name, "returns_unicode") == 0) {
if (PyObject_IsTrue(v)) {
#ifndef Py_USING_UNICODE
PyErr_SetString(PyExc_ValueError,
"Cannot return Unicode strings in Python 1.5");
return -1;
#else
self->returns_unicode = 1;
#endif
}
else
self->returns_unicode = 0;
return 0;
}
if (strcmp(name, "specified_attributes") == 0) {
if (PyObject_IsTrue(v))
self->specified_attributes = 1;
else
self->specified_attributes = 0;
return 0;
}
if (sethandler(self, name, v)) {
return 0;
}
PyErr_SetString(PyExc_AttributeError, name);
return -1;
}
#ifdef WITH_CYCLE_GC
static int
xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
{
int i, err;
for (i = 0; handler_info[i].name != NULL; i++) {
if (!op->handlers[i])
continue;
err = visit(op->handlers[i], arg);
if (err)
return err;
}
return 0;
}
static int
xmlparse_clear(xmlparseobject *op)
{
clear_handlers(op, 0);
return 0;
}
#endif
static char Xmlparsetype__doc__[] =
"XML parser";
static PyTypeObject Xmlparsetype = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"pyexpat.xmlparser", /*tp_name*/
sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)xmlparse_dealloc, /*tp_dealloc*/
(printfunc)0, /*tp_print*/
(getattrfunc)xmlparse_getattr, /*tp_getattr*/
(setattrfunc)xmlparse_setattr, /*tp_setattr*/
(cmpfunc)0, /*tp_compare*/
(reprfunc)0, /*tp_repr*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?