cinap_lenrekadd hg and python4bb9e2215 years agoHistory
2149 lines · 63.2 KB · C
RawDownload
#include "Python.h"#include <ctype.h> #include "frameobject.h"#include "expat.h" #include "pyexpat.h" #define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION) #ifndef PyDoc_STRVAR /* * fdrake says: * Don't change the PyDoc_STR macro definition to (str), because * '''the parentheses cause compile failures * ("non-constant static initializer" or something like that) * on some platforms (Irix?)''' */#define PyDoc_STR(str)         str#define PyDoc_VAR(name)        static char name[]#define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)#endif #if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)/* In Python 2.0 and  2.1, disabling Unicode was not possible. */#define Py_USING_UNICODE#else#define FIX_TRACE#endif enum HandlerTypes {    StartElement,    EndElement,    ProcessingInstruction,    CharacterData,    UnparsedEntityDecl,    NotationDecl,    StartNamespaceDecl,    EndNamespaceDecl,    Comment,    StartCdataSection,    EndCdataSection,    Default,    DefaultHandlerExpand,    NotStandalone,    ExternalEntityRef,    StartDoctypeDecl,    EndDoctypeDecl,    EntityDecl,    XmlDecl,    ElementDecl,    AttlistDecl,#if XML_COMBINED_VERSION >= 19504    SkippedEntity,#endif    _DummyDecl}; static PyObject *ErrorObject; /* ----------------------------------------------------- */ /* Declarations for objects of type xmlparser */ typedef struct {    PyObject_HEAD     XML_Parser itself;    int returns_unicode;        /* True if Unicode strings are returned;                                   if false, UTF-8 strings are returned */    int ordered_attributes;     /* Return attributes as a list. */    int specified_attributes;   /* Report only specified attributes. */    int in_callback;            /* Is a callback active? */    int ns_prefixes;            /* Namespace-triplets mode? */    XML_Char *buffer;           /* Buffer used when accumulating characters */                                /* NULL if not enabled */    int buffer_size;            /* Size of buffer, in XML_Char units */    int buffer_used;            /* Buffer units in use */    PyObject *intern;           /* Dictionary to intern strings */    PyObject **handlers;} xmlparseobject; #define CHARACTER_DATA_BUFFER_SIZE 8192 static PyTypeObject Xmlparsetype; typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);typedef void* xmlhandler; struct HandlerInfo {    const char *name;    xmlhandlersetter setter;    xmlhandler handler;    PyCodeObject *tb_code;    PyObject *nameobj;}; static struct HandlerInfo handler_info[64]; /* Set an integer attribute on the error object; return true on success, * false on an exception. */static intset_error_attr(PyObject *err, char *name, int value){    PyObject *v = PyInt_FromLong(value);     if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) {        Py_XDECREF(v);        return 0;    }    Py_DECREF(v);    return 1;} /* Build and set an Expat exception, including positioning * information.  Always returns NULL. */static PyObject *set_error(xmlparseobject *self, enum XML_Error code){    PyObject *err;    char buffer[256];    XML_Parser parser = self->itself;    int lineno = XML_GetErrorLineNumber(parser);    int column = XML_GetErrorColumnNumber(parser);     /* There is no risk of overflowing this buffer, since       even for 64-bit integers, there is sufficient space. */    sprintf(buffer, "%.200s: line %i, column %i",            XML_ErrorString(code), lineno, column);    err = PyObject_CallFunction(ErrorObject, "s", buffer);    if (  err != NULL          && set_error_attr(err, "code", code)          && set_error_attr(err, "offset", column)          && set_error_attr(err, "lineno", lineno)) {        PyErr_SetObject(ErrorObject, err);    }    Py_XDECREF(err);    return NULL;} static inthave_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/* Convert a string of XML_Chars into a Unicode string.   Returns None if str is a null pointer. */ static PyObject *conv_string_to_unicode(const XML_Char *str){    /* XXX currently this code assumes that XML_Char is 8-bit,       and hence in UTF-8.  */    /* UTF-8 from Expat, Unicode desired */    if (str == NULL) {        Py_INCREF(Py_None);        return Py_None;    }    return PyUnicode_DecodeUTF8(str, strlen(str), "strict");} static PyObject *conv_string_len_to_unicode(const XML_Char *str, int len){    /* XXX currently this code assumes that XML_Char is 8-bit,       and hence in UTF-8.  */    /* UTF-8 from Expat, Unicode desired */    if (str == NULL) {        Py_INCREF(Py_None);        return Py_None;    }    return PyUnicode_DecodeUTF8((const char *)str, len, "strict");}#endif /* Convert a string of XML_Chars into an 8-bit Python string.   Returns None if str is a null pointer. */ static PyObject *conv_string_to_utf8(const XML_Char *str){    /* XXX currently this code assumes that XML_Char is 8-bit,       and hence in UTF-8.  */    /* UTF-8 from Expat, UTF-8 desired */    if (str == NULL) {        Py_INCREF(Py_None);        return Py_None;    }    return PyString_FromString(str);} static PyObject *conv_string_len_to_utf8(const XML_Char *str, int len){    /* XXX currently this code assumes that XML_Char is 8-bit,       and hence in UTF-8.  */    /* UTF-8 from Expat, UTF-8 desired */    if (str == NULL) {        Py_INCREF(Py_None);        return Py_None;    }    return PyString_FromStringAndSize((const char *)str, len);} /* Callback routines */ static void clear_handlers(xmlparseobject *self, int initial); /* This handler is used when an error has been detected, in the hope   that actual parsing can be terminated early.  This will only help   if an external entity reference is encountered. */static interror_external_entity_ref_handler(XML_Parser parser,                                  const XML_Char *context,                                  const XML_Char *base,                                  const XML_Char *systemId,                                  const XML_Char *publicId){    return 0;} /* Dummy character data handler used when an error (exception) has   been detected, and the actual parsing can be terminated early.   This is needed since character data handler can't be safely removed   from within the character data handler, but can be replaced.  It is   used only from the character data handler trampoline, and must be   used right after `flag_error()` is called. */static voidnoop_character_data_handler(void *userData, const XML_Char *data, int len) {    /* Do nothing. */} static voidflag_error(xmlparseobject *self){    clear_handlers(self, 0);    XML_SetExternalEntityRefHandler(self->itself,                                    error_external_entity_ref_handler);} static PyCodeObject*getcode(enum HandlerTypes slot, char* func_name, int lineno){    PyObject *code = NULL;    PyObject *name = NULL;    PyObject *nulltuple = NULL;    PyObject *filename = NULL;     if (handler_info[slot].tb_code == NULL) {        code = PyString_FromString("");        if (code == NULL)            goto failed;        name = PyString_FromString(func_name);        if (name == NULL)            goto failed;        nulltuple = PyTuple_New(0);        if (nulltuple == NULL)            goto failed;        filename = PyString_FromString(__FILE__);        handler_info[slot].tb_code =            PyCode_New(0,		/* argcount */                       0,		/* nlocals */                       0,		/* stacksize */                       0,		/* flags */                       code,		/* code */                       nulltuple,	/* consts */                       nulltuple,	/* names */                       nulltuple,	/* varnames */#if PYTHON_API_VERSION >= 1010                       nulltuple,	/* freevars */                       nulltuple,	/* cellvars */#endif                       filename,	/* filename */                       name,		/* name */                       lineno,		/* firstlineno */                       code		/* lnotab */                       );        if (handler_info[slot].tb_code == NULL)            goto failed;        Py_DECREF(code);        Py_DECREF(nulltuple);        Py_DECREF(filename);        Py_DECREF(name);    }    return handler_info[slot].tb_code; failed:    Py_XDECREF(code);    Py_XDECREF(name);    return NULL;} #ifdef FIX_TRACEstatic inttrace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val){    int result = 0;    if (!tstate->use_tracing || tstate->tracing)	return 0;    if (tstate->c_profilefunc != NULL) {	tstate->tracing++;	result = tstate->c_profilefunc(tstate->c_profileobj,				       f, code , val);	tstate->use_tracing = ((tstate->c_tracefunc != NULL)			       || (tstate->c_profilefunc != NULL));	tstate->tracing--;	if (result)	    return result;    }    if (tstate->c_tracefunc != NULL) {	tstate->tracing++;	result = tstate->c_tracefunc(tstate->c_traceobj,				     f, code , val);	tstate->use_tracing = ((tstate->c_tracefunc != NULL)			       || (tstate->c_profilefunc != NULL));	tstate->tracing--;    }	    return result;} static inttrace_frame_exc(PyThreadState *tstate, PyFrameObject *f){    PyObject *type, *value, *traceback, *arg;    int err;     if (tstate->c_tracefunc == NULL)	return 0;     PyErr_Fetch(&type, &value, &traceback);    if (value == NULL) {	value = Py_None;	Py_INCREF(value);    }#if PY_VERSION_HEX < 0x02040000    arg = Py_BuildValue("(OOO)", type, value, traceback);#else    arg = PyTuple_Pack(3, type, value, traceback);#endif    if (arg == NULL) {	PyErr_Restore(type, value, traceback);	return 0;    }    err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);    Py_DECREF(arg);    if (err == 0)	PyErr_Restore(type, value, traceback);    else {	Py_XDECREF(type);	Py_XDECREF(value);	Py_XDECREF(traceback);    }    return err;}#endif static PyObject*call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,                xmlparseobject *self){    PyThreadState *tstate = PyThreadState_GET();    PyFrameObject *f;    PyObject *res;     if (c == NULL)        return NULL;        f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);    if (f == NULL)        return NULL;    tstate->frame = f;#ifdef FIX_TRACE    if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {	return NULL;    }#endif    res = PyEval_CallObject(func, args);    if (res == NULL) {	if (tstate->curexc_traceback == NULL)	    PyTraceBack_Here(f);        XML_StopParser(self->itself, XML_FALSE);#ifdef FIX_TRACE	if (trace_frame_exc(tstate, f) < 0) {	    return NULL;	}    }    else {	if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {	    Py_XDECREF(res);	    res = NULL;	}    }#else    }#endif    tstate->frame = f->f_back;    Py_DECREF(f);    return res;} #ifndef Py_USING_UNICODE#define STRING_CONV_FUNC conv_string_to_utf8#else/* Python 2.0 and later versions, when built with Unicode support */#define STRING_CONV_FUNC (self->returns_unicode \                          ? conv_string_to_unicode : conv_string_to_utf8)#endif static PyObject*string_intern(xmlparseobject *self, const char* str){    PyObject *result = STRING_CONV_FUNC(str);    PyObject *value;    /* result can be NULL if the unicode conversion failed. */    if (!result)	return result;    if (!self->intern)	return result;    value = PyDict_GetItem(self->intern, result);    if (!value) {	if (PyDict_SetItem(self->intern, result, result) == 0)            return result;        else            return NULL;    }    Py_INCREF(value);    Py_DECREF(result);    return value;} /* Return 0 on success, -1 on exception. * flag_error() will be called before return if needed. */static intcall_character_handler(xmlparseobject *self, const XML_Char *buffer, int len){    PyObject *args;    PyObject *temp;     args = PyTuple_New(1);    if (args == NULL)        return -1;#ifdef Py_USING_UNICODE    temp = (self->returns_unicode             ? conv_string_len_to_unicode(buffer, len)             : conv_string_len_to_utf8(buffer, len));#else    temp = conv_string_len_to_utf8(buffer, len);#endif    if (temp == NULL) {        Py_DECREF(args);        flag_error(self);        XML_SetCharacterDataHandler(self->itself,                                    noop_character_data_handler);        return -1;    }    PyTuple_SET_ITEM(args, 0, temp);    /* temp is now a borrowed reference; consider it unused. */    self->in_callback = 1;    temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),                           self->handlers[CharacterData], args, self);    /* temp is an owned reference again, or NULL */    self->in_callback = 0;    Py_DECREF(args);    if (temp == NULL) {        flag_error(self);        XML_SetCharacterDataHandler(self->itself,                                    noop_character_data_handler);        return -1;    }    Py_DECREF(temp);    return 0;} static intflush_character_buffer(xmlparseobject *self){    int rc;    if (self->buffer == NULL || self->buffer_used == 0)        return 0;    rc = call_character_handler(self, self->buffer, self->buffer_used);

Lines 1–500 of 2149.