Main Page | Class List | Directories | File List | Class Members | File Members

pyexpat.c File Reference

#include "Python.h"
#include <ctype.h>
#include "compile.h"
#include "frameobject.h"
#include "expat.h"

Go to the source code of this file.

Classes

struct  xmlparseobject
struct  HandlerInfo

Defines

#define XML_COMBINED_VERSION   (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
#define PyDoc_STR(str)   str
#define PyDoc_VAR(name)   static char name[]
#define PyDoc_STRVAR(name, str)   PyDoc_VAR(name) = PyDoc_STR(str)
#define FIX_TRACE
#define CHARACTER_DATA_BUFFER_SIZE   8192
#define STRING_CONV_FUNC   conv_string_to_utf8
#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, RETURN, GETUSERDATA)
#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT)
#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)
#define BUF_SIZE   2048
#define APPEND(list, str)
#define MODULE_NAME   "pyexpat"
#define MODULE_INITFUNC   initpyexpat
#define PyMODINIT_FUNC   void
#define MYCONST(name)

Typedefs

typedef void(* xmlhandlersetter )(XML_Parser self, void *meth)
typedef void * xmlhandler

Enumerations

enum  HandlerTypes {
  StartElement, EndElement, ProcessingInstruction, CharacterData,
  UnparsedEntityDecl, NotationDecl, StartNamespaceDecl, EndNamespaceDecl,
  Comment, StartCdataSection, EndCdataSection, Default,
  DefaultHandlerExpand, NotStandalone, ExternalEntityRef, StartDoctypeDecl,
  EndDoctypeDecl, EntityDecl, XmlDecl, ElementDecl,
  AttlistDecl, _DummyDecl
}

Functions

static int set_error_attr (PyObject *err, char *name, int value)
static PyObject * set_error (xmlparseobject *self, enum XML_Error code)
static int have_handler (xmlparseobject *self, int type)
static PyObject * get_handler_name (struct HandlerInfo *hinfo)
static PyObject * conv_string_to_utf8 (const XML_Char *str)
static PyObject * conv_string_len_to_utf8 (const XML_Char *str, int len)
static void clear_handlers (xmlparseobject *self, int initial)
static int error_external_entity_ref_handler (XML_Parser parser, const XML_Char *context, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId)
static void flag_error (xmlparseobject *self)
static PyCodeObject * getcode (enum HandlerTypes slot, char *func_name, int lineno)
static int trace_frame (PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
static int trace_frame_exc (PyThreadState *tstate, PyFrameObject *f)
static PyObject * call_with_frame (PyCodeObject *c, PyObject *func, PyObject *args, xmlparseobject *self)
static PyObject * string_intern (xmlparseobject *self, const char *str)
static int call_character_handler (xmlparseobject *self, const XML_Char *buffer, int len)
static int flush_character_buffer (xmlparseobject *self)
static void my_CharacterDataHandler (void *userData, const XML_Char *data, int len)
static void my_StartElementHandler (void *userData, const XML_Char *name, const XML_Char *atts[])
static void my_ElementDeclHandler (void *userData, const XML_Char *name, XML_Content *model)
static PyObject * xmlparse_Parse (xmlparseobject *self, PyObject *args)
static int readinst (char *buf, int buf_size, PyObject *meth)
static PyObject * xmlparse_ParseFile (xmlparseobject *self, PyObject *args)
static PyObject * xmlparse_SetBase (xmlparseobject *self, PyObject *args)
static PyObject * xmlparse_GetBase (xmlparseobject *self, PyObject *args)
static PyObject * xmlparse_GetInputContext (xmlparseobject *self, PyObject *args)
static PyObject * xmlparse_ExternalEntityParserCreate (xmlparseobject *self, PyObject *args)
static PyObject * xmlparse_SetParamEntityParsing (xmlparseobject *p, PyObject *args)
static PyObject * newxmlparseobject (char *encoding, char *namespace_separator, PyObject *intern)
static void xmlparse_dealloc (xmlparseobject *self)
static int handlername2int (const char *name)
static PyObject * get_pybool (int istrue)
static PyObject * xmlparse_getattr (xmlparseobject *self, char *name)
static int sethandler (xmlparseobject *self, const char *name, PyObject *v)
static int xmlparse_setattr (xmlparseobject *self, char *name, PyObject *v)
static PyObject * pyexpat_ParserCreate (PyObject *notused, PyObject *args, PyObject *kw)
static PyObject * pyexpat_ErrorString (PyObject *self, PyObject *args)
static PyObject * get_version_string (void)

Variables

static PyObject * ErrorObject
static PyTypeObject Xmlparsetype
static struct HandlerInfo handler_info [64]
static struct PyMethodDef xmlparse_methods []
static struct PyMethodDef pyexpat_methods []


Define Documentation

#define APPEND list,
str   ) 
 

