Debian Codegraph @

cpython27/Modules/pyexpat.c

raw symbols SCIP
1 #include "Python.h"
2 #include <ctype.h>
3
4 #include "frameobject.h"
5 #include "expat.h"
6
7 #include "pyexpat.h"
8
9 #define XML_COMBINED_VERSION (10000*XML_MAJOR_VERSION+100*XML_MINOR_VERSION+XML_MICRO_VERSION)
10
11 #ifndef PyDoc_STRVAR
12
13 /*
14 * fdrake says:
15 * Don't change the PyDoc_STR macro definition to (str), because
16 * '''the parentheses cause compile failures
17 * ("non-constant static initializer" or something like that)
18 * on some platforms (Irix?)'''
19 */
20 #define PyDoc_STR(str) str
21 #define PyDoc_VAR(name) static char name[]
22 #define PyDoc_STRVAR(name,str) PyDoc_VAR(name) = PyDoc_STR(str)
23 #endif
24
25 #if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 2)
26 /* In Python 2.0 and 2.1, disabling Unicode was not possible. */
27 #define Py_USING_UNICODE
28 #else
29 #define FIX_TRACE
30 #endif
31
32 enum HandlerTypes {
33 StartElement,
34 EndElement,
35 ProcessingInstruction,
36 CharacterData,
37 UnparsedEntityDecl,
38 NotationDecl,
39 StartNamespaceDecl,
40 EndNamespaceDecl,
41 Comment,
42 StartCdataSection,
43 EndCdataSection,
44 Default,
45 DefaultHandlerExpand,
46 NotStandalone,
47 ExternalEntityRef,
48 StartDoctypeDecl,
49 EndDoctypeDecl,
50 EntityDecl,
51 XmlDecl,
52 ElementDecl,
53 AttlistDecl,
54 #if XML_COMBINED_VERSION >= 19504
55 SkippedEntity,
56 #endif
57 _DummyDecl
58 };
59
60 static PyObject *ErrorObject;
61
62 /* ----------------------------------------------------- */
63
64 /* Declarations for objects of type xmlparser */
65
66 typedef struct {
67 PyObject_HEAD
68
69 XML_Parser itself;
70 int returns_unicode; /* True if Unicode strings are returned;
71 if false, UTF-8 strings are returned */
72 int ordered_attributes; /* Return attributes as a list. */
73 int specified_attributes; /* Report only specified attributes. */
74 int in_callback; /* Is a callback active? */
75 int ns_prefixes; /* Namespace-triplets mode? */
76 XML_Char *buffer; /* Buffer used when accumulating characters */
77 /* NULL if not enabled */
78 int buffer_size; /* Size of buffer, in XML_Char units */
79 int buffer_used; /* Buffer units in use */
80 PyObject *intern; /* Dictionary to intern strings */
81 PyObject **handlers;
82 } xmlparseobject;
83
84 #define CHARACTER_DATA_BUFFER_SIZE 8192
85
86 static PyTypeObject Xmlparsetype;
87
88 typedef void (*xmlhandlersetter)(XML_Parser self, void *meth);
89 typedef void* xmlhandler;
90
91 struct HandlerInfo {
92 const char *name;
93 xmlhandlersetter setter;
94 xmlhandler handler;
95 PyCodeObject *tb_code;
96 PyObject *nameobj;
97 };
98
99 static struct HandlerInfo handler_info[64];
100
101 /* Set an integer attribute on the error object; return true on success,
102 * false on an exception.
103 */
104 static int
105 set_error_attr(PyObject *err, char *name, int value)
106 {
107 PyObject *v = PyInt_FromLong(value);
108
109 if (v == NULL || PyObject_SetAttrString(err, name, v) == -1) {
110 Py_XDECREF(v);
111 return 0;
112 }
113 Py_DECREF(v);
114 return 1;
115 }
116
117 /* Build and set an Expat exception, including positioning
118 * information. Always returns NULL.
119 */
120 static PyObject *
121 set_error(xmlparseobject *self, enum XML_Error code)
122 {
123 PyObject *err;
124 char buffer[256];
125 XML_Parser parser = self->itself;
126 int lineno = XML_GetErrorLineNumber(parser);
127 int column = XML_GetErrorColumnNumber(parser);
128
129 /* There is no risk of overflowing this buffer, since
130 even for 64-bit integers, there is sufficient space. */
131 sprintf(buffer, "%.200s: line %i, column %i",
132 XML_ErrorString(code), lineno, column);
133 err = PyObject_CallFunction(ErrorObject, "s", buffer);
134 if ( err != NULL
135 && set_error_attr(err, "code", code)
136 && set_error_attr(err, "offset", column)
137 && set_error_attr(err, "lineno", lineno)) {
138 PyErr_SetObject(ErrorObject, err);
139 }
140 Py_XDECREF(err);
141 return NULL;
142 }
143
144 static int
145 have_handler(xmlparseobject *self, int type)
146 {
147 PyObject *handler = self->handlers[type];
148 return handler != NULL;
149 }
150
151 static PyObject *
152 get_handler_name(struct HandlerInfo *hinfo)
153 {
154 PyObject *name = hinfo->nameobj;
155 if (name == NULL) {
156 name = PyString_FromString(hinfo->name);
157 hinfo->nameobj = name;
158 }
159 Py_XINCREF(name);
160 return name;
161 }
162
163
164 #ifdef Py_USING_UNICODE
165 /* Convert a string of XML_Chars into a Unicode string.
166 Returns None if str is a null pointer. */
167
168 static PyObject *
169 conv_string_to_unicode(const XML_Char *str)
170 {
171 /* XXX currently this code assumes that XML_Char is 8-bit,
172 and hence in UTF-8. */
173 /* UTF-8 from Expat, Unicode desired */
174 if (str == NULL) {
175 Py_INCREF(Py_None);
176 return Py_None;
177 }
178 return PyUnicode_DecodeUTF8(str, strlen(str), "strict");
179 }
180
181 static PyObject *
182 conv_string_len_to_unicode(const XML_Char *str, int len)
183 {
184 /* XXX currently this code assumes that XML_Char is 8-bit,
185 and hence in UTF-8. */
186 /* UTF-8 from Expat, Unicode desired */
187 if (str == NULL) {
188 Py_INCREF(Py_None);
189 return Py_None;
190 }
191 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
192 }
193 #endif
194
195 /* Convert a string of XML_Chars into an 8-bit Python string.
196 Returns None if str is a null pointer. */
197
198 static PyObject *
199 conv_string_to_utf8(const XML_Char *str)
200 {
201 /* XXX currently this code assumes that XML_Char is 8-bit,
202 and hence in UTF-8. */
203 /* UTF-8 from Expat, UTF-8 desired */
204 if (str == NULL) {
205 Py_INCREF(Py_None);
206 return Py_None;
207 }
208 return PyString_FromString(str);
209 }
210
211 static PyObject *
212 conv_string_len_to_utf8(const XML_Char *str, int len)
213 {
214 /* XXX currently this code assumes that XML_Char is 8-bit,
215 and hence in UTF-8. */
216 /* UTF-8 from Expat, UTF-8 desired */
217 if (str == NULL) {
218 Py_INCREF(Py_None);
219 return Py_None;
220 }
221 return PyString_FromStringAndSize((const char *)str, len);
222 }
223
224 /* Callback routines */
225
226 static void clear_handlers(xmlparseobject *self, int initial);
227
228 /* This handler is used when an error has been detected, in the hope
229 that actual parsing can be terminated early. This will only help
230 if an external entity reference is encountered. */
231 static int
232 error_external_entity_ref_handler(XML_Parser parser,
233 const XML_Char *context,
234 const XML_Char *base,
235 const XML_Char *systemId,
236 const XML_Char *publicId)
237 {
238 return 0;
239 }
240
241 /* Dummy character data handler used when an error (exception) has
242 been detected, and the actual parsing can be terminated early.
243 This is needed since character data handler can't be safely removed
244 from within the character data handler, but can be replaced. It is
245 used only from the character data handler trampoline, and must be
246 used right after `flag_error()` is called. */
247 static void
248 noop_character_data_handler(void *userData, const XML_Char *data, int len)
249 {
250 /* Do nothing. */
251 }
252
253 static void
254 flag_error(xmlparseobject *self)
255 {
256 clear_handlers(self, 0);
257 XML_SetExternalEntityRefHandler(self->itself,
258 error_external_entity_ref_handler);
259 }
260
261 static PyCodeObject*
262 getcode(enum HandlerTypes slot, char* func_name, int lineno)
263 {
264 if (handler_info[slot].tb_code == NULL) {
265 handler_info[slot].tb_code =
266 PyCode_NewEmpty(__FILE__, func_name, lineno);
267 }
268 return handler_info[slot].tb_code;
269 }
270
271 #ifdef FIX_TRACE
272 static int
273 trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val)
274 {
275 int result = 0;
276 if (!tstate->use_tracing || tstate->tracing)
277 return 0;
278 if (tstate->c_profilefunc != NULL) {
279 tstate->tracing++;
280 result = tstate->c_profilefunc(tstate->c_profileobj,
281 f, code , val);
282 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
283 || (tstate->c_profilefunc != NULL));
284 tstate->tracing--;
285 if (result)
286 return result;
287 }
288 if (tstate->c_tracefunc != NULL) {
289 tstate->tracing++;
290 result = tstate->c_tracefunc(tstate->c_traceobj,
291 f, code , val);
292 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
293 || (tstate->c_profilefunc != NULL));
294 tstate->tracing--;
295 }
296 return result;
297 }
298
299 static int
300 trace_frame_exc(PyThreadState *tstate, PyFrameObject *f)
301 {
302 PyObject *type, *value, *traceback, *arg;
303 int err;
304
305 if (tstate->c_tracefunc == NULL)
306 return 0;
307
308 PyErr_Fetch(&type, &value, &traceback);
309 if (value == NULL) {
310 value = Py_None;
311 Py_INCREF(value);
312 }
313 #if PY_VERSION_HEX < 0x02040000
314 arg = Py_BuildValue("(OOO)", type, value, traceback);
315 #else
316 arg = PyTuple_Pack(3, type, value, traceback);
317 #endif
318 if (arg == NULL) {
319 PyErr_Restore(type, value, traceback);
320 return 0;
321 }
322 err = trace_frame(tstate, f, PyTrace_EXCEPTION, arg);
323 Py_DECREF(arg);
324 if (err == 0)
325 PyErr_Restore(type, value, traceback);
326 else {
327 Py_XDECREF(type);
328 Py_XDECREF(value);
329 Py_XDECREF(traceback);
330 }
331 return err;
332 }
333 #endif
334
335 static PyObject*
336 call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args,
337 xmlparseobject *self)
338 {
339 PyThreadState *tstate = PyThreadState_GET();
340 PyFrameObject *f;
341 PyObject *res;
342
343 if (c == NULL)
344 return NULL;
345
346 f = PyFrame_New(tstate, c, PyEval_GetGlobals(), NULL);
347 if (f == NULL)
348 return NULL;
349 tstate->frame = f;
350 #ifdef FIX_TRACE
351 if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
352 return NULL;
353 }
354 #endif
355 res = PyEval_CallObject(func, args);
356 if (res == NULL) {
357 if (tstate->curexc_traceback == NULL)
358 PyTraceBack_Here(f);
359 XML_StopParser(self->itself, XML_FALSE);
360 #ifdef FIX_TRACE
361 if (trace_frame_exc(tstate, f) < 0) {
362 return NULL;
363 }
364 }
365 else {
366 if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
367 Py_XDECREF(res);
368 res = NULL;
369 }
370 }
371 #else
372 }
373 #endif
374 tstate->frame = f->f_back;
375 Py_DECREF(f);
376 return res;
377 }
378
379 #ifndef Py_USING_UNICODE
380 #define STRING_CONV_FUNC conv_string_to_utf8
381 #else
382 /* Python 2.0 and later versions, when built with Unicode support */
383 #define STRING_CONV_FUNC (self->returns_unicode \
384 ? conv_string_to_unicode : conv_string_to_utf8)
385 #endif
386
387 static PyObject*
388 string_intern(xmlparseobject *self, const char* str)
389 {
390 PyObject *result = STRING_CONV_FUNC(str);
391 PyObject *value;
392 /* result can be NULL if the unicode conversion failed. */
393 if (!result)
394 return result;
395 if (!self->intern)
396 return result;
397 value = PyDict_GetItem(self->intern, result);
398 if (!value) {
399 if (PyDict_SetItem(self->intern, result, result) == 0)
400 return result;
401 else {
402 Py_DECREF(result);
403 return NULL;
404 }
405 }
406 Py_INCREF(value);
407 Py_DECREF(result);
408 return value;
409 }
410
411 /* Return 0 on success, -1 on exception.
412 * flag_error() will be called before return if needed.
413 */
414 static int
415 call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
416 {
417 PyObject *args;
418 PyObject *temp;
419
420 if (!have_handler(self, CharacterData))
421 return -1;
422
423 args = PyTuple_New(1);
424 if (args == NULL)
425 return -1;
426 #ifdef Py_USING_UNICODE
427 temp = (self->returns_unicode
428 ? conv_string_len_to_unicode(buffer, len)
429 : conv_string_len_to_utf8(buffer, len));
430 #else
431 temp = conv_string_len_to_utf8(buffer, len);
432 #endif
433 if (temp == NULL) {
434 Py_DECREF(args);
435 flag_error(self);
436 XML_SetCharacterDataHandler(self->itself,
437 noop_character_data_handler);
438 return -1;
439 }
440 PyTuple_SET_ITEM(args, 0, temp);
441 /* temp is now a borrowed reference; consider it unused. */
442 self->in_callback = 1;
443 temp = call_with_frame(getcode(CharacterData, "CharacterData", __LINE__),
444 self->handlers[CharacterData], args, self);
445 /* temp is an owned reference again, or NULL */
446 self->in_callback = 0;
447 Py_DECREF(args);
448 if (temp == NULL) {
449 flag_error(self);
450 XML_SetCharacterDataHandler(self->itself,
451 noop_character_data_handler);
452 return -1;
453 }
454 Py_DECREF(temp);
455 return 0;
456 }
457
458 static int
459 flush_character_buffer(xmlparseobject *self)
460 {
461 int rc;
462 if (self->buffer == NULL || self->buffer_used == 0)
463 return 0;
464 rc = call_character_handler(self, self->buffer, self->buffer_used);
465 self->buffer_used = 0;
466 return rc;
467 }
468
469 static void
470 my_CharacterDataHandler(void *userData, const XML_Char *data, int len)
471 {
472 xmlparseobject *self = (xmlparseobject *) userData;
473 if (self->buffer == NULL)
474 call_character_handler(self, data, len);
475 else {
476 if ((self->buffer_used + len) > self->buffer_size) {
477 if (flush_character_buffer(self) < 0)
478 return;
479 /* handler might have changed; drop the rest on the floor
480 * if there isn't a handler anymore
481 */
482 if (!have_handler(self, CharacterData))
483 return;
484 }
485 if (len > self->buffer_size) {
486 call_character_handler(self, data, len);
487 self->buffer_used = 0;
488 }
489 else {
490 memcpy(self->buffer + self->buffer_used,
491 data, len * sizeof(XML_Char));
492 self->buffer_used += len;
493 }
494 }
495 }
496
497 static void
498 my_StartElementHandler(void *userData,
499 const XML_Char *name, const XML_Char *atts[])
500 {
501 xmlparseobject *self = (xmlparseobject *)userData;
502
503 if (have_handler(self, StartElement)) {
504 PyObject *container, *rv, *args;
505 int i, max;
506
507 if (flush_character_buffer(self) < 0)
508 return;
509 /* Set max to the number of slots filled in atts[]; max/2 is
510 * the number of attributes we need to process.
511 */
512 if (self->specified_attributes) {
513 max = XML_GetSpecifiedAttributeCount(self->itself);
514 }
515 else {
516 max = 0;
517 while (atts[max] != NULL)
518 max += 2;
519 }
520 /* Build the container. */
521 if (self->ordered_attributes)
522 container = PyList_New(max);
523 else
524 container = PyDict_New();
525 if (container == NULL) {
526 flag_error(self);
527 return;
528 }
529 for (i = 0; i < max; i += 2) {
530 PyObject *n = string_intern(self, (XML_Char *) atts[i]);
531 PyObject *v;
532 if (n == NULL) {
533 flag_error(self);
534 Py_DECREF(container);
535 return;
536 }
537 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
538 if (v == NULL) {
539 flag_error(self);
540 Py_DECREF(container);
541 Py_DECREF(n);
542 return;
543 }
544 if (self->ordered_attributes) {
545 PyList_SET_ITEM(container, i, n);
546 PyList_SET_ITEM(container, i+1, v);
547 }
548 else if (PyDict_SetItem(container, n, v)) {
549 flag_error(self);
550 Py_DECREF(n);
551 Py_DECREF(v);
552 Py_DECREF(container);
553 return;
554 }
555 else {
556 Py_DECREF(n);
557 Py_DECREF(v);
558 }
559 }
560 args = string_intern(self, name);
561 if (args == NULL) {
562 Py_DECREF(container);
563 return;
564 }
565 args = Py_BuildValue("(NN)", args, container);
566 if (args == NULL) {
567 return;
568 }
569 /* Container is now a borrowed reference; ignore it. */
570 self->in_callback = 1;
571 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
572 self->handlers[StartElement], args, self);
573 self->in_callback = 0;
574 Py_DECREF(args);
575 if (rv == NULL) {
576 flag_error(self);
577 return;
578 }
579 Py_DECREF(rv);
580 }
581 }
582
583 #define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
584 RETURN, GETUSERDATA) \
585 static RC \
586 my_##NAME##Handler PARAMS {\
587 xmlparseobject *self = GETUSERDATA ; \
588 PyObject *args = NULL; \
589 PyObject *rv = NULL; \
590 INIT \
591 \
592 if (have_handler(self, NAME)) { \
593 if (flush_character_buffer(self) < 0) \
594 return RETURN; \
595 args = Py_BuildValue PARAM_FORMAT ;\
596 if (!args) { flag_error(self); return RETURN;} \
597 self->in_callback = 1; \
598 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
599 self->handlers[NAME], args, self); \
600 self->in_callback = 0; \
601 Py_DECREF(args); \
602 if (rv == NULL) { \
603 flag_error(self); \
604 return RETURN; \
605 } \
606 CONVERSION \
607 Py_DECREF(rv); \
608 } \
609 return RETURN; \
610 }
611
612 #define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
613 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
614 (xmlparseobject *)userData)
615
616 #define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
617 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
618 rc = PyInt_AsLong(rv);, rc, \
619 (xmlparseobject *)userData)
620
621 VOID_HANDLER(EndElement,
622 (void *userData, const XML_Char *name),
623 ("(N)", string_intern(self, name)))
624
625 VOID_HANDLER(ProcessingInstruction,
626 (void *userData,
627 const XML_Char *target,
628 const XML_Char *data),
629 ("(NO&)", string_intern(self, target), STRING_CONV_FUNC,data))
630
631 VOID_HANDLER(UnparsedEntityDecl,
632 (void *userData,
633 const XML_Char *entityName,
634 const XML_Char *base,
635 const XML_Char *systemId,
636 const XML_Char *publicId,
637 const XML_Char *notationName),
638 ("(NNNNN)",
639 string_intern(self, entityName), string_intern(self, base),
640 string_intern(self, systemId), string_intern(self, publicId),
641 string_intern(self, notationName)))
642
643 #ifndef Py_USING_UNICODE
644 VOID_HANDLER(EntityDecl,
645 (void *userData,
646 const XML_Char *entityName,
647 int is_parameter_entity,
648 const XML_Char *value,
649 int value_length,
650 const XML_Char *base,
651 const XML_Char *systemId,
652 const XML_Char *publicId,
653 const XML_Char *notationName),
654 ("NiNNNNN",
655 string_intern(self, entityName), is_parameter_entity,
656 conv_string_len_to_utf8(value, value_length),
657 string_intern(self, base), string_intern(self, systemId),
658 string_intern(self, publicId),
659 string_intern(self, notationName)))
660 #else
661 VOID_HANDLER(EntityDecl,
662 (void *userData,
663 const XML_Char *entityName,
664 int is_parameter_entity,
665 const XML_Char *value,
666 int value_length,
667 const XML_Char *base,
668 const XML_Char *systemId,
669 const XML_Char *publicId,
670 const XML_Char *notationName),
671 ("NiNNNNN",
672 string_intern(self, entityName), is_parameter_entity,
673 (self->returns_unicode
674 ? conv_string_len_to_unicode(value, value_length)
675 : conv_string_len_to_utf8(value, value_length)),
676 string_intern(self, base), string_intern(self, systemId),
677 string_intern(self, publicId),
678 string_intern(self, notationName)))
679 #endif
680
681 VOID_HANDLER(XmlDecl,
682 (void *userData,
683 const XML_Char *version,
684 const XML_Char *encoding,
685 int standalone),
686 ("(O&O&i)",
687 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
688 standalone))
689
690 static PyObject *
691 conv_content_model(XML_Content * const model,
692 PyObject *(*conv_string)(const XML_Char *))
693 {
694 PyObject *result = NULL;
695 PyObject *children = PyTuple_New(model->numchildren);
696 int i;
697
698 if (children != NULL) {
699 assert(model->numchildren < INT_MAX);
700 for (i = 0; i < (int)model->numchildren; ++i) {
701 PyObject *child = conv_content_model(&model->children[i],
702 conv_string);
703 if (child == NULL) {
704 Py_XDECREF(children);
705 return NULL;
706 }
707 PyTuple_SET_ITEM(children, i, child);
708 }
709 result = Py_BuildValue("(iiO&N)",
710 model->type, model->quant,
711 conv_string,model->name, children);
712 }
713 return result;
714 }
715
716 static void
717 my_ElementDeclHandler(void *userData,
718 const XML_Char *name,
719 XML_Content *model)
720 {
721 xmlparseobject *self = (xmlparseobject *)userData;
722 PyObject *args = NULL;
723
724 if (have_handler(self, ElementDecl)) {
725 PyObject *rv = NULL;
726 PyObject *modelobj, *nameobj;
727
728 if (flush_character_buffer(self) < 0)
729 goto finally;
730 #ifdef Py_USING_UNICODE
731 modelobj = conv_content_model(model,
732 (self->returns_unicode
733 ? conv_string_to_unicode
734 : conv_string_to_utf8));
735 #else
736 modelobj = conv_content_model(model, conv_string_to_utf8);
737 #endif
738 if (modelobj == NULL) {
739 flag_error(self);
740 goto finally;
741 }
742 nameobj = string_intern(self, name);
743 if (nameobj == NULL) {
744 Py_DECREF(modelobj);
745 flag_error(self);
746 goto finally;
747 }
748 args = Py_BuildValue("NN", nameobj, modelobj);
749 if (args == NULL) {
750 flag_error(self);
751 goto finally;
752 }
753 self->in_callback = 1;
754 rv = call_with_frame(getcode(ElementDecl, "ElementDecl", __LINE__),
755 self->handlers[ElementDecl], args, self);
756 self->in_callback = 0;
757 if (rv == NULL) {
758 flag_error(self);
759 goto finally;
760 }
761 Py_DECREF(rv);
762 }
763 finally:
764 Py_XDECREF(args);
765 XML_FreeContentModel(self->itself, model);
766 return;
767 }
768
769 VOID_HANDLER(AttlistDecl,
770 (void *userData,
771 const XML_Char *elname,
772 const XML_Char *attname,
773 const XML_Char *att_type,
774 const XML_Char *dflt,
775 int isrequired),
776 ("(NNO&O&i)",
777 string_intern(self, elname), string_intern(self, attname),
778 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
779 isrequired))
780
781 #if XML_COMBINED_VERSION >= 19504
782 VOID_HANDLER(SkippedEntity,
783 (void *userData,
784 const XML_Char *entityName,
785 int is_parameter_entity),
786 ("Ni",
787 string_intern(self, entityName), is_parameter_entity))
788 #endif
789
790 VOID_HANDLER(NotationDecl,
791 (void *userData,
792 const XML_Char *notationName,
793 const XML_Char *base,
794 const XML_Char *systemId,
795 const XML_Char *publicId),
796 ("(NNNN)",
797 string_intern(self, notationName), string_intern(self, base),
798 string_intern(self, systemId), string_intern(self, publicId)))
799
800 VOID_HANDLER(StartNamespaceDecl,
801 (void *userData,
802 const XML_Char *prefix,
803 const XML_Char *uri),
804 ("(NN)",
805 string_intern(self, prefix), string_intern(self, uri)))
806
807 VOID_HANDLER(EndNamespaceDecl,
808 (void *userData,
809 const XML_Char *prefix),
810 ("(N)", string_intern(self, prefix)))
811
812 VOID_HANDLER(Comment,
813 (void *userData, const XML_Char *data),
814 ("(O&)", STRING_CONV_FUNC,data))
815
816 VOID_HANDLER(StartCdataSection,
817 (void *userData),
818 ("()"))
819
820 VOID_HANDLER(EndCdataSection,
821 (void *userData),
822 ("()"))
823
824 #ifndef Py_USING_UNICODE
825 VOID_HANDLER(Default,
826 (void *userData, const XML_Char *s, int len),
827 ("(N)", conv_string_len_to_utf8(s,len)))
828
829 VOID_HANDLER(DefaultHandlerExpand,
830 (void *userData, const XML_Char *s, int len),
831 ("(N)", conv_string_len_to_utf8(s,len)))
832 #else
833 VOID_HANDLER(Default,
834 (void *userData, const XML_Char *s, int len),
835 ("(N)", (self->returns_unicode
836 ? conv_string_len_to_unicode(s,len)
837 : conv_string_len_to_utf8(s,len))))
838
839 VOID_HANDLER(DefaultHandlerExpand,
840 (void *userData, const XML_Char *s, int len),
841 ("(N)", (self->returns_unicode
842 ? conv_string_len_to_unicode(s,len)
843 : conv_string_len_to_utf8(s,len))))
844 #endif
845
846 INT_HANDLER(NotStandalone,
847 (void *userData),
848 ("()"))
849
850 RC_HANDLER(int, ExternalEntityRef,
851 (XML_Parser parser,
852 const XML_Char *context,
853 const XML_Char *base,
854 const XML_Char *systemId,
855 const XML_Char *publicId),
856 int rc=0;,
857 ("(O&NNN)",
858 STRING_CONV_FUNC,context, string_intern(self, base),
859 string_intern(self, systemId), string_intern(self, publicId)),
860 rc = PyInt_AsLong(rv);, rc,
861 XML_GetUserData(parser))
862
863 /* XXX UnknownEncodingHandler */
864
865 VOID_HANDLER(StartDoctypeDecl,
866 (void *userData, const XML_Char *doctypeName,
867 const XML_Char *sysid, const XML_Char *pubid,
868 int has_internal_subset),
869 ("(NNNi)", string_intern(self, doctypeName),
870 string_intern(self, sysid), string_intern(self, pubid),
871 has_internal_subset))
872
873 VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
874
875 /* ---------------------------------------------------------------- */
876
877 static PyObject *
878 get_parse_result(xmlparseobject *self, int rv)
879 {
880 if (PyErr_Occurred()) {
881 return NULL;
882 }
883 if (rv == 0) {
884 return set_error(self, XML_GetErrorCode(self->itself));
885 }
886 if (flush_character_buffer(self) < 0) {
887 return NULL;
888 }
889 return PyInt_FromLong(rv);
890 }
891
892 PyDoc_STRVAR(xmlparse_Parse__doc__,
893 "Parse(data[, isfinal])\n\
894 Parse XML data. `isfinal' should be true at end of input.");
895
896 static PyObject *
897 xmlparse_Parse(xmlparseobject *self, PyObject *args)
898 {
899 char *s;
900 int slen;
901 int isFinal = 0;
902
903 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
904 return NULL;
905
906 return get_parse_result(self, XML_Parse(self->itself, s, slen, isFinal));
907 }
908
909 /* File reading copied from cPickle */
910
911 #define BUF_SIZE 2048
912
913 static int
914 readinst(char *buf, int buf_size, PyObject *meth)
915 {
916 PyObject *arg = NULL;
917 PyObject *bytes = NULL;
918 PyObject *str = NULL;
919 int len = -1;
920
921 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
922 goto finally;
923
924 if ((arg = PyTuple_New(1)) == NULL) {
925 Py_DECREF(bytes);
926 goto finally;
927 }
928
929 PyTuple_SET_ITEM(arg, 0, bytes);
930
931 #if PY_VERSION_HEX < 0x02020000
932 str = PyObject_CallObject(meth, arg);
933 #else
934 str = PyObject_Call(meth, arg, NULL);
935 #endif
936 if (str == NULL)
937 goto finally;
938
939 /* XXX what to do if it returns a Unicode string? */
940 if (!PyString_Check(str)) {
941 PyErr_Format(PyExc_TypeError,
942 "read() did not return a string object (type=%.400s)",
943 Py_TYPE(str)->tp_name);
944 goto finally;
945 }
946 len = PyString_GET_SIZE(str);
947 if (len > buf_size) {
948 PyErr_Format(PyExc_ValueError,
949 "read() returned too much data: "
950 "%i bytes requested, %i returned",
951 buf_size, len);
952 goto finally;
953 }
954 memcpy(buf, PyString_AsString(str), len);
955 finally:
956 Py_XDECREF(arg);
957 Py_XDECREF(str);
958 return len;
959 }
960
961 PyDoc_STRVAR(xmlparse_ParseFile__doc__,
962 "ParseFile(file)\n\
963 Parse XML data from file-like object.");
964
965 static PyObject *
966 xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
967 {
968 int rv = 1;
969 PyObject *readmethod = NULL;
970
971 readmethod = PyObject_GetAttrString(f, "read");
972 if (readmethod == NULL) {
973 PyErr_SetString(PyExc_TypeError,
974 "argument must have 'read' attribute");
975 return NULL;
976
977 }
978 for (;;) {
979 int bytes_read;
980 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
981 if (buf == NULL) {
982 Py_XDECREF(readmethod);
983 return get_parse_result(self, 0);
984 }
985
986 bytes_read = readinst(buf, BUF_SIZE, readmethod);
987 if (bytes_read < 0) {
988 Py_XDECREF(readmethod);
989 return NULL;
990 }
991
992 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
993 if (PyErr_Occurred()) {
994 Py_XDECREF(readmethod);
995 return NULL;
996 }
997
998 if (!rv || bytes_read == 0)
999 break;
1000 }
1001 Py_XDECREF(readmethod);
1002 return get_parse_result(self, rv);
1003 }
1004
1005 PyDoc_STRVAR(xmlparse_SetBase__doc__,
1006 "SetBase(base_url)\n\
1007 Set the base URL for the parser.");
1008
1009 static PyObject *
1010 xmlparse_SetBase(xmlparseobject *self, PyObject *args)
1011 {
1012 char *base;
1013
1014 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
1015 return NULL;
1016 if (!XML_SetBase(self->itself, base)) {
1017 return PyErr_NoMemory();
1018 }
1019 Py_INCREF(Py_None);
1020 return Py_None;
1021 }
1022
1023 PyDoc_STRVAR(xmlparse_GetBase__doc__,
1024 "GetBase() -> url\n\
1025 Return base URL string for the parser.");
1026
1027 static PyObject *
1028 xmlparse_GetBase(xmlparseobject *self, PyObject *unused)
1029 {
1030 return Py_BuildValue("z", XML_GetBase(self->itself));
1031 }
1032
1033 PyDoc_STRVAR(xmlparse_GetInputContext__doc__,
1034 "GetInputContext() -> string\n\
1035 Return the untranslated text of the input that caused the current event.\n\
1036 If the event was generated by a large amount of text (such as a start tag\n\
1037 for an element with many attributes), not all of the text may be available.");
1038
1039 static PyObject *
1040 xmlparse_GetInputContext(xmlparseobject *self, PyObject *unused)
1041 {
1042 if (self->in_callback) {
1043 int offset, size;
1044 const char *buffer
1045 = XML_GetInputContext(self->itself, &offset, &size);
1046
1047 if (buffer != NULL)
1048 return PyString_FromStringAndSize(buffer + offset,
1049 size - offset);
1050 else
1051 Py_RETURN_NONE;
1052 }
1053 else
1054 Py_RETURN_NONE;
1055 }
1056
1057 PyDoc_STRVAR(xmlparse_ExternalEntityParserCreate__doc__,
1058 "ExternalEntityParserCreate(context[, encoding])\n\
1059 Create a parser for parsing an external entity based on the\n\
1060 information passed to the ExternalEntityRefHandler.");
1061
1062 static PyObject *
1063 xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
1064 {
1065 char *context;
1066 char *encoding = NULL;
1067 xmlparseobject *new_parser;
1068 int i;
1069
1070 if (!PyArg_ParseTuple(args, "z|s:ExternalEntityParserCreate",
1071 &context, &encoding)) {
1072 return NULL;
1073 }
1074
1075 #ifndef Py_TPFLAGS_HAVE_GC
1076 /* Python versions 2.0 and 2.1 */
1077 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
1078 #else
1079 /* Python versions 2.2 and later */
1080 new_parser = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1081 #endif
1082
1083 if (new_parser == NULL)
1084 return NULL;
1085 new_parser->buffer_size = self->buffer_size;
1086 new_parser->buffer_used = 0;
1087 new_parser->buffer = NULL;
1088 new_parser->returns_unicode = self->returns_unicode;
1089 new_parser->ordered_attributes = self->ordered_attributes;
1090 new_parser->specified_attributes = self->specified_attributes;
1091 new_parser->in_callback = 0;
1092 new_parser->ns_prefixes = self->ns_prefixes;
1093 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
1094 encoding);
1095 new_parser->handlers = 0;
1096 new_parser->intern = self->intern;
1097 Py_XINCREF(new_parser->intern);
1098 #ifdef Py_TPFLAGS_HAVE_GC
1099 PyObject_GC_Track(new_parser);
1100 #else
1101 PyObject_GC_Init(new_parser);
1102 #endif
1103
1104 if (!new_parser->itself) {
1105 Py_DECREF(new_parser);
1106 return PyErr_NoMemory();
1107 }
1108
1109 if (self->buffer != NULL) {
1110 new_parser->buffer = malloc(new_parser->buffer_size);
1111 if (new_parser->buffer == NULL) {
1112 Py_DECREF(new_parser);
1113 return PyErr_NoMemory();
1114 }
1115 }
1116
1117 XML_SetUserData(new_parser->itself, (void *)new_parser);
1118
1119 /* allocate and clear handlers first */
1120 for (i = 0; handler_info[i].name != NULL; i++)
1121 /* do nothing */;
1122
1123 new_parser->handlers = malloc(sizeof(PyObject *) * i);
1124 if (!new_parser->handlers) {
1125 Py_DECREF(new_parser);
1126 return PyErr_NoMemory();
1127 }
1128 clear_handlers(new_parser, 1);
1129
1130 /* then copy handlers from self */
1131 for (i = 0; handler_info[i].name != NULL; i++) {
1132 PyObject *handler = self->handlers[i];
1133 if (handler != NULL) {
1134 Py_INCREF(handler);
1135 new_parser->handlers[i] = handler;
1136 handler_info[i].setter(new_parser->itself,
1137 handler_info[i].handler);
1138 }
1139 }
1140 return (PyObject *)new_parser;
1141 }
1142
1143 PyDoc_STRVAR(xmlparse_SetParamEntityParsing__doc__,
1144 "SetParamEntityParsing(flag) -> success\n\
1145 Controls parsing of parameter entities (including the external DTD\n\
1146 subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
1147 XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
1148 XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
1149 was successful.");
1150
1151 static PyObject*
1152 xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
1153 {
1154 int flag;
1155 if (!PyArg_ParseTuple(args, "i", &flag))
1156 return NULL;
1157 flag = XML_SetParamEntityParsing(p->itself, flag);
1158 return PyInt_FromLong(flag);
1159 }
1160
1161
1162 #if XML_COMBINED_VERSION >= 19505
1163 PyDoc_STRVAR(xmlparse_UseForeignDTD__doc__,
1164 "UseForeignDTD([flag])\n\
1165 Allows the application to provide an artificial external subset if one is\n\
1166 not specified as part of the document instance. This readily allows the\n\
1167 use of a 'default' document type controlled by the application, while still\n\
1168 getting the advantage of providing document type information to the parser.\n\
1169 'flag' defaults to True if not provided.");
1170
1171 static PyObject *
1172 xmlparse_UseForeignDTD(xmlparseobject *self, PyObject *args)
1173 {
1174 PyObject *flagobj = NULL;
1175 int flag = 1;
1176 enum XML_Error rc;
1177 if (!PyArg_ParseTuple(args, "|O:UseForeignDTD", &flagobj))
1178 return NULL;
1179 if (flagobj != NULL) {
1180 flag = PyObject_IsTrue(flagobj);
1181 if (flag < 0)
1182 return NULL;
1183 }
1184 rc = XML_UseForeignDTD(self->itself, flag ? XML_TRUE : XML_FALSE);
1185 if (rc != XML_ERROR_NONE) {
1186 return set_error(self, rc);
1187 }
1188 Py_INCREF(Py_None);
1189 return Py_None;
1190 }
1191 #endif
1192
1193 static struct PyMethodDef xmlparse_methods[] = {
1194 {"Parse", (PyCFunction)xmlparse_Parse,
1195 METH_VARARGS, xmlparse_Parse__doc__},
1196 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
1197 METH_O, xmlparse_ParseFile__doc__},
1198 {"SetBase", (PyCFunction)xmlparse_SetBase,
1199 METH_VARARGS, xmlparse_SetBase__doc__},
1200 {"GetBase", (PyCFunction)xmlparse_GetBase,
1201 METH_NOARGS, xmlparse_GetBase__doc__},
1202 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1203 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
1204 {"SetParamEntityParsing", (PyCFunction)xmlparse_SetParamEntityParsing,
1205 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1206 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1207 METH_NOARGS, xmlparse_GetInputContext__doc__},
1208 #if XML_COMBINED_VERSION >= 19505
1209 {"UseForeignDTD", (PyCFunction)xmlparse_UseForeignDTD,
1210 METH_VARARGS, xmlparse_UseForeignDTD__doc__},
1211 #endif
1212 {NULL, NULL} /* sentinel */
1213 };
1214
1215 /* ---------- */
1216
1217
1218 #ifdef Py_USING_UNICODE
1219
1220 /* pyexpat international encoding support.
1221 Make it as simple as possible.
1222 */
1223
1224 static char template_buffer[257];
1225 PyObject *template_string = NULL;
1226
1227 static void
1228 init_template_buffer(void)
1229 {
1230 int i;
1231 for (i = 0; i < 256; i++) {
1232 template_buffer[i] = i;
1233 }
1234 template_buffer[256] = 0;
1235 }
1236
1237 static int
1238 PyUnknownEncodingHandler(void *encodingHandlerData,
1239 const XML_Char *name,
1240 XML_Encoding *info)
1241 {
1242 PyUnicodeObject *_u_string = NULL;
1243 int result = 0;
1244 int i;
1245
1246 /* Yes, supports only 8bit encodings */
1247 _u_string = (PyUnicodeObject *)
1248 PyUnicode_Decode(template_buffer, 256, name, "replace");
1249
1250 if (_u_string == NULL)
1251 return result;
1252
1253 if (PyUnicode_GET_SIZE(_u_string) != 256) {
1254 Py_DECREF(_u_string);
1255 PyErr_SetString(PyExc_ValueError,
1256 "multi-byte encodings are not supported");
1257 return result;
1258 }
1259
1260 for (i = 0; i < 256; i++) {
1261 /* Stupid to access directly, but fast */
1262 Py_UNICODE c = _u_string->str[i];
1263 if (c == Py_UNICODE_REPLACEMENT_CHARACTER)
1264 info->map[i] = -1;
1265 else
1266 info->map[i] = c;
1267 }
1268 info->data = NULL;
1269 info->convert = NULL;
1270 info->release = NULL;
1271 result = 1;
1272 Py_DECREF(_u_string);
1273 return result;
1274 }
1275
1276 #endif
1277
1278 static PyObject *
1279 newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern)
1280 {
1281 int i;
1282 xmlparseobject *self;
1283
1284 #ifdef Py_TPFLAGS_HAVE_GC
1285 /* Code for versions 2.2 and later */
1286 self = PyObject_GC_New(xmlparseobject, &Xmlparsetype);
1287 #else
1288 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1289 #endif
1290 if (self == NULL)
1291 return NULL;
1292
1293 #ifdef Py_USING_UNICODE
1294 self->returns_unicode = 1;
1295 #else
1296 self->returns_unicode = 0;
1297 #endif
1298
1299 self->buffer = NULL;
1300 self->buffer_size = CHARACTER_DATA_BUFFER_SIZE;
1301 self->buffer_used = 0;
1302 self->ordered_attributes = 0;
1303 self->specified_attributes = 0;
1304 self->in_callback = 0;
1305 self->ns_prefixes = 0;
1306 self->handlers = NULL;
1307 self->intern = intern;
1308 Py_XINCREF(self->intern);
1309 if (namespace_separator != NULL) {
1310 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1311 }
1312 else {
1313 self->itself = XML_ParserCreate(encoding);
1314 }
1315 if (self->itself == NULL) {
1316 PyErr_SetString(PyExc_RuntimeError,
1317 "XML_ParserCreate failed");
1318 Py_DECREF(self);
1319 return NULL;
1320 }
1321 #if XML_COMBINED_VERSION >= 20100
1322 /* This feature was added upstream in libexpat 2.1.0. */
1323 XML_SetHashSalt(self->itself,
1324 (unsigned long)_Py_HashSecret.prefix);
1325 #endif
1326 #ifdef Py_TPFLAGS_HAVE_GC
1327 PyObject_GC_Track(self);
1328 #else
1329 PyObject_GC_Init(self);
1330 #endif
1331 XML_SetUserData(self->itself, (void *)self);
1332 #ifdef Py_USING_UNICODE
1333 XML_SetUnknownEncodingHandler(self->itself,
1334 (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1335 #endif
1336
1337 for (i = 0; handler_info[i].name != NULL; i++)
1338 /* do nothing */;
1339
1340 self->handlers = malloc(sizeof(PyObject *) * i);
1341 if (!self->handlers) {
1342 Py_DECREF(self);
1343 return PyErr_NoMemory();
1344 }
1345 clear_handlers(self, 1);
1346
1347 return (PyObject*)self;
1348 }
1349
1350
1351 static void
1352 xmlparse_dealloc(xmlparseobject *self)
1353 {
1354 int i;
1355 #ifdef Py_TPFLAGS_HAVE_GC
1356 PyObject_GC_UnTrack(self);
1357 #else
1358 PyObject_GC_Fini(self);
1359 #endif
1360 if (self->itself != NULL)
1361 XML_ParserFree(self->itself);
1362 self->itself = NULL;
1363
1364 if (self->handlers != NULL) {
1365 PyObject *temp;
1366 for (i = 0; handler_info[i].name != NULL; i++) {
1367 temp = self->handlers[i];
1368 self->handlers[i] = NULL;
1369 Py_XDECREF(temp);
1370 }
1371 free(self->handlers);
1372 self->handlers = NULL;
1373 }
1374 if (self->buffer != NULL) {
1375 free(self->buffer);
1376 self->buffer = NULL;
1377 }
1378 Py_XDECREF(self->intern);
1379 #ifndef Py_TPFLAGS_HAVE_GC
1380 /* Code for versions 2.0 and 2.1 */
1381 PyObject_Del(self);
1382 #else
1383 /* Code for versions 2.2 and later. */
1384 PyObject_GC_Del(self);
1385 #endif
1386 }
1387
1388 static int
1389 handlername2int(const char *name)
1390 {
1391 int i;
1392 for (i = 0; handler_info[i].name != NULL; i++) {
1393 if (strcmp(name, handler_info[i].name) == 0) {
1394 return i;
1395 }
1396 }
1397 return -1;
1398 }
1399
1400 static PyObject *
1401 get_pybool(int istrue)
1402 {
1403 PyObject *result = istrue ? Py_True : Py_False;
1404 Py_INCREF(result);
1405 return result;
1406 }
1407
1408 static PyObject *
1409 xmlparse_getattr(xmlparseobject *self, char *name)
1410 {
1411 int handlernum = handlername2int(name);
1412
1413 if (handlernum != -1) {
1414 PyObject *result = self->handlers[handlernum];
1415 if (result == NULL)
1416 result = Py_None;
1417 Py_INCREF(result);
1418 return result;
1419 }
1420 if (name[0] == 'E') {
1421 if (strcmp(name, "ErrorCode") == 0)
1422 return PyInt_FromLong((long)
1423 XML_GetErrorCode(self->itself));
1424 if (strcmp(name, "ErrorLineNumber") == 0)
1425 return PyInt_FromLong((long)
1426 XML_GetErrorLineNumber(self->itself));
1427 if (strcmp(name, "ErrorColumnNumber") == 0)
1428 return PyInt_FromLong((long)
1429 XML_GetErrorColumnNumber(self->itself));
1430 if (strcmp(name, "ErrorByteIndex") == 0)
1431 return PyInt_FromLong((long)
1432 XML_GetErrorByteIndex(self->itself));
1433 }
1434 if (name[0] == 'C') {
1435 if (strcmp(name, "CurrentLineNumber") == 0)
1436 return PyInt_FromLong((long)
1437 XML_GetCurrentLineNumber(self->itself));
1438 if (strcmp(name, "CurrentColumnNumber") == 0)
1439 return PyInt_FromLong((long)
1440 XML_GetCurrentColumnNumber(self->itself));
1441 if (strcmp(name, "CurrentByteIndex") == 0)
1442 return PyInt_FromLong((long)
1443 XML_GetCurrentByteIndex(self->itself));
1444 }
1445 if (name[0] == 'b') {
1446 if (strcmp(name, "buffer_size") == 0)
1447 return PyInt_FromLong((long) self->buffer_size);
1448 if (strcmp(name, "buffer_text") == 0)
1449 return get_pybool(self->buffer != NULL);
1450 if (strcmp(name, "buffer_used") == 0)
1451 return PyInt_FromLong((long) self->buffer_used);
1452 }
1453 if (strcmp(name, "namespace_prefixes") == 0)
1454 return get_pybool(self->ns_prefixes);
1455 if (strcmp(name, "ordered_attributes") == 0)
1456 return get_pybool(self->ordered_attributes);
1457 if (strcmp(name, "returns_unicode") == 0)
1458 return get_pybool((long) self->returns_unicode);
1459 if (strcmp(name, "specified_attributes") == 0)
1460 return get_pybool((long) self->specified_attributes);
1461 if (strcmp(name, "intern") == 0) {
1462 if (self->intern == NULL) {
1463 Py_INCREF(Py_None);
1464 return Py_None;
1465 }
1466 else {
1467 Py_INCREF(self->intern);
1468 return self->intern;
1469 }
1470 }
1471
1472 #define APPEND(list, str) \
1473 do { \
1474 PyObject *o = PyString_FromString(str); \
1475 if (o != NULL) \
1476 PyList_Append(list, o); \
1477 Py_XDECREF(o); \
1478 } while (0)
1479
1480 if (strcmp(name, "__members__") == 0) {
1481 int i;
1482 PyObject *rc = PyList_New(0);
1483 if (!rc)
1484 return NULL;
1485 for (i = 0; handler_info[i].name != NULL; i++) {
1486 PyObject *o = get_handler_name(&handler_info[i]);
1487 if (o != NULL)
1488 PyList_Append(rc, o);
1489 Py_XDECREF(o);
1490 }
1491 APPEND(rc, "ErrorCode");
1492 APPEND(rc, "ErrorLineNumber");
1493 APPEND(rc, "ErrorColumnNumber");
1494 APPEND(rc, "ErrorByteIndex");
1495 APPEND(rc, "CurrentLineNumber");
1496 APPEND(rc, "CurrentColumnNumber");
1497 APPEND(rc, "CurrentByteIndex");
1498 APPEND(rc, "buffer_size");
1499 APPEND(rc, "buffer_text");
1500 APPEND(rc, "buffer_used");
1501 APPEND(rc, "namespace_prefixes");
1502 APPEND(rc, "ordered_attributes");
1503 APPEND(rc, "returns_unicode");
1504 APPEND(rc, "specified_attributes");
1505 APPEND(rc, "intern");
1506
1507 #undef APPEND
1508 return rc;
1509 }
1510 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
1511 }
1512
1513 static int
1514 sethandler(xmlparseobject *self, const char *name, PyObject* v)
1515 {
1516 int handlernum = handlername2int(name);
1517 if (handlernum >= 0) {
1518 xmlhandler c_handler = NULL;
1519 PyObject *temp = self->handlers[handlernum];
1520
1521 if (v == Py_None) {
1522 /* If this is the character data handler, and a character
1523 data handler is already active, we need to be more
1524 careful. What we can safely do is replace the existing
1525 character data handler callback function with a no-op
1526 function that will refuse to call Python. The downside
1527 is that this doesn't completely remove the character
1528 data handler from the C layer if there's any callback
1529 active, so Expat does a little more work than it
1530 otherwise would, but that's really an odd case. A more
1531 elaborate system of handlers and state could remove the
1532 C handler more effectively. */
1533 if (handlernum == CharacterData && self->in_callback)
1534 c_handler = noop_character_data_handler;
1535 v = NULL;
1536 }
1537 else if (v != NULL) {
1538 Py_INCREF(v);
1539 c_handler = handler_info[handlernum].handler;
1540 }
1541 self->handlers[handlernum] = v;
1542 Py_XDECREF(temp);
1543 handler_info[handlernum].setter(self->itself, c_handler);
1544 return 1;
1545 }
1546 return 0;
1547 }
1548
1549 static int
1550 xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
1551 {
1552 /* Set attribute 'name' to value 'v'. v==NULL means delete */
1553 if (v == NULL) {
1554 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1555 return -1;
1556 }
1557 if (strcmp(name, "buffer_text") == 0) {
1558 int b = PyObject_IsTrue(v);
1559 if (b < 0)
1560 return -1;
1561 if (b) {
1562 if (self->buffer == NULL) {
1563 self->buffer = malloc(self->buffer_size);
1564 if (self->buffer == NULL) {
1565 PyErr_NoMemory();
1566 return -1;
1567 }
1568 self->buffer_used = 0;
1569 }
1570 }
1571 else if (self->buffer != NULL) {
1572 if (flush_character_buffer(self) < 0)
1573 return -1;
1574 free(self->buffer);
1575 self->buffer = NULL;
1576 }
1577 return 0;
1578 }
1579 if (strcmp(name, "namespace_prefixes") == 0) {
1580 int b = PyObject_IsTrue(v);
1581 if (b < 0)
1582 return -1;
1583 self->ns_prefixes = b;
1584 XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
1585 return 0;
1586 }
1587 if (strcmp(name, "ordered_attributes") == 0) {
1588 int b = PyObject_IsTrue(v);
1589 if (b < 0)
1590 return -1;
1591 self->ordered_attributes = b;
1592 return 0;
1593 }
1594 if (strcmp(name, "returns_unicode") == 0) {
1595 int b = PyObject_IsTrue(v);
1596 if (b < 0)
1597 return -1;
1598 #ifndef Py_USING_UNICODE
1599 if (b) {
1600 PyErr_SetString(PyExc_ValueError,
1601 "Unicode support not available");
1602 return -1;
1603 }
1604 #endif
1605 self->returns_unicode = b;
1606 return 0;
1607 }
1608 if (strcmp(name, "specified_attributes") == 0) {
1609 int b = PyObject_IsTrue(v);
1610 if (b < 0)
1611 return -1;
1612 self->specified_attributes = b;
1613 return 0;
1614 }
1615
1616 if (strcmp(name, "buffer_size") == 0) {
1617 long new_buffer_size;
1618 if (!PyInt_Check(v)) {
1619 PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
1620 return -1;
1621 }
1622
1623 new_buffer_size=PyInt_AS_LONG(v);
1624 /* trivial case -- no change */
1625 if (new_buffer_size == self->buffer_size) {
1626 return 0;
1627 }
1628
1629 if (new_buffer_size <= 0) {
1630 PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
1631 return -1;
1632 }
1633
1634 /* check maximum */
1635 if (new_buffer_size > INT_MAX) {
1636 char errmsg[100];
1637 sprintf(errmsg, "buffer_size must not be greater than %i", INT_MAX);
1638 PyErr_SetString(PyExc_ValueError, errmsg);
1639 return -1;
1640 }
1641
1642 if (self->buffer != NULL) {
1643 /* there is already a buffer */
1644 if (self->buffer_used != 0) {
1645 flush_character_buffer(self);
1646 }
1647 /* free existing buffer */
1648 free(self->buffer);
1649 }
1650 self->buffer = malloc(new_buffer_size);
1651 if (self->buffer == NULL) {
1652 PyErr_NoMemory();
1653 return -1;
1654 }
1655 self->buffer_size = new_buffer_size;
1656 return 0;
1657 }
1658
1659 if (strcmp(name, "CharacterDataHandler") == 0) {
1660 /* If we're changing the character data handler, flush all
1661 * cached data with the old handler. Not sure there's a
1662 * "right" thing to do, though, but this probably won't
1663 * happen.
1664 */
1665 if (flush_character_buffer(self) < 0)
1666 return -1;
1667 }
1668 if (sethandler(self, name, v)) {
1669 return 0;
1670 }
1671 PyErr_SetString(PyExc_AttributeError, name);
1672 return -1;
1673 }
1674
1675 #ifdef WITH_CYCLE_GC
1676 static int
1677 xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1678 {
1679 int i;
1680 for (i = 0; handler_info[i].name != NULL; i++)
1681 Py_VISIT(op->handlers[i]);
1682 return 0;
1683 }
1684
1685 static int
1686 xmlparse_clear(xmlparseobject *op)
1687 {
1688 clear_handlers(op, 0);
1689 Py_CLEAR(op->intern);
1690 return 0;
1691 }
1692 #endif
1693
1694 PyDoc_STRVAR(Xmlparsetype__doc__, "XML parser");
1695
1696 static PyTypeObject Xmlparsetype = {
1697 PyVarObject_HEAD_INIT(NULL, 0)
1698 "pyexpat.xmlparser", /*tp_name*/
1699 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
1700 0, /*tp_itemsize*/
1701 /* methods */
1702 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1703 (printfunc)0, /*tp_print*/
1704 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1705 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1706 (cmpfunc)0, /*tp_compare*/
1707 (reprfunc)0, /*tp_repr*/
1708 0, /*tp_as_number*/
1709 0, /*tp_as_sequence*/
1710 0, /*tp_as_mapping*/
1711 (hashfunc)0, /*tp_hash*/
1712 (ternaryfunc)0, /*tp_call*/
1713 (reprfunc)0, /*tp_str*/
1714 0, /* tp_getattro */
1715 0, /* tp_setattro */
1716 0, /* tp_as_buffer */
1717 #ifdef Py_TPFLAGS_HAVE_GC
1718 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1719 #else
1720 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1721 #endif
1722 Xmlparsetype__doc__, /* tp_doc - Documentation string */
1723 #ifdef WITH_CYCLE_GC
1724 (traverseproc)xmlparse_traverse, /* tp_traverse */
1725 (inquiry)xmlparse_clear /* tp_clear */
1726 #else
1727 0, 0
1728 #endif
1729 };
1730
1731 /* End of code for xmlparser objects */
1732 /* -------------------------------------------------------- */
1733
1734 PyDoc_STRVAR(pyexpat_ParserCreate__doc__,
1735 "ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1736 Return a new XML parser object.");
1737
1738 static PyObject *
1739 pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1740 {
1741 char *encoding = NULL;
1742 char *namespace_separator = NULL;
1743 PyObject *intern = NULL;
1744 PyObject *result;
1745 int intern_decref = 0;
1746 static char *kwlist[] = {"encoding", "namespace_separator",
1747 "intern", NULL};
1748
1749 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zzO:ParserCreate", kwlist,
1750 &encoding, &namespace_separator, &intern))
1751 return NULL;
1752 if (namespace_separator != NULL
1753 && strlen(namespace_separator) > 1) {
1754 PyErr_SetString(PyExc_ValueError,
1755 "namespace_separator must be at most one"
1756 " character, omitted, or None");
1757 return NULL;
1758 }
1759 /* Explicitly passing None means no interning is desired.
1760 Not passing anything means that a new dictionary is used. */
1761 if (intern == Py_None)
1762 intern = NULL;
1763 else if (intern == NULL) {
1764 intern = PyDict_New();
1765 if (!intern)
1766 return NULL;
1767 intern_decref = 1;
1768 }
1769 else if (!PyDict_Check(intern)) {
1770 PyErr_SetString(PyExc_TypeError, "intern must be a dictionary");
1771 return NULL;
1772 }
1773
1774 result = newxmlparseobject(encoding, namespace_separator, intern);
1775 if (intern_decref) {
1776 Py_DECREF(intern);
1777 }
1778 return result;
1779 }
1780
1781 PyDoc_STRVAR(pyexpat_ErrorString__doc__,
1782 "ErrorString(errno) -> string\n\
1783 Returns string error for given number.");
1784
1785 static PyObject *
1786 pyexpat_ErrorString(PyObject *self, PyObject *args)
1787 {
1788 long code = 0;
1789
1790 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1791 return NULL;
1792 return Py_BuildValue("z", XML_ErrorString((int)code));
1793 }
1794
1795 /* List of methods defined in the module */
1796
1797 static struct PyMethodDef pyexpat_methods[] = {
1798 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1799 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1800 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1801 METH_VARARGS, pyexpat_ErrorString__doc__},
1802
1803 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
1804 };
1805
1806 /* Module docstring */
1807
1808 PyDoc_STRVAR(pyexpat_module_documentation,
1809 "Python wrapper for Expat parser.");
1810
1811 /* Initialization function for the module */
1812
1813 #ifndef MODULE_NAME
1814 #define MODULE_NAME "pyexpat"
1815 #endif
1816
1817 #ifndef MODULE_INITFUNC
1818 #define MODULE_INITFUNC initpyexpat
1819 #endif
1820
1821 #ifndef PyMODINIT_FUNC
1822 # ifdef MS_WINDOWS
1823 # define PyMODINIT_FUNC __declspec(dllexport) void
1824 # else
1825 # define PyMODINIT_FUNC void
1826 # endif
1827 #endif
1828
1829 PyMODINIT_FUNC MODULE_INITFUNC(void); /* avoid compiler warnings */
1830
1831 PyMODINIT_FUNC
1832 MODULE_INITFUNC(void)
1833 {
1834 PyObject *m, *d;
1835 PyObject *errmod_name = PyString_FromString(MODULE_NAME ".errors");
1836 PyObject *errors_module;
1837 PyObject *modelmod_name;
1838 PyObject *model_module;
1839 PyObject *sys_modules;
1840 PyObject *version;
1841 static struct PyExpat_CAPI capi;
1842 PyObject* capi_object;
1843
1844 if (errmod_name == NULL)
1845 return;
1846 modelmod_name = PyString_FromString(MODULE_NAME ".model");
1847 if (modelmod_name == NULL)
1848 return;
1849
1850 Py_TYPE(&Xmlparsetype) = &PyType_Type;
1851
1852 /* Create the module and add the functions */
1853 m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
1854 pyexpat_module_documentation);
1855 if (m == NULL)
1856 return;
1857
1858 /* Add some symbolic constants to the module */
1859 if (ErrorObject == NULL) {
1860 ErrorObject = PyErr_NewException("xml.parsers.expat.ExpatError",
1861 NULL, NULL);
1862 if (ErrorObject == NULL)
1863 return;
1864 }
1865 Py_INCREF(ErrorObject);
1866 PyModule_AddObject(m, "error", ErrorObject);
1867 Py_INCREF(ErrorObject);
1868 PyModule_AddObject(m, "ExpatError", ErrorObject);
1869 Py_INCREF(&Xmlparsetype);
1870 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
1871
1872 version = PyString_FromString(PY_VERSION);
1873 if (!version)
1874 return;
1875 PyModule_AddObject(m, "__version__", version);
1876 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1877 (char *) XML_ExpatVersion());
1878 {
1879 XML_Expat_Version info = XML_ExpatVersionInfo();
1880 PyModule_AddObject(m, "version_info",
1881 Py_BuildValue("(iii)", info.major,
1882 info.minor, info.micro));
1883 }
1884 #ifdef Py_USING_UNICODE
1885 init_template_buffer();
1886 #endif
1887 /* XXX When Expat supports some way of figuring out how it was
1888 compiled, this should check and set native_encoding
1889 appropriately.
1890 */
1891 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
1892
1893 sys_modules = PySys_GetObject("modules");
1894 d = PyModule_GetDict(m);
1895 errors_module = PyDict_GetItem(d, errmod_name);
1896 if (errors_module == NULL) {
1897 errors_module = PyModule_New(MODULE_NAME ".errors");
1898 if (errors_module != NULL) {
1899 PyDict_SetItem(sys_modules, errmod_name, errors_module);
1900 /* gives away the reference to errors_module */
1901 PyModule_AddObject(m, "errors", errors_module);
1902 }
1903 }
1904 Py_DECREF(errmod_name);
1905 model_module = PyDict_GetItem(d, modelmod_name);
1906 if (model_module == NULL) {
1907 model_module = PyModule_New(MODULE_NAME ".model");
1908 if (model_module != NULL) {
1909 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1910 /* gives away the reference to model_module */
1911 PyModule_AddObject(m, "model", model_module);
1912 }
1913 }
1914 Py_DECREF(modelmod_name);
1915 if (errors_module == NULL || model_module == NULL)
1916 /* Don't core dump later! */
1917 return;
1918
1919 #if XML_COMBINED_VERSION > 19505
1920 {
1921 const XML_Feature *features = XML_GetFeatureList();
1922 PyObject *list = PyList_New(0);
1923 if (list == NULL)
1924 /* just ignore it */
1925 PyErr_Clear();
1926 else {
1927 int i = 0;
1928 for (; features[i].feature != XML_FEATURE_END; ++i) {
1929 int ok;
1930 PyObject *item = Py_BuildValue("si", features[i].name,
1931 features[i].value);
1932 if (item == NULL) {
1933 Py_DECREF(list);
1934 list = NULL;
1935 break;
1936 }
1937 ok = PyList_Append(list, item);
1938 Py_DECREF(item);
1939 if (ok < 0) {
1940 PyErr_Clear();
1941 break;
1942 }
1943 }
1944 if (list != NULL)
1945 PyModule_AddObject(m, "features", list);
1946 }
1947 }
1948 #endif
1949
1950 #define MYCONST(name) \
1951 PyModule_AddStringConstant(errors_module, #name, \
1952 (char*)XML_ErrorString(name))
1953
1954 MYCONST(XML_ERROR_NO_MEMORY);
1955 MYCONST(XML_ERROR_SYNTAX);
1956 MYCONST(XML_ERROR_NO_ELEMENTS);
1957 MYCONST(XML_ERROR_INVALID_TOKEN);
1958 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1959 MYCONST(XML_ERROR_PARTIAL_CHAR);
1960 MYCONST(XML_ERROR_TAG_MISMATCH);
1961 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1962 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1963 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1964 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1965 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1966 MYCONST(XML_ERROR_ASYNC_ENTITY);
1967 MYCONST(XML_ERROR_BAD_CHAR_REF);
1968 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1969 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1970 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1971 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1972 MYCONST(XML_ERROR_INCORRECT_ENCODING);
1973 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1974 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1975 MYCONST(XML_ERROR_NOT_STANDALONE);
1976 MYCONST(XML_ERROR_UNEXPECTED_STATE);
1977 MYCONST(XML_ERROR_ENTITY_DECLARED_IN_PE);
1978 MYCONST(XML_ERROR_FEATURE_REQUIRES_XML_DTD);
1979 MYCONST(XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING);
1980 /* Added in Expat 1.95.7. */
1981 MYCONST(XML_ERROR_UNBOUND_PREFIX);
1982 /* Added in Expat 1.95.8. */
1983 MYCONST(XML_ERROR_UNDECLARING_PREFIX);
1984 MYCONST(XML_ERROR_INCOMPLETE_PE);
1985 MYCONST(XML_ERROR_XML_DECL);
1986 MYCONST(XML_ERROR_TEXT_DECL);
1987 MYCONST(XML_ERROR_PUBLICID);
1988 MYCONST(XML_ERROR_SUSPENDED);
1989 MYCONST(XML_ERROR_NOT_SUSPENDED);
1990 MYCONST(XML_ERROR_ABORTED);
1991 MYCONST(XML_ERROR_FINISHED);
1992 MYCONST(XML_ERROR_SUSPEND_PE);
1993
1994 PyModule_AddStringConstant(errors_module, "__doc__",
1995 "Constants used to describe error conditions.");
1996
1997 #undef MYCONST
1998
1999 #define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
2000 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
2001 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
2002 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
2003 #undef MYCONST
2004
2005 #define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
2006 PyModule_AddStringConstant(model_module, "__doc__",
2007 "Constants used to interpret content model information.");
2008
2009 MYCONST(XML_CTYPE_EMPTY);
2010 MYCONST(XML_CTYPE_ANY);
2011 MYCONST(XML_CTYPE_MIXED);
2012 MYCONST(XML_CTYPE_NAME);
2013 MYCONST(XML_CTYPE_CHOICE);
2014 MYCONST(XML_CTYPE_SEQ);
2015
2016 MYCONST(XML_CQUANT_NONE);
2017 MYCONST(XML_CQUANT_OPT);
2018 MYCONST(XML_CQUANT_REP);
2019 MYCONST(XML_CQUANT_PLUS);
2020 #undef MYCONST
2021
2022 /* initialize pyexpat dispatch table */
2023 capi.size = sizeof(capi);
2024 capi.magic = PyExpat_CAPI_MAGIC;
2025 capi.MAJOR_VERSION = XML_MAJOR_VERSION;
2026 capi.MINOR_VERSION = XML_MINOR_VERSION;
2027 capi.MICRO_VERSION = XML_MICRO_VERSION;
2028 capi.ErrorString = XML_ErrorString;
2029 capi.GetErrorCode = XML_GetErrorCode;
2030 capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
2031 capi.GetErrorLineNumber = XML_GetErrorLineNumber;
2032 capi.Parse = XML_Parse;
2033 capi.ParserCreate_MM = XML_ParserCreate_MM;
2034 capi.ParserFree = XML_ParserFree;
2035 capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
2036 capi.SetCommentHandler = XML_SetCommentHandler;
2037 capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
2038 capi.SetElementHandler = XML_SetElementHandler;
2039 capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
2040 capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
2041 capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
2042 capi.SetUserData = XML_SetUserData;
2043 #if XML_COMBINED_VERSION >= 20100
2044 capi.SetHashSalt = XML_SetHashSalt;
2045 #else
2046 capi.SetHashSalt = NULL;
2047 #endif
2048
2049 /* export using capsule */
2050 capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
2051 if (capi_object)
2052 PyModule_AddObject(m, "expat_CAPI", capi_object);
2053 }
2054
2055 static void
2056 clear_handlers(xmlparseobject *self, int initial)
2057 {
2058 int i = 0;
2059 PyObject *temp;
2060
2061 for (; handler_info[i].name != NULL; i++) {
2062 if (initial)
2063 self->handlers[i] = NULL;
2064 else {
2065 temp = self->handlers[i];
2066 self->handlers[i] = NULL;
2067 Py_XDECREF(temp);
2068 handler_info[i].setter(self->itself, NULL);
2069 }
2070 }
2071 }
2072
2073 static struct HandlerInfo handler_info[] = {
2074 {"StartElementHandler",
2075 (xmlhandlersetter)XML_SetStartElementHandler,
2076 (xmlhandler)my_StartElementHandler},
2077 {"EndElementHandler",
2078 (xmlhandlersetter)XML_SetEndElementHandler,
2079 (xmlhandler)my_EndElementHandler},
2080 {"ProcessingInstructionHandler",
2081 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
2082 (xmlhandler)my_ProcessingInstructionHandler},
2083 {"CharacterDataHandler",
2084 (xmlhandlersetter)XML_SetCharacterDataHandler,
2085 (xmlhandler)my_CharacterDataHandler},
2086 {"UnparsedEntityDeclHandler",
2087 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
2088 (xmlhandler)my_UnparsedEntityDeclHandler},
2089 {"NotationDeclHandler",
2090 (xmlhandlersetter)XML_SetNotationDeclHandler,
2091 (xmlhandler)my_NotationDeclHandler},
2092 {"StartNamespaceDeclHandler",
2093 (xmlhandlersetter)XML_SetStartNamespaceDeclHandler,
2094 (xmlhandler)my_StartNamespaceDeclHandler},
2095 {"EndNamespaceDeclHandler",
2096 (xmlhandlersetter)XML_SetEndNamespaceDeclHandler,
2097 (xmlhandler)my_EndNamespaceDeclHandler},
2098 {"CommentHandler",
2099 (xmlhandlersetter)XML_SetCommentHandler,
2100 (xmlhandler)my_CommentHandler},
2101 {"StartCdataSectionHandler",
2102 (xmlhandlersetter)XML_SetStartCdataSectionHandler,
2103 (xmlhandler)my_StartCdataSectionHandler},
2104 {"EndCdataSectionHandler",
2105 (xmlhandlersetter)XML_SetEndCdataSectionHandler,
2106 (xmlhandler)my_EndCdataSectionHandler},
2107 {"DefaultHandler",
2108 (xmlhandlersetter)XML_SetDefaultHandler,
2109 (xmlhandler)my_DefaultHandler},
2110 {"DefaultHandlerExpand",
2111 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
2112 (xmlhandler)my_DefaultHandlerExpandHandler},
2113 {"NotStandaloneHandler",
2114 (xmlhandlersetter)XML_SetNotStandaloneHandler,
2115 (xmlhandler)my_NotStandaloneHandler},
2116 {"ExternalEntityRefHandler",
2117 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
2118 (xmlhandler)my_ExternalEntityRefHandler},
2119 {"StartDoctypeDeclHandler",
2120 (xmlhandlersetter)XML_SetStartDoctypeDeclHandler,
2121 (xmlhandler)my_StartDoctypeDeclHandler},
2122 {"EndDoctypeDeclHandler",
2123 (xmlhandlersetter)XML_SetEndDoctypeDeclHandler,
2124 (xmlhandler)my_EndDoctypeDeclHandler},
2125 {"EntityDeclHandler",
2126 (xmlhandlersetter)XML_SetEntityDeclHandler,
2127 (xmlhandler)my_EntityDeclHandler},
2128 {"XmlDeclHandler",
2129 (xmlhandlersetter)XML_SetXmlDeclHandler,
2130 (xmlhandler)my_XmlDeclHandler},
2131 {"ElementDeclHandler",
2132 (xmlhandlersetter)XML_SetElementDeclHandler,
2133 (xmlhandler)my_ElementDeclHandler},
2134 {"AttlistDeclHandler",
2135 (xmlhandlersetter)XML_SetAttlistDeclHandler,
2136 (xmlhandler)my_AttlistDeclHandler},
2137 #if XML_COMBINED_VERSION >= 19504
2138 {"SkippedEntityHandler",
2139 (xmlhandlersetter)XML_SetSkippedEntityHandler,
2140 (xmlhandler)my_SkippedEntityHandler},
2141 #endif
2142
2143 {NULL, NULL, NULL} /* sentinel */
2144 };