blob: d0967aa1d77a08a0a4c38963458f5b878e4a3d78 [file] [log] [blame]
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001#include "Python.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00002#include "compile.h"
3#include "frameobject.h"
Fred Drakea77254a2000-09-29 19:23:29 +00004#ifdef HAVE_EXPAT_H
5#include "expat.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00006#ifdef XML_MAJOR_VERSION
Fred Drake85d835f2001-02-08 15:39:08 +00007#define EXPAT_VERSION (0x10000 * XML_MAJOR_VERSION \
8 + 0x100 * XML_MINOR_VERSION \
9 + XML_MICRO_VERSION)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000010#else
Fred Drake85d835f2001-02-08 15:39:08 +000011/* Assume the oldest Expat that used expat.h and did not have version info */
12#define EXPAT_VERSION 0x015f00
13#endif
14#else /* !defined(HAVE_EXPAT_H) */
15#include "xmlparse.h"
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000016/* Assume Expat 1.1 unless told otherwise */
Fred Drake85d835f2001-02-08 15:39:08 +000017#ifndef EXPAT_VERSION
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000018#define EXPAT_VERSION 0x010100
19#endif
Fred Drake85d835f2001-02-08 15:39:08 +000020#endif /* !defined(HAVE_EXPAT_H) */
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000021
22#ifndef PyGC_HEAD_SIZE
23#define PyGC_HEAD_SIZE 0
24#define PyObject_GC_Init(x)
25#define PyObject_GC_Fini(m)
26#define Py_TPFLAGS_GC 0
27#endif
28
Fred Drake0582df92000-07-12 04:49:00 +000029enum HandlerTypes {
30 StartElement,
31 EndElement,
32 ProcessingInstruction,
33 CharacterData,
34 UnparsedEntityDecl,
35 NotationDecl,
36 StartNamespaceDecl,
37 EndNamespaceDecl,
38 Comment,
39 StartCdataSection,
40 EndCdataSection,
41 Default,
42 DefaultHandlerExpand,
43 NotStandalone,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000044 ExternalEntityRef,
Fred Drake85d835f2001-02-08 15:39:08 +000045#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000046 StartDoctypeDecl,
47 EndDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000048#endif
49#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000050 ExternalParsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +000051 InternalParsedEntityDecl,
52#endif
53#if EXPAT_VERSION >= 0x015f00
54 EntityDecl,
55 XmlDecl,
56 ElementDecl,
57 AttlistDecl,
58#endif
59 _DummyDecl
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000060};
61
62static PyObject *ErrorObject;
63
64/* ----------------------------------------------------- */
65
66/* Declarations for objects of type xmlparser */
67
68typedef struct {
Fred Drake0582df92000-07-12 04:49:00 +000069 PyObject_HEAD
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000070
Fred Drake0582df92000-07-12 04:49:00 +000071 XML_Parser itself;
Fred Drake85d835f2001-02-08 15:39:08 +000072 int returns_unicode; /* True if Unicode strings are returned;
73 if false, UTF-8 strings are returned */
74 int ordered_attributes; /* Return attributes as a list. */
75 int specified_attributes; /* Report only specified attributes. */
76 PyObject *(*conv_attrs)(XML_Char **);
Fred Drake0582df92000-07-12 04:49:00 +000077 PyObject **handlers;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000078} xmlparseobject;
79
80staticforward PyTypeObject Xmlparsetype;
81
Fred Drake6f987622000-08-25 18:03:30 +000082typedef void (*xmlhandlersetter)(XML_Parser *self, void *meth);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000083typedef void* xmlhandler;
84
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +000085struct HandlerInfo {
Fred Drake0582df92000-07-12 04:49:00 +000086 const char *name;
87 xmlhandlersetter setter;
88 xmlhandler handler;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +000089 PyCodeObject *tb_code;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000090};
91
Andrew M. Kuchling637f6642000-07-04 14:53:43 +000092staticforward struct HandlerInfo handler_info[64];
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +000093
Fred Drake85d835f2001-02-08 15:39:08 +000094
95static PyObject *
96set_error(xmlparseobject *self)
97{
98 PyObject *err;
99 char buffer[256];
100 XML_Parser parser = self->itself;
101
102 sprintf(buffer, "%.200s: line %i, column %i",
103 XML_ErrorString(XML_GetErrorCode(parser)),
104 XML_GetErrorLineNumber(parser),
105 XML_GetErrorColumnNumber(parser));
106 err = PyObject_CallFunction(ErrorObject, "s", buffer);
107 if (err != NULL) {
108 PyObject *code = PyInt_FromLong(XML_GetErrorCode(parser));
109
110 if (code != NULL && PyObject_SetAttrString(err, "code", code) != -1)
111 PyErr_SetObject(ErrorObject, err);
112 }
113 return NULL;
114}
115
116
117#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000118/* Convert an array of attributes and their values into a Python dict */
119
Fred Drake0582df92000-07-12 04:49:00 +0000120static PyObject *
121conv_atts_using_string(XML_Char **atts)
Andrew M. Kuchlinga4e75d72000-07-12 00:53:41 +0000122{
Fred Drake0582df92000-07-12 04:49:00 +0000123 PyObject *attrs_obj = NULL;
124 XML_Char **attrs_p, **attrs_k = NULL;
125 int attrs_len;
126 PyObject *rv;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000127
Fred Drake0582df92000-07-12 04:49:00 +0000128 if ((attrs_obj = PyDict_New()) == NULL)
129 goto finally;
130 for (attrs_len = 0, attrs_p = atts;
131 *attrs_p;
132 attrs_p++, attrs_len++) {
133 if (attrs_len % 2) {
134 rv = PyString_FromString(*attrs_p);
135 if (!rv) {
136 Py_DECREF(attrs_obj);
137 attrs_obj = NULL;
138 goto finally;
139 }
140 if (PyDict_SetItemString(attrs_obj,
141 (char*)*attrs_k, rv) < 0) {
142 Py_DECREF(attrs_obj);
143 attrs_obj = NULL;
144 goto finally;
145 }
146 Py_DECREF(rv);
147 }
148 else
149 attrs_k = attrs_p;
150 }
151 finally:
152 return attrs_obj;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000153}
Fred Drake85d835f2001-02-08 15:39:08 +0000154#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000155
156#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
Fred Drake85d835f2001-02-08 15:39:08 +0000157#if EXPAT_VERSION == 0x010200
Fred Drake0582df92000-07-12 04:49:00 +0000158static PyObject *
159conv_atts_using_unicode(XML_Char **atts)
160{
Fred Drakeca1f4262000-09-21 20:10:23 +0000161 PyObject *attrs_obj;
Fred Drake0582df92000-07-12 04:49:00 +0000162 XML_Char **attrs_p, **attrs_k = NULL;
163 int attrs_len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000164
Fred Drake0582df92000-07-12 04:49:00 +0000165 if ((attrs_obj = PyDict_New()) == NULL)
166 goto finally;
167 for (attrs_len = 0, attrs_p = atts;
168 *attrs_p;
169 attrs_p++, attrs_len++) {
170 if (attrs_len % 2) {
171 PyObject *attr_str, *value_str;
172 const char *p = (const char *) (*attrs_k);
173 attr_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
174 if (!attr_str) {
175 Py_DECREF(attrs_obj);
176 attrs_obj = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000177 goto finally;
Fred Drake0582df92000-07-12 04:49:00 +0000178 }
179 p = (const char *) *attrs_p;
180 value_str = PyUnicode_DecodeUTF8(p, strlen(p), "strict");
181 if (!value_str) {
182 Py_DECREF(attrs_obj);
183 Py_DECREF(attr_str);
184 attrs_obj = NULL;
185 goto finally;
186 }
187 if (PyDict_SetItem(attrs_obj, attr_str, value_str) < 0) {
188 Py_DECREF(attrs_obj);
Fred Drakeca1f4262000-09-21 20:10:23 +0000189 Py_DECREF(attr_str);
190 Py_DECREF(value_str);
Fred Drake0582df92000-07-12 04:49:00 +0000191 attrs_obj = NULL;
192 goto finally;
193 }
194 Py_DECREF(attr_str);
195 Py_DECREF(value_str);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000196 }
Fred Drake0582df92000-07-12 04:49:00 +0000197 else
198 attrs_k = attrs_p;
199 }
200 finally:
201 return attrs_obj;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000202}
Fred Drake85d835f2001-02-08 15:39:08 +0000203#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000204
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000205/* Convert a string of XML_Chars into a Unicode string.
206 Returns None if str is a null pointer. */
207
Fred Drake0582df92000-07-12 04:49:00 +0000208static PyObject *
209conv_string_to_unicode(XML_Char *str)
210{
211 /* XXX currently this code assumes that XML_Char is 8-bit,
212 and hence in UTF-8. */
213 /* UTF-8 from Expat, Unicode desired */
214 if (str == NULL) {
215 Py_INCREF(Py_None);
216 return Py_None;
217 }
218 return PyUnicode_DecodeUTF8((const char *)str,
219 strlen((const char *)str),
220 "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000221}
222
Fred Drake0582df92000-07-12 04:49:00 +0000223static PyObject *
224conv_string_len_to_unicode(const XML_Char *str, int len)
225{
226 /* XXX currently this code assumes that XML_Char is 8-bit,
227 and hence in UTF-8. */
228 /* UTF-8 from Expat, Unicode desired */
229 if (str == NULL) {
230 Py_INCREF(Py_None);
231 return Py_None;
232 }
Fred Drake6f987622000-08-25 18:03:30 +0000233 return PyUnicode_DecodeUTF8((const char *)str, len, "strict");
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000234}
235#endif
236
237/* Convert a string of XML_Chars into an 8-bit Python string.
238 Returns None if str is a null pointer. */
239
Fred Drake6f987622000-08-25 18:03:30 +0000240static PyObject *
241conv_string_to_utf8(XML_Char *str)
242{
243 /* XXX currently this code assumes that XML_Char is 8-bit,
244 and hence in UTF-8. */
245 /* UTF-8 from Expat, UTF-8 desired */
246 if (str == NULL) {
247 Py_INCREF(Py_None);
248 return Py_None;
249 }
250 return PyString_FromString((const char *)str);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000251}
252
Fred Drake6f987622000-08-25 18:03:30 +0000253static PyObject *
254conv_string_len_to_utf8(const XML_Char *str, int len)
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000255{
Fred Drake6f987622000-08-25 18:03:30 +0000256 /* XXX currently this code assumes that XML_Char is 8-bit,
257 and hence in UTF-8. */
258 /* UTF-8 from Expat, UTF-8 desired */
259 if (str == NULL) {
260 Py_INCREF(Py_None);
261 return Py_None;
262 }
263 return PyString_FromStringAndSize((const char *)str, len);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000264}
265
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000266/* Callback routines */
267
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000268static void clear_handlers(xmlparseobject *self, int decref);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000269
Fred Drake6f987622000-08-25 18:03:30 +0000270static void
271flag_error(xmlparseobject *self)
272{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000273 clear_handlers(self, 1);
274}
275
276static PyCodeObject*
277getcode(enum HandlerTypes slot, char* func_name, int lineno)
278{
279 PyObject *code = NULL;
280 PyObject *name = NULL;
281 PyObject *nulltuple = NULL;
282 PyObject *filename = NULL;
283 if (handler_info[slot].tb_code == NULL) {
284 code = PyString_FromString("");
285 if (code == NULL)
286 goto failed;
287 name = PyString_FromString(func_name);
288 if (name == NULL)
289 goto failed;
290 nulltuple = PyTuple_New(0);
291 if (nulltuple == NULL)
292 goto failed;
Fred Drake85d835f2001-02-08 15:39:08 +0000293 filename = PyString_FromString(__FILE__);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000294 handler_info[slot].tb_code = PyCode_New(
295 0, /* argcount */
296 0, /* nlocals */
297 0, /* stacksize */
298 0, /* flags */
299 code, /* code */
300 nulltuple, /* consts */
301 nulltuple, /* names */
302 nulltuple, /* varnames */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000303#if PYTHON_API_VERSION >= 1010
Jeremy Hylton903f6542001-01-25 20:07:56 +0000304 nulltuple, /* freevars */
305 nulltuple, /* cellvars */
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000306#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000307 filename, /* filename */
308 name, /* name */
309 lineno, /* firstlineno */
310 code /* lnotab */
311 );
312 if (handler_info[slot].tb_code == NULL)
313 goto failed;
314 Py_DECREF(code);
315 Py_DECREF(nulltuple);
316 Py_DECREF(filename);
317 Py_DECREF(name);
318 }
319 return handler_info[slot].tb_code;
320 failed:
321 Py_XDECREF(code);
322 Py_XDECREF(name);
323 return NULL;
324}
325
326static PyObject*
327call_with_frame(PyCodeObject *c, PyObject* func, PyObject* args)
328{
329 PyThreadState *tstate = PyThreadState_GET();
330 PyFrameObject *f;
331 PyObject *res;
332 if (c == NULL)
333 return NULL;
334 f = PyFrame_New(
335 tstate, /*back*/
336 c, /*code*/
337 tstate->frame->f_globals, /*globals*/
Martin v. Löwis76192ee2001-02-06 09:34:40 +0000338 NULL /*locals*/
339#if PYTHON_API_VERSION >= 1010
340 ,NULL /*closure*/
341#endif
342 );
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000343 if (f == NULL)
344 return NULL;
345 tstate->frame = f;
346 res = PyEval_CallObject(func, args);
347 if (res == NULL && tstate->curexc_traceback == NULL)
348 PyTraceBack_Here(f);
349 tstate->frame = f->f_back;
350 Py_DECREF(f);
351 return res;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000352}
353
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000354#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
355#define STRING_CONV_FUNC conv_string_to_utf8
356#else
357/* Python 1.6 and later versions */
358#define STRING_CONV_FUNC (self->returns_unicode \
359 ? conv_string_to_unicode : conv_string_to_utf8)
360#endif
Guido van Rossum5961f5a2000-03-31 16:18:11 +0000361
Fred Drake85d835f2001-02-08 15:39:08 +0000362static void
363my_StartElementHandler(void *userData,
364 const XML_Char *name, const XML_Char **atts)
365{
366 xmlparseobject *self = (xmlparseobject *)userData;
367
368 if (self->handlers[StartElement]
369 && self->handlers[StartElement] != Py_None) {
370 PyObject *container, *rv, *args;
371 int i, max;
372
373 /* Set max to the number of slots filled in atts[]; max/2 is
374 * the number of attributes we need to process.
375 */
376 if (self->specified_attributes) {
377 max = XML_GetSpecifiedAttributeCount(self->itself);
378 }
379 else {
380 max = 0;
381 while (atts[max] != NULL)
382 max += 2;
383 }
384 /* Build the container. */
385 if (self->ordered_attributes)
386 container = PyList_New(max);
387 else
388 container = PyDict_New();
389 if (container == NULL) {
390 flag_error(self);
391 return;
392 }
393 for (i = 0; i < max; i += 2) {
394 PyObject *n = STRING_CONV_FUNC((XML_Char *) atts[i]);
395 PyObject *v;
396 if (n == NULL) {
397 flag_error(self);
398 Py_DECREF(container);
399 return;
400 }
401 v = STRING_CONV_FUNC((XML_Char *) atts[i+1]);
402 if (v == NULL) {
403 flag_error(self);
404 Py_DECREF(container);
405 Py_DECREF(n);
406 return;
407 }
408 if (self->ordered_attributes) {
409 PyList_SET_ITEM(container, i, n);
410 PyList_SET_ITEM(container, i+1, v);
411 }
412 else if (PyDict_SetItem(container, n, v)) {
413 flag_error(self);
414 Py_DECREF(n);
415 Py_DECREF(v);
416 return;
417 }
418 else {
419 Py_DECREF(n);
420 Py_DECREF(v);
421 }
422 }
423 args = Py_BuildValue("(O&N)", STRING_CONV_FUNC,name, container);
424 if (args == NULL) {
425 Py_DECREF(container);
426 return;
427 }
428 /* Container is now a borrowed reference; ignore it. */
429 rv = call_with_frame(getcode(StartElement, "StartElement", __LINE__),
430 self->handlers[StartElement], args);
431 Py_DECREF(args);
432 if (rv == NULL) {
433 flag_error(self);
434 return;
435 }
436 Py_DECREF(rv);
437 }
438}
439
440#define RC_HANDLER(RC, NAME, PARAMS, INIT, PARAM_FORMAT, CONVERSION, \
441 RETURN, GETUSERDATA) \
442static RC \
443my_##NAME##Handler PARAMS {\
444 xmlparseobject *self = GETUSERDATA ; \
445 PyObject *args = NULL; \
446 PyObject *rv = NULL; \
447 INIT \
448\
449 if (self->handlers[NAME] \
450 && self->handlers[NAME] != Py_None) { \
451 args = Py_BuildValue PARAM_FORMAT ;\
452 if (!args) \
453 return RETURN; \
454 rv = call_with_frame(getcode(NAME,#NAME,__LINE__), \
455 self->handlers[NAME], args); \
456 Py_DECREF(args); \
457 if (rv == NULL) { \
458 flag_error(self); \
459 return RETURN; \
460 } \
461 CONVERSION \
462 Py_DECREF(rv); \
463 } \
464 return RETURN; \
465}
466
Fred Drake6f987622000-08-25 18:03:30 +0000467#define VOID_HANDLER(NAME, PARAMS, PARAM_FORMAT) \
468 RC_HANDLER(void, NAME, PARAMS, ;, PARAM_FORMAT, ;, ;,\
469 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000470
Fred Drake6f987622000-08-25 18:03:30 +0000471#define INT_HANDLER(NAME, PARAMS, PARAM_FORMAT)\
472 RC_HANDLER(int, NAME, PARAMS, int rc=0;, PARAM_FORMAT, \
473 rc = PyInt_AsLong(rv);, rc, \
474 (xmlparseobject *)userData)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000475
Fred Drake85d835f2001-02-08 15:39:08 +0000476#if EXPAT_VERSION == 0x010200
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000477#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake85d835f2001-02-08 15:39:08 +0000478VOID_HANDLER(StartElement,
479 (void *userData, const XML_Char *name, const XML_Char **atts),
480 ("(O&O&)", STRING_CONV_FUNC, name,
481 conv_atts_using_string, atts))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000482#else
483/* Python 1.6 and later */
Fred Drake85d835f2001-02-08 15:39:08 +0000484VOID_HANDLER(StartElement,
485 (void *userData, const XML_Char *name, const XML_Char **atts),
486 ("(O&O&)", STRING_CONV_FUNC, name,
487 (self->returns_unicode
488 ? conv_atts_using_unicode
489 : conv_atts_using_string), atts))
490#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000491#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000492
Fred Drake6f987622000-08-25 18:03:30 +0000493VOID_HANDLER(EndElement,
Fred Drake85d835f2001-02-08 15:39:08 +0000494 (void *userData, const XML_Char *name),
495 ("(O&)", STRING_CONV_FUNC, name))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000496
Fred Drake6f987622000-08-25 18:03:30 +0000497VOID_HANDLER(ProcessingInstruction,
Fred Drake85d835f2001-02-08 15:39:08 +0000498 (void *userData,
499 const XML_Char *target,
500 const XML_Char *data),
501 ("(O&O&)",STRING_CONV_FUNC,target, STRING_CONV_FUNC,data))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000502
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000503#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000504VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000505 (void *userData, const XML_Char *data, int len),
506 ("(N)", conv_string_len_to_utf8(data,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000507#else
Fred Drake6f987622000-08-25 18:03:30 +0000508VOID_HANDLER(CharacterData,
Fred Drake85d835f2001-02-08 15:39:08 +0000509 (void *userData, const XML_Char *data, int len),
510 ("(N)", (self->returns_unicode
511 ? conv_string_len_to_unicode(data,len)
512 : conv_string_len_to_utf8(data,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000513#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000514
Fred Drake6f987622000-08-25 18:03:30 +0000515VOID_HANDLER(UnparsedEntityDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000516 (void *userData,
517 const XML_Char *entityName,
518 const XML_Char *base,
519 const XML_Char *systemId,
520 const XML_Char *publicId,
521 const XML_Char *notationName),
522 ("(O&O&O&O&O&)",
523 STRING_CONV_FUNC,entityName, STRING_CONV_FUNC,base,
524 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId,
525 STRING_CONV_FUNC,notationName))
526
527#if EXPAT_VERSION >= 0x015f00
528#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
529VOID_HANDLER(EntityDecl,
530 (void *userData,
531 const XML_Char *entityName,
532 int is_parameter_entity,
533 const XML_Char *value,
534 int value_length,
535 const XML_Char *base,
536 const XML_Char *systemId,
537 const XML_Char *publicId,
538 const XML_Char *notationName),
539 ("O&iNO&O&O&O&",
540 STRING_CONV_FUNC,entityName, is_parameter_entity,
541 conv_string_len_to_utf8(value, value_length),
542 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
543 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
544#else
545VOID_HANDLER(EntityDecl,
546 (void *userData,
547 const XML_Char *entityName,
548 int is_parameter_entity,
549 const XML_Char *value,
550 int value_length,
551 const XML_Char *base,
552 const XML_Char *systemId,
553 const XML_Char *publicId,
554 const XML_Char *notationName),
555 ("O&iNO&O&O&O&",
556 STRING_CONV_FUNC,entityName, is_parameter_entity,
557 (self->returns_unicode
558 ? conv_string_len_to_unicode(value, value_length)
559 : conv_string_len_to_utf8(value, value_length)),
560 STRING_CONV_FUNC,base, STRING_CONV_FUNC,systemId,
561 STRING_CONV_FUNC,publicId, STRING_CONV_FUNC,notationName))
562#endif
563
564VOID_HANDLER(XmlDecl,
565 (void *userData,
566 const XML_Char *version,
567 const XML_Char *encoding,
568 int standalone),
569 ("(O&O&i)",
570 STRING_CONV_FUNC,version, STRING_CONV_FUNC,encoding,
571 standalone))
572
573static PyObject *
574conv_content_model(XML_Content * const model,
575 PyObject *(*conv_string)(XML_Char *))
576{
577 PyObject *result = NULL;
578 PyObject *children = PyTuple_New(model->numchildren);
579 int i;
580
581 if (children != NULL) {
582 for (i = 0; i < model->numchildren; ++i) {
583 PyObject *child = conv_content_model(&model->children[i],
584 conv_string);
585 if (child == NULL) {
586 Py_XDECREF(children);
587 return NULL;
588 }
589 PyTuple_SET_ITEM(children, i, child);
590 }
591 result = Py_BuildValue("(iiO&N)",
592 model->type, model->quant,
593 conv_string,model->name, children);
594 }
595 return result;
596}
597
598static PyObject *
599conv_content_model_utf8(XML_Content * const model)
600{
601 return conv_content_model(model, conv_string_to_utf8);
602}
603
604#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
605static PyObject *
606conv_content_model_unicode(XML_Content * const model)
607{
608 return conv_content_model(model, conv_string_to_unicode);
609}
610
611VOID_HANDLER(ElementDecl,
612 (void *userData,
613 const XML_Char *name,
614 XML_Content *model),
615 ("O&O&",
616 STRING_CONV_FUNC,name,
617 (self->returns_unicode ? conv_content_model_unicode
618 : conv_content_model_utf8),model))
619#else
620VOID_HANDLER(ElementDecl,
621 (void *userData,
622 const XML_Char *name,
623 XML_Content *model),
624 ("O&O&",
625 STRING_CONV_FUNC,name, conv_content_model_utf8,model))
626#endif
627
628VOID_HANDLER(AttlistDecl,
629 (void *userData,
630 const XML_Char *elname,
631 const XML_Char *attname,
632 const XML_Char *att_type,
633 const XML_Char *dflt,
634 int isrequired),
635 ("(O&O&O&O&i)",
636 STRING_CONV_FUNC,elname, STRING_CONV_FUNC,attname,
637 STRING_CONV_FUNC,att_type, STRING_CONV_FUNC,dflt,
638 isrequired))
639#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000640
Fred Drake6f987622000-08-25 18:03:30 +0000641VOID_HANDLER(NotationDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000642 (void *userData,
643 const XML_Char *notationName,
644 const XML_Char *base,
645 const XML_Char *systemId,
646 const XML_Char *publicId),
647 ("(O&O&O&O&)",
648 STRING_CONV_FUNC,notationName, STRING_CONV_FUNC,base,
649 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000650
Fred Drake6f987622000-08-25 18:03:30 +0000651VOID_HANDLER(StartNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000652 (void *userData,
653 const XML_Char *prefix,
654 const XML_Char *uri),
Fred Drake6f987622000-08-25 18:03:30 +0000655 ("(O&O&)", STRING_CONV_FUNC,prefix, STRING_CONV_FUNC,uri))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000656
Fred Drake6f987622000-08-25 18:03:30 +0000657VOID_HANDLER(EndNamespaceDecl,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000658 (void *userData,
659 const XML_Char *prefix),
Fred Drake6f987622000-08-25 18:03:30 +0000660 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000661
Fred Drake6f987622000-08-25 18:03:30 +0000662VOID_HANDLER(Comment,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000663 (void *userData, const XML_Char *prefix),
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000664 ("(O&)", STRING_CONV_FUNC,prefix))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000665
Fred Drake6f987622000-08-25 18:03:30 +0000666VOID_HANDLER(StartCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000667 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000668 ("()"))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000669
Fred Drake6f987622000-08-25 18:03:30 +0000670VOID_HANDLER(EndCdataSection,
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000671 (void *userData),
Fred Drake6f987622000-08-25 18:03:30 +0000672 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000673
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000674#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +0000675VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000676 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000677 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000678
Fred Drake6f987622000-08-25 18:03:30 +0000679VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000680 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000681 ("(N)", conv_string_len_to_utf8(s,len)))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000682#else
Fred Drake6f987622000-08-25 18:03:30 +0000683VOID_HANDLER(Default,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000684 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000685 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000686 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000687 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000688
Fred Drake6f987622000-08-25 18:03:30 +0000689VOID_HANDLER(DefaultHandlerExpand,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000690 (void *userData, const XML_Char *s, int len),
Fred Drakeca1f4262000-09-21 20:10:23 +0000691 ("(N)", (self->returns_unicode
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000692 ? conv_string_len_to_unicode(s,len)
Fred Drake6f987622000-08-25 18:03:30 +0000693 : conv_string_len_to_utf8(s,len))))
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000694#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000695
Fred Drake6f987622000-08-25 18:03:30 +0000696INT_HANDLER(NotStandalone,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000697 (void *userData),
698 ("()"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000699
Fred Drake6f987622000-08-25 18:03:30 +0000700RC_HANDLER(int, ExternalEntityRef,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +0000701 (XML_Parser parser,
702 const XML_Char *context,
703 const XML_Char *base,
704 const XML_Char *systemId,
705 const XML_Char *publicId),
706 int rc=0;,
707 ("(O&O&O&O&)",
708 STRING_CONV_FUNC,context, STRING_CONV_FUNC,base,
Fred Drake6f987622000-08-25 18:03:30 +0000709 STRING_CONV_FUNC,systemId, STRING_CONV_FUNC,publicId),
710 rc = PyInt_AsLong(rv);, rc,
711 XML_GetUserData(parser))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000712
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000713/* XXX UnknownEncodingHandler */
714
Fred Drake85d835f2001-02-08 15:39:08 +0000715#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000716VOID_HANDLER(StartDoctypeDecl,
Fred Drake85d835f2001-02-08 15:39:08 +0000717 (void *userData, const XML_Char *doctypeName),
718 ("(O&OOi)", STRING_CONV_FUNC,doctypeName,
719 Py_None, Py_None, -1))
720#elif EXPAT_VERSION >= 0x015f00
721VOID_HANDLER(StartDoctypeDecl,
722 (void *userData, const XML_Char *doctypeName,
723 const XML_Char *sysid, const XML_Char *pubid,
724 int has_internal_subset),
725 ("(O&O&O&i)", STRING_CONV_FUNC,doctypeName,
726 STRING_CONV_FUNC,sysid, STRING_CONV_FUNC,pubid,
727 has_internal_subset))
728#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000729
Fred Drake85d835f2001-02-08 15:39:08 +0000730#if EXPAT_VERSION >= 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000731VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
Fred Drake85d835f2001-02-08 15:39:08 +0000732#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000733
Fred Drake85d835f2001-02-08 15:39:08 +0000734#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000735VOID_HANDLER(ExternalParsedEntityDecl,
736 (void *userData, const XML_Char *entityName,
737 const XML_Char *base, const XML_Char *systemId,
738 const XML_Char *publicId),
739 ("(O&O&O&O&)", STRING_CONV_FUNC, entityName,
740 STRING_CONV_FUNC, base, STRING_CONV_FUNC, systemId,
741 STRING_CONV_FUNC, publicId))
742
743VOID_HANDLER(InternalParsedEntityDecl,
744 (void *userData, const XML_Char *entityName,
745 const XML_Char *replacementText, int replacementTextLength),
746 ("(O&O&i)", STRING_CONV_FUNC, entityName,
747 STRING_CONV_FUNC, replacementText, replacementTextLength))
748
Fred Drake85d835f2001-02-08 15:39:08 +0000749#endif /* Expat version 1.2 & better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000750
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000751/* ---------------------------------------------------------------- */
752
753static char xmlparse_Parse__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000754"Parse(data[, isfinal])\n\
Fred Drake0582df92000-07-12 04:49:00 +0000755Parse XML data. `isfinal' should be true at end of input.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000756
757static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000758xmlparse_Parse(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000759{
Fred Drake0582df92000-07-12 04:49:00 +0000760 char *s;
761 int slen;
762 int isFinal = 0;
763 int rv;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000764
Fred Drake0582df92000-07-12 04:49:00 +0000765 if (!PyArg_ParseTuple(args, "s#|i:Parse", &s, &slen, &isFinal))
766 return NULL;
767 rv = XML_Parse(self->itself, s, slen, isFinal);
768 if (PyErr_Occurred()) {
769 return NULL;
770 }
771 else if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000772 return set_error(self);
Fred Drake0582df92000-07-12 04:49:00 +0000773 }
774 return PyInt_FromLong(rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000775}
776
Fred Drakeca1f4262000-09-21 20:10:23 +0000777/* File reading copied from cPickle */
778
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000779#define BUF_SIZE 2048
780
Fred Drake0582df92000-07-12 04:49:00 +0000781static int
782readinst(char *buf, int buf_size, PyObject *meth)
783{
784 PyObject *arg = NULL;
785 PyObject *bytes = NULL;
786 PyObject *str = NULL;
787 int len = -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000788
Fred Drake676940b2000-09-22 15:21:31 +0000789 if ((bytes = PyInt_FromLong(buf_size)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000790 goto finally;
Fred Drake676940b2000-09-22 15:21:31 +0000791
Fred Drakeca1f4262000-09-21 20:10:23 +0000792 if ((arg = PyTuple_New(1)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000793 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000794
Tim Peters954eef72000-09-22 06:01:11 +0000795 PyTuple_SET_ITEM(arg, 0, bytes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000796
Fred Drakeca1f4262000-09-21 20:10:23 +0000797 if ((str = PyObject_CallObject(meth, arg)) == NULL)
Fred Drake0582df92000-07-12 04:49:00 +0000798 goto finally;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000799
Fred Drake0582df92000-07-12 04:49:00 +0000800 /* XXX what to do if it returns a Unicode string? */
Fred Drakeca1f4262000-09-21 20:10:23 +0000801 if (!PyString_Check(str)) {
Fred Drake0582df92000-07-12 04:49:00 +0000802 PyErr_Format(PyExc_TypeError,
803 "read() did not return a string object (type=%.400s)",
804 str->ob_type->tp_name);
805 goto finally;
806 }
807 len = PyString_GET_SIZE(str);
808 if (len > buf_size) {
809 PyErr_Format(PyExc_ValueError,
810 "read() returned too much data: "
811 "%i bytes requested, %i returned",
812 buf_size, len);
813 Py_DECREF(str);
814 goto finally;
815 }
816 memcpy(buf, PyString_AsString(str), len);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000817finally:
Fred Drake0582df92000-07-12 04:49:00 +0000818 Py_XDECREF(arg);
Fred Drakeca1f4262000-09-21 20:10:23 +0000819 Py_XDECREF(str);
Fred Drake0582df92000-07-12 04:49:00 +0000820 return len;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000821}
822
823static char xmlparse_ParseFile__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000824"ParseFile(file)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000825Parse XML data from file-like object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000826
827static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000828xmlparse_ParseFile(xmlparseobject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000829{
Fred Drake0582df92000-07-12 04:49:00 +0000830 int rv = 1;
831 PyObject *f;
832 FILE *fp;
833 PyObject *readmethod = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000834
Fred Drake0582df92000-07-12 04:49:00 +0000835 if (!PyArg_ParseTuple(args, "O:ParseFile", &f))
836 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000837
Fred Drake0582df92000-07-12 04:49:00 +0000838 if (PyFile_Check(f)) {
839 fp = PyFile_AsFile(f);
840 }
841 else{
842 fp = NULL;
Fred Drakeca1f4262000-09-21 20:10:23 +0000843 readmethod = PyObject_GetAttrString(f, "read");
844 if (readmethod == NULL) {
Fred Drake0582df92000-07-12 04:49:00 +0000845 PyErr_Clear();
846 PyErr_SetString(PyExc_TypeError,
847 "argument must have 'read' attribute");
848 return 0;
849 }
850 }
851 for (;;) {
852 int bytes_read;
853 void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
854 if (buf == NULL)
855 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000856
Fred Drake0582df92000-07-12 04:49:00 +0000857 if (fp) {
858 bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
859 if (bytes_read < 0) {
860 PyErr_SetFromErrno(PyExc_IOError);
861 return NULL;
862 }
863 }
864 else {
865 bytes_read = readinst(buf, BUF_SIZE, readmethod);
866 if (bytes_read < 0)
867 return NULL;
868 }
869 rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
870 if (PyErr_Occurred())
871 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000872
Fred Drake0582df92000-07-12 04:49:00 +0000873 if (!rv || bytes_read == 0)
874 break;
875 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000876 if (rv == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +0000877 return set_error(self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000878 }
Fred Drake0582df92000-07-12 04:49:00 +0000879 return Py_BuildValue("i", rv);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000880}
881
882static char xmlparse_SetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000883"SetBase(base_url)\n\
Fred Drake0582df92000-07-12 04:49:00 +0000884Set the base URL for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000885
886static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000887xmlparse_SetBase(xmlparseobject *self, PyObject *args)
888{
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000889 char *base;
890
Fred Drake0582df92000-07-12 04:49:00 +0000891 if (!PyArg_ParseTuple(args, "s:SetBase", &base))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000892 return NULL;
Fred Drake0582df92000-07-12 04:49:00 +0000893 if (!XML_SetBase(self->itself, base)) {
894 return PyErr_NoMemory();
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000895 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000896 Py_INCREF(Py_None);
897 return Py_None;
898}
899
900static char xmlparse_GetBase__doc__[] =
Thomas Wouters35317302000-07-22 16:34:15 +0000901"GetBase() -> url\n\
Fred Drake0582df92000-07-12 04:49:00 +0000902Return base URL string for the parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000903
904static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +0000905xmlparse_GetBase(xmlparseobject *self, PyObject *args)
906{
907 if (!PyArg_ParseTuple(args, ":GetBase"))
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000908 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000909
Fred Drake0582df92000-07-12 04:49:00 +0000910 return Py_BuildValue("z", XML_GetBase(self->itself));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +0000911}
912
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000913static char xmlparse_ExternalEntityParserCreate__doc__[] =
Fred Drake2d4ac202001-01-03 15:36:25 +0000914"ExternalEntityParserCreate(context[, encoding])\n\
Tim Peters51dc9682000-09-24 22:12:45 +0000915Create a parser for parsing an external entity based on the\n\
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000916information passed to the ExternalEntityRefHandler.";
917
918static PyObject *
919xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args)
920{
921 char *context;
922 char *encoding = NULL;
923 xmlparseobject *new_parser;
924 int i;
925
926 if (!PyArg_ParseTuple(args, "s|s:ExternalEntityParserCreate", &context,
927 &encoding)) {
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000928 return NULL;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000929 }
930
931#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
932 new_parser = PyObject_NEW(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000933#else
Fred Drake85d835f2001-02-08 15:39:08 +0000934 /* Python versions 1.6 and later */
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000935 new_parser = PyObject_New(xmlparseobject, &Xmlparsetype);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000936#endif
Fred Drake85d835f2001-02-08 15:39:08 +0000937
938 if (new_parser == NULL)
939 return NULL;
940 new_parser->returns_unicode = self->returns_unicode;
941 new_parser->ordered_attributes = self->ordered_attributes;
942 new_parser->specified_attributes = self->specified_attributes;
943 new_parser->conv_attrs = self->conv_attrs;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000944 new_parser->itself = XML_ExternalEntityParserCreate(self->itself, context,
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000945 encoding);
946 new_parser->handlers = 0;
947 PyObject_GC_Init(new_parser);
948
949 if (!new_parser->itself) {
Fred Drake85d835f2001-02-08 15:39:08 +0000950 Py_DECREF(new_parser);
951 return PyErr_NoMemory();
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000952 }
953
954 XML_SetUserData(new_parser->itself, (void *)new_parser);
955
956 /* allocate and clear handlers first */
957 for(i = 0; handler_info[i].name != NULL; i++)
Fred Drake85d835f2001-02-08 15:39:08 +0000958 /* do nothing */;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000959
960 new_parser->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000961 if (!new_parser->handlers) {
Fred Drake85d835f2001-02-08 15:39:08 +0000962 Py_DECREF(new_parser);
963 return PyErr_NoMemory();
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000964 }
965 clear_handlers(new_parser, 0);
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000966
967 /* then copy handlers from self */
968 for (i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +0000969 if (self->handlers[i]) {
970 Py_INCREF(self->handlers[i]);
971 new_parser->handlers[i] = self->handlers[i];
972 handler_info[i].setter(new_parser->itself,
973 handler_info[i].handler);
974 }
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000975 }
Fred Drake28adf522000-09-24 22:07:59 +0000976 return (PyObject *)new_parser;
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000977}
978
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000979#if EXPAT_VERSION >= 0x010200
Lars Gustäbel4a30a072000-09-24 20:50:52 +0000980
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000981static char xmlparse_SetParamEntityParsing__doc__[] =
982"SetParamEntityParsing(flag) -> success\n\
983Controls parsing of parameter entities (including the external DTD\n\
984subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER,\n\
985XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and\n\
986XML_PARAM_ENTITY_PARSING_ALWAYS. Returns true if setting the flag\n\
987was successful.";
988
989static PyObject*
990xmlparse_SetParamEntityParsing(PyObject *p, PyObject* args)
991{
Fred Drake85d835f2001-02-08 15:39:08 +0000992 int flag;
993 if (!PyArg_ParseTuple(args, "i", &flag))
994 return NULL;
995 flag = XML_SetParamEntityParsing(((xmlparseobject*)p)->itself, flag);
996 return PyInt_FromLong(flag);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +0000997}
998
Fred Drake85d835f2001-02-08 15:39:08 +0000999#endif /* Expat version 1.2 or better */
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001000
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001001static struct PyMethodDef xmlparse_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001002 {"Parse", (PyCFunction)xmlparse_Parse,
1003 METH_VARARGS, xmlparse_Parse__doc__},
1004 {"ParseFile", (PyCFunction)xmlparse_ParseFile,
1005 METH_VARARGS, xmlparse_ParseFile__doc__},
1006 {"SetBase", (PyCFunction)xmlparse_SetBase,
1007 METH_VARARGS, xmlparse_SetBase__doc__},
1008 {"GetBase", (PyCFunction)xmlparse_GetBase,
1009 METH_VARARGS, xmlparse_GetBase__doc__},
Lars Gustäbel4a30a072000-09-24 20:50:52 +00001010 {"ExternalEntityParserCreate", (PyCFunction)xmlparse_ExternalEntityParserCreate,
1011 METH_VARARGS, xmlparse_ExternalEntityParserCreate__doc__},
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001012#if EXPAT_VERSION >= 0x010200
1013 {"SetParamEntityParsing", xmlparse_SetParamEntityParsing,
1014 METH_VARARGS, xmlparse_SetParamEntityParsing__doc__},
1015#endif
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001016 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001017};
1018
1019/* ---------- */
1020
1021
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001022#if !(PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6)
1023
1024/*
1025 pyexpat international encoding support.
1026 Make it as simple as possible.
1027*/
1028
Martin v. Löwis3af7cc02001-01-22 08:19:10 +00001029static char template_buffer[257];
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001030PyObject * template_string=NULL;
1031
1032static void
1033init_template_buffer(void)
1034{
1035 int i;
1036 for (i=0;i<256;i++) {
1037 template_buffer[i]=i;
1038 };
1039 template_buffer[256]=0;
1040};
1041
1042int
1043PyUnknownEncodingHandler(void *encodingHandlerData,
1044const XML_Char *name,
1045XML_Encoding * info)
1046{
1047 PyUnicodeObject * _u_string=NULL;
1048 int result=0;
1049 int i;
1050
1051 _u_string=(PyUnicodeObject *) PyUnicode_Decode(template_buffer, 256, name, "replace"); // Yes, supports only 8bit encodings
1052
1053 if (_u_string==NULL) {
1054 return result;
1055 };
1056
1057 for (i=0; i<256; i++) {
1058 Py_UNICODE c = _u_string->str[i] ; // Stupid to access directly, but fast
1059 if (c==Py_UNICODE_REPLACEMENT_CHARACTER) {
1060 info->map[i] = -1;
1061 } else {
1062 info->map[i] = c;
1063 };
1064 };
1065
1066 info->data = NULL;
1067 info->convert = NULL;
1068 info->release = NULL;
1069 result=1;
1070
1071 Py_DECREF(_u_string);
1072 return result;
1073}
1074
1075#endif
1076
1077static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001078newxmlparseobject(char *encoding, char *namespace_separator)
1079{
1080 int i;
1081 xmlparseobject *self;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001082
1083#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001084 self = PyObject_NEW(xmlparseobject, &Xmlparsetype);
1085 if (self == NULL)
1086 return NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001087
Fred Drake0582df92000-07-12 04:49:00 +00001088 self->returns_unicode = 0;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001089#else
Fred Drake0582df92000-07-12 04:49:00 +00001090 /* Code for versions 1.6 and later */
1091 self = PyObject_New(xmlparseobject, &Xmlparsetype);
1092 if (self == NULL)
1093 return NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001094
Fred Drake0582df92000-07-12 04:49:00 +00001095 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001096#endif
Fred Drake85d835f2001-02-08 15:39:08 +00001097 self->ordered_attributes = 0;
1098 self->specified_attributes = 0;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001099 self->handlers = NULL;
Fred Drake0582df92000-07-12 04:49:00 +00001100 if (namespace_separator) {
1101 self->itself = XML_ParserCreateNS(encoding, *namespace_separator);
1102 }
Fred Drake85d835f2001-02-08 15:39:08 +00001103 else {
Fred Drake0582df92000-07-12 04:49:00 +00001104 self->itself = XML_ParserCreate(encoding);
1105 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001106 PyObject_GC_Init(self);
Fred Drake0582df92000-07-12 04:49:00 +00001107 if (self->itself == NULL) {
1108 PyErr_SetString(PyExc_RuntimeError,
1109 "XML_ParserCreate failed");
1110 Py_DECREF(self);
1111 return NULL;
1112 }
1113 XML_SetUserData(self->itself, (void *)self);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001114#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1115#else
1116 XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL);
1117#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001118
Fred Drake0582df92000-07-12 04:49:00 +00001119 for(i = 0; handler_info[i].name != NULL; i++)
1120 /* do nothing */;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001121
Fred Drake0582df92000-07-12 04:49:00 +00001122 self->handlers = malloc(sizeof(PyObject *)*i);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001123 if (!self->handlers){
1124 Py_DECREF(self);
1125 return PyErr_NoMemory();
1126 }
1127 clear_handlers(self, 0);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001128
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001129 return (PyObject*)self;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001130}
1131
1132
1133static void
Fred Drake0582df92000-07-12 04:49:00 +00001134xmlparse_dealloc(xmlparseobject *self)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001135{
Fred Drake0582df92000-07-12 04:49:00 +00001136 int i;
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001137 PyObject_GC_Fini(self);
Fred Drake85d835f2001-02-08 15:39:08 +00001138 if (self->itself != NULL)
Fred Drake0582df92000-07-12 04:49:00 +00001139 XML_ParserFree(self->itself);
1140 self->itself = NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001141
Fred Drake85d835f2001-02-08 15:39:08 +00001142 if (self->handlers != NULL) {
1143 for (i = 0; handler_info[i].name != NULL; i++) {
1144 Py_XDECREF(self->handlers[i]);
1145 }
1146 free(self->handlers);
Fred Drake0582df92000-07-12 04:49:00 +00001147 }
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001148#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake0582df92000-07-12 04:49:00 +00001149 /* Code for versions before 1.6 */
1150 free(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001151#else
Fred Drake0582df92000-07-12 04:49:00 +00001152 /* Code for versions 1.6 and later */
1153 PyObject_Del(self);
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001154#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001155}
1156
Fred Drake0582df92000-07-12 04:49:00 +00001157static int
1158handlername2int(const char *name)
1159{
1160 int i;
1161 for (i=0; handler_info[i].name != NULL; i++) {
1162 if (strcmp(name, handler_info[i].name) == 0) {
1163 return i;
1164 }
1165 }
1166 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001167}
1168
1169static PyObject *
1170xmlparse_getattr(xmlparseobject *self, char *name)
1171{
Fred Drake0582df92000-07-12 04:49:00 +00001172 int handlernum;
1173 if (strcmp(name, "ErrorCode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001174 return PyInt_FromLong((long) XML_GetErrorCode(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001175 if (strcmp(name, "ErrorLineNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001176 return PyInt_FromLong((long) XML_GetErrorLineNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001177 if (strcmp(name, "ErrorColumnNumber") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001178 return PyInt_FromLong((long) XML_GetErrorColumnNumber(self->itself));
Fred Drake0582df92000-07-12 04:49:00 +00001179 if (strcmp(name, "ErrorByteIndex") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001180 return PyInt_FromLong((long) XML_GetErrorByteIndex(self->itself));
1181 if (strcmp(name, "ordered_attributes") == 0)
1182 return PyInt_FromLong((long) self->ordered_attributes);
Fred Drake0582df92000-07-12 04:49:00 +00001183 if (strcmp(name, "returns_unicode") == 0)
Fred Drake85d835f2001-02-08 15:39:08 +00001184 return PyInt_FromLong((long) self->returns_unicode);
1185 if (strcmp(name, "specified_attributes") == 0)
1186 return PyInt_FromLong((long) self->specified_attributes);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001187
Fred Drake0582df92000-07-12 04:49:00 +00001188 handlernum = handlername2int(name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001189
Fred Drake0582df92000-07-12 04:49:00 +00001190 if (handlernum != -1 && self->handlers[handlernum] != NULL) {
1191 Py_INCREF(self->handlers[handlernum]);
1192 return self->handlers[handlernum];
1193 }
1194 if (strcmp(name, "__members__") == 0) {
1195 int i;
1196 PyObject *rc = PyList_New(0);
Fred Drakee8f3ad52000-12-16 01:48:29 +00001197 for(i = 0; handler_info[i].name != NULL; i++) {
Fred Drake85d835f2001-02-08 15:39:08 +00001198 PyList_Append(rc, PyString_FromString(handler_info[i].name));
Fred Drake0582df92000-07-12 04:49:00 +00001199 }
1200 PyList_Append(rc, PyString_FromString("ErrorCode"));
1201 PyList_Append(rc, PyString_FromString("ErrorLineNumber"));
1202 PyList_Append(rc, PyString_FromString("ErrorColumnNumber"));
1203 PyList_Append(rc, PyString_FromString("ErrorByteIndex"));
Fred Drake85d835f2001-02-08 15:39:08 +00001204 PyList_Append(rc, PyString_FromString("ordered_attributes"));
Fred Drakee8f3ad52000-12-16 01:48:29 +00001205 PyList_Append(rc, PyString_FromString("returns_unicode"));
Fred Drake85d835f2001-02-08 15:39:08 +00001206 PyList_Append(rc, PyString_FromString("specified_attributes"));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001207
Fred Drake0582df92000-07-12 04:49:00 +00001208 return rc;
1209 }
1210 return Py_FindMethod(xmlparse_methods, (PyObject *)self, name);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001211}
1212
Fred Drake6f987622000-08-25 18:03:30 +00001213static int
1214sethandler(xmlparseobject *self, const char *name, PyObject* v)
Fred Drake0582df92000-07-12 04:49:00 +00001215{
1216 int handlernum = handlername2int(name);
1217 if (handlernum != -1) {
1218 Py_INCREF(v);
1219 Py_XDECREF(self->handlers[handlernum]);
1220 self->handlers[handlernum] = v;
1221 handler_info[handlernum].setter(self->itself,
1222 handler_info[handlernum].handler);
1223 return 1;
1224 }
1225 return 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001226}
1227
1228static int
Fred Drake6f987622000-08-25 18:03:30 +00001229xmlparse_setattr(xmlparseobject *self, char *name, PyObject *v)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001230{
Fred Drake6f987622000-08-25 18:03:30 +00001231 /* Set attribute 'name' to value 'v'. v==NULL means delete */
Fred Drake85d835f2001-02-08 15:39:08 +00001232 if (v == NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001233 PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
1234 return -1;
1235 }
Fred Drake85d835f2001-02-08 15:39:08 +00001236 if (strcmp(name, "ordered_attributes") == 0) {
1237 if (PyObject_IsTrue(v))
1238 self->ordered_attributes = 1;
1239 else
1240 self->ordered_attributes = 0;
1241 return 0;
1242 }
Fred Drake6f987622000-08-25 18:03:30 +00001243 if (strcmp(name, "returns_unicode") == 0) {
Fred Drake85d835f2001-02-08 15:39:08 +00001244 if (PyObject_IsTrue(v)) {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001245#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
Fred Drake6f987622000-08-25 18:03:30 +00001246 PyErr_SetString(PyExc_ValueError,
1247 "Cannot return Unicode strings in Python 1.5");
1248 return -1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001249#else
Fred Drake6f987622000-08-25 18:03:30 +00001250 self->returns_unicode = 1;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001251#endif
Fred Drake6f987622000-08-25 18:03:30 +00001252 }
1253 else
1254 self->returns_unicode = 0;
Fred Drake85d835f2001-02-08 15:39:08 +00001255 return 0;
1256 }
1257 if (strcmp(name, "specified_attributes") == 0) {
1258 if (PyObject_IsTrue(v))
1259 self->specified_attributes = 1;
1260 else
1261 self->specified_attributes = 0;
Fred Drake6f987622000-08-25 18:03:30 +00001262 return 0;
1263 }
1264 if (sethandler(self, name, v)) {
1265 return 0;
1266 }
1267 PyErr_SetString(PyExc_AttributeError, name);
1268 return -1;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001269}
1270
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001271#ifdef WITH_CYCLE_GC
1272static int
1273xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
1274{
1275 int i, err;
1276 for (i = 0; handler_info[i].name != NULL; i++) {
1277 if (!op->handlers[i])
1278 continue;
1279 err = visit(op->handlers[i], arg);
1280 if (err)
1281 return err;
1282 }
1283 return 0;
1284}
1285
1286static int
1287xmlparse_clear(xmlparseobject *op)
1288{
1289 clear_handlers(op, 1);
1290 return 0;
1291}
1292#endif
1293
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001294static char Xmlparsetype__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001295"XML parser";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001296
1297static PyTypeObject Xmlparsetype = {
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001298 PyObject_HEAD_INIT(NULL)
1299 0, /*ob_size*/
1300 "xmlparser", /*tp_name*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001301 sizeof(xmlparseobject) + PyGC_HEAD_SIZE,/*tp_basicsize*/
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001302 0, /*tp_itemsize*/
1303 /* methods */
1304 (destructor)xmlparse_dealloc, /*tp_dealloc*/
1305 (printfunc)0, /*tp_print*/
1306 (getattrfunc)xmlparse_getattr, /*tp_getattr*/
1307 (setattrfunc)xmlparse_setattr, /*tp_setattr*/
1308 (cmpfunc)0, /*tp_compare*/
1309 (reprfunc)0, /*tp_repr*/
1310 0, /*tp_as_number*/
1311 0, /*tp_as_sequence*/
1312 0, /*tp_as_mapping*/
1313 (hashfunc)0, /*tp_hash*/
1314 (ternaryfunc)0, /*tp_call*/
1315 (reprfunc)0, /*tp_str*/
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001316 0, /* tp_getattro */
1317 0, /* tp_setattro */
1318 0, /* tp_as_buffer */
1319 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
1320 Xmlparsetype__doc__, /* Documentation string */
1321#ifdef WITH_CYCLE_GC
1322 (traverseproc)xmlparse_traverse, /* tp_traverse */
1323 (inquiry)xmlparse_clear /* tp_clear */
1324#else
1325 0, 0
1326#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001327};
1328
1329/* End of code for xmlparser objects */
1330/* -------------------------------------------------------- */
1331
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001332static char pyexpat_ParserCreate__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001333"ParserCreate([encoding[, namespace_separator]]) -> parser\n\
1334Return a new XML parser object.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001335
1336static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001337pyexpat_ParserCreate(PyObject *notused, PyObject *args, PyObject *kw)
1338{
1339 char *encoding = NULL;
1340 char *namespace_separator = NULL;
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001341 static char *kwlist[] = {"encoding", "namespace_separator", NULL};
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001342
Fred Drake0582df92000-07-12 04:49:00 +00001343 if (!PyArg_ParseTupleAndKeywords(args, kw, "|zz:ParserCreate", kwlist,
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001344 &encoding, &namespace_separator))
1345 return NULL;
Fred Drake4ba298c2000-10-29 04:57:53 +00001346 if (namespace_separator != NULL
1347 && strlen(namespace_separator) != 1) {
1348 PyErr_SetString(PyExc_ValueError,
1349 "namespace_separator must be one character,"
1350 " omitted, or None");
1351 return NULL;
1352 }
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001353 return newxmlparseobject(encoding, namespace_separator);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001354}
1355
1356static char pyexpat_ErrorString__doc__[] =
Fred Drake0582df92000-07-12 04:49:00 +00001357"ErrorString(errno) -> string\n\
1358Returns string error for given number.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001359
1360static PyObject *
Fred Drake0582df92000-07-12 04:49:00 +00001361pyexpat_ErrorString(PyObject *self, PyObject *args)
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001362{
Fred Drake0582df92000-07-12 04:49:00 +00001363 long code = 0;
1364
1365 if (!PyArg_ParseTuple(args, "l:ErrorString", &code))
1366 return NULL;
1367 return Py_BuildValue("z", XML_ErrorString((int)code));
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001368}
1369
1370/* List of methods defined in the module */
1371
1372static struct PyMethodDef pyexpat_methods[] = {
Fred Drake0582df92000-07-12 04:49:00 +00001373 {"ParserCreate", (PyCFunction)pyexpat_ParserCreate,
1374 METH_VARARGS|METH_KEYWORDS, pyexpat_ParserCreate__doc__},
1375 {"ErrorString", (PyCFunction)pyexpat_ErrorString,
1376 METH_VARARGS, pyexpat_ErrorString__doc__},
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001377
Fred Drake0582df92000-07-12 04:49:00 +00001378 {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001379};
1380
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001381/* Module docstring */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001382
1383static char pyexpat_module_documentation[] =
Fred Drake0582df92000-07-12 04:49:00 +00001384"Python wrapper for Expat parser.";
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001385
Andrew M. Kuchlingbeba0562000-06-27 00:33:30 +00001386/* Initialization function for the module */
1387
Fred Drake93adb692000-09-23 04:55:48 +00001388void initpyexpat(void); /* avoid compiler warnings */
Fred Drake6f987622000-08-25 18:03:30 +00001389
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001390#if PY_VERSION_HEX < 0x20000F0
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001391
1392/* 1.5 compatibility: PyModule_AddObject */
1393static int
1394PyModule_AddObject(PyObject *m, char *name, PyObject *o)
1395{
1396 PyObject *dict;
1397 if (!PyModule_Check(m) || o == NULL)
1398 return -1;
1399 dict = PyModule_GetDict(m);
1400 if (dict == NULL)
1401 return -1;
1402 if (PyDict_SetItemString(dict, name, o))
1403 return -1;
1404 Py_DECREF(o);
1405 return 0;
1406}
1407
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001408int
1409PyModule_AddIntConstant(PyObject *m, char *name, long value)
1410{
1411 return PyModule_AddObject(m, name, PyInt_FromLong(value));
1412}
1413
Fred Drakea77254a2000-09-29 19:23:29 +00001414static int
Martin v. Löwisc0718eb2000-09-29 19:05:48 +00001415PyModule_AddStringConstant(PyObject *m, char *name, char *value)
1416{
1417 return PyModule_AddObject(m, name, PyString_FromString(value));
1418}
1419
1420#endif
1421
Fred Drake6f987622000-08-25 18:03:30 +00001422DL_EXPORT(void)
Fred Drake0582df92000-07-12 04:49:00 +00001423initpyexpat(void)
1424{
1425 PyObject *m, *d;
1426 char *rev = "$Revision$";
Fred Drake6f987622000-08-25 18:03:30 +00001427 PyObject *errmod_name = PyString_FromString("pyexpat.errors");
Fred Drake85d835f2001-02-08 15:39:08 +00001428 PyObject *errors_module;
1429 PyObject *modelmod_name;
1430 PyObject *model_module;
Fred Drake0582df92000-07-12 04:49:00 +00001431 PyObject *sys_modules;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001432
Fred Drake6f987622000-08-25 18:03:30 +00001433 if (errmod_name == NULL)
1434 return;
Fred Drake85d835f2001-02-08 15:39:08 +00001435 modelmod_name = PyString_FromString("pyexpat.model");
1436 if (modelmod_name == NULL)
1437 return;
Fred Drake6f987622000-08-25 18:03:30 +00001438
Fred Drake0582df92000-07-12 04:49:00 +00001439 Xmlparsetype.ob_type = &PyType_Type;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001440
Fred Drake0582df92000-07-12 04:49:00 +00001441 /* Create the module and add the functions */
Fred Drake85d835f2001-02-08 15:39:08 +00001442 m = Py_InitModule3("pyexpat", pyexpat_methods,
1443 pyexpat_module_documentation);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001444
Fred Drake0582df92000-07-12 04:49:00 +00001445 /* Add some symbolic constants to the module */
Fred Drakec23b5232000-08-24 21:57:43 +00001446 if (ErrorObject == NULL)
Fred Drake93adb692000-09-23 04:55:48 +00001447 ErrorObject = PyErr_NewException("xml.parsers.expat.error",
1448 NULL, NULL);
1449 PyModule_AddObject(m, "error", ErrorObject);
Fred Drake4ba298c2000-10-29 04:57:53 +00001450 Py_INCREF(&Xmlparsetype);
1451 PyModule_AddObject(m, "XMLParserType", (PyObject *) &Xmlparsetype);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001452
Fred Drake93adb692000-09-23 04:55:48 +00001453 PyModule_AddObject(m, "__version__",
1454 PyString_FromStringAndSize(rev+11, strlen(rev+11)-2));
Fred Drake85d835f2001-02-08 15:39:08 +00001455#if EXPAT_VERSION >= 0x015f02
Fred Drake738293d2000-12-21 17:25:07 +00001456 PyModule_AddStringConstant(m, "EXPAT_VERSION",
1457 (char *) XML_ExpatVersion());
Fred Drake85d835f2001-02-08 15:39:08 +00001458 {
1459 XML_Expat_Version info = XML_ExpatVersionInfo();
1460 PyModule_AddObject(m, "version_info",
1461 Py_BuildValue("(iii)", info.major,
1462 info.minor, info.micro));
1463 }
Fred Drake738293d2000-12-21 17:25:07 +00001464#endif
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001465#if PY_MAJOR_VERSION == 1 && PY_MINOR_VERSION < 6
1466#else
1467 init_template_buffer();
1468#endif
Fred Drake0582df92000-07-12 04:49:00 +00001469 /* XXX When Expat supports some way of figuring out how it was
1470 compiled, this should check and set native_encoding
1471 appropriately.
1472 */
Fred Drake93adb692000-09-23 04:55:48 +00001473 PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
Fred Drakec23b5232000-08-24 21:57:43 +00001474
Fred Drake85d835f2001-02-08 15:39:08 +00001475 sys_modules = PySys_GetObject("modules");
Fred Drake93adb692000-09-23 04:55:48 +00001476 d = PyModule_GetDict(m);
Fred Drake6f987622000-08-25 18:03:30 +00001477 errors_module = PyDict_GetItem(d, errmod_name);
1478 if (errors_module == NULL) {
1479 errors_module = PyModule_New("pyexpat.errors");
1480 if (errors_module != NULL) {
Fred Drake6f987622000-08-25 18:03:30 +00001481 PyDict_SetItem(sys_modules, errmod_name, errors_module);
Fred Drake93adb692000-09-23 04:55:48 +00001482 /* gives away the reference to errors_module */
1483 PyModule_AddObject(m, "errors", errors_module);
Fred Drakec23b5232000-08-24 21:57:43 +00001484 }
1485 }
Fred Drake6f987622000-08-25 18:03:30 +00001486 Py_DECREF(errmod_name);
Fred Drake85d835f2001-02-08 15:39:08 +00001487 model_module = PyDict_GetItem(d, modelmod_name);
1488 if (model_module == NULL) {
1489 model_module = PyModule_New("pyexpat.model");
1490 if (model_module != NULL) {
1491 PyDict_SetItem(sys_modules, modelmod_name, model_module);
1492 /* gives away the reference to model_module */
1493 PyModule_AddObject(m, "model", model_module);
1494 }
1495 }
1496 Py_DECREF(modelmod_name);
1497 if (errors_module == NULL || model_module == NULL)
1498 /* Don't core dump later! */
Fred Drake6f987622000-08-25 18:03:30 +00001499 return;
1500
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001501#define MYCONST(name) \
Fred Drake93adb692000-09-23 04:55:48 +00001502 PyModule_AddStringConstant(errors_module, #name, \
1503 (char*)XML_ErrorString(name))
Fred Drake7bd9f412000-07-04 23:51:31 +00001504
Fred Drake0582df92000-07-12 04:49:00 +00001505 MYCONST(XML_ERROR_NO_MEMORY);
1506 MYCONST(XML_ERROR_SYNTAX);
1507 MYCONST(XML_ERROR_NO_ELEMENTS);
1508 MYCONST(XML_ERROR_INVALID_TOKEN);
1509 MYCONST(XML_ERROR_UNCLOSED_TOKEN);
1510 MYCONST(XML_ERROR_PARTIAL_CHAR);
1511 MYCONST(XML_ERROR_TAG_MISMATCH);
1512 MYCONST(XML_ERROR_DUPLICATE_ATTRIBUTE);
1513 MYCONST(XML_ERROR_JUNK_AFTER_DOC_ELEMENT);
1514 MYCONST(XML_ERROR_PARAM_ENTITY_REF);
1515 MYCONST(XML_ERROR_UNDEFINED_ENTITY);
1516 MYCONST(XML_ERROR_RECURSIVE_ENTITY_REF);
1517 MYCONST(XML_ERROR_ASYNC_ENTITY);
1518 MYCONST(XML_ERROR_BAD_CHAR_REF);
1519 MYCONST(XML_ERROR_BINARY_ENTITY_REF);
1520 MYCONST(XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF);
1521 MYCONST(XML_ERROR_MISPLACED_XML_PI);
1522 MYCONST(XML_ERROR_UNKNOWN_ENCODING);
1523 MYCONST(XML_ERROR_INCORRECT_ENCODING);
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001524 MYCONST(XML_ERROR_UNCLOSED_CDATA_SECTION);
1525 MYCONST(XML_ERROR_EXTERNAL_ENTITY_HANDLING);
1526 MYCONST(XML_ERROR_NOT_STANDALONE);
1527
Fred Drake85d835f2001-02-08 15:39:08 +00001528 PyModule_AddStringConstant(errors_module, "__doc__",
1529 "Constants used to describe error conditions.");
1530
Fred Drake93adb692000-09-23 04:55:48 +00001531#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001532
1533#if EXPAT_VERSION >= 0x010200
Fred Drake85d835f2001-02-08 15:39:08 +00001534#define MYCONST(c) PyModule_AddIntConstant(m, #c, c)
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001535 MYCONST(XML_PARAM_ENTITY_PARSING_NEVER);
1536 MYCONST(XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
1537 MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
Fred Drake85d835f2001-02-08 15:39:08 +00001538#undef MYCONST
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001539#endif
1540
Fred Drake85d835f2001-02-08 15:39:08 +00001541#if EXPAT_VERSION >= 0x015f00
1542#define MYCONST(c) PyModule_AddIntConstant(model_module, #c, c)
1543 PyModule_AddStringConstant(model_module, "__doc__",
1544 "Constants used to interpret content model information.");
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001545
Fred Drake85d835f2001-02-08 15:39:08 +00001546 MYCONST(XML_CTYPE_EMPTY);
1547 MYCONST(XML_CTYPE_ANY);
1548 MYCONST(XML_CTYPE_MIXED);
1549 MYCONST(XML_CTYPE_NAME);
1550 MYCONST(XML_CTYPE_CHOICE);
1551 MYCONST(XML_CTYPE_SEQ);
1552
1553 MYCONST(XML_CQUANT_NONE);
1554 MYCONST(XML_CQUANT_OPT);
1555 MYCONST(XML_CQUANT_REP);
1556 MYCONST(XML_CQUANT_PLUS);
1557#undef MYCONST
1558#endif
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001559}
1560
Fred Drake6f987622000-08-25 18:03:30 +00001561static void
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001562clear_handlers(xmlparseobject *self, int decref)
Fred Drake0582df92000-07-12 04:49:00 +00001563{
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001564 int i = 0;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001565
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001566 for (; handler_info[i].name!=NULL; i++) {
1567 if (decref){
1568 Py_XDECREF(self->handlers[i]);
1569 }
1570 self->handlers[i]=NULL;
1571 handler_info[i].setter(self->itself, NULL);
1572 }
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001573}
1574
Fred Drake6f987622000-08-25 18:03:30 +00001575typedef void (*pairsetter)(XML_Parser, void *handler1, void *handler2);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001576
Fred Drake6f987622000-08-25 18:03:30 +00001577static void
1578pyxml_UpdatePairedHandlers(xmlparseobject *self,
1579 int startHandler,
1580 int endHandler,
1581 pairsetter setter)
Fred Drake0582df92000-07-12 04:49:00 +00001582{
1583 void *start_handler=NULL;
1584 void *end_handler=NULL;
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001585
Fred Drake0582df92000-07-12 04:49:00 +00001586 if (self->handlers[startHandler]
1587 && self->handlers[endHandler]!=Py_None) {
1588 start_handler=handler_info[startHandler].handler;
1589 }
1590 if (self->handlers[EndElement]
1591 && self->handlers[EndElement] !=Py_None) {
1592 end_handler=handler_info[endHandler].handler;
1593 }
1594 setter(self->itself, start_handler, end_handler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001595}
1596
Fred Drake6f987622000-08-25 18:03:30 +00001597static void
1598pyxml_SetStartElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001599{
1600 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1601 StartElement, EndElement,
1602 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001603}
1604
Fred Drake6f987622000-08-25 18:03:30 +00001605static void
1606pyxml_SetEndElementHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001607{
1608 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1609 StartElement, EndElement,
1610 (pairsetter)XML_SetElementHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001611}
1612
Fred Drake6f987622000-08-25 18:03:30 +00001613static void
1614pyxml_SetStartNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001615{
1616 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1617 StartNamespaceDecl, EndNamespaceDecl,
1618 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001619}
1620
Fred Drake6f987622000-08-25 18:03:30 +00001621static void
1622pyxml_SetEndNamespaceDeclHandler(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001623{
1624 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1625 StartNamespaceDecl, EndNamespaceDecl,
1626 (pairsetter)XML_SetNamespaceDeclHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001627}
1628
Fred Drake6f987622000-08-25 18:03:30 +00001629static void
1630pyxml_SetStartCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001631{
1632 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1633 StartCdataSection, EndCdataSection,
1634 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001635}
1636
Fred Drake6f987622000-08-25 18:03:30 +00001637static void
1638pyxml_SetEndCdataSection(XML_Parser *parser, void *junk)
Fred Drake0582df92000-07-12 04:49:00 +00001639{
1640 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1641 StartCdataSection, EndCdataSection,
1642 (pairsetter)XML_SetCdataSectionHandler);
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001643}
1644
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001645#if EXPAT_VERSION >= 0x010200
1646
1647static void
1648pyxml_SetStartDoctypeDeclHandler(XML_Parser *parser, void *junk)
1649{
1650 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1651 StartDoctypeDecl, EndDoctypeDecl,
1652 (pairsetter)XML_SetDoctypeDeclHandler);
1653}
1654
1655static void
1656pyxml_SetEndDoctypeDeclHandler(XML_Parser *parser, void *junk)
1657{
1658 pyxml_UpdatePairedHandlers((xmlparseobject *)XML_GetUserData(parser),
1659 StartDoctypeDecl, EndDoctypeDecl,
1660 (pairsetter)XML_SetDoctypeDeclHandler);
1661}
1662
1663#endif
1664
Fred Drake0582df92000-07-12 04:49:00 +00001665statichere struct HandlerInfo handler_info[] = {
1666 {"StartElementHandler",
1667 pyxml_SetStartElementHandler,
1668 (xmlhandler)my_StartElementHandler},
1669 {"EndElementHandler",
1670 pyxml_SetEndElementHandler,
1671 (xmlhandler)my_EndElementHandler},
1672 {"ProcessingInstructionHandler",
1673 (xmlhandlersetter)XML_SetProcessingInstructionHandler,
1674 (xmlhandler)my_ProcessingInstructionHandler},
1675 {"CharacterDataHandler",
1676 (xmlhandlersetter)XML_SetCharacterDataHandler,
1677 (xmlhandler)my_CharacterDataHandler},
1678 {"UnparsedEntityDeclHandler",
1679 (xmlhandlersetter)XML_SetUnparsedEntityDeclHandler,
1680 (xmlhandler)my_UnparsedEntityDeclHandler },
1681 {"NotationDeclHandler",
1682 (xmlhandlersetter)XML_SetNotationDeclHandler,
1683 (xmlhandler)my_NotationDeclHandler },
1684 {"StartNamespaceDeclHandler",
1685 pyxml_SetStartNamespaceDeclHandler,
1686 (xmlhandler)my_StartNamespaceDeclHandler },
1687 {"EndNamespaceDeclHandler",
1688 pyxml_SetEndNamespaceDeclHandler,
1689 (xmlhandler)my_EndNamespaceDeclHandler },
1690 {"CommentHandler",
1691 (xmlhandlersetter)XML_SetCommentHandler,
1692 (xmlhandler)my_CommentHandler},
1693 {"StartCdataSectionHandler",
1694 pyxml_SetStartCdataSection,
1695 (xmlhandler)my_StartCdataSectionHandler},
1696 {"EndCdataSectionHandler",
1697 pyxml_SetEndCdataSection,
1698 (xmlhandler)my_EndCdataSectionHandler},
1699 {"DefaultHandler",
1700 (xmlhandlersetter)XML_SetDefaultHandler,
1701 (xmlhandler)my_DefaultHandler},
1702 {"DefaultHandlerExpand",
1703 (xmlhandlersetter)XML_SetDefaultHandlerExpand,
1704 (xmlhandler)my_DefaultHandlerExpandHandler},
1705 {"NotStandaloneHandler",
1706 (xmlhandlersetter)XML_SetNotStandaloneHandler,
1707 (xmlhandler)my_NotStandaloneHandler},
1708 {"ExternalEntityRefHandler",
1709 (xmlhandlersetter)XML_SetExternalEntityRefHandler,
1710 (xmlhandler)my_ExternalEntityRefHandler },
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001711#if EXPAT_VERSION >= 0x010200
1712 {"StartDoctypeDeclHandler",
1713 pyxml_SetStartDoctypeDeclHandler,
1714 (xmlhandler)my_StartDoctypeDeclHandler},
1715 {"EndDoctypeDeclHandler",
1716 pyxml_SetEndDoctypeDeclHandler,
1717 (xmlhandler)my_EndDoctypeDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001718#endif
1719#if EXPAT_VERSION == 0x010200
Martin v. Löwis0078f6c2001-01-21 10:18:10 +00001720 {"ExternalParsedEntityDeclHandler",
1721 (xmlhandlersetter)XML_SetExternalParsedEntityDeclHandler,
1722 (xmlhandler)my_ExternalParsedEntityDeclHandler},
1723 {"InternalParsedEntityDeclHandler",
1724 (xmlhandlersetter)XML_SetInternalParsedEntityDeclHandler,
1725 (xmlhandler)my_InternalParsedEntityDeclHandler},
Fred Drake85d835f2001-02-08 15:39:08 +00001726#endif
1727#if EXPAT_VERSION >= 0x015f00
1728 {"EntityDeclHandler",
1729 (xmlhandlersetter)XML_SetEntityDeclHandler,
1730 (xmlhandler)my_EntityDeclHandler},
1731 {"XmlDeclHandler",
1732 (xmlhandlersetter)XML_SetXmlDeclHandler,
1733 (xmlhandler)my_XmlDeclHandler},
1734 {"ElementDeclHandler",
1735 (xmlhandlersetter)XML_SetElementDeclHandler,
1736 (xmlhandler)my_ElementDeclHandler},
1737 {"AttlistDeclHandler",
1738 (xmlhandlersetter)XML_SetAttlistDeclHandler,
1739 (xmlhandler)my_AttlistDeclHandler},
1740#endif /* Expat version 1.95 or better */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001741
Fred Drake0582df92000-07-12 04:49:00 +00001742 {NULL, NULL, NULL} /* sentinel */
Andrew M. Kuchlingb7f10532000-03-31 15:43:31 +00001743};