Value:

do {                                            \
                PyObject *o = PyString_FromString(str); \
                if (o != NULL)                          \
                        PyList_Append(list, o);         \
                Py_XDECREF(o);                          \
        } while (0)

Referenced by xmlparse_getattr().

#define BUF_SIZE   2048
 

Definition at line 927 of file pyexpat.c.

Referenced by xmlparse_ParseFile().

#define CHARACTER_DATA_BUFFER_SIZE   8192
 

Definition at line 87 of file pyexpat.c.

Referenced by newxmlparseobject().

#define FIX_TRACE
 

Definition at line 32 of file pyexpat.c.

#define INT_HANDLER NAME,
PARAMS,
PARAM_FORMAT   ) 
 

Value:

RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
                        rc = PyInt_AsLong(rv);, rc, \
        (xmlparseobject *)userData)

Definition at line 631 of file pyexpat.c.

#define MODULE_INITFUNC   initpyexpat
 

Definition at line 1819 of file pyexpat.c.

#define MODULE_NAME   "pyexpat"
 

Definition at line 1815 of file pyexpat.c.

Referenced by MODULE_INITFUNC().

#define MYCONST name   ) 
 

Value:

PyModule_AddStringConstant(errors_module, #name, \
                               (char*)XML_ErrorString(name))

Referenced by MODULE_INITFUNC().

#define PyDoc_STR str   )     str
 

Definition at line 23 of file pyexpat.c.

#define PyDoc_STRVAR name,
str   )     PyDoc_VAR(name) = PyDoc_STR(str)
 

Definition at line 25 of file pyexpat.c.

#define PyDoc_VAR name   )     static char name[]
 

Definition at line 24 of file pyexpat.c.

#define PyMODINIT_FUNC   void
 

Definition at line 1826 of file pyexpat.c.

#define RC_HANDLER RC,
NAME,
PARAMS,
INIT,
PARAM_FORMAT,
CONVERSION,
RETURN,
GETUSERDATA   ) 
 

Value:

static RC \
my_##NAME##Handler PARAMS {\
    xmlparseobject *self = GETUSERDATA ; \
    PyObject *args = NULL; \
    PyObject *rv = NULL; \
    INIT \
\
    if (have_handler(self, NAME)) { \
        if (flush_character_buffer(self) < 0) \
            return RETURN; \
        args = Py_BuildValue PARAM_FORMAT ;\
        if (!args) { flag_error(self); return RETURN;} \
        self->in_callback = 1; \
        rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
                             self->handlers[NAME], args, self); \
        self->in_callback = 0; \
        Py_DECREF(args); \
        if (rv == NULL) { \
            flag_error(self); \
            return RETURN; \
        } \
        CONVERSION \
        Py_DECREF(rv); \
    } \
    return RETURN; \
}

Definition at line 599 of file pyexpat.c.

#define STRING_CONV_FUNC   conv_string_to_utf8
 

Definition at line 412 of file pyexpat.c.

Referenced by my_StartElementHandler(), and string_intern().

#define VOID_HANDLER NAME,
PARAMS,
PARAM_FORMAT   ) 
 

Value:

RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
        (xmlparseobject *)userData)

Definition at line 627 of file pyexpat.c.

#define XML_COMBINED_VERSION   (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
 

Definition at line 12 of file pyexpat.c.


Typedef Documentation

typedef void* xmlhandler
 

Definition at line 92 of file pyexpat.c.

typedef void(* xmlhandlersetter)(XML_Parser self, void *meth)
 

Definition at line 91 of file pyexpat.c.


Enumeration Type Documentation

enum HandlerTypes
 

Enumerator:
StartElement 
EndElement 
ProcessingInstruction 
CharacterData 
UnparsedEntityDecl 
NotationDecl 
StartNamespaceDecl 
EndNamespaceDecl 
Comment 
StartCdataSection 
EndCdataSection 
Default 
DefaultHandlerExpand 
NotStandalone 
ExternalEntityRef 
StartDoctypeDecl 
EndDoctypeDecl 
EntityDecl 
XmlDecl 
ElementDecl 
AttlistDecl 
_DummyDecl 

