Apparently the code to forestall Tk eating events was too aggressive (Tk user input...
[python/dscho.git] / Modules / pyexpat.c
blob9300039d12024429fb62b79499b0d2a4ddd33acc
1 #include <ctype.h>
3 #include "Python.h"
4 #include "compile.h"
5 #include "frameobject.h"
6 #ifdef HAVE_EXPAT_H
7 #include "expat.h"
8 #ifdef XML_MAJOR_VERSION
9 #define EXPAT_VERSION (0x10000 * XML_MAJOR_VERSION \
10 + 0x100 * XML_MINOR_VERSION \
11 + XML_MICRO_VERSION)
12 #else
13 /* Assume the oldest Expat that used expat.h and did not have version info */
14 #define EXPAT_VERSION 0x015f00
15 #endif
16 #else /* !defined(HAVE_EXPAT_H) */
17 #include "xmlparse.h"
18 /* Assume Expat 1.1 unless told otherwise */
19 #ifndef EXPAT_VERSION
20 #define EXPAT_VERSION 0x010100
21 #endif
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
29 #endif
31 enum HandlerTypes {
32 StartElement,
33 EndElement,
34 ProcessingInstruction,
35 CharacterData,
36 UnparsedEntityDecl,
37 NotationDecl,
38 StartNamespaceDecl,
39 EndNamespaceDecl,
40 Comment,
41 StartCdataSection,
42 EndCdataSection,
43 Default,
44 DefaultHandlerExpand,
45 NotStandalone,
46 ExternalEntityRef,
47 #if EXPAT_VERSION >= 0x010200
48 StartDoctypeDecl,
49 EndDoctypeDecl,
50 #endif
51 #if EXPAT_VERSION == 0x010200
52 ExternalParsedEntityDecl,
53 InternalParsedEntityDecl,
54 #endif
55 #if EXPAT_VERSION >= 0x015f00
56 EntityDecl,
57 XmlDecl,
58 ElementDecl,
59 AttlistDecl,
60 #endif
61 _DummyDecl
64 static PyObject *ErrorObject;
66 /* ----------------------------------------------------- */
68 /* Declarations for objects of type xmlparser */
70 typedef struct {
71 PyObject_HEAD
73 XML_Parser itself;
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? */
79 PyObject **handlers;
80 } xmlparseobject;
82 staticforward PyTypeObject Xmlparsetype;
84 typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
85 typedef void* xmlhandler;
87 struct HandlerInfo {
88 const char *name;
89 xmlhandlersetter setter;
90 xmlhandler handler;
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.
99 static int
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) {
105 Py_DECREF(v);
106 return 0;
108 return 1;
111 /* Build and set an Expat exception, including positioning
112 * information. Always returns NULL.
114 static PyObject *
115 set_error(xmlparseobject *self)
117 PyObject *err;
118 char buffer[256];
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);
127 if ( err != NULL
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);
133 return NULL;
137 #if EXPAT_VERSION == 0x010200
138 /* Convert an array of attributes and their values into a Python dict */
140 static PyObject *
141 conv_atts_using_string(XML_Char **atts)
143 PyObject *attrs_obj = NULL;
144 XML_Char **attrs_p, **attrs_k = NULL;
145 int attrs_len;
146 PyObject *rv;
148 if ((attrs_obj = PyDict_New()) == NULL)
149 goto finally;
150 for (attrs_len = 0, attrs_p = atts;
151 *attrs_p;
152 attrs_p++, attrs_len++) {
153 if (attrs_len % 2) {
154 rv = PyString_FromString(*attrs_p);
155 if (!rv) {
156 Py_DECREF(attrs_obj);
157 attrs_obj = NULL;
158 goto finally;
160 if (PyDict_SetItemString(attrs_obj,
161 (char*)*attrs_k, rv) < 0) {
162 Py_DECREF(attrs_obj);
163 attrs_obj = NULL;
164 goto finally;
166 Py_DECREF(rv);
168 else
169 attrs_k = attrs_p;
171 finally:
172 return attrs_obj;
174 #endif
176 #if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
177 #if EXPAT_VERSION == 0x010200
178 static PyObject *
179 conv_atts_using_unicode(XML_Char **atts)
181 PyObject *attrs_obj;
182 XML_Char **attrs_p, **attrs_k = NULL;
183 int attrs_len;
185 if ((attrs_obj = PyDict_New()) == NULL)
186 goto finally;
187 for (attrs_len = 0, attrs_p = atts;
188 *attrs_p;
189 attrs_p++, attrs_len++) {
190 if (attrs_len % 2) {
191 PyObject *attr_str, *value_str;
192 const char *p = (const char *) (*attrs_k);
193 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
194 if (!attr_str) {
195 Py_DECREF(attrs_obj);
196 attrs_obj = NULL;
197 goto finally;
199 p = (const char *) *attrs_p;
200 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
201 if (!value_str) {
202 Py_DECREF(attrs_obj);
203 Py_DECREF(attr_str);
204 attrs_obj = NULL;
205 goto finally;
207 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
208 Py_DECREF(attrs_obj);
209 Py_DECREF(attr_str);
210 Py_DECREF(value_str);
211 attrs_obj = NULL;
212 goto finally;
214 Py_DECREF(attr_str);
215 Py_DECREF(value_str);
217 else
218 attrs_k = attrs_p;
220 finally:
221 return attrs_obj;
223 #endif
225 /* Convert a string of XML_Chars into a Unicode string.
226 Returns None if str is a null pointer. */
228 static PyObject *
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 */
234 if (str == NULL) {
235 Py_INCREF(Py_None);
236 return Py_None;
238 return PyUnicode_DecodeUTF8((const char *)str,
239 strlen((const char *)str),
240 "strict");
243 static PyObject *
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 */
249 if (str == NULL) {
250 Py_INCREF(Py_None);
251 return Py_None;
253 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
255 #endif
257 /* Convert a string of XML_Chars into an 8-bit Python string.
258 Returns None if str is a null pointer. */
260 static PyObject *
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 */
266 if (str == NULL) {
267 Py_INCREF(Py_None);
268 return Py_None;
270 return PyString_FromString((const char *)str);
273 static PyObject *
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 */
279 if (str == NULL) {
280 Py_INCREF(Py_None);
281 return Py_None;
283 return PyString_FromStringAndSize((const char *)str, len);
286 /* Callback routines */
288 static void clear_handlers(xmlparseobject *self, int decref);
290 static void
291 flag_error(xmlparseobject *self)
293 clear_handlers(self, 1);
296 static PyCodeObject*
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("");
306 if (code == NULL)
307 goto failed;
308 name = PyString_FromString(func_name);
309 if (name == NULL)
310 goto failed;
311 nulltuple = PyTuple_New(0);
312 if (nulltuple == NULL)
313 goto failed;
314 filename = PyString_FromString(__FILE__);
315 handler_info[slot].tb_code =
316 PyCode_New(0, /* argcount */
317 0, /* nlocals */
318 0, /* stacksize */
319 0, /* flags */
320 code, /* code */
321 nulltuple, /* consts */
322 nulltuple, /* names */
323 nulltuple, /* varnames */
324 #if PYTHON_API_VERSION >= 1010
325 nulltuple, /* freevars */
326 nulltuple, /* cellvars */
327 #endif
328 filename, /* filename */
329 name, /* name */
330 lineno, /* firstlineno */
331 code /* lnotab */
333 if (handler_info[slot].tb_code == NULL)
334 goto failed;
335 Py_DECREF(code);
336 Py_DECREF(nulltuple);
337 Py_DECREF(filename);
338 Py_DECREF(name);
340 return handler_info[slot].tb_code;
341 failed:
342 Py_XDECREF(code);
343 Py_XDECREF(name);
344 return NULL;
347 static PyObject*
348 call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
350 PyThreadState *tstate = PyThreadState_GET();
351 PyFrameObject *f;
352 PyObject *res;
354 if (c == NULL)
355 return NULL;
356 f = PyFrame_New(
357 tstate, /*back*/
358 c, /*code*/
359 tstate->frame->f_globals, /*globals*/
360 NULL /*locals*/
362 if (f == NULL)
363 return NULL;
364 tstate->frame = f;
365 res = PyEval_CallObject(func, args);
366 if (res == NULL && tstate->curexc_traceback == NULL)
367 PyTraceBack_Here(f);
368 tstate->frame = f->f_back;
369 Py_DECREF(f);
370 return res;
373 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
374 #define STRING_CONV_FUNC conv_string_to_utf8
375 #else
376 /* Python 1.6 and later versions */
377 #define STRING_CONV_FUNC (self->returns_unicode \
378 ? conv_string_to_unicode : conv_string_to_utf8)
379 #endif
381 static void
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;
390 int i, max;
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);
398 else {
399 max = 0;
400 while (atts[max] != NULL)
401 max += 2;
403 /* Build the container. */
404 if (self->ordered_attributes)
405 container = PyList_New(max);
406 else
407 container = PyDict_New();
408 if (container == NULL) {
409 flag_error(self);
410 return;
412 for (i = 0; i < max; i += 2) {
413 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
414 PyObject *v;
415 if (n == NULL) {
416 flag_error(self);
417 Py_DECREF(container);
418 return;
420 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
421 if (v == NULL) {
422 flag_error(self);
423 Py_DECREF(container);
424 Py_DECREF(n);
425 return;
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)) {
432 flag_error(self);
433 Py_DECREF(n);
434 Py_DECREF(v);
435 return;
437 else {
438 Py_DECREF(n);
439 Py_DECREF(v);
442 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
443 if (args == NULL) {
444 Py_DECREF(container);
445 return;
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;
452 Py_DECREF(args);
453 if (rv == NULL) {
454 flag_error(self);
455 return;
457 Py_DECREF(rv);
461 #define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
462 RETURN, GETUSERDATA) \
463 static RC \
464 my_##NAME##Handler PARAMS {\
465 xmlparseobject *self = GETUSERDATA ; \
466 PyObject *args = NULL; \
467 PyObject *rv = NULL; \
468 INIT \
470 if (self->handlers[NAME] \
471 && self->handlers[NAME] != Py_None) { \
472 args = Py_BuildValue PARAM_FORMAT ;\
473 if (!args) \
474 return RETURN; \
475 self->in_callback = 1; \
476 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
477 self->handlers[NAME], args); \
478 self->in_callback = 0; \
479 Py_DECREF(args); \
480 if (rv == NULL) { \
481 flag_error(self); \
482 return RETURN; \
484 CONVERSION \
485 Py_DECREF(rv); \
487 return RETURN; \
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,
504 (void *userData,
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)))
513 #else
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))))
519 #endif
521 VOID_HANDLER(UnparsedEntityDecl,
522 (void *userData,
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),
528 ("(O&O&O&O&O&)",
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,
536 (void *userData,
537 const XML_Char *entityName,
538 int is_parameter_entity,
539 const XML_Char *value,
540 int value_length,
541 const XML_Char *base,
542 const XML_Char *systemId,
543 const XML_Char *publicId,
544 const XML_Char *notationName),
545 ("O&iNO&O&O&O&",
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))
550 #else
551 VOID_HANDLER(EntityDecl,
552 (void *userData,
553 const XML_Char *entityName,
554 int is_parameter_entity,
555 const XML_Char *value,
556 int value_length,
557 const XML_Char *base,
558 const XML_Char *systemId,
559 const XML_Char *publicId,
560 const XML_Char *notationName),
561 ("O&iNO&O&O&O&",
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))
568 #endif
570 VOID_HANDLER(XmlDecl,
571 (void *userData,
572 const XML_Char *version,
573 const XML_Char *encoding,
574 int standalone),
575 ("(O&O&i)",
576 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
577 standalone))
579 static PyObject *
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);
585 int i;
587 if (children != NULL) {
588 for (i = 0; i < model->numchildren; ++i) {
589 PyObject *child = conv_content_model(&model->children[i],
590 conv_string);
591 if (child == NULL) {
592 Py_XDECREF(children);
593 return NULL;
595 PyTuple_SET_ITEM(children, i, child);
597 result = Py_BuildValue("(iiO&N)",
598 model->type, model->quant,
599 conv_string,model->name, children);
601 return result;
604 static PyObject *
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)
611 static PyObject *
612 conv_content_model_unicode(XML_Content * const model)
614 return conv_content_model(model, conv_string_to_unicode);
617 VOID_HANDLER(ElementDecl,
618 (void *userData,
619 const XML_Char *name,
620 XML_Content *model),
621 ("O&O&",
622 STRING_CONV_FUNC,name,
623 (self->returns_unicode ? conv_content_model_unicode
624 : conv_content_model_utf8),model))
625 #else
626 VOID_HANDLER(ElementDecl,
627 (void *userData,
628 const XML_Char *name,
629 XML_Content *model),
630 ("O&O&",
631 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
632 #endif
634 VOID_HANDLER(AttlistDecl,
635 (void *userData,
636 const XML_Char *elname,
637 const XML_Char *attname,
638 const XML_Char *att_type,
639 const XML_Char *dflt,
640 int isrequired),
641 ("(O&O&O&O&i)",
642 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
643 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
644 isrequired))
645 #endif
647 VOID_HANDLER(NotationDecl,
648 (void *userData,
649 const XML_Char *notationName,
650 const XML_Char *base,
651 const XML_Char *systemId,
652 const XML_Char *publicId),
653 ("(O&O&O&O&)",
654 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
655 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
657 VOID_HANDLER(StartNamespaceDecl,
658 (void *userData,
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,
664 (void *userData,
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,
673 (void *userData),
674 ("()"))
676 VOID_HANDLER(EndCdataSection,
677 (void *userData),
678 ("()"))
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)))
688 #else
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))))
700 #endif
702 INT_HANDLER(NotStandalone,
703 (void *userData),
704 ("()"))
706 RC_HANDLER(int, ExternalEntityRef,
707 (XML_Parser parser,
708 const XML_Char *context,
709 const XML_Char *base,
710 const XML_Char *systemId,
711 const XML_Char *publicId),
712 int rc=0;,
713 ("(O&O&O&O&)",
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))
734 #endif
736 #if EXPAT_VERSION >= 0x010200
737 VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
738 #endif
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.";
763 static PyObject *
764 xmlparse_Parse(xmlparseobject *self, PyObject *args)
766 char *s;
767 int slen;
768 int isFinal = 0;
769 int rv;
771 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
772 return NULL;
773 rv = XML_Parse(self->itself, s, slen, isFinal);
774 if (PyErr_Occurred()) {
775 return NULL;
777 else if (rv == 0) {
778 return set_error(self);
780 return PyInt_FromLong(rv);
783 /* File reading copied from cPickle */
785 #define BUF_SIZE 2048
787 static int
788 readinst(char *buf, int buf_size, PyObject *meth)
790 PyObject *arg = NULL;
791 PyObject *bytes = NULL;
792 PyObject *str = NULL;
793 int len = -1;
795 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
796 goto finally;
798 if ((arg = PyTuple_New(1)) == NULL)
799 goto finally;
801 PyTuple_SET_ITEM(arg, 0, bytes);
803 if ((str = PyObject_CallObject(meth, arg)) == NULL)
804 goto finally;
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);
811 goto finally;
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",
818 buf_size, len);
819 Py_DECREF(str);
820 goto finally;
822 memcpy(buf, PyString_AsString(str), len);
823 finally:
824 Py_XDECREF(arg);
825 Py_XDECREF(str);
826 return len;
829 static char xmlparse_ParseFile__doc__[] =
830 "ParseFile(file)\n\
831 Parse XML data from file-like object.";
833 static PyObject *
834 xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
836 int rv = 1;
837 PyObject *f;
838 FILE *fp;
839 PyObject *readmethod = NULL;
841 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
842 return NULL;
844 if (PyFile_Check(f)) {
845 fp = PyFile_AsFile(f);
847 else{
848 fp = NULL;
849 readmethod = PyObject_GetAttrString(f, "read");
850 if (readmethod == NULL) {
851 PyErr_Clear();
852 PyErr_SetString(PyExc_TypeError,
853 "argument must have 'read' attribute");
854 return 0;
857 for (;;) {
858 int bytes_read;
859 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
860 if (buf == NULL)
861 return PyErr_NoMemory();
863 if (fp) {
864 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
865 if (bytes_read < 0) {
866 PyErr_SetFromErrno(PyExc_IOError);
867 return NULL;
870 else {
871 bytes_read = readinst(buf, BUF_SIZE, readmethod);
872 if (bytes_read < 0)
873 return NULL;
875 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
876 if (PyErr_Occurred())
877 return NULL;
879 if (!rv || bytes_read == 0)
880 break;
882 if (rv == 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.";
892 static PyObject *
893 xmlparse_SetBase(xmlparseobject *self, PyObject *args)
895 char *base;
897 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
898 return NULL;
899 if (!XML_SetBase(self->itself, base)) {
900 return PyErr_NoMemory();
902 Py_INCREF(Py_None);
903 return Py_None;
906 static char xmlparse_GetBase__doc__[] =
907 "GetBase() -> url\n\
908 Return base URL string for the parser.";
910 static PyObject *
911 xmlparse_GetBase(xmlparseobject *self, PyObject *args)
913 if (!PyArg_ParseTuple(args, ":GetBase"))
914 return NULL;
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.";
926 static PyObject *
927 xmlparse_GetInputContext(xmlparseobject *self, PyObject *args)
929 PyObject *result = NULL;
931 if (PyArg_ParseTuple(args, ":GetInputContext")) {
932 if (self->in_callback) {
933 int offset, size;
934 const char *buffer
935 = XML_GetInputContext(self->itself, &offset, &size);
937 if (buffer != NULL)
938 result = PyString_FromStringAndSize(buffer + offset, size);
939 else {
940 result = Py_None;
941 Py_INCREF(result);
944 else {
945 result = Py_None;
946 Py_INCREF(result);
949 return result;
951 #endif
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.";
958 static PyObject *
959 xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
961 char *context;
962 char *encoding = NULL;
963 xmlparseobject *new_parser;
964 int i;
966 if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate",
967 &context, &encoding)) {
968 return NULL;
971 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
972 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
973 #else
974 /* Python versions 1.6 and later */
975 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
976 #endif
978 if (new_parser == NULL)
979 return 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,
985 encoding);
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++)
998 /* do nothing */;
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\
1027 was successful.";
1029 static PyObject*
1030 xmlparse_SetParamEntityParsing(xmlparseobject *p, PyObject* args)
1032 int flag;
1033 if (!PyArg_ParseTuple(args, "i", &flag))
1034 return NULL;
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__},
1055 #endif
1056 #if EXPAT_VERSION >= 0x015f00
1057 {"GetInputContext", (PyCFunction)xmlparse_GetInputContext,
1058 METH_VARARGS, xmlparse_GetInputContext__doc__},
1059 #endif
1060 {NULL, NULL} /* sentinel */
1063 /* ---------- */
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;
1076 static void
1077 init_template_buffer(void)
1079 int i;
1080 for (i = 0; i < 256; i++) {
1081 template_buffer[i] = i;
1083 template_buffer[256] = 0;
1086 int
1087 PyUnknownEncodingHandler(void *encodingHandlerData,
1088 const XML_Char *name,
1089 XML_Encoding * info)
1091 PyUnicodeObject *_u_string = NULL;
1092 int result = 0;
1093 int i;
1095 /* Yes, supports only 8bit encodings */
1096 _u_string = (PyUnicodeObject *)
1097 PyUnicode_Decode(template_buffer, 256, name, "replace");
1099 if (_u_string == NULL)
1100 return result;
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)
1106 info->map[i] = -1;
1107 else
1108 info->map[i] = c;
1111 info->data = NULL;
1112 info->convert = NULL;
1113 info->release = NULL;
1114 result=1;
1116 Py_DECREF(_u_string);
1117 return result;
1120 #endif
1122 static PyObject *
1123 newxmlparseobject(char *encoding, char *namespace_separator)
1125 int i;
1126 xmlparseobject *self;
1128 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1129 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1130 if (self == NULL)
1131 return NULL;
1133 self->returns_unicode = 0;
1134 #else
1135 /* Code for versions 1.6 and later */
1136 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1137 if (self == NULL)
1138 return NULL;
1140 self->returns_unicode = 1;
1141 #endif
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);
1149 else {
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");
1156 Py_DECREF(self);
1157 return NULL;
1159 XML_SetUserData(self->itself, (void *)self);
1160 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1161 #else
1162 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1163 #endif
1165 for(i = 0; handler_info[i].name != NULL; i++)
1166 /* do nothing */;
1168 self->handlers = malloc(sizeof(PyObject *)*i);
1169 if (!self->handlers){
1170 Py_DECREF(self);
1171 return PyErr_NoMemory();
1173 clear_handlers(self, 0);
1175 return (PyObject*)self;
1179 static void
1180 xmlparse_dealloc(xmlparseobject *self)
1182 int i;
1183 PyObject_GC_Fini(self);
1184 if (self->itself != NULL)
1185 XML_ParserFree(self->itself);
1186 self->itself = NULL;
1188 if (self->handlers != NULL) {
1189 PyObject *temp;
1190 for (i = 0; handler_info[i].name != NULL; i++) {
1191 temp = self->handlers[i];
1192 self->handlers[i] = NULL;
1193 Py_XDECREF(temp);
1195 free(self->handlers);
1197 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1198 /* Code for versions before 1.6 */
1199 free(self);
1200 #else
1201 /* Code for versions 1.6 and later */
1202 PyObject_Del(self);
1203 #endif
1206 static int
1207 handlername2int(const char *name)
1209 int i;
1210 for (i=0; handler_info[i].name != NULL; i++) {
1211 if (strcmp(name, handler_info[i].name) == 0) {
1212 return i;
1215 return -1;
1218 static PyObject *
1219 xmlparse_getattr(xmlparseobject *self, char *name)
1221 int handlernum;
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) {
1244 int i;
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"));
1257 return rc;
1259 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
1262 static int
1263 sethandler(xmlparseobject *self, const char *name, PyObject* v)
1265 int handlernum = handlername2int(name);
1266 if (handlernum != -1) {
1267 Py_INCREF(v);
1268 Py_XDECREF(self->handlers[handlernum]);
1269 self->handlers[handlernum] = v;
1270 handler_info[handlernum].setter(self->itself,
1271 handler_info[handlernum].handler);
1272 return 1;
1274 return 0;
1277 static int
1278 xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
1280 /* Set attribute 'name' to value 'v'. v==NULL means delete */
1281 if (v == NULL) {
1282 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1283 return -1;
1285 if (strcmp(name, "ordered_attributes") == 0) {
1286 if (PyObject_IsTrue(v))
1287 self->ordered_attributes = 1;
1288 else
1289 self->ordered_attributes = 0;
1290 return 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");
1297 return -1;
1298 #else
1299 self->returns_unicode = 1;
1300 #endif
1302 else
1303 self->returns_unicode = 0;
1304 return 0;
1306 if (strcmp(name, "specified_attributes") == 0) {
1307 if (PyObject_IsTrue(v))
1308 self->specified_attributes = 1;
1309 else
1310 self->specified_attributes = 0;
1311 return 0;
1313 if (sethandler(self, name, v)) {
1314 return 0;
1316 PyErr_SetString(PyExc_AttributeError, name);
1317 return -1;
1320 #ifdef WITH_CYCLE_GC
1321 static int
1322 xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1324 int i, err;
1325 for (i = 0; handler_info[i].name != NULL; i++) {
1326 if (!op->handlers[i])
1327 continue;
1328 err = visit(op->handlers[i], arg);
1329 if (err)
1330 return err;
1332 return 0;
1335 static int
1336 xmlparse_clear(xmlparseobject *op)
1338 clear_handlers(op, 1);
1339 return 0;
1341 #endif
1343 static char Xmlparsetype__doc__[] =
1344 "XML parser";
1346 static PyTypeObject Xmlparsetype = {
1347 PyObject_HEAD_INIT(NULL)
1348 0, /*ob_size*/
1349 "xmlparser", /*tp_name*/
1350 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
1351 0, /*tp_itemsize*/
1352 /* methods */
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*/
1359 0, /*tp_as_number*/
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 */
1373 #else
1374 0, 0
1375 #endif
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.";
1385 static PyObject *
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))
1394 return NULL;
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");
1400 return NULL;
1402 return newxmlparseobject(encoding, namespace_separator);
1405 static char pyexpat_ErrorString__doc__[] =
1406 "ErrorString(errno) -> string\n\
1407 Returns string error for given number.";
1409 static PyObject *
1410 pyexpat_ErrorString(PyObject *self, PyObject *args)
1412 long code = 0;
1414 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1415 return NULL;
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 */
1438 static int
1439 PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1441 PyObject *dict;
1442 if (!PyModule_Check(m) || o == NULL)
1443 return -1;
1444 dict = PyModule_GetDict(m);
1445 if (dict == NULL)
1446 return -1;
1447 if (PyDict_SetItemString(dict, name, o))
1448 return -1;
1449 Py_DECREF(o);
1450 return 0;
1453 int
1454 PyModule_AddIntConstant(PyObject *m, char *name, long value)
1456 return PyModule_AddObject(m, name, PyInt_FromLong(value));
1459 static int
1460 PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1462 return PyModule_AddObject(m, name, PyString_FromString(value));
1465 #endif
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
1471 * cruft.
1473 static PyObject *
1474 get_version_string(void)
1476 static char *rcsid = "$Revision$";
1477 char *rev = rcsid;
1478 int i = 0;
1480 while (!isdigit(*rev))
1481 ++rev;
1482 while (rev[i] != ' ' && rev[i] != '\0')
1483 ++i;
1485 return PyString_FromStringAndSize(rev, i);
1488 /* Initialization function for the module */
1490 #ifndef MODULE_NAME
1491 #define MODULE_NAME "pyexpat"
1492 #endif
1494 #ifndef MODULE_INITFUNC
1495 #define MODULE_INITFUNC initpyexpat
1496 #endif
1498 void MODULE_INITFUNC(void); /* avoid compiler warnings */
1500 DL_EXPORT(void)
1501 MODULE_INITFUNC(void)
1503 PyObject *m, *d;
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)
1511 return;
1512 modelmod_name = PyString_FromString(MODULE_NAME ".model");
1513 if (modelmod_name == NULL)
1514 return;
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",
1525 NULL, NULL);
1526 if (ErrorObject == NULL)
1527 return;
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));
1546 #endif
1547 #if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1548 #else
1549 init_template_buffer();
1550 #endif
1551 /* XXX When Expat supports some way of figuring out how it was
1552 compiled, this should check and set native_encoding
1553 appropriately.
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! */
1581 return;
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.");
1613 #undef MYCONST
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);
1620 #undef MYCONST
1621 #endif
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);
1639 #undef MYCONST
1640 #endif
1643 static void
1644 clear_handlers(xmlparseobject *self, int decref)
1646 int i = 0;
1647 PyObject *temp;
1649 for (; handler_info[i].name!=NULL; i++) {
1650 if (decref) {
1651 temp = self->handlers[i];
1652 self->handlers[i] = NULL;
1653 Py_XDECREF(temp);
1655 self->handlers[i]=NULL;
1656 handler_info[i].setter(self->itself, NULL);
1660 typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
1662 static void
1663 pyxml_UpdatePairedHandlers(xmlparseobject *self,
1664 int startHandler,
1665 int endHandler,
1666 pairsetter setter)
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);
1682 static void
1683 pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
1685 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1686 StartElement, EndElement,
1687 (pairsetter)XML_SetElementHandler);
1690 static void
1691 pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
1693 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1694 StartElement, EndElement,
1695 (pairsetter)XML_SetElementHandler);
1698 static void
1699 pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
1701 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1702 StartNamespaceDecl, EndNamespaceDecl,
1703 (pairsetter)XML_SetNamespaceDeclHandler);
1706 static void
1707 pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
1709 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1710 StartNamespaceDecl, EndNamespaceDecl,
1711 (pairsetter)XML_SetNamespaceDeclHandler);
1714 static void
1715 pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
1717 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1718 StartCdataSection, EndCdataSection,
1719 (pairsetter)XML_SetCdataSectionHandler);
1722 static void
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
1732 static void
1733 pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1735 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1736 StartDoctypeDecl, EndDoctypeDecl,
1737 (pairsetter)XML_SetDoctypeDeclHandler);
1740 static void
1741 pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1743 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1744 StartDoctypeDecl, EndDoctypeDecl,
1745 (pairsetter)XML_SetDoctypeDeclHandler);
1748 #endif
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 },
1775 {"CommentHandler",
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},
1784 {"DefaultHandler",
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},
1803 #endif
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},
1811 #endif
1812 #if EXPAT_VERSION >= 0x015f00
1813 {"EntityDeclHandler",
1814 (xmlhandlersetter)XML_SetEntityDeclHandler,
1815 (xmlhandler)my_EntityDeclHandler},
1816 {"XmlDeclHandler",
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 */