5 #include "frameobject.h"
8 #ifdef XML_MAJOR_VERSION
9 #define EXPAT_VERSION (0x10000 * XML_MAJOR_VERSION \
10 + 0x100 * XML_MINOR_VERSION \
13 /* Assume the oldest Expat that used expat.h and did not have version info */
14 #define EXPAT_VERSION 0x015f00
16 #else /* !defined(HAVE_EXPAT_H) */
18 /* Assume Expat 1.1 unless told otherwise */
20 #define EXPAT_VERSION 0x010100
22 #endif /* !defined(HAVE_EXPAT_H) */
24 #ifndef PyGC_HEAD_SIZE
25 #define PyGC_HEAD_SIZE 0
26 #define PyObject_GC_Init(x)
27 #define PyObject_GC_Fini(m)
28 #define Py_TPFLAGS_GC 0
34 ProcessingInstruction
,
47 #if EXPAT_VERSION >= 0x010200
51 #if EXPAT_VERSION == 0x010200
52 ExternalParsedEntityDecl
,
53 InternalParsedEntityDecl
,
55 #if EXPAT_VERSION >= 0x015f00
64 static PyObject
*ErrorObject
;
66 /* ----------------------------------------------------- */
68 /* Declarations for objects of type xmlparser */
74 int returns_unicode
; /* True if Unicode strings are returned;
75 if false, UTF-8 strings are returned */
76 int ordered_attributes
; /* Return attributes as a list. */
77 int specified_attributes
; /* Report only specified attributes. */
78 int in_callback
; /* Is a callback active? */
82 staticforward PyTypeObject Xmlparsetype
;
84 typedef void (*xmlhandlersetter
)(XML_Parser
*self
, void *meth
);
85 typedef void* xmlhandler
;
89 xmlhandlersetter setter
;
91 PyCodeObject
*tb_code
;
94 staticforward
struct HandlerInfo handler_info
[64];
96 /* Set an integer attribute on the error object; return true on success,
97 * false on an exception.
100 set_error_attr(PyObject
*err
, char *name
, int value
)
102 PyObject
*v
= PyInt_FromLong(value
);
104 if (v
!= NULL
&& PyObject_SetAttrString(err
, name
, v
) == -1) {
111 /* Build and set an Expat exception, including positioning
112 * information. Always returns NULL.
115 set_error(xmlparseobject
*self
)
119 XML_Parser parser
= self
->itself
;
120 int lineno
= XML_GetErrorLineNumber(parser
);
121 int column
= XML_GetErrorColumnNumber(parser
);
122 enum XML_Error code
= XML_GetErrorCode(parser
);
124 sprintf(buffer
, "%.200s: line %i, column %i",
125 XML_ErrorString(code
), lineno
, column
);
126 err
= PyObject_CallFunction(ErrorObject
, "s", buffer
);
128 && set_error_attr(err
, "code", code
)
129 && set_error_attr(err
, "offset", column
)
130 && set_error_attr(err
, "lineno", lineno
)) {
131 PyErr_SetObject(ErrorObject
, err
);
137 #if EXPAT_VERSION == 0x010200
138 /* Convert an array of attributes and their values into a Python dict */
141 conv_atts_using_string(XML_Char
**atts
)
143 PyObject
*attrs_obj
= NULL
;
144 XML_Char
**attrs_p
, **attrs_k
= NULL
;
148 if ((attrs_obj
= PyDict_New()) == NULL
)
150 for (attrs_len
= 0, attrs_p
= atts
;
152 attrs_p
++, attrs_len
++) {
154 rv
= PyString_FromString(*attrs_p
);
156 Py_DECREF(attrs_obj
);
160 if (PyDict_SetItemString(attrs_obj
,
161 (char*)*attrs_k
, rv
) < 0) {
162 Py_DECREF(attrs_obj
);
176 #if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
177 #if EXPAT_VERSION == 0x010200
179 conv_atts_using_unicode(XML_Char
**atts
)
182 XML_Char
**attrs_p
, **attrs_k
= NULL
;
185 if ((attrs_obj
= PyDict_New()) == NULL
)
187 for (attrs_len
= 0, attrs_p
= atts
;
189 attrs_p
++, attrs_len
++) {
191 PyObject
*attr_str
, *value_str
;
192 const char *p
= (const char *) (*attrs_k
);
193 attr_str
= PyUnicode_DecodeUTF8(p
, strlen(p
), "strict");
195 Py_DECREF(attrs_obj
);
199 p
= (const char *) *attrs_p
;
200 value_str
= PyUnicode_DecodeUTF8(p
, strlen(p
), "strict");
202 Py_DECREF(attrs_obj
);
207 if (PyDict_SetItem(attrs_obj
, attr_str
, value_str
) < 0) {
208 Py_DECREF(attrs_obj
);
210 Py_DECREF(value_str
);
215 Py_DECREF(value_str
);
225 /* Convert a string of XML_Chars into a Unicode string.
226 Returns None if str is a null pointer. */
229 conv_string_to_unicode(XML_Char
*str
)
231 /* XXX currently this code assumes that XML_Char is 8-bit,
232 and hence in UTF-8. */
233 /* UTF-8 from Expat, Unicode desired */
238 return PyUnicode_DecodeUTF8((const char *)str
,
239 strlen((const char *)str
),
244 conv_string_len_to_unicode(const XML_Char
*str
, int len
)
246 /* XXX currently this code assumes that XML_Char is 8-bit,
247 and hence in UTF-8. */
248 /* UTF-8 from Expat, Unicode desired */
253 return PyUnicode_DecodeUTF8((const char *)str
, len
, "strict");
257 /* Convert a string of XML_Chars into an 8-bit Python string.
258 Returns None if str is a null pointer. */
261 conv_string_to_utf8(XML_Char
*str
)
263 /* XXX currently this code assumes that XML_Char is 8-bit,
264 and hence in UTF-8. */
265 /* UTF-8 from Expat, UTF-8 desired */
270 return PyString_FromString((const char *)str
);
274 conv_string_len_to_utf8(const XML_Char
*str
, int len
)
276 /* XXX currently this code assumes that XML_Char is 8-bit,
277 and hence in UTF-8. */
278 /* UTF-8 from Expat, UTF-8 desired */
283 return PyString_FromStringAndSize((const char *)str
, len
);
286 /* Callback routines */
288 static void clear_handlers(xmlparseobject
*self
, int decref
);
291 flag_error(xmlparseobject
*self
)
293 clear_handlers(self
, 1);
297 getcode(enum HandlerTypes slot
, char* func_name
, int lineno
)
299 PyObject
*code
= NULL
;
300 PyObject
*name
= NULL
;
301 PyObject
*nulltuple
= NULL
;
302 PyObject
*filename
= NULL
;
304 if (handler_info
[slot
].tb_code
== NULL
) {
305 code
= PyString_FromString("");
308 name
= PyString_FromString(func_name
);
311 nulltuple
= PyTuple_New(0);
312 if (nulltuple
== NULL
)
314 filename
= PyString_FromString(__FILE__
);
315 handler_info
[slot
].tb_code
=
316 PyCode_New(0, /* argcount */
321 nulltuple
, /* consts */
322 nulltuple
, /* names */
323 nulltuple
, /* varnames */
324 #if PYTHON_API_VERSION >= 1010
325 nulltuple
, /* freevars */
326 nulltuple
, /* cellvars */
328 filename
, /* filename */
330 lineno
, /* firstlineno */
333 if (handler_info
[slot
].tb_code
== NULL
)
336 Py_DECREF(nulltuple
);
340 return handler_info
[slot
].tb_code
;
348 call_with_frame(PyCodeObject
*c
, PyObject
* func
, PyObject
* args
)
350 PyThreadState
*tstate
= PyThreadState_GET();
359 tstate
->frame
->f_globals
, /*globals*/
365 res
= PyEval_CallObject(func
, args
);
366 if (res
== NULL
&& tstate
->curexc_traceback
== NULL
)
368 tstate
->frame
= f
->f_back
;
373 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
374 #define STRING_CONV_FUNC conv_string_to_utf8
376 /* Python 1.6 and later versions */
377 #define STRING_CONV_FUNC (self->returns_unicode \
378 ? conv_string_to_unicode : conv_string_to_utf8)
382 my_StartElementHandler(void *userData
,
383 const XML_Char
*name
, const XML_Char
**atts
)
385 xmlparseobject
*self
= (xmlparseobject
*)userData
;
387 if (self
->handlers
[StartElement
]
388 && self
->handlers
[StartElement
] != Py_None
) {
389 PyObject
*container
, *rv
, *args
;
392 /* Set max to the number of slots filled in atts[]; max/2 is
393 * the number of attributes we need to process.
395 if (self
->specified_attributes
) {
396 max
= XML_GetSpecifiedAttributeCount(self
->itself
);
400 while (atts
[max
] != NULL
)
403 /* Build the container. */
404 if (self
->ordered_attributes
)
405 container
= PyList_New(max
);
407 container
= PyDict_New();
408 if (container
== NULL
) {
412 for (i
= 0; i
< max
; i
+= 2) {
413 PyObject
*n
= STRING_CONV_FUNC((XML_Char
*) atts
[i
]);
417 Py_DECREF(container
);
420 v
= STRING_CONV_FUNC((XML_Char
*) atts
[i
+1]);
423 Py_DECREF(container
);
427 if (self
->ordered_attributes
) {
428 PyList_SET_ITEM(container
, i
, n
);
429 PyList_SET_ITEM(container
, i
+1, v
);
431 else if (PyDict_SetItem(container
, n
, v
)) {
442 args
= Py_BuildValue("(O&N)", STRING_CONV_FUNC
,name
, container
);
444 Py_DECREF(container
);
447 /* Container is now a borrowed reference; ignore it. */
448 self
->in_callback
= 1;
449 rv
= call_with_frame(getcode(StartElement
, "StartElement", __LINE__
),
450 self
->handlers
[StartElement
], args
);
451 self
->in_callback
= 0;
461 #define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
462 RETURN, GETUSERDATA) \
464 my_##NAME##Handler PARAMS {\
465 xmlparseobject *self = GETUSERDATA ; \
466 PyObject *args = NULL; \
467 PyObject *rv = NULL; \
470 if (self->handlers[NAME] \
471 && self->handlers[NAME] != Py_None) { \
472 args = Py_BuildValue PARAM_FORMAT ;\
475 self->in_callback = 1; \
476 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
477 self->handlers[NAME], args); \
478 self->in_callback = 0; \
490 #define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
491 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
492 (xmlparseobject *)userData)
494 #define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
495 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
496 rc = PyInt_AsLong(rv);, rc, \
497 (xmlparseobject *)userData)
499 VOID_HANDLER(EndElement
,
500 (void *userData
, const XML_Char
*name
),
501 ("(O&)", STRING_CONV_FUNC
, name
))
503 VOID_HANDLER(ProcessingInstruction
,
505 const XML_Char
*target
,
506 const XML_Char
*data
),
507 ("(O&O&)",STRING_CONV_FUNC
,target
, STRING_CONV_FUNC
,data
))
509 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
510 VOID_HANDLER(CharacterData
,
511 (void *userData
, const XML_Char
*data
, int len
),
512 ("(N)", conv_string_len_to_utf8(data
,len
)))
514 VOID_HANDLER(CharacterData
,
515 (void *userData
, const XML_Char
*data
, int len
),
516 ("(N)", (self
->returns_unicode
517 ? conv_string_len_to_unicode(data
,len
)
518 : conv_string_len_to_utf8(data
,len
))))
521 VOID_HANDLER(UnparsedEntityDecl
,
523 const XML_Char
*entityName
,
524 const XML_Char
*base
,
525 const XML_Char
*systemId
,
526 const XML_Char
*publicId
,
527 const XML_Char
*notationName
),
529 STRING_CONV_FUNC
,entityName
, STRING_CONV_FUNC
,base
,
530 STRING_CONV_FUNC
,systemId
, STRING_CONV_FUNC
,publicId
,
531 STRING_CONV_FUNC
,notationName
))
533 #if EXPAT_VERSION >= 0x015f00
534 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
535 VOID_HANDLER(EntityDecl
,
537 const XML_Char
*entityName
,
538 int is_parameter_entity
,
539 const XML_Char
*value
,
541 const XML_Char
*base
,
542 const XML_Char
*systemId
,
543 const XML_Char
*publicId
,
544 const XML_Char
*notationName
),
546 STRING_CONV_FUNC
,entityName
, is_parameter_entity
,
547 conv_string_len_to_utf8(value
, value_length
),
548 STRING_CONV_FUNC
,base
, STRING_CONV_FUNC
,systemId
,
549 STRING_CONV_FUNC
,publicId
, STRING_CONV_FUNC
,notationName
))
551 VOID_HANDLER(EntityDecl
,
553 const XML_Char
*entityName
,
554 int is_parameter_entity
,
555 const XML_Char
*value
,
557 const XML_Char
*base
,
558 const XML_Char
*systemId
,
559 const XML_Char
*publicId
,
560 const XML_Char
*notationName
),
562 STRING_CONV_FUNC
,entityName
, is_parameter_entity
,
563 (self
->returns_unicode
564 ? conv_string_len_to_unicode(value
, value_length
)
565 : conv_string_len_to_utf8(value
, value_length
)),
566 STRING_CONV_FUNC
,base
, STRING_CONV_FUNC
,systemId
,
567 STRING_CONV_FUNC
,publicId
, STRING_CONV_FUNC
,notationName
))
570 VOID_HANDLER(XmlDecl
,
572 const XML_Char
*version
,
573 const XML_Char
*encoding
,
576 STRING_CONV_FUNC
,version
, STRING_CONV_FUNC
,encoding
,
580 conv_content_model(XML_Content
* const model
,
581 PyObject
*(*conv_string
)(XML_Char
*))
583 PyObject
*result
= NULL
;
584 PyObject
*children
= PyTuple_New(model
->numchildren
);
587 if (children
!= NULL
) {
588 for (i
= 0; i
< model
->numchildren
; ++i
) {
589 PyObject
*child
= conv_content_model(&model
->children
[i
],
592 Py_XDECREF(children
);
595 PyTuple_SET_ITEM(children
, i
, child
);
597 result
= Py_BuildValue("(iiO&N)",
598 model
->type
, model
->quant
,
599 conv_string
,model
->name
, children
);
605 conv_content_model_utf8(XML_Content
* const model
)
607 return conv_content_model(model
, conv_string_to_utf8
);
610 #if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
612 conv_content_model_unicode(XML_Content
* const model
)
614 return conv_content_model(model
, conv_string_to_unicode
);
617 VOID_HANDLER(ElementDecl
,
619 const XML_Char
*name
,
622 STRING_CONV_FUNC
,name
,
623 (self
->returns_unicode
? conv_content_model_unicode
624 : conv_content_model_utf8
),model
))
626 VOID_HANDLER(ElementDecl
,
628 const XML_Char
*name
,
631 STRING_CONV_FUNC
,name
, conv_content_model_utf8
,model
))
634 VOID_HANDLER(AttlistDecl
,
636 const XML_Char
*elname
,
637 const XML_Char
*attname
,
638 const XML_Char
*att_type
,
639 const XML_Char
*dflt
,
642 STRING_CONV_FUNC
,elname
, STRING_CONV_FUNC
,attname
,
643 STRING_CONV_FUNC
,att_type
, STRING_CONV_FUNC
,dflt
,
647 VOID_HANDLER(NotationDecl
,
649 const XML_Char
*notationName
,
650 const XML_Char
*base
,
651 const XML_Char
*systemId
,
652 const XML_Char
*publicId
),
654 STRING_CONV_FUNC
,notationName
, STRING_CONV_FUNC
,base
,
655 STRING_CONV_FUNC
,systemId
, STRING_CONV_FUNC
,publicId
))
657 VOID_HANDLER(StartNamespaceDecl
,
659 const XML_Char
*prefix
,
660 const XML_Char
*uri
),
661 ("(O&O&)", STRING_CONV_FUNC
,prefix
, STRING_CONV_FUNC
,uri
))
663 VOID_HANDLER(EndNamespaceDecl
,
665 const XML_Char
*prefix
),
666 ("(O&)", STRING_CONV_FUNC
,prefix
))
668 VOID_HANDLER(Comment
,
669 (void *userData
, const XML_Char
*prefix
),
670 ("(O&)", STRING_CONV_FUNC
,prefix
))
672 VOID_HANDLER(StartCdataSection
,
676 VOID_HANDLER(EndCdataSection
,
680 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
681 VOID_HANDLER(Default
,
682 (void *userData
, const XML_Char
*s
, int len
),
683 ("(N)", conv_string_len_to_utf8(s
,len
)))
685 VOID_HANDLER(DefaultHandlerExpand
,
686 (void *userData
, const XML_Char
*s
, int len
),
687 ("(N)", conv_string_len_to_utf8(s
,len
)))
689 VOID_HANDLER(Default
,
690 (void *userData
, const XML_Char
*s
, int len
),
691 ("(N)", (self
->returns_unicode
692 ? conv_string_len_to_unicode(s
,len
)
693 : conv_string_len_to_utf8(s
,len
))))
695 VOID_HANDLER(DefaultHandlerExpand
,
696 (void *userData
, const XML_Char
*s
, int len
),
697 ("(N)", (self
->returns_unicode
698 ? conv_string_len_to_unicode(s
,len
)
699 : conv_string_len_to_utf8(s
,len
))))
702 INT_HANDLER(NotStandalone
,
706 RC_HANDLER(int, ExternalEntityRef
,
708 const XML_Char
*context
,
709 const XML_Char
*base
,
710 const XML_Char
*systemId
,
711 const XML_Char
*publicId
),
714 STRING_CONV_FUNC
,context
, STRING_CONV_FUNC
,base
,
715 STRING_CONV_FUNC
,systemId
, STRING_CONV_FUNC
,publicId
),
716 rc
= PyInt_AsLong(rv
);, rc
,
717 XML_GetUserData(parser
))
719 /* XXX UnknownEncodingHandler */
721 #if EXPAT_VERSION == 0x010200
722 VOID_HANDLER(StartDoctypeDecl
,
723 (void *userData
, const XML_Char
*doctypeName
),
724 ("(O&OOi)", STRING_CONV_FUNC
,doctypeName
,
725 Py_None
, Py_None
, -1))
726 #elif EXPAT_VERSION >= 0x015f00
727 VOID_HANDLER(StartDoctypeDecl
,
728 (void *userData
, const XML_Char
*doctypeName
,
729 const XML_Char
*sysid
, const XML_Char
*pubid
,
730 int has_internal_subset
),
731 ("(O&O&O&i)", STRING_CONV_FUNC
,doctypeName
,
732 STRING_CONV_FUNC
,sysid
, STRING_CONV_FUNC
,pubid
,
733 has_internal_subset
))
736 #if EXPAT_VERSION >= 0x010200
737 VOID_HANDLER(EndDoctypeDecl
, (void *userData
), ("()"))
740 #if EXPAT_VERSION == 0x010200
741 VOID_HANDLER(ExternalParsedEntityDecl
,
742 (void *userData
, const XML_Char
*entityName
,
743 const XML_Char
*base
, const XML_Char
*systemId
,
744 const XML_Char
*publicId
),
745 ("(O&O&O&O&)", STRING_CONV_FUNC
, entityName
,
746 STRING_CONV_FUNC
, base
, STRING_CONV_FUNC
, systemId
,
747 STRING_CONV_FUNC
, publicId
))
749 VOID_HANDLER(InternalParsedEntityDecl
,
750 (void *userData
, const XML_Char
*entityName
,
751 const XML_Char
*replacementText
, int replacementTextLength
),
752 ("(O&O&i)", STRING_CONV_FUNC
, entityName
,
753 STRING_CONV_FUNC
, replacementText
, replacementTextLength
))
755 #endif /* Expat version 1.2 & better */
757 /* ---------------------------------------------------------------- */
759 static char xmlparse_Parse__doc__
[] =
760 "Parse(data[, isfinal])\n\
761 Parse XML data. `isfinal' should be true at end of input.";
764 xmlparse_Parse(xmlparseobject
*self
, PyObject
*args
)
771 if (!PyArg_ParseTuple(args
, "s#|i:Parse", &s
, &slen
, &isFinal
))
773 rv
= XML_Parse(self
->itself
, s
, slen
, isFinal
);
774 if (PyErr_Occurred()) {
778 return set_error(self
);
780 return PyInt_FromLong(rv
);
783 /* File reading copied from cPickle */
785 #define BUF_SIZE 2048
788 readinst(char *buf
, int buf_size
, PyObject
*meth
)
790 PyObject
*arg
= NULL
;
791 PyObject
*bytes
= NULL
;
792 PyObject
*str
= NULL
;
795 if ((bytes
= PyInt_FromLong(buf_size
)) == NULL
)
798 if ((arg
= PyTuple_New(1)) == NULL
)
801 PyTuple_SET_ITEM(arg
, 0, bytes
);
803 if ((str
= PyObject_CallObject(meth
, arg
)) == NULL
)
806 /* XXX what to do if it returns a Unicode string? */
807 if (!PyString_Check(str
)) {
808 PyErr_Format(PyExc_TypeError
,
809 "read() did not return a string object (type=%.400s)",
810 str
->ob_type
->tp_name
);
813 len
= PyString_GET_SIZE(str
);
814 if (len
> buf_size
) {
815 PyErr_Format(PyExc_ValueError
,
816 "read() returned too much data: "
817 "%i bytes requested, %i returned",
822 memcpy(buf
, PyString_AsString(str
), len
);
829 static char xmlparse_ParseFile__doc__
[] =
831 Parse XML data from file-like object.";
834 xmlparse_ParseFile(xmlparseobject
*self
, PyObject
*args
)
839 PyObject
*readmethod
= NULL
;
841 if (!PyArg_ParseTuple(args
, "O:ParseFile", &f
))
844 if (PyFile_Check(f
)) {
845 fp
= PyFile_AsFile(f
);
849 readmethod
= PyObject_GetAttrString(f
, "read");
850 if (readmethod
== NULL
) {
852 PyErr_SetString(PyExc_TypeError
,
853 "argument must have 'read' attribute");
859 void *buf
= XML_GetBuffer(self
->itself
, BUF_SIZE
);
861 return PyErr_NoMemory();
864 bytes_read
= fread(buf
, sizeof(char), BUF_SIZE
, fp
);
865 if (bytes_read
< 0) {
866 PyErr_SetFromErrno(PyExc_IOError
);
871 bytes_read
= readinst(buf
, BUF_SIZE
, readmethod
);
875 rv
= XML_ParseBuffer(self
->itself
, bytes_read
, bytes_read
== 0);
876 if (PyErr_Occurred())
879 if (!rv
|| bytes_read
== 0)
883 return set_error(self
);
885 return Py_BuildValue("i", rv
);
888 static char xmlparse_SetBase__doc__
[] =
889 "SetBase(base_url)\n\
890 Set the base URL for the parser.";
893 xmlparse_SetBase(xmlparseobject
*self
, PyObject
*args
)
897 if (!PyArg_ParseTuple(args
, "s:SetBase", &base
))
899 if (!XML_SetBase(self
->itself
, base
)) {
900 return PyErr_NoMemory();
906 static char xmlparse_GetBase__doc__
[] =
908 Return base URL string for the parser.";
911 xmlparse_GetBase(xmlparseobject
*self
, PyObject
*args
)
913 if (!PyArg_ParseTuple(args
, ":GetBase"))
916 return Py_BuildValue("z", XML_GetBase(self
->itself
));
919 #if EXPAT_VERSION >= 0x015f00
920 static char xmlparse_GetInputContext__doc__
[] =
921 "GetInputContext() -> string\n\
922 Return the untranslated text of the input that caused the current event.\n\
923 If the event was generated by a large amount of text (such as a start tag\n\
924 for an element with many attributes), not all of the text may be available.";
927 xmlparse_GetInputContext(xmlparseobject
*self
, PyObject
*args
)
929 PyObject
*result
= NULL
;
931 if (PyArg_ParseTuple(args
, ":GetInputContext")) {
932 if (self
->in_callback
) {
935 = XML_GetInputContext(self
->itself
, &offset
, &size
);
938 result
= PyString_FromStringAndSize(buffer
+ offset
, size
);
953 static char xmlparse_ExternalEntityParserCreate__doc__
[] =
954 "ExternalEntityParserCreate(context[, encoding])\n\
955 Create a parser for parsing an external entity based on the\n\
956 information passed to the ExternalEntityRefHandler.";
959 xmlparse_ExternalEntityParserCreate(xmlparseobject
*self
, PyObject
*args
)
962 char *encoding
= NULL
;
963 xmlparseobject
*new_parser
;
966 if (!PyArg_ParseTuple(args
, "s|s:ExternalEntityParserCreate",
967 &context
, &encoding
)) {
971 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
972 new_parser
= PyObject_NEW(xmlparseobject
, &Xmlparsetype
);
974 /* Python versions 1.6 and later */
975 new_parser
= PyObject_New(xmlparseobject
, &Xmlparsetype
);
978 if (new_parser
== NULL
)
980 new_parser
->returns_unicode
= self
->returns_unicode
;
981 new_parser
->ordered_attributes
= self
->ordered_attributes
;
982 new_parser
->specified_attributes
= self
->specified_attributes
;
983 new_parser
->in_callback
= 0;
984 new_parser
->itself
= XML_ExternalEntityParserCreate(self
->itself
, context
,
986 new_parser
->handlers
= 0;
987 PyObject_GC_Init(new_parser
);
989 if (!new_parser
->itself
) {
990 Py_DECREF(new_parser
);
991 return PyErr_NoMemory();
994 XML_SetUserData(new_parser
->itself
, (void *)new_parser
);
996 /* allocate and clear handlers first */
997 for(i
= 0; handler_info
[i
].name
!= NULL
; i
++)
1000 new_parser
->handlers
= malloc(sizeof(PyObject
*)*i
);
1001 if (!new_parser
->handlers
) {
1002 Py_DECREF(new_parser
);
1003 return PyErr_NoMemory();
1005 clear_handlers(new_parser
, 0);
1007 /* then copy handlers from self */
1008 for (i
= 0; handler_info
[i
].name
!= NULL
; i
++) {
1009 if (self
->handlers
[i
]) {
1010 Py_INCREF(self
->handlers
[i
]);
1011 new_parser
->handlers
[i
] = self
->handlers
[i
];
1012 handler_info
[i
].setter(new_parser
->itself
,
1013 handler_info
[i
].handler
);
1016 return (PyObject
*)new_parser
;
1019 #if EXPAT_VERSION >= 0x010200
1021 static char xmlparse_SetParamEntityParsing__doc__
[] =
1022 "SetParamEntityParsing(flag) -> success\n\
1023 Controls parsing of parameter entities (including the external DTD\n\
1024 subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1025 XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1026 XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1030 xmlparse_SetParamEntityParsing(xmlparseobject
*p
, PyObject
* args
)
1033 if (!PyArg_ParseTuple(args
, "i", &flag
))
1035 flag
= XML_SetParamEntityParsing(p
->itself
, flag
);
1036 return PyInt_FromLong(flag
);
1039 #endif /* Expat version 1.2 or better */
1041 static struct PyMethodDef xmlparse_methods
[] = {
1042 {"Parse", (PyCFunction
)xmlparse_Parse
,
1043 METH_VARARGS
, xmlparse_Parse__doc__
},
1044 {"ParseFile", (PyCFunction
)xmlparse_ParseFile
,
1045 METH_VARARGS
, xmlparse_ParseFile__doc__
},
1046 {"SetBase", (PyCFunction
)xmlparse_SetBase
,
1047 METH_VARARGS
, xmlparse_SetBase__doc__
},
1048 {"GetBase", (PyCFunction
)xmlparse_GetBase
,
1049 METH_VARARGS
, xmlparse_GetBase__doc__
},
1050 {"ExternalEntityParserCreate", (PyCFunction
)xmlparse_ExternalEntityParserCreate
,
1051 METH_VARARGS
, xmlparse_ExternalEntityParserCreate__doc__
},
1052 #if EXPAT_VERSION >= 0x010200
1053 {"SetParamEntityParsing", (PyCFunction
)xmlparse_SetParamEntityParsing
,
1054 METH_VARARGS
, xmlparse_SetParamEntityParsing__doc__
},
1056 #if EXPAT_VERSION >= 0x015f00
1057 {"GetInputContext", (PyCFunction
)xmlparse_GetInputContext
,
1058 METH_VARARGS
, xmlparse_GetInputContext__doc__
},
1060 {NULL
, NULL
} /* sentinel */
1066 #if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
1069 pyexpat international encoding support.
1070 Make it as simple as possible.
1073 static char template_buffer
[257];
1074 PyObject
*template_string
= NULL
;
1077 init_template_buffer(void)
1080 for (i
= 0; i
< 256; i
++) {
1081 template_buffer
[i
] = i
;
1083 template_buffer
[256] = 0;
1087 PyUnknownEncodingHandler(void *encodingHandlerData
,
1088 const XML_Char
*name
,
1089 XML_Encoding
* info
)
1091 PyUnicodeObject
*_u_string
= NULL
;
1095 /* Yes, supports only 8bit encodings */
1096 _u_string
= (PyUnicodeObject
*)
1097 PyUnicode_Decode(template_buffer
, 256, name
, "replace");
1099 if (_u_string
== NULL
)
1102 for (i
= 0; i
< 256; i
++) {
1103 /* Stupid to access directly, but fast */
1104 Py_UNICODE c
= _u_string
->str
[i
];
1105 if (c
== Py_UNICODE_REPLACEMENT_CHARACTER
)
1112 info
->convert
= NULL
;
1113 info
->release
= NULL
;
1116 Py_DECREF(_u_string
);
1123 newxmlparseobject(char *encoding
, char *namespace_separator
)
1126 xmlparseobject
*self
;
1128 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1129 self
= PyObject_NEW(xmlparseobject
, &Xmlparsetype
);
1133 self
->returns_unicode
= 0;
1135 /* Code for versions 1.6 and later */
1136 self
= PyObject_New(xmlparseobject
, &Xmlparsetype
);
1140 self
->returns_unicode
= 1;
1142 self
->ordered_attributes
= 0;
1143 self
->specified_attributes
= 0;
1144 self
->in_callback
= 0;
1145 self
->handlers
= NULL
;
1146 if (namespace_separator
!= NULL
) {
1147 self
->itself
= XML_ParserCreateNS(encoding
, *namespace_separator
);
1150 self
->itself
= XML_ParserCreate(encoding
);
1152 PyObject_GC_Init(self
);
1153 if (self
->itself
== NULL
) {
1154 PyErr_SetString(PyExc_RuntimeError
,
1155 "XML_ParserCreate failed");
1159 XML_SetUserData(self
->itself
, (void *)self
);
1160 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1162 XML_SetUnknownEncodingHandler(self
->itself
, (XML_UnknownEncodingHandler
) PyUnknownEncodingHandler
, NULL
);
1165 for(i
= 0; handler_info
[i
].name
!= NULL
; i
++)
1168 self
->handlers
= malloc(sizeof(PyObject
*)*i
);
1169 if (!self
->handlers
){
1171 return PyErr_NoMemory();
1173 clear_handlers(self
, 0);
1175 return (PyObject
*)self
;
1180 xmlparse_dealloc(xmlparseobject
*self
)
1183 PyObject_GC_Fini(self
);
1184 if (self
->itself
!= NULL
)
1185 XML_ParserFree(self
->itself
);
1186 self
->itself
= NULL
;
1188 if (self
->handlers
!= NULL
) {
1190 for (i
= 0; handler_info
[i
].name
!= NULL
; i
++) {
1191 temp
= self
->handlers
[i
];
1192 self
->handlers
[i
] = NULL
;
1195 free(self
->handlers
);
1197 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1198 /* Code for versions before 1.6 */
1201 /* Code for versions 1.6 and later */
1207 handlername2int(const char *name
)
1210 for (i
=0; handler_info
[i
].name
!= NULL
; i
++) {
1211 if (strcmp(name
, handler_info
[i
].name
) == 0) {
1219 xmlparse_getattr(xmlparseobject
*self
, char *name
)
1222 if (strcmp(name
, "ErrorCode") == 0)
1223 return PyInt_FromLong((long) XML_GetErrorCode(self
->itself
));
1224 if (strcmp(name
, "ErrorLineNumber") == 0)
1225 return PyInt_FromLong((long) XML_GetErrorLineNumber(self
->itself
));
1226 if (strcmp(name
, "ErrorColumnNumber") == 0)
1227 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self
->itself
));
1228 if (strcmp(name
, "ErrorByteIndex") == 0)
1229 return PyInt_FromLong((long) XML_GetErrorByteIndex(self
->itself
));
1230 if (strcmp(name
, "ordered_attributes") == 0)
1231 return PyInt_FromLong((long) self
->ordered_attributes
);
1232 if (strcmp(name
, "returns_unicode") == 0)
1233 return PyInt_FromLong((long) self
->returns_unicode
);
1234 if (strcmp(name
, "specified_attributes") == 0)
1235 return PyInt_FromLong((long) self
->specified_attributes
);
1237 handlernum
= handlername2int(name
);
1239 if (handlernum
!= -1 && self
->handlers
[handlernum
] != NULL
) {
1240 Py_INCREF(self
->handlers
[handlernum
]);
1241 return self
->handlers
[handlernum
];
1243 if (strcmp(name
, "__members__") == 0) {
1245 PyObject
*rc
= PyList_New(0);
1246 for(i
= 0; handler_info
[i
].name
!= NULL
; i
++) {
1247 PyList_Append(rc
, PyString_FromString(handler_info
[i
].name
));
1249 PyList_Append(rc
, PyString_FromString("ErrorCode"));
1250 PyList_Append(rc
, PyString_FromString("ErrorLineNumber"));
1251 PyList_Append(rc
, PyString_FromString("ErrorColumnNumber"));
1252 PyList_Append(rc
, PyString_FromString("ErrorByteIndex"));
1253 PyList_Append(rc
, PyString_FromString("ordered_attributes"));
1254 PyList_Append(rc
, PyString_FromString("returns_unicode"));
1255 PyList_Append(rc
, PyString_FromString("specified_attributes"));
1259 return Py_FindMethod(xmlparse_methods
, (PyObject
*)self
, name
);
1263 sethandler(xmlparseobject
*self
, const char *name
, PyObject
* v
)
1265 int handlernum
= handlername2int(name
);
1266 if (handlernum
!= -1) {
1268 Py_XDECREF(self
->handlers
[handlernum
]);
1269 self
->handlers
[handlernum
] = v
;
1270 handler_info
[handlernum
].setter(self
->itself
,
1271 handler_info
[handlernum
].handler
);
1278 xmlparse_setattr(xmlparseobject
*self
, char *name
, PyObject
*v
)
1280 /* Set attribute 'name' to value 'v'. v==NULL means delete */
1282 PyErr_SetString(PyExc_RuntimeError
, "Cannot delete attribute");
1285 if (strcmp(name
, "ordered_attributes") == 0) {
1286 if (PyObject_IsTrue(v
))
1287 self
->ordered_attributes
= 1;
1289 self
->ordered_attributes
= 0;
1292 if (strcmp(name
, "returns_unicode") == 0) {
1293 if (PyObject_IsTrue(v
)) {
1294 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1295 PyErr_SetString(PyExc_ValueError
,
1296 "Cannot return Unicode strings in Python 1.5");
1299 self
->returns_unicode
= 1;
1303 self
->returns_unicode
= 0;
1306 if (strcmp(name
, "specified_attributes") == 0) {
1307 if (PyObject_IsTrue(v
))
1308 self
->specified_attributes
= 1;
1310 self
->specified_attributes
= 0;
1313 if (sethandler(self
, name
, v
)) {
1316 PyErr_SetString(PyExc_AttributeError
, name
);
1320 #ifdef WITH_CYCLE_GC
1322 xmlparse_traverse(xmlparseobject
*op
, visitproc visit
, void *arg
)
1325 for (i
= 0; handler_info
[i
].name
!= NULL
; i
++) {
1326 if (!op
->handlers
[i
])
1328 err
= visit(op
->handlers
[i
], arg
);
1336 xmlparse_clear(xmlparseobject
*op
)
1338 clear_handlers(op
, 1);
1343 static char Xmlparsetype__doc__
[] =
1346 static PyTypeObject Xmlparsetype
= {
1347 PyObject_HEAD_INIT(NULL
)
1349 "xmlparser", /*tp_name*/
1350 sizeof(xmlparseobject
) + PyGC_HEAD_SIZE
,/*tp_basicsize*/
1353 (destructor
)xmlparse_dealloc
, /*tp_dealloc*/
1354 (printfunc
)0, /*tp_print*/
1355 (getattrfunc
)xmlparse_getattr
, /*tp_getattr*/
1356 (setattrfunc
)xmlparse_setattr
, /*tp_setattr*/
1357 (cmpfunc
)0, /*tp_compare*/
1358 (reprfunc
)0, /*tp_repr*/
1360 0, /*tp_as_sequence*/
1361 0, /*tp_as_mapping*/
1362 (hashfunc
)0, /*tp_hash*/
1363 (ternaryfunc
)0, /*tp_call*/
1364 (reprfunc
)0, /*tp_str*/
1365 0, /* tp_getattro */
1366 0, /* tp_setattro */
1367 0, /* tp_as_buffer */
1368 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /*tp_flags*/
1369 Xmlparsetype__doc__
, /* Documentation string */
1370 #ifdef WITH_CYCLE_GC
1371 (traverseproc
)xmlparse_traverse
, /* tp_traverse */
1372 (inquiry
)xmlparse_clear
/* tp_clear */
1378 /* End of code for xmlparser objects */
1379 /* -------------------------------------------------------- */
1381 static char pyexpat_ParserCreate__doc__
[] =
1382 "ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1383 Return a new XML parser object.";
1386 pyexpat_ParserCreate(PyObject
*notused
, PyObject
*args
, PyObject
*kw
)
1388 char *encoding
= NULL
;
1389 char *namespace_separator
= NULL
;
1390 static char *kwlist
[] = {"encoding", "namespace_separator", NULL
};
1392 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "|zz:ParserCreate", kwlist
,
1393 &encoding
, &namespace_separator
))
1395 if (namespace_separator
!= NULL
1396 && strlen(namespace_separator
) > 1) {
1397 PyErr_SetString(PyExc_ValueError
,
1398 "namespace_separator must be at most one"
1399 " character, omitted, or None");
1402 return newxmlparseobject(encoding
, namespace_separator
);
1405 static char pyexpat_ErrorString__doc__
[] =
1406 "ErrorString(errno) -> string\n\
1407 Returns string error for given number.";
1410 pyexpat_ErrorString(PyObject
*self
, PyObject
*args
)
1414 if (!PyArg_ParseTuple(args
, "l:ErrorString", &code
))
1416 return Py_BuildValue("z", XML_ErrorString((int)code
));
1419 /* List of methods defined in the module */
1421 static struct PyMethodDef pyexpat_methods
[] = {
1422 {"ParserCreate", (PyCFunction
)pyexpat_ParserCreate
,
1423 METH_VARARGS
|METH_KEYWORDS
, pyexpat_ParserCreate__doc__
},
1424 {"ErrorString", (PyCFunction
)pyexpat_ErrorString
,
1425 METH_VARARGS
, pyexpat_ErrorString__doc__
},
1427 {NULL
, (PyCFunction
)NULL
, 0, NULL
} /* sentinel */
1430 /* Module docstring */
1432 static char pyexpat_module_documentation
[] =
1433 "Python wrapper for Expat parser.";
1435 #if PY_VERSION_HEX < 0x20000F0
1437 /* 1.5 compatibility: PyModule_AddObject */
1439 PyModule_AddObject(PyObject
*m
, char *name
, PyObject
*o
)
1442 if (!PyModule_Check(m
) || o
== NULL
)
1444 dict
= PyModule_GetDict(m
);
1447 if (PyDict_SetItemString(dict
, name
, o
))
1454 PyModule_AddIntConstant(PyObject
*m
, char *name
, long value
)
1456 return PyModule_AddObject(m
, name
, PyInt_FromLong(value
));
1460 PyModule_AddStringConstant(PyObject
*m
, char *name
, char *value
)
1462 return PyModule_AddObject(m
, name
, PyString_FromString(value
));
1468 /* Return a Python string that represents the version number without the
1469 * extra cruft added by revision control, even if the right options were
1470 * given to the "cvs export" command to make it not include the extra
1474 get_version_string(void)
1476 static char *rcsid
= "$Revision$";
1480 while (!isdigit(*rev
))
1482 while (rev
[i
] != ' ' && rev
[i
] != '\0')
1485 return PyString_FromStringAndSize(rev
, i
);
1488 /* Initialization function for the module */
1491 #define MODULE_NAME "pyexpat"
1494 #ifndef MODULE_INITFUNC
1495 #define MODULE_INITFUNC initpyexpat
1498 void MODULE_INITFUNC(void); /* avoid compiler warnings */
1501 MODULE_INITFUNC(void)
1504 PyObject
*errmod_name
= PyString_FromString(MODULE_NAME
".errors");
1505 PyObject
*errors_module
;
1506 PyObject
*modelmod_name
;
1507 PyObject
*model_module
;
1508 PyObject
*sys_modules
;
1510 if (errmod_name
== NULL
)
1512 modelmod_name
= PyString_FromString(MODULE_NAME
".model");
1513 if (modelmod_name
== NULL
)
1516 Xmlparsetype
.ob_type
= &PyType_Type
;
1518 /* Create the module and add the functions */
1519 m
= Py_InitModule3(MODULE_NAME
, pyexpat_methods
,
1520 pyexpat_module_documentation
);
1522 /* Add some symbolic constants to the module */
1523 if (ErrorObject
== NULL
) {
1524 ErrorObject
= PyErr_NewException("xml.parsers.expat.ExpatError",
1526 if (ErrorObject
== NULL
)
1529 Py_INCREF(ErrorObject
);
1530 PyModule_AddObject(m
, "error", ErrorObject
);
1531 Py_INCREF(ErrorObject
);
1532 PyModule_AddObject(m
, "ExpatError", ErrorObject
);
1533 Py_INCREF(&Xmlparsetype
);
1534 PyModule_AddObject(m
, "XMLParserType", (PyObject
*) &Xmlparsetype
);
1536 PyModule_AddObject(m
, "__version__", get_version_string());
1537 #if EXPAT_VERSION >= 0x015f02
1538 PyModule_AddStringConstant(m
, "EXPAT_VERSION",
1539 (char *) XML_ExpatVersion());
1541 XML_Expat_Version info
= XML_ExpatVersionInfo();
1542 PyModule_AddObject(m
, "version_info",
1543 Py_BuildValue("(iii)", info
.major
,
1544 info
.minor
, info
.micro
));
1547 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1549 init_template_buffer();
1551 /* XXX When Expat supports some way of figuring out how it was
1552 compiled, this should check and set native_encoding
1555 PyModule_AddStringConstant(m
, "native_encoding", "UTF-8");
1557 sys_modules
= PySys_GetObject("modules");
1558 d
= PyModule_GetDict(m
);
1559 errors_module
= PyDict_GetItem(d
, errmod_name
);
1560 if (errors_module
== NULL
) {
1561 errors_module
= PyModule_New(MODULE_NAME
".errors");
1562 if (errors_module
!= NULL
) {
1563 PyDict_SetItem(sys_modules
, errmod_name
, errors_module
);
1564 /* gives away the reference to errors_module */
1565 PyModule_AddObject(m
, "errors", errors_module
);
1568 Py_DECREF(errmod_name
);
1569 model_module
= PyDict_GetItem(d
, modelmod_name
);
1570 if (model_module
== NULL
) {
1571 model_module
= PyModule_New(MODULE_NAME
".model");
1572 if (model_module
!= NULL
) {
1573 PyDict_SetItem(sys_modules
, modelmod_name
, model_module
);
1574 /* gives away the reference to model_module */
1575 PyModule_AddObject(m
, "model", model_module
);
1578 Py_DECREF(modelmod_name
);
1579 if (errors_module
== NULL
|| model_module
== NULL
)
1580 /* Don't core dump later! */
1583 #define MYCONST(name) \
1584 PyModule_AddStringConstant(errors_module, #name, \
1585 (char*)XML_ErrorString(name))
1587 MYCONST(XML_ERROR_NO_MEMORY
);
1588 MYCONST(XML_ERROR_SYNTAX
);
1589 MYCONST(XML_ERROR_NO_ELEMENTS
);
1590 MYCONST(XML_ERROR_INVALID_TOKEN
);
1591 MYCONST(XML_ERROR_UNCLOSED_TOKEN
);
1592 MYCONST(XML_ERROR_PARTIAL_CHAR
);
1593 MYCONST(XML_ERROR_TAG_MISMATCH
);
1594 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE
);
1595 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT
);
1596 MYCONST(XML_ERROR_PARAM_ENTITY_REF
);
1597 MYCONST(XML_ERROR_UNDEFINED_ENTITY
);
1598 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF
);
1599 MYCONST(XML_ERROR_ASYNC_ENTITY
);
1600 MYCONST(XML_ERROR_BAD_CHAR_REF
);
1601 MYCONST(XML_ERROR_BINARY_ENTITY_REF
);
1602 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF
);
1603 MYCONST(XML_ERROR_MISPLACED_XML_PI
);
1604 MYCONST(XML_ERROR_UNKNOWN_ENCODING
);
1605 MYCONST(XML_ERROR_INCORRECT_ENCODING
);
1606 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION
);
1607 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING
);
1608 MYCONST(XML_ERROR_NOT_STANDALONE
);
1610 PyModule_AddStringConstant(errors_module
, "__doc__",
1611 "Constants used to describe error conditions.");
1615 #if EXPAT_VERSION >= 0x010200
1616 #define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
1617 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER
);
1618 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE
);
1619 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS
);
1623 #if EXPAT_VERSION >= 0x015f00
1624 #define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1625 PyModule_AddStringConstant(model_module
, "__doc__",
1626 "Constants used to interpret content model information.");
1628 MYCONST(XML_CTYPE_EMPTY
);
1629 MYCONST(XML_CTYPE_ANY
);
1630 MYCONST(XML_CTYPE_MIXED
);
1631 MYCONST(XML_CTYPE_NAME
);
1632 MYCONST(XML_CTYPE_CHOICE
);
1633 MYCONST(XML_CTYPE_SEQ
);
1635 MYCONST(XML_CQUANT_NONE
);
1636 MYCONST(XML_CQUANT_OPT
);
1637 MYCONST(XML_CQUANT_REP
);
1638 MYCONST(XML_CQUANT_PLUS
);
1644 clear_handlers(xmlparseobject
*self
, int decref
)
1649 for (; handler_info
[i
].name
!=NULL
; i
++) {
1651 temp
= self
->handlers
[i
];
1652 self
->handlers
[i
] = NULL
;
1655 self
->handlers
[i
]=NULL
;
1656 handler_info
[i
].setter(self
->itself
, NULL
);
1660 typedef void (*pairsetter
)(XML_Parser
, void *handler1
, void *handler2
);
1663 pyxml_UpdatePairedHandlers(xmlparseobject
*self
,
1668 void *start_handler
= NULL
;
1669 void *end_handler
= NULL
;
1671 if (self
->handlers
[startHandler
]
1672 && self
->handlers
[endHandler
] != Py_None
) {
1673 start_handler
= handler_info
[startHandler
].handler
;
1675 if (self
->handlers
[EndElement
]
1676 && self
->handlers
[EndElement
] != Py_None
) {
1677 end_handler
= handler_info
[endHandler
].handler
;
1679 setter(self
->itself
, start_handler
, end_handler
);
1683 pyxml_SetStartElementHandler(XML_Parser
*parser
, void *junk
)
1685 pyxml_UpdatePairedHandlers((xmlparseobject
*)XML_GetUserData(parser
),
1686 StartElement
, EndElement
,
1687 (pairsetter
)XML_SetElementHandler
);
1691 pyxml_SetEndElementHandler(XML_Parser
*parser
, void *junk
)
1693 pyxml_UpdatePairedHandlers((xmlparseobject
*)XML_GetUserData(parser
),
1694 StartElement
, EndElement
,
1695 (pairsetter
)XML_SetElementHandler
);
1699 pyxml_SetStartNamespaceDeclHandler(XML_Parser
*parser
, void *junk
)
1701 pyxml_UpdatePairedHandlers((xmlparseobject
*)XML_GetUserData(parser
),
1702 StartNamespaceDecl
, EndNamespaceDecl
,
1703 (pairsetter
)XML_SetNamespaceDeclHandler
);
1707 pyxml_SetEndNamespaceDeclHandler(XML_Parser
*parser
, void *junk
)
1709 pyxml_UpdatePairedHandlers((xmlparseobject
*)XML_GetUserData(parser
),
1710 StartNamespaceDecl
, EndNamespaceDecl
,
1711 (pairsetter
)XML_SetNamespaceDeclHandler
);
1715 pyxml_SetStartCdataSection(XML_Parser
*parser
, void *junk
)
1717 pyxml_UpdatePairedHandlers((xmlparseobject
*)XML_GetUserData(parser
),
1718 StartCdataSection
, EndCdataSection
,
1719 (pairsetter
)XML_SetCdataSectionHandler
);
1723 pyxml_SetEndCdataSection(XML_Parser
*parser
, void *junk
)
1725 pyxml_UpdatePairedHandlers((xmlparseobject
*)XML_GetUserData(parser
),
1726 StartCdataSection
, EndCdataSection
,
1727 (pairsetter
)XML_SetCdataSectionHandler
);
1730 #if EXPAT_VERSION >= 0x010200
1733 pyxml_SetStartDoctypeDeclHandler(XML_Parser
*parser
, void *junk
)
1735 pyxml_UpdatePairedHandlers((xmlparseobject
*)XML_GetUserData(parser
),
1736 StartDoctypeDecl
, EndDoctypeDecl
,
1737 (pairsetter
)XML_SetDoctypeDeclHandler
);
1741 pyxml_SetEndDoctypeDeclHandler(XML_Parser
*parser
, void *junk
)
1743 pyxml_UpdatePairedHandlers((xmlparseobject
*)XML_GetUserData(parser
),
1744 StartDoctypeDecl
, EndDoctypeDecl
,
1745 (pairsetter
)XML_SetDoctypeDeclHandler
);
1750 statichere
struct HandlerInfo handler_info
[] = {
1751 {"StartElementHandler",
1752 pyxml_SetStartElementHandler
,
1753 (xmlhandler
)my_StartElementHandler
},
1754 {"EndElementHandler",
1755 pyxml_SetEndElementHandler
,
1756 (xmlhandler
)my_EndElementHandler
},
1757 {"ProcessingInstructionHandler",
1758 (xmlhandlersetter
)XML_SetProcessingInstructionHandler
,
1759 (xmlhandler
)my_ProcessingInstructionHandler
},
1760 {"CharacterDataHandler",
1761 (xmlhandlersetter
)XML_SetCharacterDataHandler
,
1762 (xmlhandler
)my_CharacterDataHandler
},
1763 {"UnparsedEntityDeclHandler",
1764 (xmlhandlersetter
)XML_SetUnparsedEntityDeclHandler
,
1765 (xmlhandler
)my_UnparsedEntityDeclHandler
},
1766 {"NotationDeclHandler",
1767 (xmlhandlersetter
)XML_SetNotationDeclHandler
,
1768 (xmlhandler
)my_NotationDeclHandler
},
1769 {"StartNamespaceDeclHandler",
1770 pyxml_SetStartNamespaceDeclHandler
,
1771 (xmlhandler
)my_StartNamespaceDeclHandler
},
1772 {"EndNamespaceDeclHandler",
1773 pyxml_SetEndNamespaceDeclHandler
,
1774 (xmlhandler
)my_EndNamespaceDeclHandler
},
1776 (xmlhandlersetter
)XML_SetCommentHandler
,
1777 (xmlhandler
)my_CommentHandler
},
1778 {"StartCdataSectionHandler",
1779 pyxml_SetStartCdataSection
,
1780 (xmlhandler
)my_StartCdataSectionHandler
},
1781 {"EndCdataSectionHandler",
1782 pyxml_SetEndCdataSection
,
1783 (xmlhandler
)my_EndCdataSectionHandler
},
1785 (xmlhandlersetter
)XML_SetDefaultHandler
,
1786 (xmlhandler
)my_DefaultHandler
},
1787 {"DefaultHandlerExpand",
1788 (xmlhandlersetter
)XML_SetDefaultHandlerExpand
,
1789 (xmlhandler
)my_DefaultHandlerExpandHandler
},
1790 {"NotStandaloneHandler",
1791 (xmlhandlersetter
)XML_SetNotStandaloneHandler
,
1792 (xmlhandler
)my_NotStandaloneHandler
},
1793 {"ExternalEntityRefHandler",
1794 (xmlhandlersetter
)XML_SetExternalEntityRefHandler
,
1795 (xmlhandler
)my_ExternalEntityRefHandler
},
1796 #if EXPAT_VERSION >= 0x010200
1797 {"StartDoctypeDeclHandler",
1798 pyxml_SetStartDoctypeDeclHandler
,
1799 (xmlhandler
)my_StartDoctypeDeclHandler
},
1800 {"EndDoctypeDeclHandler",
1801 pyxml_SetEndDoctypeDeclHandler
,
1802 (xmlhandler
)my_EndDoctypeDeclHandler
},
1804 #if EXPAT_VERSION == 0x010200
1805 {"ExternalParsedEntityDeclHandler",
1806 (xmlhandlersetter
)XML_SetExternalParsedEntityDeclHandler
,
1807 (xmlhandler
)my_ExternalParsedEntityDeclHandler
},
1808 {"InternalParsedEntityDeclHandler",
1809 (xmlhandlersetter
)XML_SetInternalParsedEntityDeclHandler
,
1810 (xmlhandler
)my_InternalParsedEntityDeclHandler
},
1812 #if EXPAT_VERSION >= 0x015f00
1813 {"EntityDeclHandler",
1814 (xmlhandlersetter
)XML_SetEntityDeclHandler
,
1815 (xmlhandler
)my_EntityDeclHandler
},
1817 (xmlhandlersetter
)XML_SetXmlDeclHandler
,
1818 (xmlhandler
)my_XmlDeclHandler
},
1819 {"ElementDeclHandler",
1820 (xmlhandlersetter
)XML_SetElementDeclHandler
,
1821 (xmlhandler
)my_ElementDeclHandler
},
1822 {"AttlistDeclHandler",
1823 (xmlhandlersetter
)XML_SetAttlistDeclHandler
,
1824 (xmlhandler
)my_AttlistDeclHandler
},
1825 #endif /* Expat version 1.95 or better */
1827 {NULL
, NULL
, NULL
} /* sentinel */