Definition at line 35 of file pyexpat.c.

00035                   {
00036     StartElement,
00037     EndElement,
00038     ProcessingInstruction,
00039     CharacterData,
00040     UnparsedEntityDecl,
00041     NotationDecl,
00042     StartNamespaceDecl,
00043     EndNamespaceDecl,
00044     Comment,
00045     StartCdataSection,
00046     EndCdataSection,
00047     Default,
00048     DefaultHandlerExpand,
00049     NotStandalone,
00050     ExternalEntityRef,
00051     StartDoctypeDecl,
00052     EndDoctypeDecl,
00053     EntityDecl,
00054     XmlDecl,
00055     ElementDecl,
00056     AttlistDecl,
00057 #if XML_COMBINED_VERSION >= 19504
00058     SkippedEntity,
00059 #endif
00060     _DummyDecl
00061 };


Function Documentation

static int call_character_handler xmlparseobject self,
const XML_Char buffer,
int  len
[static]
 

Definition at line 442 of file pyexpat.c.

References call_with_frame(), CharacterData, conv_string_len_to_utf8(), flag_error(), getcode(), and xmlparseobject::handlers.

Referenced by flush_character_buffer(), and my_CharacterDataHandler().

00443 {
00444     PyObject *args;
00445     PyObject *temp;
00446 
00447     args = PyTuple_New(1);
00448     if (args == NULL)
00449         return -1;
00450 #ifdef Py_USING_UNICODE
00451     temp = (self->returns_unicode 
00452             ? conv_string_len_to_unicode(buffer, len) 
00453             : conv_string_len_to_utf8(buffer, len));
00454 #else
00455     temp = conv_string_len_to_utf8(buffer, len);
00456 #endif
00457     if (temp == NULL) {
00458         Py_DECREF(args);
00459         flag_error(self);
00460         return -1;
00461     }
00462     PyTuple_SET_ITEM(args, 0, temp);
00463     /* temp is now a borrowed reference; consider it unused. */
00464     self->in_callback = 1;
00465     temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
00466                            self->handlers[CharacterData], args, self);
00467     /* temp is an owned reference again, or NULL */
00468     self->in_callback = 0;
00469     Py_DECREF(args);
00470     if (temp == NULL) {
00471         flag_error(self);
00472         return -1;
00473     }
00474     Py_DECREF(temp);
00475     return 0;
00476 }

static PyObject* call_with_frame PyCodeObject *  c,
PyObject *  func,
PyObject *  args,
xmlparseobject self
[static]
 

Definition at line 368 of file pyexpat.c.

References xmlparseobject::itself, trace_frame(), trace_frame_exc(), XML_FALSE, and XML_StopParser().

Referenced by call_character_handler(), my_ElementDeclHandler(), and my_StartElementHandler().

00370 {
00371     PyThreadState *tstate = PyThreadState_GET();
00372     PyFrameObject *f;
00373     PyObject *res;
00374 
00375     if (c == NULL)
00376         return NULL;
00377     
00378     f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
00379     if (f == NULL)
00380         return NULL;
00381     tstate->frame = f;
00382 #ifdef FIX_TRACE
00383     if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
00384         return NULL;
00385     }
00386 #endif
00387     res = PyEval_CallObject(func, args);
00388     if (res == NULL) {
00389         if (tstate->curexc_traceback == NULL)
00390             PyTraceBack_Here(f);
00391         XML_StopParser(self->itself, XML_FALSE);
00392 #ifdef FIX_TRACE
00393         if (trace_frame_exc(tstate, f) < 0) {
00394             return NULL;
00395         }
00396     }
00397     else {
00398         if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
00399             Py_XDECREF(res);
00400             res = NULL;
00401         }
00402     }
00403 #else
00404     }
00405 #endif
00406     tstate->frame = f->f_back;
00407     Py_DECREF(f);
00408     return res;
00409 }

static void clear_handlers xmlparseobject self,
int  initial
[static]
 

Definition at line 2020 of file pyexpat.c.

References xmlparseobject::itself, and HandlerInfo::setter.

Referenced by flag_error(), newxmlparseobject(), and xmlparse_ExternalEntityParserCreate().

02021 {
02022     int i = 0;
02023     PyObject *temp;
02024 
02025     for (; handler_info[i].name != NULL; i++) {
02026         if (initial)
02027             self->handlers[i] = NULL;
02028         else {
02029             temp = self->handlers[i];
02030             self->handlers[i] = NULL;
02031             Py_XDECREF(temp);
02032             handler_info[i].setter(self->itself, NULL);
02033         }
02034     }
02035 }

static PyObject* conv_string_len_to_utf8 const XML_Char str,
int  len
[static]
 

Definition at line 215 of file pyexpat.c.

Referenced by call_character_handler().

00216 {
00217     /* XXX currently this code assumes that XML_Char is 8-bit,
00218        and hence in UTF-8.  */
00219     /* UTF-8 from Expat, UTF-8 desired */
00220     if (str == NULL) {
00221         Py_INCREF(Py_None);
00222         return Py_None;
00223     }
00224     return PyString_FromStringAndSize((const char *)str, len);
00225 }

static PyObject* conv_string_to_utf8 const XML_Char str  )  [static]
 

Definition at line 202 of file pyexpat.c.

Referenced by my_ElementDeclHandler().

00203 {
00204     /* XXX currently this code assumes that XML_Char is 8-bit,
00205        and hence in UTF-8.  */
00206     /* UTF-8 from Expat, UTF-8 desired */
00207     if (str == NULL) {
00208         Py_INCREF(Py_None);
00209         return Py_None;
00210     }
00211     return PyString_FromString(str);
00212 }

static int error_external_entity_ref_handler XML_Parser  parser,
const XML_Char context,
const XML_Char base,
const XML_Char systemId,
const XML_Char publicId
[static]
 

Definition at line 235 of file pyexpat.c.

Referenced by flag_error().

00240 {
00241     return 0;
00242 }

static void flag_error xmlparseobject self  )  [static]
 

Definition at line 245 of file pyexpat.c.

References clear_handlers(), error_external_entity_ref_handler(), xmlparseobject::itself, and XML_SetExternalEntityRefHandler().

Referenced by call_character_handler(), my_ElementDeclHandler(), and my_StartElementHandler().

00246 {
00247     clear_handlers(self, 0);
00248     XML_SetExternalEntityRefHandler(self->itself,
00249                                     error_external_entity_ref_handler);
00250 }

static int flush_character_buffer xmlparseobject self  )  [static]
 

Definition at line 479 of file pyexpat.c.

References xmlparseobject::buffer, xmlparseobject::buffer_used, and call_character_handler().

Referenced by my_CharacterDataHandler(), my_ElementDeclHandler(), my_StartElementHandler(), VOID_HANDLER(), and xmlparse_setattr().

00480 {
00481     int rc;
00482     if (self->buffer == NULL || self->buffer_used == 0)
00483         return 0;
00484     rc = call_character_handler(self, self->buffer, self->buffer_used);
00485     self->buffer_used = 0;
00486     return rc;
00487 }

static PyObject* get_handler_name struct HandlerInfo hinfo  )  [static]
 

Definition at line 155 of file pyexpat.c.

References HandlerInfo::name, and HandlerInfo::nameobj.

Referenced by xmlparse_getattr().

00156 {
00157     PyObject *name = hinfo->nameobj;
00158     if (name == NULL) {
00159         name = PyString_FromString(hinfo->name);
00160         hinfo->nameobj = name;
00161     }
00162     Py_XINCREF(name);
00163     return name;
00164 }

static PyObject* get_pybool int  istrue  )  [static]
 

Definition at line 1438 of file pyexpat.c.

Referenced by xmlparse_getattr().

01439 {
01440     PyObject *result = istrue ? Py_True : Py_False;
01441     Py_INCREF(result);
01442     return result;
01443 }

static PyObject* get_version_string void   )  [static]
 

Definition at line 1798 of file pyexpat.c.

Referenced by MODULE_INITFUNC().

01799 {
01800     static char *rcsid = "#Revision: 2.88 $";
01801     char *rev = rcsid;
01802     int i = 0;
01803 
01804     while (!isdigit((int)*rev))
01805         ++rev;
01806     while (rev[i] != ' ' && rev[i] != '\0')
01807         ++i;
01808 
01809     return PyString_FromStringAndSize(rev, i);
01810 }

static PyCodeObject* getcode enum HandlerTypes  slot,
char *  func_name,
int  lineno
[static]
 

Definition at line 253 of file pyexpat.c.

References HandlerInfo::tb_code.

Referenced by call_character_handler(), my_ElementDeclHandler(), and my_StartElementHandler().

00254 {
00255     PyObject *code = NULL;
00256     PyObject *name = NULL;
00257     PyObject *nulltuple = NULL;
00258     PyObject *filename = NULL;
00259 
00260     if (handler_info[slot].tb_code == NULL) {
00261         code = PyString_FromString("");
00262         if (code == NULL)
00263             goto failed;
00264         name = PyString_FromString(func_name);
00265         if (name == NULL)
00266             goto failed;
00267         nulltuple = PyTuple_New(0);
00268         if (nulltuple == NULL)
00269             goto failed;
00270         filename = PyString_FromString(__FILE__);
00271         handler_info[slot].tb_code =
00272             PyCode_New(0,               /* argcount */
00273                        0,               /* nlocals */
00274                        0,               /* stacksize */
00275                        0,               /* flags */
00276                        code,            /* code */
00277                        nulltuple,       /* consts */
00278                        nulltuple,       /* names */
00279                        nulltuple,       /* varnames */
00280 #if PYTHON_API_VERSION >= 1010
00281                        nulltuple,       /* freevars */
00282                        nulltuple,       /* cellvars */
00283 #endif
00284                        filename,        /* filename */
00285                        name,            /* name */
00286                        lineno,          /* firstlineno */
00287                        code             /* lnotab */
00288                        );
00289         if (handler_info[slot].tb_code == NULL)
00290             goto failed;
00291         Py_DECREF(code);
00292         Py_DECREF(nulltuple);
00293         Py_DECREF(filename);
00294         Py_DECREF(name);
00295     }
00296     return handler_info[slot].tb_code;
00297  failed:
00298     Py_XDECREF(code);
00299     Py_XDECREF(name);
00300     return NULL;
00301 }

static int handlername2int const char *  name  )  [static]
 

Definition at line 1426 of file pyexpat.c.

Referenced by sethandler(), and xmlparse_getattr().

01427 {
01428     int i;
01429     for (i = 0; handler_info[i].name != NULL; i++) {
01430         if (strcmp(name, handler_info[i].name) == 0) {
01431             return i;
01432         }
01433     }
01434     return -1;
01435 }

static int have_handler xmlparseobject self,
int  type
[static]
 

Definition at line 148 of file pyexpat.c.

References HandlerInfo::handler.

Referenced by my_CharacterDataHandler(), my_ElementDeclHandler(), and my_StartElementHandler().

00149 {
00150     PyObject *handler = self->handlers[type];
00151     return handler != NULL;
00152 }

static void my_CharacterDataHandler void *  userData,
const XML_Char data,
int  len
[static]
 

Definition at line 490 of file pyexpat.c.

References call_character_handler(), CharacterData, flush_character_buffer(), and have_handler().

00491 {
00492     xmlparseobject *self = (xmlparseobject *) userData;
00493     if (self->buffer == NULL)
00494         call_character_handler(self, data, len);
00495     else {
00496         if ((self->buffer_used + len) > self->buffer_size) {
00497             if (flush_character_buffer(self) < 0)
00498                 return;
00499             /* handler might have changed; drop the rest on the floor
00500              * if there isn't a handler anymore
00501              */
00502             if (!have_handler(self, CharacterData))
00503                 return;
00504         }
00505         if (len > self->buffer_size) {
00506             call_character_handler(self, data, len);
00507             self->buffer_used = 0;
00508         }
00509         else {
00510             memcpy(self->buffer + self->buffer_used,
00511                    data, len * sizeof(XML_Char));
00512             self->buffer_used += len;
00513         }
00514     }
00515 }

static void my_ElementDeclHandler void *  userData,
const XML_Char name,
XML_Content model
[static]
 

Definition at line 732 of file pyexpat.c.

References call_with_frame(), conv_string_to_utf8(), ElementDecl, flag_error(), flush_character_buffer(), getcode(), have_handler(), HandlerInfo::nameobj, string_intern(), and XML_FreeContentModel().

00735 {
00736     xmlparseobject *self = (xmlparseobject *)userData;
00737     PyObject *args = NULL;
00738 
00739     if (have_handler(self, ElementDecl)) {
00740         PyObject *rv = NULL;
00741         PyObject *modelobj, *nameobj;
00742 
00743         if (flush_character_buffer(self) < 0)
00744             goto finally;
00745 #ifdef Py_USING_UNICODE
00746         modelobj = conv_content_model(model,
00747                                       (self->returns_unicode
00748                                        ? conv_string_to_unicode
00749                                        : conv_string_to_utf8));
00750 #else
00751         modelobj = conv_content_model(model, conv_string_to_utf8);
00752 #endif
00753         if (modelobj == NULL) {
00754             flag_error(self);
00755             goto finally;
00756         }
00757         nameobj = string_intern(self, name);
00758         if (nameobj == NULL) {
00759             Py_DECREF(modelobj);
00760             flag_error(self);
00761             goto finally;
00762         }
00763         args = Py_BuildValue("NN", nameobj, modelobj);
00764         if (args == NULL) {
00765             Py_DECREF(modelobj);
00766             flag_error(self);
00767             goto finally;
00768         }
00769         self->in_callback = 1;
00770         rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
00771                              self->handlers[ElementDecl], args, self);
00772         self->in_callback = 0;
00773         if (rv == NULL) {
00774             flag_error(self);
00775             goto finally;
00776         }
00777         Py_DECREF(rv);
00778     }
00779  finally:
00780     Py_XDECREF(args);
00781     XML_FreeContentModel(self->itself, model);
00782     return;
00783 }

static void my_StartElementHandler void *  userData,
const XML_Char name,
const XML_Char atts[]
[static]
 

Definition at line 518 of file pyexpat.c.

References call_with_frame(), flag_error(), flush_character_buffer(), getcode(), have_handler(), StartElement, STRING_CONV_FUNC, string_intern(), and XML_GetSpecifiedAttributeCount().

00520 {
00521     xmlparseobject *self = (xmlparseobject *)userData;
00522 
00523     if (have_handler(self, StartElement)) {
00524         PyObject *container, *rv, *args;
00525         int i, max;
00526 
00527         if (flush_character_buffer(self) < 0)
00528             return;
00529         /* Set max to the number of slots filled in atts[]; max/2 is
00530          * the number of attributes we need to process.
00531          */
00532         if (self->specified_attributes) {
00533             max = XML_GetSpecifiedAttributeCount(self->itself);
00534         }
00535         else {
00536             max = 0;
00537             while (atts[max] != NULL)
00538                 max += 2;
00539         }
00540         /* Build the container. */
00541         if (self->ordered_attributes)
00542             container = PyList_New(max);
00543         else
00544             container = PyDict_New();
00545         if (container == NULL) {
00546             flag_error(self);
00547             return;
00548         }
00549         for (i = 0; i < max; i += 2) {
00550             PyObject *n = string_intern(self, (XML_Char *) atts[i]);
00551             PyObject *v;
00552             if (n == NULL) {
00553                 flag_error(self);
00554                 Py_DECREF(container);
00555                 return;
00556             }
00557             v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
00558             if (v == NULL) {
00559                 flag_error(self);
00560                 Py_DECREF(container);
00561                 Py_DECREF(n);
00562                 return;
00563             }
00564             if (self->ordered_attributes) {
00565                 PyList_SET_ITEM(container, i, n);
00566                 PyList_SET_ITEM(container, i+1, v);
00567             }
00568             else if (PyDict_SetItem(container, n, v)) {
00569                 flag_error(self);
00570                 Py_DECREF(n);
00571                 Py_DECREF(v);
00572                 return;
00573             }
00574             else {
00575                 Py_DECREF(n);
00576                 Py_DECREF(v);
00577             }
00578         }
00579         args = Py_BuildValue("(NN)", string_intern(self, name), container);
00580         if (args == NULL) {
00581             Py_DECREF(container);
00582             return;
00583         }
00584         /* Container is now a borrowed reference; ignore it. */
00585         self->in_callback = 1;
00586         rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
00587                              self->handlers[StartElement], args, self);
00588         self->in_callback = 0;
00589         Py_DECREF(args);
00590         if (rv == NULL) {
00591             flag_error(self);
00592             return;
00593         }
00594         Py_DECREF(rv);
00595     }
00596 }

static PyObject* newxmlparseobject char *  encoding,
char *  namespace_separator,
PyObject *  intern
[static]
 

Definition at line 1321 of file pyexpat.c.

References CHARACTER_DATA_BUFFER_SIZE, clear_handlers(), xmlparseobject::intern, xmlparseobject::itself, XML_ParserCreate(), XML_ParserCreateNS(), XML_SetUnknownEncodingHandler(), XML_SetUserData(), and Xmlparsetype.

Referenced by pyexpat_ParserCreate().

01322 {
01323     int i;
01324     xmlparseobject *self;
01325 
01326 #ifdef Py_TPFLAGS_HAVE_GC
01327     /* Code for versions 2.2 and later */
01328     self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
01329 #else
01330     self = PyObject_New(xmlparseobject, &Xmlparsetype);
01331 #endif
01332     if (self == NULL)
01333         return NULL;
01334 
01335 #ifdef Py_USING_UNICODE
01336     self->returns_unicode = 1;
01337 #else
01338     self->returns_unicode = 0;
01339 #endif
01340 
01341     self->buffer = NULL;
01342     self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
01343     self->buffer_used = 0;
01344     self->ordered_attributes = 0;
01345     self->specified_attributes = 0;
01346     self->in_callback = 0;
01347     self->ns_prefixes = 0;
01348     self->handlers = NULL;
01349     if (namespace_separator != NULL) {
01350         self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
01351     }
01352     else {
01353         self->itself = XML_ParserCreate(encoding);
01354     }
01355     self->intern = intern;
01356     Py_XINCREF(self->intern);
01357 #ifdef Py_TPFLAGS_HAVE_GC
01358     PyObject_GC_Track(self);
01359 #else
01360     PyObject_GC_Init(self);
01361 #endif
01362     if (self->itself == NULL) {
01363         PyErr_SetString(PyExc_RuntimeError,
01364                         "XML_ParserCreate failed");
01365         Py_DECREF(self);
01366         return NULL;
01367     }
01368     XML_SetUserData(self->itself, (void *)self);
01369 #ifdef Py_USING_UNICODE
01370     XML_SetUnknownEncodingHandler(self->itself,
01371                   (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
01372 #endif
01373 
01374     for (i = 0; handler_info[i].name != NULL; i++)
01375         /* do nothing */;
01376 
01377     self->handlers = malloc(sizeof(PyObject *) * i);
01378     if (!self->handlers) {
01379         Py_DECREF(self);
01380         return PyErr_NoMemory();
01381     }
01382     clear_handlers(self, 1);
01383 
01384     return (PyObject*)self;
01385 }

static PyObject* pyexpat_ErrorString PyObject *  self,
PyObject *  args
[static]
 

Definition at line 1767 of file pyexpat.c.

References XML_ErrorString().

01768 {
01769     long code = 0;
01770 
01771     if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
01772         return NULL;
01773     return Py_BuildValue("z", XML_ErrorString((int)code));
01774 }

static PyObject* pyexpat_ParserCreate PyObject *  notused,
PyObject *  args,
PyObject *  kw
[static]
 

Definition at line 1720 of file pyexpat.c.

References newxmlparseobject().

01721 {
01722     char *encoding = NULL;
01723     char *namespace_separator = NULL;
01724     PyObject *intern = NULL;
01725     PyObject *result;
01726     int intern_decref = 0;
01727     static char *kwlist[] = {"encoding", "namespace_separator",
01728                              "intern", NULL};
01729 
01730     if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
01731                                      &encoding, &namespace_separator, &intern))
01732         return NULL;
01733     if (namespace_separator != NULL
01734         && strlen(namespace_separator) > 1) {
01735         PyErr_SetString(PyExc_ValueError,
01736                         "namespace_separator must be at most one"
01737                         " character, omitted, or None");
01738         return NULL;
01739     }
01740     /* Explicitly passing None means no interning is desired.
01741        Not passing anything means that a new dictionary is used. */
01742     if (intern == Py_None)
01743         intern = NULL;
01744     else if (intern == NULL) {
01745         intern = PyDict_New();
01746         if (!intern)
01747             return NULL;
01748         intern_decref = 1;
01749     }
01750     else if (!PyDict_Check(intern)) {
01751         PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
01752         return NULL;
01753     }
01754 
01755     result = newxmlparseobject(encoding, namespace_separator, intern);
01756     if (intern_decref) {
01757         Py_DECREF(intern);
01758     }
01759     return result;
01760 }

static int readinst char *  buf,
int  buf_size,
PyObject *  meth