Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ElementTree |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3 | * $Id: _elementtree.c 3473 2009-01-11 22:53:55Z fredrik $ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4 | * |
| 5 | * elementtree accelerator |
| 6 | * |
| 7 | * History: |
| 8 | * 1999-06-20 fl created (as part of sgmlop) |
| 9 | * 2001-05-29 fl effdom edition |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 10 | * 2003-02-27 fl elementtree edition (alpha) |
| 11 | * 2004-06-03 fl updates for elementtree 1.2 |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 12 | * 2005-01-05 fl major optimization effort |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 13 | * 2005-01-11 fl first public release (cElementTree 0.8) |
| 14 | * 2005-01-12 fl split element object into base and extras |
| 15 | * 2005-01-13 fl use tagged pointers for tail/text (cElementTree 0.9) |
| 16 | * 2005-01-17 fl added treebuilder close method |
| 17 | * 2005-01-17 fl fixed crash in getchildren |
| 18 | * 2005-01-18 fl removed observer api, added iterparse (cElementTree 0.9.3) |
| 19 | * 2005-01-23 fl revised iterparse api; added namespace event support (0.9.8) |
| 20 | * 2005-01-26 fl added VERSION module property (cElementTree 1.0) |
| 21 | * 2005-01-28 fl added remove method (1.0.1) |
| 22 | * 2005-03-01 fl added iselement function; fixed makeelement aliasing (1.0.2) |
| 23 | * 2005-03-13 fl export Comment and ProcessingInstruction/PI helpers |
| 24 | * 2005-03-26 fl added Comment and PI support to XMLParser |
| 25 | * 2005-03-27 fl event optimizations; complain about bogus events |
| 26 | * 2005-08-08 fl fixed read error handling in parse |
| 27 | * 2005-08-11 fl added runtime test for copy workaround (1.0.3) |
| 28 | * 2005-12-13 fl added expat_capi support (for xml.etree) (1.0.4) |
| 29 | * 2005-12-16 fl added support for non-standard encodings |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 30 | * 2006-03-08 fl fixed a couple of potential null-refs and leaks |
| 31 | * 2006-03-12 fl merge in 2.5 ssize_t changes |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 32 | * 2007-08-25 fl call custom builder's close method from XMLParser |
| 33 | * 2007-08-31 fl added iter, extend from ET 1.3 |
| 34 | * 2007-09-01 fl fixed ParseError exception, setslice source type, etc |
| 35 | * 2007-09-03 fl fixed handling of negative insert indexes |
| 36 | * 2007-09-04 fl added itertext from ET 1.3 |
| 37 | * 2007-09-06 fl added position attribute to ParseError exception |
| 38 | * 2008-06-06 fl delay error reporting in iterparse (from Hrvoje Niksic) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 39 | * |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 40 | * Copyright (c) 1999-2009 by Secret Labs AB. All rights reserved. |
| 41 | * Copyright (c) 1999-2009 by Fredrik Lundh. |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 42 | * |
| 43 | * info@pythonware.com |
| 44 | * http://www.pythonware.com |
| 45 | */ |
| 46 | |
Fredrik Lundh | 6d52b55 | 2005-12-16 22:06:43 +0000 | [diff] [blame] | 47 | /* Licensed to PSF under a Contributor Agreement. */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 48 | /* See http://www.python.org/psf/license for licensing details. */ |
Fredrik Lundh | 6d52b55 | 2005-12-16 22:06:43 +0000 | [diff] [blame] | 49 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 50 | #include "Python.h" |
| 51 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 52 | #define VERSION "1.0.6" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 53 | |
| 54 | /* -------------------------------------------------------------------- */ |
| 55 | /* configuration */ |
| 56 | |
| 57 | /* Leave defined to include the expat-based XMLParser type */ |
| 58 | #define USE_EXPAT |
| 59 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 60 | /* Define to do all expat calls via pyexpat's embedded expat library */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 61 | /* #define USE_PYEXPAT_CAPI */ |
| 62 | |
| 63 | /* An element can hold this many children without extra memory |
| 64 | allocations. */ |
| 65 | #define STATIC_CHILDREN 4 |
| 66 | |
| 67 | /* For best performance, chose a value so that 80-90% of all nodes |
| 68 | have no more than the given number of children. Set this to zero |
| 69 | to minimize the size of the element structure itself (this only |
| 70 | helps if you have lots of leaf nodes with attributes). */ |
| 71 | |
| 72 | /* Also note that pymalloc always allocates blocks in multiples of |
| 73 | eight bytes. For the current version of cElementTree, this means |
| 74 | that the number of children should be an even number, at least on |
| 75 | 32-bit platforms. */ |
| 76 | |
| 77 | /* -------------------------------------------------------------------- */ |
| 78 | |
| 79 | #if 0 |
| 80 | static int memory = 0; |
| 81 | #define ALLOC(size, comment)\ |
| 82 | do { memory += size; printf("%8d - %s\n", memory, comment); } while (0) |
| 83 | #define RELEASE(size, comment)\ |
| 84 | do { memory -= size; printf("%8d - %s\n", memory, comment); } while (0) |
| 85 | #else |
| 86 | #define ALLOC(size, comment) |
| 87 | #define RELEASE(size, comment) |
| 88 | #endif |
| 89 | |
| 90 | /* compiler tweaks */ |
| 91 | #if defined(_MSC_VER) |
| 92 | #define LOCAL(type) static __inline type __fastcall |
| 93 | #else |
| 94 | #define LOCAL(type) static type |
| 95 | #endif |
| 96 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 97 | /* macros used to store 'join' flags in string object pointers. note |
| 98 | that all use of text and tail as object pointers must be wrapped in |
| 99 | JOIN_OBJ. see comments in the ElementObject definition for more |
| 100 | info. */ |
| 101 | #define JOIN_GET(p) ((Py_uintptr_t) (p) & 1) |
| 102 | #define JOIN_SET(p, flag) ((void*) ((Py_uintptr_t) (JOIN_OBJ(p)) | (flag))) |
| 103 | #define JOIN_OBJ(p) ((PyObject*) ((Py_uintptr_t) (p) & ~1)) |
| 104 | |
| 105 | /* glue functions (see the init function for details) */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 106 | static PyObject* elementtree_parseerror_obj; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 107 | static PyObject* elementtree_deepcopy_obj; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 108 | static PyObject* elementtree_iter_obj; |
| 109 | static PyObject* elementtree_itertext_obj; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 110 | static PyObject* elementpath_obj; |
| 111 | |
| 112 | /* helpers */ |
| 113 | |
| 114 | LOCAL(PyObject*) |
| 115 | deepcopy(PyObject* object, PyObject* memo) |
| 116 | { |
| 117 | /* do a deep copy of the given object */ |
| 118 | |
| 119 | PyObject* args; |
| 120 | PyObject* result; |
| 121 | |
| 122 | if (!elementtree_deepcopy_obj) { |
| 123 | PyErr_SetString( |
| 124 | PyExc_RuntimeError, |
| 125 | "deepcopy helper not found" |
| 126 | ); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | args = PyTuple_New(2); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 131 | if (!args) |
| 132 | return NULL; |
| 133 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 134 | Py_INCREF(object); PyTuple_SET_ITEM(args, 0, (PyObject*) object); |
| 135 | Py_INCREF(memo); PyTuple_SET_ITEM(args, 1, (PyObject*) memo); |
| 136 | |
| 137 | result = PyObject_CallObject(elementtree_deepcopy_obj, args); |
| 138 | |
| 139 | Py_DECREF(args); |
| 140 | |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | LOCAL(PyObject*) |
| 145 | list_join(PyObject* list) |
| 146 | { |
| 147 | /* join list elements (destroying the list in the process) */ |
| 148 | |
| 149 | PyObject* joiner; |
| 150 | PyObject* function; |
| 151 | PyObject* args; |
| 152 | PyObject* result; |
| 153 | |
| 154 | switch (PyList_GET_SIZE(list)) { |
| 155 | case 0: |
| 156 | Py_DECREF(list); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 157 | return PyBytes_FromString(""); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 158 | case 1: |
| 159 | result = PyList_GET_ITEM(list, 0); |
| 160 | Py_INCREF(result); |
| 161 | Py_DECREF(list); |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | /* two or more elements: slice out a suitable separator from the |
| 166 | first member, and use that to join the entire list */ |
| 167 | |
| 168 | joiner = PySequence_GetSlice(PyList_GET_ITEM(list, 0), 0, 0); |
| 169 | if (!joiner) |
| 170 | return NULL; |
| 171 | |
| 172 | function = PyObject_GetAttrString(joiner, "join"); |
| 173 | if (!function) { |
| 174 | Py_DECREF(joiner); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | args = PyTuple_New(1); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 179 | if (!args) |
| 180 | return NULL; |
| 181 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 182 | PyTuple_SET_ITEM(args, 0, list); |
| 183 | |
| 184 | result = PyObject_CallObject(function, args); |
| 185 | |
| 186 | Py_DECREF(args); /* also removes list */ |
| 187 | Py_DECREF(function); |
| 188 | Py_DECREF(joiner); |
| 189 | |
| 190 | return result; |
| 191 | } |
| 192 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 193 | /* -------------------------------------------------------------------- */ |
| 194 | /* the element type */ |
| 195 | |
| 196 | typedef struct { |
| 197 | |
| 198 | /* attributes (a dictionary object), or None if no attributes */ |
| 199 | PyObject* attrib; |
| 200 | |
| 201 | /* child elements */ |
| 202 | int length; /* actual number of items */ |
| 203 | int allocated; /* allocated items */ |
| 204 | |
| 205 | /* this either points to _children or to a malloced buffer */ |
| 206 | PyObject* *children; |
| 207 | |
| 208 | PyObject* _children[STATIC_CHILDREN]; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 209 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 210 | } ElementObjectExtra; |
| 211 | |
| 212 | typedef struct { |
| 213 | PyObject_HEAD |
| 214 | |
| 215 | /* element tag (a string). */ |
| 216 | PyObject* tag; |
| 217 | |
| 218 | /* text before first child. note that this is a tagged pointer; |
| 219 | use JOIN_OBJ to get the object pointer. the join flag is used |
| 220 | to distinguish lists created by the tree builder from lists |
| 221 | assigned to the attribute by application code; the former |
| 222 | should be joined before being returned to the user, the latter |
| 223 | should be left intact. */ |
| 224 | PyObject* text; |
| 225 | |
| 226 | /* text after this element, in parent. note that this is a tagged |
| 227 | pointer; use JOIN_OBJ to get the object pointer. */ |
| 228 | PyObject* tail; |
| 229 | |
| 230 | ElementObjectExtra* extra; |
| 231 | |
| 232 | } ElementObject; |
| 233 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 234 | static PyTypeObject Element_Type; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 235 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 236 | #define Element_CheckExact(op) (Py_TYPE(op) == &Element_Type) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 237 | |
| 238 | /* -------------------------------------------------------------------- */ |
| 239 | /* element constructor and destructor */ |
| 240 | |
| 241 | LOCAL(int) |
| 242 | element_new_extra(ElementObject* self, PyObject* attrib) |
| 243 | { |
| 244 | self->extra = PyObject_Malloc(sizeof(ElementObjectExtra)); |
| 245 | if (!self->extra) |
| 246 | return -1; |
| 247 | |
| 248 | if (!attrib) |
| 249 | attrib = Py_None; |
| 250 | |
| 251 | Py_INCREF(attrib); |
| 252 | self->extra->attrib = attrib; |
| 253 | |
| 254 | self->extra->length = 0; |
| 255 | self->extra->allocated = STATIC_CHILDREN; |
| 256 | self->extra->children = self->extra->_children; |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | LOCAL(void) |
| 262 | element_dealloc_extra(ElementObject* self) |
| 263 | { |
| 264 | int i; |
| 265 | |
| 266 | Py_DECREF(self->extra->attrib); |
| 267 | |
| 268 | for (i = 0; i < self->extra->length; i++) |
| 269 | Py_DECREF(self->extra->children[i]); |
| 270 | |
| 271 | if (self->extra->children != self->extra->_children) |
| 272 | PyObject_Free(self->extra->children); |
| 273 | |
| 274 | PyObject_Free(self->extra); |
| 275 | } |
| 276 | |
| 277 | LOCAL(PyObject*) |
| 278 | element_new(PyObject* tag, PyObject* attrib) |
| 279 | { |
| 280 | ElementObject* self; |
| 281 | |
| 282 | self = PyObject_New(ElementObject, &Element_Type); |
| 283 | if (self == NULL) |
| 284 | return NULL; |
| 285 | |
| 286 | /* use None for empty dictionaries */ |
| 287 | if (PyDict_CheckExact(attrib) && !PyDict_Size(attrib)) |
| 288 | attrib = Py_None; |
| 289 | |
| 290 | self->extra = NULL; |
| 291 | |
| 292 | if (attrib != Py_None) { |
| 293 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 294 | if (element_new_extra(self, attrib) < 0) { |
| 295 | PyObject_Del(self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 296 | return NULL; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 297 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 298 | |
| 299 | self->extra->length = 0; |
| 300 | self->extra->allocated = STATIC_CHILDREN; |
| 301 | self->extra->children = self->extra->_children; |
| 302 | |
| 303 | } |
| 304 | |
| 305 | Py_INCREF(tag); |
| 306 | self->tag = tag; |
| 307 | |
| 308 | Py_INCREF(Py_None); |
| 309 | self->text = Py_None; |
| 310 | |
| 311 | Py_INCREF(Py_None); |
| 312 | self->tail = Py_None; |
| 313 | |
| 314 | ALLOC(sizeof(ElementObject), "create element"); |
| 315 | |
| 316 | return (PyObject*) self; |
| 317 | } |
| 318 | |
| 319 | LOCAL(int) |
| 320 | element_resize(ElementObject* self, int extra) |
| 321 | { |
| 322 | int size; |
| 323 | PyObject* *children; |
| 324 | |
| 325 | /* make sure self->children can hold the given number of extra |
| 326 | elements. set an exception and return -1 if allocation failed */ |
| 327 | |
| 328 | if (!self->extra) |
| 329 | element_new_extra(self, NULL); |
| 330 | |
| 331 | size = self->extra->length + extra; |
| 332 | |
| 333 | if (size > self->extra->allocated) { |
| 334 | /* use Python 2.4's list growth strategy */ |
| 335 | size = (size >> 3) + (size < 9 ? 3 : 6) + size; |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 336 | /* Coverity CID #182 size_error: Allocating 1 bytes to pointer "children" |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 337 | * which needs at least 4 bytes. |
| 338 | * Although it's a false alarm always assume at least one child to |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 339 | * be safe. |
| 340 | */ |
| 341 | size = size ? size : 1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 342 | if (self->extra->children != self->extra->_children) { |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 343 | /* Coverity CID #182 size_error: Allocating 1 bytes to pointer |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 344 | * "children", which needs at least 4 bytes. Although it's a |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 345 | * false alarm always assume at least one child to be safe. |
| 346 | */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 347 | children = PyObject_Realloc(self->extra->children, |
| 348 | size * sizeof(PyObject*)); |
| 349 | if (!children) |
| 350 | goto nomemory; |
| 351 | } else { |
| 352 | children = PyObject_Malloc(size * sizeof(PyObject*)); |
| 353 | if (!children) |
| 354 | goto nomemory; |
| 355 | /* copy existing children from static area to malloc buffer */ |
| 356 | memcpy(children, self->extra->children, |
| 357 | self->extra->length * sizeof(PyObject*)); |
| 358 | } |
| 359 | self->extra->children = children; |
| 360 | self->extra->allocated = size; |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | |
| 365 | nomemory: |
| 366 | PyErr_NoMemory(); |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | LOCAL(int) |
| 371 | element_add_subelement(ElementObject* self, PyObject* element) |
| 372 | { |
| 373 | /* add a child element to a parent */ |
| 374 | |
| 375 | if (element_resize(self, 1) < 0) |
| 376 | return -1; |
| 377 | |
| 378 | Py_INCREF(element); |
| 379 | self->extra->children[self->extra->length] = element; |
| 380 | |
| 381 | self->extra->length++; |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | LOCAL(PyObject*) |
| 387 | element_get_attrib(ElementObject* self) |
| 388 | { |
| 389 | /* return borrowed reference to attrib dictionary */ |
| 390 | /* note: this function assumes that the extra section exists */ |
| 391 | |
| 392 | PyObject* res = self->extra->attrib; |
| 393 | |
| 394 | if (res == Py_None) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 395 | Py_DECREF(res); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 396 | /* create missing dictionary */ |
| 397 | res = PyDict_New(); |
| 398 | if (!res) |
| 399 | return NULL; |
| 400 | self->extra->attrib = res; |
| 401 | } |
| 402 | |
| 403 | return res; |
| 404 | } |
| 405 | |
| 406 | LOCAL(PyObject*) |
| 407 | element_get_text(ElementObject* self) |
| 408 | { |
| 409 | /* return borrowed reference to text attribute */ |
| 410 | |
| 411 | PyObject* res = self->text; |
| 412 | |
| 413 | if (JOIN_GET(res)) { |
| 414 | res = JOIN_OBJ(res); |
| 415 | if (PyList_CheckExact(res)) { |
| 416 | res = list_join(res); |
| 417 | if (!res) |
| 418 | return NULL; |
| 419 | self->text = res; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return res; |
| 424 | } |
| 425 | |
| 426 | LOCAL(PyObject*) |
| 427 | element_get_tail(ElementObject* self) |
| 428 | { |
| 429 | /* return borrowed reference to text attribute */ |
| 430 | |
| 431 | PyObject* res = self->tail; |
| 432 | |
| 433 | if (JOIN_GET(res)) { |
| 434 | res = JOIN_OBJ(res); |
| 435 | if (PyList_CheckExact(res)) { |
| 436 | res = list_join(res); |
| 437 | if (!res) |
| 438 | return NULL; |
| 439 | self->tail = res; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | return res; |
| 444 | } |
| 445 | |
| 446 | static PyObject* |
| 447 | element(PyObject* self, PyObject* args, PyObject* kw) |
| 448 | { |
| 449 | PyObject* elem; |
| 450 | |
| 451 | PyObject* tag; |
| 452 | PyObject* attrib = NULL; |
| 453 | if (!PyArg_ParseTuple(args, "O|O!:Element", &tag, |
| 454 | &PyDict_Type, &attrib)) |
| 455 | return NULL; |
| 456 | |
| 457 | if (attrib || kw) { |
| 458 | attrib = (attrib) ? PyDict_Copy(attrib) : PyDict_New(); |
| 459 | if (!attrib) |
| 460 | return NULL; |
| 461 | if (kw) |
| 462 | PyDict_Update(attrib, kw); |
| 463 | } else { |
| 464 | Py_INCREF(Py_None); |
| 465 | attrib = Py_None; |
| 466 | } |
| 467 | |
| 468 | elem = element_new(tag, attrib); |
| 469 | |
| 470 | Py_DECREF(attrib); |
| 471 | |
| 472 | return elem; |
| 473 | } |
| 474 | |
| 475 | static PyObject* |
| 476 | subelement(PyObject* self, PyObject* args, PyObject* kw) |
| 477 | { |
| 478 | PyObject* elem; |
| 479 | |
| 480 | ElementObject* parent; |
| 481 | PyObject* tag; |
| 482 | PyObject* attrib = NULL; |
| 483 | if (!PyArg_ParseTuple(args, "O!O|O!:SubElement", |
| 484 | &Element_Type, &parent, &tag, |
| 485 | &PyDict_Type, &attrib)) |
| 486 | return NULL; |
| 487 | |
| 488 | if (attrib || kw) { |
| 489 | attrib = (attrib) ? PyDict_Copy(attrib) : PyDict_New(); |
| 490 | if (!attrib) |
| 491 | return NULL; |
| 492 | if (kw) |
| 493 | PyDict_Update(attrib, kw); |
| 494 | } else { |
| 495 | Py_INCREF(Py_None); |
| 496 | attrib = Py_None; |
| 497 | } |
| 498 | |
| 499 | elem = element_new(tag, attrib); |
| 500 | |
| 501 | Py_DECREF(attrib); |
| 502 | |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 503 | if (element_add_subelement(parent, elem) < 0) { |
| 504 | Py_DECREF(elem); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 505 | return NULL; |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 506 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 507 | |
| 508 | return elem; |
| 509 | } |
| 510 | |
| 511 | static void |
| 512 | element_dealloc(ElementObject* self) |
| 513 | { |
| 514 | if (self->extra) |
| 515 | element_dealloc_extra(self); |
| 516 | |
| 517 | /* discard attributes */ |
| 518 | Py_DECREF(self->tag); |
| 519 | Py_DECREF(JOIN_OBJ(self->text)); |
| 520 | Py_DECREF(JOIN_OBJ(self->tail)); |
| 521 | |
| 522 | RELEASE(sizeof(ElementObject), "destroy element"); |
| 523 | |
| 524 | PyObject_Del(self); |
| 525 | } |
| 526 | |
| 527 | /* -------------------------------------------------------------------- */ |
| 528 | /* methods (in alphabetical order) */ |
| 529 | |
| 530 | static PyObject* |
| 531 | element_append(ElementObject* self, PyObject* args) |
| 532 | { |
| 533 | PyObject* element; |
| 534 | if (!PyArg_ParseTuple(args, "O!:append", &Element_Type, &element)) |
| 535 | return NULL; |
| 536 | |
| 537 | if (element_add_subelement(self, element) < 0) |
| 538 | return NULL; |
| 539 | |
| 540 | Py_RETURN_NONE; |
| 541 | } |
| 542 | |
| 543 | static PyObject* |
| 544 | element_clear(ElementObject* self, PyObject* args) |
| 545 | { |
| 546 | if (!PyArg_ParseTuple(args, ":clear")) |
| 547 | return NULL; |
| 548 | |
| 549 | if (self->extra) { |
| 550 | element_dealloc_extra(self); |
| 551 | self->extra = NULL; |
| 552 | } |
| 553 | |
| 554 | Py_INCREF(Py_None); |
| 555 | Py_DECREF(JOIN_OBJ(self->text)); |
| 556 | self->text = Py_None; |
| 557 | |
| 558 | Py_INCREF(Py_None); |
| 559 | Py_DECREF(JOIN_OBJ(self->tail)); |
| 560 | self->tail = Py_None; |
| 561 | |
| 562 | Py_RETURN_NONE; |
| 563 | } |
| 564 | |
| 565 | static PyObject* |
| 566 | element_copy(ElementObject* self, PyObject* args) |
| 567 | { |
| 568 | int i; |
| 569 | ElementObject* element; |
| 570 | |
| 571 | if (!PyArg_ParseTuple(args, ":__copy__")) |
| 572 | return NULL; |
| 573 | |
| 574 | element = (ElementObject*) element_new( |
| 575 | self->tag, (self->extra) ? self->extra->attrib : Py_None |
| 576 | ); |
| 577 | if (!element) |
| 578 | return NULL; |
| 579 | |
| 580 | Py_DECREF(JOIN_OBJ(element->text)); |
| 581 | element->text = self->text; |
| 582 | Py_INCREF(JOIN_OBJ(element->text)); |
| 583 | |
| 584 | Py_DECREF(JOIN_OBJ(element->tail)); |
| 585 | element->tail = self->tail; |
| 586 | Py_INCREF(JOIN_OBJ(element->tail)); |
| 587 | |
| 588 | if (self->extra) { |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 589 | |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 590 | if (element_resize(element, self->extra->length) < 0) { |
| 591 | Py_DECREF(element); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 592 | return NULL; |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 593 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 594 | |
| 595 | for (i = 0; i < self->extra->length; i++) { |
| 596 | Py_INCREF(self->extra->children[i]); |
| 597 | element->extra->children[i] = self->extra->children[i]; |
| 598 | } |
| 599 | |
| 600 | element->extra->length = self->extra->length; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 601 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | return (PyObject*) element; |
| 605 | } |
| 606 | |
| 607 | static PyObject* |
| 608 | element_deepcopy(ElementObject* self, PyObject* args) |
| 609 | { |
| 610 | int i; |
| 611 | ElementObject* element; |
| 612 | PyObject* tag; |
| 613 | PyObject* attrib; |
| 614 | PyObject* text; |
| 615 | PyObject* tail; |
| 616 | PyObject* id; |
| 617 | |
| 618 | PyObject* memo; |
| 619 | if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo)) |
| 620 | return NULL; |
| 621 | |
| 622 | tag = deepcopy(self->tag, memo); |
| 623 | if (!tag) |
| 624 | return NULL; |
| 625 | |
| 626 | if (self->extra) { |
| 627 | attrib = deepcopy(self->extra->attrib, memo); |
| 628 | if (!attrib) { |
| 629 | Py_DECREF(tag); |
| 630 | return NULL; |
| 631 | } |
| 632 | } else { |
| 633 | Py_INCREF(Py_None); |
| 634 | attrib = Py_None; |
| 635 | } |
| 636 | |
| 637 | element = (ElementObject*) element_new(tag, attrib); |
| 638 | |
| 639 | Py_DECREF(tag); |
| 640 | Py_DECREF(attrib); |
| 641 | |
| 642 | if (!element) |
| 643 | return NULL; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 644 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 645 | text = deepcopy(JOIN_OBJ(self->text), memo); |
| 646 | if (!text) |
| 647 | goto error; |
| 648 | Py_DECREF(element->text); |
| 649 | element->text = JOIN_SET(text, JOIN_GET(self->text)); |
| 650 | |
| 651 | tail = deepcopy(JOIN_OBJ(self->tail), memo); |
| 652 | if (!tail) |
| 653 | goto error; |
| 654 | Py_DECREF(element->tail); |
| 655 | element->tail = JOIN_SET(tail, JOIN_GET(self->tail)); |
| 656 | |
| 657 | if (self->extra) { |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 658 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 659 | if (element_resize(element, self->extra->length) < 0) |
| 660 | goto error; |
| 661 | |
| 662 | for (i = 0; i < self->extra->length; i++) { |
| 663 | PyObject* child = deepcopy(self->extra->children[i], memo); |
| 664 | if (!child) { |
| 665 | element->extra->length = i; |
| 666 | goto error; |
| 667 | } |
| 668 | element->extra->children[i] = child; |
| 669 | } |
| 670 | |
| 671 | element->extra->length = self->extra->length; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 672 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* add object to memo dictionary (so deepcopy won't visit it again) */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 676 | id = PyLong_FromLong((Py_uintptr_t) self); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 677 | if (!id) |
| 678 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 679 | |
| 680 | i = PyDict_SetItem(memo, id, (PyObject*) element); |
| 681 | |
| 682 | Py_DECREF(id); |
| 683 | |
| 684 | if (i < 0) |
| 685 | goto error; |
| 686 | |
| 687 | return (PyObject*) element; |
| 688 | |
| 689 | error: |
| 690 | Py_DECREF(element); |
| 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | LOCAL(int) |
| 695 | checkpath(PyObject* tag) |
| 696 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 697 | Py_ssize_t i; |
| 698 | int check = 1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 699 | |
| 700 | /* check if a tag contains an xpath character */ |
| 701 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 702 | #define PATHCHAR(ch) \ |
| 703 | (ch == '/' || ch == '*' || ch == '[' || ch == '@' || ch == '.') |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 704 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 705 | if (PyUnicode_Check(tag)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 706 | const Py_ssize_t len = PyUnicode_GET_LENGTH(tag); |
| 707 | void *data = PyUnicode_DATA(tag); |
| 708 | unsigned int kind = PyUnicode_KIND(tag); |
| 709 | for (i = 0; i < len; i++) { |
| 710 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 711 | if (ch == '{') |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 712 | check = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 713 | else if (ch == '}') |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 714 | check = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 715 | else if (check && PATHCHAR(ch)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 716 | return 1; |
| 717 | } |
| 718 | return 0; |
| 719 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 720 | if (PyBytes_Check(tag)) { |
| 721 | char *p = PyBytes_AS_STRING(tag); |
| 722 | for (i = 0; i < PyBytes_GET_SIZE(tag); i++) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 723 | if (p[i] == '{') |
| 724 | check = 0; |
| 725 | else if (p[i] == '}') |
| 726 | check = 1; |
| 727 | else if (check && PATHCHAR(p[i])) |
| 728 | return 1; |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | return 1; /* unknown type; might be path expression */ |
| 734 | } |
| 735 | |
| 736 | static PyObject* |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 737 | element_extend(ElementObject* self, PyObject* args) |
| 738 | { |
| 739 | PyObject* seq; |
| 740 | Py_ssize_t i, seqlen = 0; |
| 741 | |
| 742 | PyObject* seq_in; |
| 743 | if (!PyArg_ParseTuple(args, "O:extend", &seq_in)) |
| 744 | return NULL; |
| 745 | |
| 746 | seq = PySequence_Fast(seq_in, ""); |
| 747 | if (!seq) { |
| 748 | PyErr_Format( |
| 749 | PyExc_TypeError, |
| 750 | "expected sequence, not \"%.200s\"", Py_TYPE(seq_in)->tp_name |
| 751 | ); |
| 752 | return NULL; |
| 753 | } |
| 754 | |
| 755 | seqlen = PySequence_Size(seq); |
| 756 | for (i = 0; i < seqlen; i++) { |
| 757 | PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
| 758 | if (element_add_subelement(self, element) < 0) { |
| 759 | Py_DECREF(seq); |
| 760 | return NULL; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | Py_DECREF(seq); |
| 765 | |
| 766 | Py_RETURN_NONE; |
| 767 | } |
| 768 | |
| 769 | static PyObject* |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 770 | element_find(ElementObject* self, PyObject* args) |
| 771 | { |
| 772 | int i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 773 | PyObject* tag; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 774 | PyObject* namespaces = Py_None; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 775 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 776 | if (!PyArg_ParseTuple(args, "O|O:find", &tag, &namespaces)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 777 | return NULL; |
| 778 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 779 | if (checkpath(tag) || namespaces != Py_None) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 780 | _Py_IDENTIFIER(find); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 781 | return _PyObject_CallMethodId( |
| 782 | elementpath_obj, &PyId_find, "OOO", self, tag, namespaces |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 783 | ); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 784 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 785 | |
| 786 | if (!self->extra) |
| 787 | Py_RETURN_NONE; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 788 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 789 | for (i = 0; i < self->extra->length; i++) { |
| 790 | PyObject* item = self->extra->children[i]; |
| 791 | if (Element_CheckExact(item) && |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 792 | PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ) == 1) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 793 | Py_INCREF(item); |
| 794 | return item; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | Py_RETURN_NONE; |
| 799 | } |
| 800 | |
| 801 | static PyObject* |
| 802 | element_findtext(ElementObject* self, PyObject* args) |
| 803 | { |
| 804 | int i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 805 | PyObject* tag; |
| 806 | PyObject* default_value = Py_None; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 807 | PyObject* namespaces = Py_None; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 808 | _Py_IDENTIFIER(findtext); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 809 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 810 | if (!PyArg_ParseTuple(args, "O|OO:findtext", &tag, &default_value, &namespaces)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 811 | return NULL; |
| 812 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 813 | if (checkpath(tag) || namespaces != Py_None) |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 814 | return _PyObject_CallMethodId( |
| 815 | elementpath_obj, &PyId_findtext, "OOOO", self, tag, default_value, namespaces |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 816 | ); |
| 817 | |
| 818 | if (!self->extra) { |
| 819 | Py_INCREF(default_value); |
| 820 | return default_value; |
| 821 | } |
| 822 | |
| 823 | for (i = 0; i < self->extra->length; i++) { |
| 824 | ElementObject* item = (ElementObject*) self->extra->children[i]; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 825 | if (Element_CheckExact(item) && (PyObject_RichCompareBool(item->tag, tag, Py_EQ) == 1)) { |
| 826 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 827 | PyObject* text = element_get_text(item); |
| 828 | if (text == Py_None) |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 829 | return PyBytes_FromString(""); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 830 | Py_XINCREF(text); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 831 | return text; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | Py_INCREF(default_value); |
| 836 | return default_value; |
| 837 | } |
| 838 | |
| 839 | static PyObject* |
| 840 | element_findall(ElementObject* self, PyObject* args) |
| 841 | { |
| 842 | int i; |
| 843 | PyObject* out; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 844 | PyObject* tag; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 845 | PyObject* namespaces = Py_None; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 846 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 847 | if (!PyArg_ParseTuple(args, "O|O:findall", &tag, &namespaces)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 848 | return NULL; |
| 849 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 850 | if (checkpath(tag) || namespaces != Py_None) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 851 | _Py_IDENTIFIER(findall); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 852 | return _PyObject_CallMethodId( |
| 853 | elementpath_obj, &PyId_findall, "OOO", self, tag, namespaces |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 854 | ); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 855 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 856 | |
| 857 | out = PyList_New(0); |
| 858 | if (!out) |
| 859 | return NULL; |
| 860 | |
| 861 | if (!self->extra) |
| 862 | return out; |
| 863 | |
| 864 | for (i = 0; i < self->extra->length; i++) { |
| 865 | PyObject* item = self->extra->children[i]; |
| 866 | if (Element_CheckExact(item) && |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 867 | PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ) == 1) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 868 | if (PyList_Append(out, item) < 0) { |
| 869 | Py_DECREF(out); |
| 870 | return NULL; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | return out; |
| 876 | } |
| 877 | |
| 878 | static PyObject* |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 879 | element_iterfind(ElementObject* self, PyObject* args) |
| 880 | { |
| 881 | PyObject* tag; |
| 882 | PyObject* namespaces = Py_None; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 883 | _Py_IDENTIFIER(iterfind); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 884 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 885 | if (!PyArg_ParseTuple(args, "O|O:iterfind", &tag, &namespaces)) |
| 886 | return NULL; |
| 887 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 888 | return _PyObject_CallMethodId( |
| 889 | elementpath_obj, &PyId_iterfind, "OOO", self, tag, namespaces |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 890 | ); |
| 891 | } |
| 892 | |
| 893 | static PyObject* |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 894 | element_get(ElementObject* self, PyObject* args) |
| 895 | { |
| 896 | PyObject* value; |
| 897 | |
| 898 | PyObject* key; |
| 899 | PyObject* default_value = Py_None; |
| 900 | if (!PyArg_ParseTuple(args, "O|O:get", &key, &default_value)) |
| 901 | return NULL; |
| 902 | |
| 903 | if (!self->extra || self->extra->attrib == Py_None) |
| 904 | value = default_value; |
| 905 | else { |
| 906 | value = PyDict_GetItem(self->extra->attrib, key); |
| 907 | if (!value) |
| 908 | value = default_value; |
| 909 | } |
| 910 | |
| 911 | Py_INCREF(value); |
| 912 | return value; |
| 913 | } |
| 914 | |
| 915 | static PyObject* |
| 916 | element_getchildren(ElementObject* self, PyObject* args) |
| 917 | { |
| 918 | int i; |
| 919 | PyObject* list; |
| 920 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 921 | /* FIXME: report as deprecated? */ |
| 922 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 923 | if (!PyArg_ParseTuple(args, ":getchildren")) |
| 924 | return NULL; |
| 925 | |
| 926 | if (!self->extra) |
| 927 | return PyList_New(0); |
| 928 | |
| 929 | list = PyList_New(self->extra->length); |
| 930 | if (!list) |
| 931 | return NULL; |
| 932 | |
| 933 | for (i = 0; i < self->extra->length; i++) { |
| 934 | PyObject* item = self->extra->children[i]; |
| 935 | Py_INCREF(item); |
| 936 | PyList_SET_ITEM(list, i, item); |
| 937 | } |
| 938 | |
| 939 | return list; |
| 940 | } |
| 941 | |
| 942 | static PyObject* |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 943 | element_iter(ElementObject* self, PyObject* args) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 944 | { |
| 945 | PyObject* result; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 946 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 947 | PyObject* tag = Py_None; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 948 | if (!PyArg_ParseTuple(args, "|O:iter", &tag)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 949 | return NULL; |
| 950 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 951 | if (!elementtree_iter_obj) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 952 | PyErr_SetString( |
| 953 | PyExc_RuntimeError, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 954 | "iter helper not found" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 955 | ); |
| 956 | return NULL; |
| 957 | } |
| 958 | |
| 959 | args = PyTuple_New(2); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 960 | if (!args) |
| 961 | return NULL; |
Neal Norwitz | 02876df | 2006-02-07 06:58:52 +0000 | [diff] [blame] | 962 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 963 | Py_INCREF(self); PyTuple_SET_ITEM(args, 0, (PyObject*) self); |
| 964 | Py_INCREF(tag); PyTuple_SET_ITEM(args, 1, (PyObject*) tag); |
| 965 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 966 | result = PyObject_CallObject(elementtree_iter_obj, args); |
| 967 | |
| 968 | Py_DECREF(args); |
| 969 | |
| 970 | return result; |
| 971 | } |
| 972 | |
| 973 | |
| 974 | static PyObject* |
| 975 | element_itertext(ElementObject* self, PyObject* args) |
| 976 | { |
| 977 | PyObject* result; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 978 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 979 | if (!PyArg_ParseTuple(args, ":itertext")) |
| 980 | return NULL; |
| 981 | |
| 982 | if (!elementtree_itertext_obj) { |
| 983 | PyErr_SetString( |
| 984 | PyExc_RuntimeError, |
| 985 | "itertext helper not found" |
| 986 | ); |
| 987 | return NULL; |
| 988 | } |
| 989 | |
| 990 | args = PyTuple_New(1); |
| 991 | if (!args) |
| 992 | return NULL; |
| 993 | |
| 994 | Py_INCREF(self); PyTuple_SET_ITEM(args, 0, (PyObject*) self); |
| 995 | |
| 996 | result = PyObject_CallObject(elementtree_itertext_obj, args); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 997 | |
| 998 | Py_DECREF(args); |
| 999 | |
| 1000 | return result; |
| 1001 | } |
| 1002 | |
| 1003 | static PyObject* |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1004 | element_getitem(PyObject* self_, Py_ssize_t index) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1005 | { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1006 | ElementObject* self = (ElementObject*) self_; |
| 1007 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1008 | if (!self->extra || index < 0 || index >= self->extra->length) { |
| 1009 | PyErr_SetString( |
| 1010 | PyExc_IndexError, |
| 1011 | "child index out of range" |
| 1012 | ); |
| 1013 | return NULL; |
| 1014 | } |
| 1015 | |
| 1016 | Py_INCREF(self->extra->children[index]); |
| 1017 | return self->extra->children[index]; |
| 1018 | } |
| 1019 | |
| 1020 | static PyObject* |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1021 | element_insert(ElementObject* self, PyObject* args) |
| 1022 | { |
| 1023 | int i; |
| 1024 | |
| 1025 | int index; |
| 1026 | PyObject* element; |
| 1027 | if (!PyArg_ParseTuple(args, "iO!:insert", &index, |
| 1028 | &Element_Type, &element)) |
| 1029 | return NULL; |
| 1030 | |
| 1031 | if (!self->extra) |
| 1032 | element_new_extra(self, NULL); |
| 1033 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1034 | if (index < 0) { |
| 1035 | index += self->extra->length; |
| 1036 | if (index < 0) |
| 1037 | index = 0; |
| 1038 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1039 | if (index > self->extra->length) |
| 1040 | index = self->extra->length; |
| 1041 | |
| 1042 | if (element_resize(self, 1) < 0) |
| 1043 | return NULL; |
| 1044 | |
| 1045 | for (i = self->extra->length; i > index; i--) |
| 1046 | self->extra->children[i] = self->extra->children[i-1]; |
| 1047 | |
| 1048 | Py_INCREF(element); |
| 1049 | self->extra->children[index] = element; |
| 1050 | |
| 1051 | self->extra->length++; |
| 1052 | |
| 1053 | Py_RETURN_NONE; |
| 1054 | } |
| 1055 | |
| 1056 | static PyObject* |
| 1057 | element_items(ElementObject* self, PyObject* args) |
| 1058 | { |
| 1059 | if (!PyArg_ParseTuple(args, ":items")) |
| 1060 | return NULL; |
| 1061 | |
| 1062 | if (!self->extra || self->extra->attrib == Py_None) |
| 1063 | return PyList_New(0); |
| 1064 | |
| 1065 | return PyDict_Items(self->extra->attrib); |
| 1066 | } |
| 1067 | |
| 1068 | static PyObject* |
| 1069 | element_keys(ElementObject* self, PyObject* args) |
| 1070 | { |
| 1071 | if (!PyArg_ParseTuple(args, ":keys")) |
| 1072 | return NULL; |
| 1073 | |
| 1074 | if (!self->extra || self->extra->attrib == Py_None) |
| 1075 | return PyList_New(0); |
| 1076 | |
| 1077 | return PyDict_Keys(self->extra->attrib); |
| 1078 | } |
| 1079 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1080 | static Py_ssize_t |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1081 | element_length(ElementObject* self) |
| 1082 | { |
| 1083 | if (!self->extra) |
| 1084 | return 0; |
| 1085 | |
| 1086 | return self->extra->length; |
| 1087 | } |
| 1088 | |
| 1089 | static PyObject* |
| 1090 | element_makeelement(PyObject* self, PyObject* args, PyObject* kw) |
| 1091 | { |
| 1092 | PyObject* elem; |
| 1093 | |
| 1094 | PyObject* tag; |
| 1095 | PyObject* attrib; |
| 1096 | if (!PyArg_ParseTuple(args, "OO:makeelement", &tag, &attrib)) |
| 1097 | return NULL; |
| 1098 | |
| 1099 | attrib = PyDict_Copy(attrib); |
| 1100 | if (!attrib) |
| 1101 | return NULL; |
| 1102 | |
| 1103 | elem = element_new(tag, attrib); |
| 1104 | |
| 1105 | Py_DECREF(attrib); |
| 1106 | |
| 1107 | return elem; |
| 1108 | } |
| 1109 | |
| 1110 | static PyObject* |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1111 | element_remove(ElementObject* self, PyObject* args) |
| 1112 | { |
| 1113 | int i; |
| 1114 | |
| 1115 | PyObject* element; |
| 1116 | if (!PyArg_ParseTuple(args, "O!:remove", &Element_Type, &element)) |
| 1117 | return NULL; |
| 1118 | |
| 1119 | if (!self->extra) { |
| 1120 | /* element has no children, so raise exception */ |
| 1121 | PyErr_SetString( |
| 1122 | PyExc_ValueError, |
| 1123 | "list.remove(x): x not in list" |
| 1124 | ); |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | |
| 1128 | for (i = 0; i < self->extra->length; i++) { |
| 1129 | if (self->extra->children[i] == element) |
| 1130 | break; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 1131 | if (PyObject_RichCompareBool(self->extra->children[i], element, Py_EQ) == 1) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1132 | break; |
| 1133 | } |
| 1134 | |
| 1135 | if (i == self->extra->length) { |
| 1136 | /* element is not in children, so raise exception */ |
| 1137 | PyErr_SetString( |
| 1138 | PyExc_ValueError, |
| 1139 | "list.remove(x): x not in list" |
| 1140 | ); |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | |
| 1144 | Py_DECREF(self->extra->children[i]); |
| 1145 | |
| 1146 | self->extra->length--; |
| 1147 | |
| 1148 | for (; i < self->extra->length; i++) |
| 1149 | self->extra->children[i] = self->extra->children[i+1]; |
| 1150 | |
| 1151 | Py_RETURN_NONE; |
| 1152 | } |
| 1153 | |
| 1154 | static PyObject* |
| 1155 | element_repr(ElementObject* self) |
| 1156 | { |
Walter Dörwald | 7569dfe | 2007-05-19 21:49:49 +0000 | [diff] [blame] | 1157 | return PyUnicode_FromFormat("<Element %R at %p>", self->tag, self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | static PyObject* |
| 1161 | element_set(ElementObject* self, PyObject* args) |
| 1162 | { |
| 1163 | PyObject* attrib; |
| 1164 | |
| 1165 | PyObject* key; |
| 1166 | PyObject* value; |
| 1167 | if (!PyArg_ParseTuple(args, "OO:set", &key, &value)) |
| 1168 | return NULL; |
| 1169 | |
| 1170 | if (!self->extra) |
| 1171 | element_new_extra(self, NULL); |
| 1172 | |
| 1173 | attrib = element_get_attrib(self); |
| 1174 | if (!attrib) |
| 1175 | return NULL; |
| 1176 | |
| 1177 | if (PyDict_SetItem(attrib, key, value) < 0) |
| 1178 | return NULL; |
| 1179 | |
| 1180 | Py_RETURN_NONE; |
| 1181 | } |
| 1182 | |
| 1183 | static int |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1184 | element_setitem(PyObject* self_, Py_ssize_t index, PyObject* item) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1185 | { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1186 | ElementObject* self = (ElementObject*) self_; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1187 | int i; |
| 1188 | PyObject* old; |
| 1189 | |
| 1190 | if (!self->extra || index < 0 || index >= self->extra->length) { |
| 1191 | PyErr_SetString( |
| 1192 | PyExc_IndexError, |
| 1193 | "child assignment index out of range"); |
| 1194 | return -1; |
| 1195 | } |
| 1196 | |
| 1197 | old = self->extra->children[index]; |
| 1198 | |
| 1199 | if (item) { |
| 1200 | Py_INCREF(item); |
| 1201 | self->extra->children[index] = item; |
| 1202 | } else { |
| 1203 | self->extra->length--; |
| 1204 | for (i = index; i < self->extra->length; i++) |
| 1205 | self->extra->children[i] = self->extra->children[i+1]; |
| 1206 | } |
| 1207 | |
| 1208 | Py_DECREF(old); |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1213 | static PyObject* |
| 1214 | element_subscr(PyObject* self_, PyObject* item) |
| 1215 | { |
| 1216 | ElementObject* self = (ElementObject*) self_; |
| 1217 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1218 | if (PyIndex_Check(item)) { |
| 1219 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1220 | |
| 1221 | if (i == -1 && PyErr_Occurred()) { |
| 1222 | return NULL; |
| 1223 | } |
| 1224 | if (i < 0 && self->extra) |
| 1225 | i += self->extra->length; |
| 1226 | return element_getitem(self_, i); |
| 1227 | } |
| 1228 | else if (PySlice_Check(item)) { |
| 1229 | Py_ssize_t start, stop, step, slicelen, cur, i; |
| 1230 | PyObject* list; |
| 1231 | |
| 1232 | if (!self->extra) |
| 1233 | return PyList_New(0); |
| 1234 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1235 | if (PySlice_GetIndicesEx(item, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1236 | self->extra->length, |
| 1237 | &start, &stop, &step, &slicelen) < 0) { |
| 1238 | return NULL; |
| 1239 | } |
| 1240 | |
| 1241 | if (slicelen <= 0) |
| 1242 | return PyList_New(0); |
| 1243 | else { |
| 1244 | list = PyList_New(slicelen); |
| 1245 | if (!list) |
| 1246 | return NULL; |
| 1247 | |
| 1248 | for (cur = start, i = 0; i < slicelen; |
| 1249 | cur += step, i++) { |
| 1250 | PyObject* item = self->extra->children[cur]; |
| 1251 | Py_INCREF(item); |
| 1252 | PyList_SET_ITEM(list, i, item); |
| 1253 | } |
| 1254 | |
| 1255 | return list; |
| 1256 | } |
| 1257 | } |
| 1258 | else { |
| 1259 | PyErr_SetString(PyExc_TypeError, |
| 1260 | "element indices must be integers"); |
| 1261 | return NULL; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | static int |
| 1266 | element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) |
| 1267 | { |
| 1268 | ElementObject* self = (ElementObject*) self_; |
| 1269 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1270 | if (PyIndex_Check(item)) { |
| 1271 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (i == -1 && PyErr_Occurred()) { |
| 1274 | return -1; |
| 1275 | } |
| 1276 | if (i < 0 && self->extra) |
| 1277 | i += self->extra->length; |
| 1278 | return element_setitem(self_, i, value); |
| 1279 | } |
| 1280 | else if (PySlice_Check(item)) { |
| 1281 | Py_ssize_t start, stop, step, slicelen, newlen, cur, i; |
| 1282 | |
| 1283 | PyObject* recycle = NULL; |
| 1284 | PyObject* seq = NULL; |
| 1285 | |
| 1286 | if (!self->extra) |
| 1287 | element_new_extra(self, NULL); |
| 1288 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1289 | if (PySlice_GetIndicesEx(item, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1290 | self->extra->length, |
| 1291 | &start, &stop, &step, &slicelen) < 0) { |
| 1292 | return -1; |
| 1293 | } |
| 1294 | |
| 1295 | if (value == NULL) |
| 1296 | newlen = 0; |
| 1297 | else { |
| 1298 | seq = PySequence_Fast(value, ""); |
| 1299 | if (!seq) { |
| 1300 | PyErr_Format( |
| 1301 | PyExc_TypeError, |
| 1302 | "expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name |
| 1303 | ); |
| 1304 | return -1; |
| 1305 | } |
| 1306 | newlen = PySequence_Size(seq); |
| 1307 | } |
| 1308 | |
| 1309 | if (step != 1 && newlen != slicelen) |
| 1310 | { |
| 1311 | PyErr_Format(PyExc_ValueError, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1312 | "attempt to assign sequence of size %zd " |
| 1313 | "to extended slice of size %zd", |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1314 | newlen, slicelen |
| 1315 | ); |
| 1316 | return -1; |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | /* Resize before creating the recycle bin, to prevent refleaks. */ |
| 1321 | if (newlen > slicelen) { |
| 1322 | if (element_resize(self, newlen - slicelen) < 0) { |
| 1323 | if (seq) { |
| 1324 | Py_DECREF(seq); |
| 1325 | } |
| 1326 | return -1; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | if (slicelen > 0) { |
| 1331 | /* to avoid recursive calls to this method (via decref), move |
| 1332 | old items to the recycle bin here, and get rid of them when |
| 1333 | we're done modifying the element */ |
| 1334 | recycle = PyList_New(slicelen); |
| 1335 | if (!recycle) { |
| 1336 | if (seq) { |
| 1337 | Py_DECREF(seq); |
| 1338 | } |
| 1339 | return -1; |
| 1340 | } |
| 1341 | for (cur = start, i = 0; i < slicelen; |
| 1342 | cur += step, i++) |
| 1343 | PyList_SET_ITEM(recycle, i, self->extra->children[cur]); |
| 1344 | } |
| 1345 | |
| 1346 | if (newlen < slicelen) { |
| 1347 | /* delete slice */ |
| 1348 | for (i = stop; i < self->extra->length; i++) |
| 1349 | self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
| 1350 | } else if (newlen > slicelen) { |
| 1351 | /* insert slice */ |
| 1352 | for (i = self->extra->length-1; i >= stop; i--) |
| 1353 | self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
| 1354 | } |
| 1355 | |
| 1356 | /* replace the slice */ |
| 1357 | for (cur = start, i = 0; i < newlen; |
| 1358 | cur += step, i++) { |
| 1359 | PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
| 1360 | Py_INCREF(element); |
| 1361 | self->extra->children[cur] = element; |
| 1362 | } |
| 1363 | |
| 1364 | self->extra->length += newlen - slicelen; |
| 1365 | |
| 1366 | if (seq) { |
| 1367 | Py_DECREF(seq); |
| 1368 | } |
| 1369 | |
| 1370 | /* discard the recycle bin, and everything in it */ |
| 1371 | Py_XDECREF(recycle); |
| 1372 | |
| 1373 | return 0; |
| 1374 | } |
| 1375 | else { |
| 1376 | PyErr_SetString(PyExc_TypeError, |
| 1377 | "element indices must be integers"); |
| 1378 | return -1; |
| 1379 | } |
| 1380 | } |
| 1381 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1382 | static PyMethodDef element_methods[] = { |
| 1383 | |
| 1384 | {"clear", (PyCFunction) element_clear, METH_VARARGS}, |
| 1385 | |
| 1386 | {"get", (PyCFunction) element_get, METH_VARARGS}, |
| 1387 | {"set", (PyCFunction) element_set, METH_VARARGS}, |
| 1388 | |
| 1389 | {"find", (PyCFunction) element_find, METH_VARARGS}, |
| 1390 | {"findtext", (PyCFunction) element_findtext, METH_VARARGS}, |
| 1391 | {"findall", (PyCFunction) element_findall, METH_VARARGS}, |
| 1392 | |
| 1393 | {"append", (PyCFunction) element_append, METH_VARARGS}, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1394 | {"extend", (PyCFunction) element_extend, METH_VARARGS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1395 | {"insert", (PyCFunction) element_insert, METH_VARARGS}, |
| 1396 | {"remove", (PyCFunction) element_remove, METH_VARARGS}, |
| 1397 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1398 | {"iter", (PyCFunction) element_iter, METH_VARARGS}, |
| 1399 | {"itertext", (PyCFunction) element_itertext, METH_VARARGS}, |
| 1400 | {"iterfind", (PyCFunction) element_iterfind, METH_VARARGS}, |
| 1401 | |
| 1402 | {"getiterator", (PyCFunction) element_iter, METH_VARARGS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1403 | {"getchildren", (PyCFunction) element_getchildren, METH_VARARGS}, |
| 1404 | |
| 1405 | {"items", (PyCFunction) element_items, METH_VARARGS}, |
| 1406 | {"keys", (PyCFunction) element_keys, METH_VARARGS}, |
| 1407 | |
| 1408 | {"makeelement", (PyCFunction) element_makeelement, METH_VARARGS}, |
| 1409 | |
| 1410 | {"__copy__", (PyCFunction) element_copy, METH_VARARGS}, |
| 1411 | {"__deepcopy__", (PyCFunction) element_deepcopy, METH_VARARGS}, |
| 1412 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1413 | {NULL, NULL} |
| 1414 | }; |
| 1415 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1416 | static PyObject* |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1417 | element_getattro(ElementObject* self, PyObject* nameobj) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1418 | { |
| 1419 | PyObject* res; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1420 | char *name = ""; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1421 | |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1422 | if (PyUnicode_Check(nameobj)) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1423 | name = _PyUnicode_AsString(nameobj); |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 1424 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1425 | if (name == NULL) |
| 1426 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1427 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1428 | /* handle common attributes first */ |
| 1429 | if (strcmp(name, "tag") == 0) { |
| 1430 | res = self->tag; |
| 1431 | Py_INCREF(res); |
| 1432 | return res; |
| 1433 | } else if (strcmp(name, "text") == 0) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1434 | res = element_get_text(self); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1435 | Py_INCREF(res); |
| 1436 | return res; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1439 | /* methods */ |
| 1440 | res = PyObject_GenericGetAttr((PyObject*) self, nameobj); |
| 1441 | if (res) |
| 1442 | return res; |
| 1443 | |
| 1444 | /* less common attributes */ |
| 1445 | if (strcmp(name, "tail") == 0) { |
| 1446 | PyErr_Clear(); |
| 1447 | res = element_get_tail(self); |
| 1448 | } else if (strcmp(name, "attrib") == 0) { |
| 1449 | PyErr_Clear(); |
| 1450 | if (!self->extra) |
| 1451 | element_new_extra(self, NULL); |
| 1452 | res = element_get_attrib(self); |
| 1453 | } |
| 1454 | |
| 1455 | if (!res) |
| 1456 | return NULL; |
| 1457 | |
| 1458 | Py_INCREF(res); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1459 | return res; |
| 1460 | } |
| 1461 | |
| 1462 | static int |
| 1463 | element_setattr(ElementObject* self, const char* name, PyObject* value) |
| 1464 | { |
| 1465 | if (value == NULL) { |
| 1466 | PyErr_SetString( |
| 1467 | PyExc_AttributeError, |
| 1468 | "can't delete element attributes" |
| 1469 | ); |
| 1470 | return -1; |
| 1471 | } |
| 1472 | |
| 1473 | if (strcmp(name, "tag") == 0) { |
| 1474 | Py_DECREF(self->tag); |
| 1475 | self->tag = value; |
| 1476 | Py_INCREF(self->tag); |
| 1477 | } else if (strcmp(name, "text") == 0) { |
| 1478 | Py_DECREF(JOIN_OBJ(self->text)); |
| 1479 | self->text = value; |
| 1480 | Py_INCREF(self->text); |
| 1481 | } else if (strcmp(name, "tail") == 0) { |
| 1482 | Py_DECREF(JOIN_OBJ(self->tail)); |
| 1483 | self->tail = value; |
| 1484 | Py_INCREF(self->tail); |
| 1485 | } else if (strcmp(name, "attrib") == 0) { |
| 1486 | if (!self->extra) |
| 1487 | element_new_extra(self, NULL); |
| 1488 | Py_DECREF(self->extra->attrib); |
| 1489 | self->extra->attrib = value; |
| 1490 | Py_INCREF(self->extra->attrib); |
| 1491 | } else { |
| 1492 | PyErr_SetString(PyExc_AttributeError, name); |
| 1493 | return -1; |
| 1494 | } |
| 1495 | |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | static PySequenceMethods element_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1500 | (lenfunc) element_length, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1501 | 0, /* sq_concat */ |
| 1502 | 0, /* sq_repeat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1503 | element_getitem, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1504 | 0, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1505 | element_setitem, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1506 | 0, |
| 1507 | }; |
| 1508 | |
| 1509 | static PyMappingMethods element_as_mapping = { |
| 1510 | (lenfunc) element_length, |
| 1511 | (binaryfunc) element_subscr, |
| 1512 | (objobjargproc) element_ass_subscr, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1513 | }; |
| 1514 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 1515 | static PyTypeObject Element_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1516 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1517 | "Element", sizeof(ElementObject), 0, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1518 | /* methods */ |
| 1519 | (destructor)element_dealloc, /* tp_dealloc */ |
| 1520 | 0, /* tp_print */ |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1521 | 0, /* tp_getattr */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1522 | (setattrfunc)element_setattr, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1523 | 0, /* tp_reserved */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1524 | (reprfunc)element_repr, /* tp_repr */ |
| 1525 | 0, /* tp_as_number */ |
| 1526 | &element_as_sequence, /* tp_as_sequence */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1527 | &element_as_mapping, /* tp_as_mapping */ |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1528 | 0, /* tp_hash */ |
| 1529 | 0, /* tp_call */ |
| 1530 | 0, /* tp_str */ |
| 1531 | (getattrofunc)element_getattro, /* tp_getattro */ |
| 1532 | 0, /* tp_setattro */ |
| 1533 | 0, /* tp_as_buffer */ |
| 1534 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1535 | 0, /* tp_doc */ |
| 1536 | 0, /* tp_traverse */ |
| 1537 | 0, /* tp_clear */ |
| 1538 | 0, /* tp_richcompare */ |
| 1539 | 0, /* tp_weaklistoffset */ |
| 1540 | 0, /* tp_iter */ |
| 1541 | 0, /* tp_iternext */ |
| 1542 | element_methods, /* tp_methods */ |
| 1543 | 0, /* tp_members */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1544 | }; |
| 1545 | |
| 1546 | /* ==================================================================== */ |
| 1547 | /* the tree builder type */ |
| 1548 | |
| 1549 | typedef struct { |
| 1550 | PyObject_HEAD |
| 1551 | |
| 1552 | PyObject* root; /* root node (first created node) */ |
| 1553 | |
| 1554 | ElementObject* this; /* current node */ |
| 1555 | ElementObject* last; /* most recently created node */ |
| 1556 | |
| 1557 | PyObject* data; /* data collector (string or list), or NULL */ |
| 1558 | |
| 1559 | PyObject* stack; /* element stack */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1560 | Py_ssize_t index; /* current stack size (0=empty) */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1561 | |
| 1562 | /* element tracing */ |
| 1563 | PyObject* events; /* list of events, or NULL if not collecting */ |
| 1564 | PyObject* start_event_obj; /* event objects (NULL to ignore) */ |
| 1565 | PyObject* end_event_obj; |
| 1566 | PyObject* start_ns_event_obj; |
| 1567 | PyObject* end_ns_event_obj; |
| 1568 | |
| 1569 | } TreeBuilderObject; |
| 1570 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 1571 | static PyTypeObject TreeBuilder_Type; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1572 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 1573 | #define TreeBuilder_CheckExact(op) (Py_TYPE(op) == &TreeBuilder_Type) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1574 | |
| 1575 | /* -------------------------------------------------------------------- */ |
| 1576 | /* constructor and destructor */ |
| 1577 | |
| 1578 | LOCAL(PyObject*) |
| 1579 | treebuilder_new(void) |
| 1580 | { |
| 1581 | TreeBuilderObject* self; |
| 1582 | |
| 1583 | self = PyObject_New(TreeBuilderObject, &TreeBuilder_Type); |
| 1584 | if (self == NULL) |
| 1585 | return NULL; |
| 1586 | |
| 1587 | self->root = NULL; |
| 1588 | |
| 1589 | Py_INCREF(Py_None); |
| 1590 | self->this = (ElementObject*) Py_None; |
| 1591 | |
| 1592 | Py_INCREF(Py_None); |
| 1593 | self->last = (ElementObject*) Py_None; |
| 1594 | |
| 1595 | self->data = NULL; |
| 1596 | |
| 1597 | self->stack = PyList_New(20); |
| 1598 | self->index = 0; |
| 1599 | |
| 1600 | self->events = NULL; |
| 1601 | self->start_event_obj = self->end_event_obj = NULL; |
| 1602 | self->start_ns_event_obj = self->end_ns_event_obj = NULL; |
| 1603 | |
| 1604 | ALLOC(sizeof(TreeBuilderObject), "create treebuilder"); |
| 1605 | |
| 1606 | return (PyObject*) self; |
| 1607 | } |
| 1608 | |
| 1609 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1610 | treebuilder(PyObject* self_, PyObject* args) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1611 | { |
| 1612 | if (!PyArg_ParseTuple(args, ":TreeBuilder")) |
| 1613 | return NULL; |
| 1614 | |
| 1615 | return treebuilder_new(); |
| 1616 | } |
| 1617 | |
| 1618 | static void |
| 1619 | treebuilder_dealloc(TreeBuilderObject* self) |
| 1620 | { |
| 1621 | Py_XDECREF(self->end_ns_event_obj); |
| 1622 | Py_XDECREF(self->start_ns_event_obj); |
| 1623 | Py_XDECREF(self->end_event_obj); |
| 1624 | Py_XDECREF(self->start_event_obj); |
| 1625 | Py_XDECREF(self->events); |
| 1626 | Py_DECREF(self->stack); |
| 1627 | Py_XDECREF(self->data); |
| 1628 | Py_DECREF(self->last); |
| 1629 | Py_DECREF(self->this); |
| 1630 | Py_XDECREF(self->root); |
| 1631 | |
| 1632 | RELEASE(sizeof(TreeBuilderObject), "destroy treebuilder"); |
| 1633 | |
| 1634 | PyObject_Del(self); |
| 1635 | } |
| 1636 | |
| 1637 | /* -------------------------------------------------------------------- */ |
| 1638 | /* handlers */ |
| 1639 | |
| 1640 | LOCAL(PyObject*) |
| 1641 | treebuilder_handle_xml(TreeBuilderObject* self, PyObject* encoding, |
| 1642 | PyObject* standalone) |
| 1643 | { |
| 1644 | Py_RETURN_NONE; |
| 1645 | } |
| 1646 | |
| 1647 | LOCAL(PyObject*) |
| 1648 | treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, |
| 1649 | PyObject* attrib) |
| 1650 | { |
| 1651 | PyObject* node; |
| 1652 | PyObject* this; |
| 1653 | |
| 1654 | if (self->data) { |
| 1655 | if (self->this == self->last) { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1656 | Py_DECREF(JOIN_OBJ(self->last->text)); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1657 | self->last->text = JOIN_SET( |
| 1658 | self->data, PyList_CheckExact(self->data) |
| 1659 | ); |
| 1660 | } else { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1661 | Py_DECREF(JOIN_OBJ(self->last->tail)); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1662 | self->last->tail = JOIN_SET( |
| 1663 | self->data, PyList_CheckExact(self->data) |
| 1664 | ); |
| 1665 | } |
| 1666 | self->data = NULL; |
| 1667 | } |
| 1668 | |
| 1669 | node = element_new(tag, attrib); |
| 1670 | if (!node) |
| 1671 | return NULL; |
| 1672 | |
| 1673 | this = (PyObject*) self->this; |
| 1674 | |
| 1675 | if (this != Py_None) { |
| 1676 | if (element_add_subelement((ElementObject*) this, node) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1677 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1678 | } else { |
| 1679 | if (self->root) { |
| 1680 | PyErr_SetString( |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1681 | elementtree_parseerror_obj, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1682 | "multiple elements on top level" |
| 1683 | ); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1684 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1685 | } |
| 1686 | Py_INCREF(node); |
| 1687 | self->root = node; |
| 1688 | } |
| 1689 | |
| 1690 | if (self->index < PyList_GET_SIZE(self->stack)) { |
| 1691 | if (PyList_SetItem(self->stack, self->index, this) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1692 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1693 | Py_INCREF(this); |
| 1694 | } else { |
| 1695 | if (PyList_Append(self->stack, this) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1696 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1697 | } |
| 1698 | self->index++; |
| 1699 | |
| 1700 | Py_DECREF(this); |
| 1701 | Py_INCREF(node); |
| 1702 | self->this = (ElementObject*) node; |
| 1703 | |
| 1704 | Py_DECREF(self->last); |
| 1705 | Py_INCREF(node); |
| 1706 | self->last = (ElementObject*) node; |
| 1707 | |
| 1708 | if (self->start_event_obj) { |
| 1709 | PyObject* res; |
| 1710 | PyObject* action = self->start_event_obj; |
| 1711 | res = PyTuple_New(2); |
| 1712 | if (res) { |
| 1713 | Py_INCREF(action); PyTuple_SET_ITEM(res, 0, (PyObject*) action); |
| 1714 | Py_INCREF(node); PyTuple_SET_ITEM(res, 1, (PyObject*) node); |
| 1715 | PyList_Append(self->events, res); |
| 1716 | Py_DECREF(res); |
| 1717 | } else |
| 1718 | PyErr_Clear(); /* FIXME: propagate error */ |
| 1719 | } |
| 1720 | |
| 1721 | return node; |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1722 | |
| 1723 | error: |
| 1724 | Py_DECREF(node); |
| 1725 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | LOCAL(PyObject*) |
| 1729 | treebuilder_handle_data(TreeBuilderObject* self, PyObject* data) |
| 1730 | { |
| 1731 | if (!self->data) { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1732 | if (self->last == (ElementObject*) Py_None) { |
| 1733 | /* ignore calls to data before the first call to start */ |
| 1734 | Py_RETURN_NONE; |
| 1735 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1736 | /* store the first item as is */ |
| 1737 | Py_INCREF(data); self->data = data; |
| 1738 | } else { |
| 1739 | /* more than one item; use a list to collect items */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1740 | if (PyBytes_CheckExact(self->data) && Py_REFCNT(self->data) == 1 && |
| 1741 | PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1742 | /* expat often generates single character data sections; handle |
| 1743 | the most common case by resizing the existing string... */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1744 | Py_ssize_t size = PyBytes_GET_SIZE(self->data); |
| 1745 | if (_PyBytes_Resize(&self->data, size + 1) < 0) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1746 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1747 | PyBytes_AS_STRING(self->data)[size] = PyBytes_AS_STRING(data)[0]; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1748 | } else if (PyList_CheckExact(self->data)) { |
| 1749 | if (PyList_Append(self->data, data) < 0) |
| 1750 | return NULL; |
| 1751 | } else { |
| 1752 | PyObject* list = PyList_New(2); |
| 1753 | if (!list) |
| 1754 | return NULL; |
| 1755 | PyList_SET_ITEM(list, 0, self->data); |
| 1756 | Py_INCREF(data); PyList_SET_ITEM(list, 1, data); |
| 1757 | self->data = list; |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | Py_RETURN_NONE; |
| 1762 | } |
| 1763 | |
| 1764 | LOCAL(PyObject*) |
| 1765 | treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag) |
| 1766 | { |
| 1767 | PyObject* item; |
| 1768 | |
| 1769 | if (self->data) { |
| 1770 | if (self->this == self->last) { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1771 | Py_DECREF(JOIN_OBJ(self->last->text)); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1772 | self->last->text = JOIN_SET( |
| 1773 | self->data, PyList_CheckExact(self->data) |
| 1774 | ); |
| 1775 | } else { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1776 | Py_DECREF(JOIN_OBJ(self->last->tail)); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1777 | self->last->tail = JOIN_SET( |
| 1778 | self->data, PyList_CheckExact(self->data) |
| 1779 | ); |
| 1780 | } |
| 1781 | self->data = NULL; |
| 1782 | } |
| 1783 | |
| 1784 | if (self->index == 0) { |
| 1785 | PyErr_SetString( |
| 1786 | PyExc_IndexError, |
| 1787 | "pop from empty stack" |
| 1788 | ); |
| 1789 | return NULL; |
| 1790 | } |
| 1791 | |
| 1792 | self->index--; |
| 1793 | |
| 1794 | item = PyList_GET_ITEM(self->stack, self->index); |
| 1795 | Py_INCREF(item); |
| 1796 | |
| 1797 | Py_DECREF(self->last); |
| 1798 | |
| 1799 | self->last = (ElementObject*) self->this; |
| 1800 | self->this = (ElementObject*) item; |
| 1801 | |
| 1802 | if (self->end_event_obj) { |
| 1803 | PyObject* res; |
| 1804 | PyObject* action = self->end_event_obj; |
| 1805 | PyObject* node = (PyObject*) self->last; |
| 1806 | res = PyTuple_New(2); |
| 1807 | if (res) { |
| 1808 | Py_INCREF(action); PyTuple_SET_ITEM(res, 0, (PyObject*) action); |
| 1809 | Py_INCREF(node); PyTuple_SET_ITEM(res, 1, (PyObject*) node); |
| 1810 | PyList_Append(self->events, res); |
| 1811 | Py_DECREF(res); |
| 1812 | } else |
| 1813 | PyErr_Clear(); /* FIXME: propagate error */ |
| 1814 | } |
| 1815 | |
| 1816 | Py_INCREF(self->last); |
| 1817 | return (PyObject*) self->last; |
| 1818 | } |
| 1819 | |
| 1820 | LOCAL(void) |
| 1821 | treebuilder_handle_namespace(TreeBuilderObject* self, int start, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1822 | PyObject *prefix, PyObject *uri) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1823 | { |
| 1824 | PyObject* res; |
| 1825 | PyObject* action; |
| 1826 | PyObject* parcel; |
| 1827 | |
| 1828 | if (!self->events) |
| 1829 | return; |
| 1830 | |
| 1831 | if (start) { |
| 1832 | if (!self->start_ns_event_obj) |
| 1833 | return; |
| 1834 | action = self->start_ns_event_obj; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1835 | parcel = Py_BuildValue("OO", prefix, uri); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1836 | if (!parcel) |
| 1837 | return; |
| 1838 | Py_INCREF(action); |
| 1839 | } else { |
| 1840 | if (!self->end_ns_event_obj) |
| 1841 | return; |
| 1842 | action = self->end_ns_event_obj; |
| 1843 | Py_INCREF(action); |
| 1844 | parcel = Py_None; |
| 1845 | Py_INCREF(parcel); |
| 1846 | } |
| 1847 | |
| 1848 | res = PyTuple_New(2); |
| 1849 | |
| 1850 | if (res) { |
| 1851 | PyTuple_SET_ITEM(res, 0, action); |
| 1852 | PyTuple_SET_ITEM(res, 1, parcel); |
| 1853 | PyList_Append(self->events, res); |
| 1854 | Py_DECREF(res); |
| 1855 | } else |
| 1856 | PyErr_Clear(); /* FIXME: propagate error */ |
| 1857 | } |
| 1858 | |
| 1859 | /* -------------------------------------------------------------------- */ |
| 1860 | /* methods (in alphabetical order) */ |
| 1861 | |
| 1862 | static PyObject* |
| 1863 | treebuilder_data(TreeBuilderObject* self, PyObject* args) |
| 1864 | { |
| 1865 | PyObject* data; |
| 1866 | if (!PyArg_ParseTuple(args, "O:data", &data)) |
| 1867 | return NULL; |
| 1868 | |
| 1869 | return treebuilder_handle_data(self, data); |
| 1870 | } |
| 1871 | |
| 1872 | static PyObject* |
| 1873 | treebuilder_end(TreeBuilderObject* self, PyObject* args) |
| 1874 | { |
| 1875 | PyObject* tag; |
| 1876 | if (!PyArg_ParseTuple(args, "O:end", &tag)) |
| 1877 | return NULL; |
| 1878 | |
| 1879 | return treebuilder_handle_end(self, tag); |
| 1880 | } |
| 1881 | |
| 1882 | LOCAL(PyObject*) |
| 1883 | treebuilder_done(TreeBuilderObject* self) |
| 1884 | { |
| 1885 | PyObject* res; |
| 1886 | |
| 1887 | /* FIXME: check stack size? */ |
| 1888 | |
| 1889 | if (self->root) |
| 1890 | res = self->root; |
| 1891 | else |
| 1892 | res = Py_None; |
| 1893 | |
| 1894 | Py_INCREF(res); |
| 1895 | return res; |
| 1896 | } |
| 1897 | |
| 1898 | static PyObject* |
| 1899 | treebuilder_close(TreeBuilderObject* self, PyObject* args) |
| 1900 | { |
| 1901 | if (!PyArg_ParseTuple(args, ":close")) |
| 1902 | return NULL; |
| 1903 | |
| 1904 | return treebuilder_done(self); |
| 1905 | } |
| 1906 | |
| 1907 | static PyObject* |
| 1908 | treebuilder_start(TreeBuilderObject* self, PyObject* args) |
| 1909 | { |
| 1910 | PyObject* tag; |
| 1911 | PyObject* attrib = Py_None; |
| 1912 | if (!PyArg_ParseTuple(args, "O|O:start", &tag, &attrib)) |
| 1913 | return NULL; |
| 1914 | |
| 1915 | return treebuilder_handle_start(self, tag, attrib); |
| 1916 | } |
| 1917 | |
| 1918 | static PyObject* |
| 1919 | treebuilder_xml(TreeBuilderObject* self, PyObject* args) |
| 1920 | { |
| 1921 | PyObject* encoding; |
| 1922 | PyObject* standalone; |
| 1923 | if (!PyArg_ParseTuple(args, "OO:xml", &encoding, &standalone)) |
| 1924 | return NULL; |
| 1925 | |
| 1926 | return treebuilder_handle_xml(self, encoding, standalone); |
| 1927 | } |
| 1928 | |
| 1929 | static PyMethodDef treebuilder_methods[] = { |
| 1930 | {"data", (PyCFunction) treebuilder_data, METH_VARARGS}, |
| 1931 | {"start", (PyCFunction) treebuilder_start, METH_VARARGS}, |
| 1932 | {"end", (PyCFunction) treebuilder_end, METH_VARARGS}, |
| 1933 | {"xml", (PyCFunction) treebuilder_xml, METH_VARARGS}, |
| 1934 | {"close", (PyCFunction) treebuilder_close, METH_VARARGS}, |
| 1935 | {NULL, NULL} |
| 1936 | }; |
| 1937 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 1938 | static PyTypeObject TreeBuilder_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1939 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1940 | "TreeBuilder", sizeof(TreeBuilderObject), 0, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1941 | /* methods */ |
| 1942 | (destructor)treebuilder_dealloc, /* tp_dealloc */ |
| 1943 | 0, /* tp_print */ |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1944 | 0, /* tp_getattr */ |
| 1945 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 1946 | 0, /* tp_reserved */ |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1947 | 0, /* tp_repr */ |
| 1948 | 0, /* tp_as_number */ |
| 1949 | 0, /* tp_as_sequence */ |
| 1950 | 0, /* tp_as_mapping */ |
| 1951 | 0, /* tp_hash */ |
| 1952 | 0, /* tp_call */ |
| 1953 | 0, /* tp_str */ |
| 1954 | 0, /* tp_getattro */ |
| 1955 | 0, /* tp_setattro */ |
| 1956 | 0, /* tp_as_buffer */ |
| 1957 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1958 | 0, /* tp_doc */ |
| 1959 | 0, /* tp_traverse */ |
| 1960 | 0, /* tp_clear */ |
| 1961 | 0, /* tp_richcompare */ |
| 1962 | 0, /* tp_weaklistoffset */ |
| 1963 | 0, /* tp_iter */ |
| 1964 | 0, /* tp_iternext */ |
| 1965 | treebuilder_methods, /* tp_methods */ |
| 1966 | 0, /* tp_members */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1967 | }; |
| 1968 | |
| 1969 | /* ==================================================================== */ |
| 1970 | /* the expat interface */ |
| 1971 | |
| 1972 | #if defined(USE_EXPAT) |
| 1973 | |
| 1974 | #include "expat.h" |
| 1975 | |
| 1976 | #if defined(USE_PYEXPAT_CAPI) |
| 1977 | #include "pyexpat.h" |
| 1978 | static struct PyExpat_CAPI* expat_capi; |
| 1979 | #define EXPAT(func) (expat_capi->func) |
| 1980 | #else |
| 1981 | #define EXPAT(func) (XML_##func) |
| 1982 | #endif |
| 1983 | |
| 1984 | typedef struct { |
| 1985 | PyObject_HEAD |
| 1986 | |
| 1987 | XML_Parser parser; |
| 1988 | |
| 1989 | PyObject* target; |
| 1990 | PyObject* entity; |
| 1991 | |
| 1992 | PyObject* names; |
| 1993 | |
| 1994 | PyObject* handle_xml; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1995 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1996 | PyObject* handle_start; |
| 1997 | PyObject* handle_data; |
| 1998 | PyObject* handle_end; |
| 1999 | |
| 2000 | PyObject* handle_comment; |
| 2001 | PyObject* handle_pi; |
| 2002 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2003 | PyObject* handle_close; |
| 2004 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2005 | } XMLParserObject; |
| 2006 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 2007 | static PyTypeObject XMLParser_Type; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2008 | |
| 2009 | /* helpers */ |
| 2010 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2011 | LOCAL(PyObject*) |
| 2012 | makeuniversal(XMLParserObject* self, const char* string) |
| 2013 | { |
| 2014 | /* convert a UTF-8 tag/attribute name from the expat parser |
| 2015 | to a universal name string */ |
| 2016 | |
| 2017 | int size = strlen(string); |
| 2018 | PyObject* key; |
| 2019 | PyObject* value; |
| 2020 | |
| 2021 | /* look the 'raw' name up in the names dictionary */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2022 | key = PyBytes_FromStringAndSize(string, size); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2023 | if (!key) |
| 2024 | return NULL; |
| 2025 | |
| 2026 | value = PyDict_GetItem(self->names, key); |
| 2027 | |
| 2028 | if (value) { |
| 2029 | Py_INCREF(value); |
| 2030 | } else { |
| 2031 | /* new name. convert to universal name, and decode as |
| 2032 | necessary */ |
| 2033 | |
| 2034 | PyObject* tag; |
| 2035 | char* p; |
| 2036 | int i; |
| 2037 | |
| 2038 | /* look for namespace separator */ |
| 2039 | for (i = 0; i < size; i++) |
| 2040 | if (string[i] == '}') |
| 2041 | break; |
| 2042 | if (i != size) { |
| 2043 | /* convert to universal name */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2044 | tag = PyBytes_FromStringAndSize(NULL, size+1); |
| 2045 | p = PyBytes_AS_STRING(tag); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2046 | p[0] = '{'; |
| 2047 | memcpy(p+1, string, size); |
| 2048 | size++; |
| 2049 | } else { |
| 2050 | /* plain name; use key as tag */ |
| 2051 | Py_INCREF(key); |
| 2052 | tag = key; |
| 2053 | } |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 2054 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2055 | /* decode universal name */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2056 | p = PyBytes_AS_STRING(tag); |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2057 | value = PyUnicode_DecodeUTF8(p, size, "strict"); |
| 2058 | Py_DECREF(tag); |
| 2059 | if (!value) { |
| 2060 | Py_DECREF(key); |
| 2061 | return NULL; |
| 2062 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2063 | |
| 2064 | /* add to names dictionary */ |
| 2065 | if (PyDict_SetItem(self->names, key, value) < 0) { |
| 2066 | Py_DECREF(key); |
| 2067 | Py_DECREF(value); |
| 2068 | return NULL; |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | Py_DECREF(key); |
| 2073 | return value; |
| 2074 | } |
| 2075 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2076 | static void |
| 2077 | expat_set_error(const char* message, int line, int column) |
| 2078 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 2079 | PyObject *errmsg, *error, *position; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2080 | |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 2081 | errmsg = PyUnicode_FromFormat("%s: line %d, column %d", |
| 2082 | message, line, column); |
| 2083 | if (errmsg == NULL) |
| 2084 | return; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2085 | |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 2086 | error = PyObject_CallFunction(elementtree_parseerror_obj, "O", errmsg); |
| 2087 | Py_DECREF(errmsg); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2088 | if (!error) |
| 2089 | return; |
| 2090 | |
| 2091 | /* add position attribute */ |
| 2092 | position = Py_BuildValue("(ii)", line, column); |
| 2093 | if (!position) { |
| 2094 | Py_DECREF(error); |
| 2095 | return; |
| 2096 | } |
| 2097 | if (PyObject_SetAttrString(error, "position", position) == -1) { |
| 2098 | Py_DECREF(error); |
| 2099 | Py_DECREF(position); |
| 2100 | return; |
| 2101 | } |
| 2102 | Py_DECREF(position); |
| 2103 | |
| 2104 | PyErr_SetObject(elementtree_parseerror_obj, error); |
| 2105 | Py_DECREF(error); |
| 2106 | } |
| 2107 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2108 | /* -------------------------------------------------------------------- */ |
| 2109 | /* handlers */ |
| 2110 | |
| 2111 | static void |
| 2112 | expat_default_handler(XMLParserObject* self, const XML_Char* data_in, |
| 2113 | int data_len) |
| 2114 | { |
| 2115 | PyObject* key; |
| 2116 | PyObject* value; |
| 2117 | PyObject* res; |
| 2118 | |
| 2119 | if (data_len < 2 || data_in[0] != '&') |
| 2120 | return; |
| 2121 | |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2122 | key = PyUnicode_DecodeUTF8(data_in + 1, data_len - 2, "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2123 | if (!key) |
| 2124 | return; |
| 2125 | |
| 2126 | value = PyDict_GetItem(self->entity, key); |
| 2127 | |
| 2128 | if (value) { |
| 2129 | if (TreeBuilder_CheckExact(self->target)) |
| 2130 | res = treebuilder_handle_data( |
| 2131 | (TreeBuilderObject*) self->target, value |
| 2132 | ); |
| 2133 | else if (self->handle_data) |
| 2134 | res = PyObject_CallFunction(self->handle_data, "O", value); |
| 2135 | else |
| 2136 | res = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2137 | Py_XDECREF(res); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2138 | } else if (!PyErr_Occurred()) { |
| 2139 | /* Report the first error, not the last */ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2140 | char message[128] = "undefined entity "; |
| 2141 | strncat(message, data_in, data_len < 100?data_len:100); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2142 | expat_set_error( |
| 2143 | message, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2144 | EXPAT(GetErrorLineNumber)(self->parser), |
| 2145 | EXPAT(GetErrorColumnNumber)(self->parser) |
| 2146 | ); |
| 2147 | } |
| 2148 | |
| 2149 | Py_DECREF(key); |
| 2150 | } |
| 2151 | |
| 2152 | static void |
| 2153 | expat_start_handler(XMLParserObject* self, const XML_Char* tag_in, |
| 2154 | const XML_Char **attrib_in) |
| 2155 | { |
| 2156 | PyObject* res; |
| 2157 | PyObject* tag; |
| 2158 | PyObject* attrib; |
| 2159 | int ok; |
| 2160 | |
| 2161 | /* tag name */ |
| 2162 | tag = makeuniversal(self, tag_in); |
| 2163 | if (!tag) |
| 2164 | return; /* parser will look for errors */ |
| 2165 | |
| 2166 | /* attributes */ |
| 2167 | if (attrib_in[0]) { |
| 2168 | attrib = PyDict_New(); |
| 2169 | if (!attrib) |
| 2170 | return; |
| 2171 | while (attrib_in[0] && attrib_in[1]) { |
| 2172 | PyObject* key = makeuniversal(self, attrib_in[0]); |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2173 | PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2174 | if (!key || !value) { |
| 2175 | Py_XDECREF(value); |
| 2176 | Py_XDECREF(key); |
| 2177 | Py_DECREF(attrib); |
| 2178 | return; |
| 2179 | } |
| 2180 | ok = PyDict_SetItem(attrib, key, value); |
| 2181 | Py_DECREF(value); |
| 2182 | Py_DECREF(key); |
| 2183 | if (ok < 0) { |
| 2184 | Py_DECREF(attrib); |
| 2185 | return; |
| 2186 | } |
| 2187 | attrib_in += 2; |
| 2188 | } |
| 2189 | } else { |
| 2190 | Py_INCREF(Py_None); |
| 2191 | attrib = Py_None; |
| 2192 | } |
| 2193 | |
| 2194 | if (TreeBuilder_CheckExact(self->target)) |
| 2195 | /* shortcut */ |
| 2196 | res = treebuilder_handle_start((TreeBuilderObject*) self->target, |
| 2197 | tag, attrib); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2198 | else if (self->handle_start) { |
| 2199 | if (attrib == Py_None) { |
| 2200 | Py_DECREF(attrib); |
| 2201 | attrib = PyDict_New(); |
| 2202 | if (!attrib) |
| 2203 | return; |
| 2204 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2205 | res = PyObject_CallFunction(self->handle_start, "OO", tag, attrib); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2206 | } else |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2207 | res = NULL; |
| 2208 | |
| 2209 | Py_DECREF(tag); |
| 2210 | Py_DECREF(attrib); |
| 2211 | |
| 2212 | Py_XDECREF(res); |
| 2213 | } |
| 2214 | |
| 2215 | static void |
| 2216 | expat_data_handler(XMLParserObject* self, const XML_Char* data_in, |
| 2217 | int data_len) |
| 2218 | { |
| 2219 | PyObject* data; |
| 2220 | PyObject* res; |
| 2221 | |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2222 | data = PyUnicode_DecodeUTF8(data_in, data_len, "strict"); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2223 | if (!data) |
| 2224 | return; /* parser will look for errors */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2225 | |
| 2226 | if (TreeBuilder_CheckExact(self->target)) |
| 2227 | /* shortcut */ |
| 2228 | res = treebuilder_handle_data((TreeBuilderObject*) self->target, data); |
| 2229 | else if (self->handle_data) |
| 2230 | res = PyObject_CallFunction(self->handle_data, "O", data); |
| 2231 | else |
| 2232 | res = NULL; |
| 2233 | |
| 2234 | Py_DECREF(data); |
| 2235 | |
| 2236 | Py_XDECREF(res); |
| 2237 | } |
| 2238 | |
| 2239 | static void |
| 2240 | expat_end_handler(XMLParserObject* self, const XML_Char* tag_in) |
| 2241 | { |
| 2242 | PyObject* tag; |
| 2243 | PyObject* res = NULL; |
| 2244 | |
| 2245 | if (TreeBuilder_CheckExact(self->target)) |
| 2246 | /* shortcut */ |
| 2247 | /* the standard tree builder doesn't look at the end tag */ |
| 2248 | res = treebuilder_handle_end( |
| 2249 | (TreeBuilderObject*) self->target, Py_None |
| 2250 | ); |
| 2251 | else if (self->handle_end) { |
| 2252 | tag = makeuniversal(self, tag_in); |
| 2253 | if (tag) { |
| 2254 | res = PyObject_CallFunction(self->handle_end, "O", tag); |
| 2255 | Py_DECREF(tag); |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | Py_XDECREF(res); |
| 2260 | } |
| 2261 | |
| 2262 | static void |
| 2263 | expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix, |
| 2264 | const XML_Char *uri) |
| 2265 | { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2266 | PyObject* sprefix = NULL; |
| 2267 | PyObject* suri = NULL; |
| 2268 | |
| 2269 | suri = PyUnicode_DecodeUTF8(uri, strlen(uri), "strict"); |
| 2270 | if (!suri) |
| 2271 | return; |
| 2272 | |
| 2273 | if (prefix) |
| 2274 | sprefix = PyUnicode_DecodeUTF8(prefix, strlen(prefix), "strict"); |
| 2275 | else |
| 2276 | sprefix = PyUnicode_FromString(""); |
| 2277 | if (!sprefix) { |
| 2278 | Py_DECREF(suri); |
| 2279 | return; |
| 2280 | } |
| 2281 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2282 | treebuilder_handle_namespace( |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2283 | (TreeBuilderObject*) self->target, 1, sprefix, suri |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2284 | ); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2285 | |
| 2286 | Py_DECREF(sprefix); |
| 2287 | Py_DECREF(suri); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | static void |
| 2291 | expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in) |
| 2292 | { |
| 2293 | treebuilder_handle_namespace( |
| 2294 | (TreeBuilderObject*) self->target, 0, NULL, NULL |
| 2295 | ); |
| 2296 | } |
| 2297 | |
| 2298 | static void |
| 2299 | expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in) |
| 2300 | { |
| 2301 | PyObject* comment; |
| 2302 | PyObject* res; |
| 2303 | |
| 2304 | if (self->handle_comment) { |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2305 | comment = PyUnicode_DecodeUTF8(comment_in, strlen(comment_in), "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2306 | if (comment) { |
| 2307 | res = PyObject_CallFunction(self->handle_comment, "O", comment); |
| 2308 | Py_XDECREF(res); |
| 2309 | Py_DECREF(comment); |
| 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | static void |
| 2315 | expat_pi_handler(XMLParserObject* self, const XML_Char* target_in, |
| 2316 | const XML_Char* data_in) |
| 2317 | { |
| 2318 | PyObject* target; |
| 2319 | PyObject* data; |
| 2320 | PyObject* res; |
| 2321 | |
| 2322 | if (self->handle_pi) { |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2323 | target = PyUnicode_DecodeUTF8(target_in, strlen(target_in), "strict"); |
| 2324 | data = PyUnicode_DecodeUTF8(data_in, strlen(data_in), "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2325 | if (target && data) { |
| 2326 | res = PyObject_CallFunction(self->handle_pi, "OO", target, data); |
| 2327 | Py_XDECREF(res); |
| 2328 | Py_DECREF(data); |
| 2329 | Py_DECREF(target); |
| 2330 | } else { |
| 2331 | Py_XDECREF(data); |
| 2332 | Py_XDECREF(target); |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2337 | static int |
| 2338 | expat_unknown_encoding_handler(XMLParserObject *self, const XML_Char *name, |
| 2339 | XML_Encoding *info) |
| 2340 | { |
| 2341 | PyObject* u; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2342 | unsigned char s[256]; |
| 2343 | int i; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2344 | void *data; |
| 2345 | unsigned int kind; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2346 | |
| 2347 | memset(info, 0, sizeof(XML_Encoding)); |
| 2348 | |
| 2349 | for (i = 0; i < 256; i++) |
| 2350 | s[i] = i; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 2351 | |
Fredrik Lundh | c338999 | 2005-12-25 11:40:19 +0000 | [diff] [blame] | 2352 | u = PyUnicode_Decode((char*) s, 256, name, "replace"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2353 | if (!u) |
| 2354 | return XML_STATUS_ERROR; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2355 | if (PyUnicode_READY(u)) |
| 2356 | return XML_STATUS_ERROR; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2357 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2358 | if (PyUnicode_GET_LENGTH(u) != 256) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2359 | Py_DECREF(u); |
| 2360 | return XML_STATUS_ERROR; |
| 2361 | } |
| 2362 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2363 | kind = PyUnicode_KIND(u); |
| 2364 | data = PyUnicode_DATA(u); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2365 | for (i = 0; i < 256; i++) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2366 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 2367 | if (ch != Py_UNICODE_REPLACEMENT_CHARACTER) |
| 2368 | info->map[i] = ch; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2369 | else |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2370 | info->map[i] = -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | Py_DECREF(u); |
| 2374 | |
| 2375 | return XML_STATUS_OK; |
| 2376 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2377 | |
| 2378 | /* -------------------------------------------------------------------- */ |
| 2379 | /* constructor and destructor */ |
| 2380 | |
| 2381 | static PyObject* |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2382 | xmlparser(PyObject* self_, PyObject* args, PyObject* kw) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2383 | { |
| 2384 | XMLParserObject* self; |
| 2385 | /* FIXME: does this need to be static? */ |
| 2386 | static XML_Memory_Handling_Suite memory_handler; |
| 2387 | |
| 2388 | PyObject* target = NULL; |
| 2389 | char* encoding = NULL; |
Martin v. Löwis | 02cbf4a | 2006-02-27 17:20:04 +0000 | [diff] [blame] | 2390 | static char* kwlist[] = { "target", "encoding", NULL }; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2391 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Oz:XMLParser", kwlist, |
| 2392 | &target, &encoding)) |
| 2393 | return NULL; |
| 2394 | |
| 2395 | #if defined(USE_PYEXPAT_CAPI) |
| 2396 | if (!expat_capi) { |
| 2397 | PyErr_SetString( |
| 2398 | PyExc_RuntimeError, "cannot load dispatch table from pyexpat" |
| 2399 | ); |
| 2400 | return NULL; |
| 2401 | } |
| 2402 | #endif |
| 2403 | |
| 2404 | self = PyObject_New(XMLParserObject, &XMLParser_Type); |
| 2405 | if (self == NULL) |
| 2406 | return NULL; |
| 2407 | |
| 2408 | self->entity = PyDict_New(); |
| 2409 | if (!self->entity) { |
| 2410 | PyObject_Del(self); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2411 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2412 | } |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 2413 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2414 | self->names = PyDict_New(); |
| 2415 | if (!self->names) { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2416 | PyObject_Del(self->entity); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2417 | PyObject_Del(self); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2418 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | memory_handler.malloc_fcn = PyObject_Malloc; |
| 2422 | memory_handler.realloc_fcn = PyObject_Realloc; |
| 2423 | memory_handler.free_fcn = PyObject_Free; |
| 2424 | |
| 2425 | self->parser = EXPAT(ParserCreate_MM)(encoding, &memory_handler, "}"); |
| 2426 | if (!self->parser) { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2427 | PyObject_Del(self->names); |
| 2428 | PyObject_Del(self->entity); |
| 2429 | PyObject_Del(self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2430 | PyErr_NoMemory(); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2431 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | /* setup target handlers */ |
| 2435 | if (!target) { |
| 2436 | target = treebuilder_new(); |
| 2437 | if (!target) { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2438 | EXPAT(ParserFree)(self->parser); |
| 2439 | PyObject_Del(self->names); |
| 2440 | PyObject_Del(self->entity); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2441 | PyObject_Del(self); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2442 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2443 | } |
| 2444 | } else |
| 2445 | Py_INCREF(target); |
| 2446 | self->target = target; |
| 2447 | |
| 2448 | self->handle_xml = PyObject_GetAttrString(target, "xml"); |
| 2449 | self->handle_start = PyObject_GetAttrString(target, "start"); |
| 2450 | self->handle_data = PyObject_GetAttrString(target, "data"); |
| 2451 | self->handle_end = PyObject_GetAttrString(target, "end"); |
| 2452 | self->handle_comment = PyObject_GetAttrString(target, "comment"); |
| 2453 | self->handle_pi = PyObject_GetAttrString(target, "pi"); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2454 | self->handle_close = PyObject_GetAttrString(target, "close"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2455 | |
| 2456 | PyErr_Clear(); |
| 2457 | |
| 2458 | /* configure parser */ |
| 2459 | EXPAT(SetUserData)(self->parser, self); |
| 2460 | EXPAT(SetElementHandler)( |
| 2461 | self->parser, |
| 2462 | (XML_StartElementHandler) expat_start_handler, |
| 2463 | (XML_EndElementHandler) expat_end_handler |
| 2464 | ); |
| 2465 | EXPAT(SetDefaultHandlerExpand)( |
| 2466 | self->parser, |
| 2467 | (XML_DefaultHandler) expat_default_handler |
| 2468 | ); |
| 2469 | EXPAT(SetCharacterDataHandler)( |
| 2470 | self->parser, |
| 2471 | (XML_CharacterDataHandler) expat_data_handler |
| 2472 | ); |
| 2473 | if (self->handle_comment) |
| 2474 | EXPAT(SetCommentHandler)( |
| 2475 | self->parser, |
| 2476 | (XML_CommentHandler) expat_comment_handler |
| 2477 | ); |
| 2478 | if (self->handle_pi) |
| 2479 | EXPAT(SetProcessingInstructionHandler)( |
| 2480 | self->parser, |
| 2481 | (XML_ProcessingInstructionHandler) expat_pi_handler |
| 2482 | ); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2483 | EXPAT(SetUnknownEncodingHandler)( |
| 2484 | self->parser, |
| 2485 | (XML_UnknownEncodingHandler) expat_unknown_encoding_handler, NULL |
| 2486 | ); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2487 | |
| 2488 | ALLOC(sizeof(XMLParserObject), "create expatparser"); |
| 2489 | |
| 2490 | return (PyObject*) self; |
| 2491 | } |
| 2492 | |
| 2493 | static void |
| 2494 | xmlparser_dealloc(XMLParserObject* self) |
| 2495 | { |
| 2496 | EXPAT(ParserFree)(self->parser); |
| 2497 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2498 | Py_XDECREF(self->handle_close); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2499 | Py_XDECREF(self->handle_pi); |
| 2500 | Py_XDECREF(self->handle_comment); |
| 2501 | Py_XDECREF(self->handle_end); |
| 2502 | Py_XDECREF(self->handle_data); |
| 2503 | Py_XDECREF(self->handle_start); |
| 2504 | Py_XDECREF(self->handle_xml); |
| 2505 | |
| 2506 | Py_DECREF(self->target); |
| 2507 | Py_DECREF(self->entity); |
| 2508 | Py_DECREF(self->names); |
| 2509 | |
| 2510 | RELEASE(sizeof(XMLParserObject), "destroy expatparser"); |
| 2511 | |
| 2512 | PyObject_Del(self); |
| 2513 | } |
| 2514 | |
| 2515 | /* -------------------------------------------------------------------- */ |
| 2516 | /* methods (in alphabetical order) */ |
| 2517 | |
| 2518 | LOCAL(PyObject*) |
| 2519 | expat_parse(XMLParserObject* self, char* data, int data_len, int final) |
| 2520 | { |
| 2521 | int ok; |
| 2522 | |
| 2523 | ok = EXPAT(Parse)(self->parser, data, data_len, final); |
| 2524 | |
| 2525 | if (PyErr_Occurred()) |
| 2526 | return NULL; |
| 2527 | |
| 2528 | if (!ok) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2529 | expat_set_error( |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2530 | EXPAT(ErrorString)(EXPAT(GetErrorCode)(self->parser)), |
| 2531 | EXPAT(GetErrorLineNumber)(self->parser), |
| 2532 | EXPAT(GetErrorColumnNumber)(self->parser) |
| 2533 | ); |
| 2534 | return NULL; |
| 2535 | } |
| 2536 | |
| 2537 | Py_RETURN_NONE; |
| 2538 | } |
| 2539 | |
| 2540 | static PyObject* |
| 2541 | xmlparser_close(XMLParserObject* self, PyObject* args) |
| 2542 | { |
| 2543 | /* end feeding data to parser */ |
| 2544 | |
| 2545 | PyObject* res; |
| 2546 | if (!PyArg_ParseTuple(args, ":close")) |
| 2547 | return NULL; |
| 2548 | |
| 2549 | res = expat_parse(self, "", 0, 1); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2550 | if (!res) |
| 2551 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2552 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2553 | if (TreeBuilder_CheckExact(self->target)) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2554 | Py_DECREF(res); |
| 2555 | return treebuilder_done((TreeBuilderObject*) self->target); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2556 | } if (self->handle_close) { |
| 2557 | Py_DECREF(res); |
| 2558 | return PyObject_CallFunction(self->handle_close, ""); |
| 2559 | } else |
| 2560 | return res; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | static PyObject* |
| 2564 | xmlparser_feed(XMLParserObject* self, PyObject* args) |
| 2565 | { |
| 2566 | /* feed data to parser */ |
| 2567 | |
| 2568 | char* data; |
| 2569 | int data_len; |
| 2570 | if (!PyArg_ParseTuple(args, "s#:feed", &data, &data_len)) |
| 2571 | return NULL; |
| 2572 | |
| 2573 | return expat_parse(self, data, data_len, 0); |
| 2574 | } |
| 2575 | |
| 2576 | static PyObject* |
| 2577 | xmlparser_parse(XMLParserObject* self, PyObject* args) |
| 2578 | { |
| 2579 | /* (internal) parse until end of input stream */ |
| 2580 | |
| 2581 | PyObject* reader; |
| 2582 | PyObject* buffer; |
| 2583 | PyObject* res; |
| 2584 | |
| 2585 | PyObject* fileobj; |
| 2586 | if (!PyArg_ParseTuple(args, "O:_parse", &fileobj)) |
| 2587 | return NULL; |
| 2588 | |
| 2589 | reader = PyObject_GetAttrString(fileobj, "read"); |
| 2590 | if (!reader) |
| 2591 | return NULL; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 2592 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2593 | /* read from open file object */ |
| 2594 | for (;;) { |
| 2595 | |
| 2596 | buffer = PyObject_CallFunction(reader, "i", 64*1024); |
| 2597 | |
| 2598 | if (!buffer) { |
| 2599 | /* read failed (e.g. due to KeyboardInterrupt) */ |
| 2600 | Py_DECREF(reader); |
| 2601 | return NULL; |
| 2602 | } |
| 2603 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2604 | if (!PyBytes_CheckExact(buffer) || PyBytes_GET_SIZE(buffer) == 0) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2605 | Py_DECREF(buffer); |
| 2606 | break; |
| 2607 | } |
| 2608 | |
| 2609 | res = expat_parse( |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2610 | self, PyBytes_AS_STRING(buffer), PyBytes_GET_SIZE(buffer), 0 |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2611 | ); |
| 2612 | |
| 2613 | Py_DECREF(buffer); |
| 2614 | |
| 2615 | if (!res) { |
| 2616 | Py_DECREF(reader); |
| 2617 | return NULL; |
| 2618 | } |
| 2619 | Py_DECREF(res); |
| 2620 | |
| 2621 | } |
| 2622 | |
| 2623 | Py_DECREF(reader); |
| 2624 | |
| 2625 | res = expat_parse(self, "", 0, 1); |
| 2626 | |
| 2627 | if (res && TreeBuilder_CheckExact(self->target)) { |
| 2628 | Py_DECREF(res); |
| 2629 | return treebuilder_done((TreeBuilderObject*) self->target); |
| 2630 | } |
| 2631 | |
| 2632 | return res; |
| 2633 | } |
| 2634 | |
| 2635 | static PyObject* |
| 2636 | xmlparser_setevents(XMLParserObject* self, PyObject* args) |
| 2637 | { |
| 2638 | /* activate element event reporting */ |
| 2639 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2640 | Py_ssize_t i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2641 | TreeBuilderObject* target; |
| 2642 | |
| 2643 | PyObject* events; /* event collector */ |
| 2644 | PyObject* event_set = Py_None; |
| 2645 | if (!PyArg_ParseTuple(args, "O!|O:_setevents", &PyList_Type, &events, |
| 2646 | &event_set)) |
| 2647 | return NULL; |
| 2648 | |
| 2649 | if (!TreeBuilder_CheckExact(self->target)) { |
| 2650 | PyErr_SetString( |
| 2651 | PyExc_TypeError, |
| 2652 | "event handling only supported for cElementTree.Treebuilder " |
| 2653 | "targets" |
| 2654 | ); |
| 2655 | return NULL; |
| 2656 | } |
| 2657 | |
| 2658 | target = (TreeBuilderObject*) self->target; |
| 2659 | |
| 2660 | Py_INCREF(events); |
| 2661 | Py_XDECREF(target->events); |
| 2662 | target->events = events; |
| 2663 | |
| 2664 | /* clear out existing events */ |
| 2665 | Py_XDECREF(target->start_event_obj); target->start_event_obj = NULL; |
| 2666 | Py_XDECREF(target->end_event_obj); target->end_event_obj = NULL; |
| 2667 | Py_XDECREF(target->start_ns_event_obj); target->start_ns_event_obj = NULL; |
| 2668 | Py_XDECREF(target->end_ns_event_obj); target->end_ns_event_obj = NULL; |
| 2669 | |
| 2670 | if (event_set == Py_None) { |
| 2671 | /* default is "end" only */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2672 | target->end_event_obj = PyUnicode_FromString("end"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2673 | Py_RETURN_NONE; |
| 2674 | } |
| 2675 | |
| 2676 | if (!PyTuple_Check(event_set)) /* FIXME: handle arbitrary sequences */ |
| 2677 | goto error; |
| 2678 | |
| 2679 | for (i = 0; i < PyTuple_GET_SIZE(event_set); i++) { |
| 2680 | PyObject* item = PyTuple_GET_ITEM(event_set, i); |
| 2681 | char* event; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2682 | if (PyUnicode_Check(item)) { |
| 2683 | event = _PyUnicode_AsString(item); |
Victor Stinner | 0477bf3 | 2010-03-22 12:11:44 +0000 | [diff] [blame] | 2684 | if (event == NULL) |
| 2685 | goto error; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2686 | } else if (PyBytes_Check(item)) |
| 2687 | event = PyBytes_AS_STRING(item); |
| 2688 | else { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2689 | goto error; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2690 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2691 | if (strcmp(event, "start") == 0) { |
| 2692 | Py_INCREF(item); |
| 2693 | target->start_event_obj = item; |
| 2694 | } else if (strcmp(event, "end") == 0) { |
| 2695 | Py_INCREF(item); |
| 2696 | Py_XDECREF(target->end_event_obj); |
| 2697 | target->end_event_obj = item; |
| 2698 | } else if (strcmp(event, "start-ns") == 0) { |
| 2699 | Py_INCREF(item); |
| 2700 | Py_XDECREF(target->start_ns_event_obj); |
| 2701 | target->start_ns_event_obj = item; |
| 2702 | EXPAT(SetNamespaceDeclHandler)( |
| 2703 | self->parser, |
| 2704 | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 2705 | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 2706 | ); |
| 2707 | } else if (strcmp(event, "end-ns") == 0) { |
| 2708 | Py_INCREF(item); |
| 2709 | Py_XDECREF(target->end_ns_event_obj); |
| 2710 | target->end_ns_event_obj = item; |
| 2711 | EXPAT(SetNamespaceDeclHandler)( |
| 2712 | self->parser, |
| 2713 | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 2714 | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 2715 | ); |
| 2716 | } else { |
| 2717 | PyErr_Format( |
| 2718 | PyExc_ValueError, |
| 2719 | "unknown event '%s'", event |
| 2720 | ); |
| 2721 | return NULL; |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | Py_RETURN_NONE; |
| 2726 | |
| 2727 | error: |
| 2728 | PyErr_SetString( |
| 2729 | PyExc_TypeError, |
| 2730 | "invalid event tuple" |
| 2731 | ); |
| 2732 | return NULL; |
| 2733 | } |
| 2734 | |
| 2735 | static PyMethodDef xmlparser_methods[] = { |
| 2736 | {"feed", (PyCFunction) xmlparser_feed, METH_VARARGS}, |
| 2737 | {"close", (PyCFunction) xmlparser_close, METH_VARARGS}, |
| 2738 | {"_parse", (PyCFunction) xmlparser_parse, METH_VARARGS}, |
| 2739 | {"_setevents", (PyCFunction) xmlparser_setevents, METH_VARARGS}, |
| 2740 | {NULL, NULL} |
| 2741 | }; |
| 2742 | |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 2743 | static PyObject* |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 2744 | xmlparser_getattro(XMLParserObject* self, PyObject* nameobj) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2745 | { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2746 | if (PyUnicode_Check(nameobj)) { |
| 2747 | PyObject* res; |
| 2748 | if (PyUnicode_CompareWithASCIIString(nameobj, "entity") == 0) |
| 2749 | res = self->entity; |
| 2750 | else if (PyUnicode_CompareWithASCIIString(nameobj, "target") == 0) |
| 2751 | res = self->target; |
| 2752 | else if (PyUnicode_CompareWithASCIIString(nameobj, "version") == 0) { |
| 2753 | return PyUnicode_FromFormat( |
| 2754 | "Expat %d.%d.%d", XML_MAJOR_VERSION, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2755 | XML_MINOR_VERSION, XML_MICRO_VERSION); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2756 | } |
| 2757 | else |
| 2758 | goto generic; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2759 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2760 | Py_INCREF(res); |
| 2761 | return res; |
| 2762 | } |
| 2763 | generic: |
| 2764 | return PyObject_GenericGetAttr((PyObject*) self, nameobj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 2767 | static PyTypeObject XMLParser_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2768 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2769 | "XMLParser", sizeof(XMLParserObject), 0, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2770 | /* methods */ |
| 2771 | (destructor)xmlparser_dealloc, /* tp_dealloc */ |
| 2772 | 0, /* tp_print */ |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 2773 | 0, /* tp_getattr */ |
| 2774 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 2775 | 0, /* tp_reserved */ |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 2776 | 0, /* tp_repr */ |
| 2777 | 0, /* tp_as_number */ |
| 2778 | 0, /* tp_as_sequence */ |
| 2779 | 0, /* tp_as_mapping */ |
| 2780 | 0, /* tp_hash */ |
| 2781 | 0, /* tp_call */ |
| 2782 | 0, /* tp_str */ |
| 2783 | (getattrofunc)xmlparser_getattro, /* tp_getattro */ |
| 2784 | 0, /* tp_setattro */ |
| 2785 | 0, /* tp_as_buffer */ |
| 2786 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2787 | 0, /* tp_doc */ |
| 2788 | 0, /* tp_traverse */ |
| 2789 | 0, /* tp_clear */ |
| 2790 | 0, /* tp_richcompare */ |
| 2791 | 0, /* tp_weaklistoffset */ |
| 2792 | 0, /* tp_iter */ |
| 2793 | 0, /* tp_iternext */ |
| 2794 | xmlparser_methods, /* tp_methods */ |
| 2795 | 0, /* tp_members */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2796 | }; |
| 2797 | |
| 2798 | #endif |
| 2799 | |
| 2800 | /* ==================================================================== */ |
| 2801 | /* python module interface */ |
| 2802 | |
| 2803 | static PyMethodDef _functions[] = { |
| 2804 | {"Element", (PyCFunction) element, METH_VARARGS|METH_KEYWORDS}, |
| 2805 | {"SubElement", (PyCFunction) subelement, METH_VARARGS|METH_KEYWORDS}, |
| 2806 | {"TreeBuilder", (PyCFunction) treebuilder, METH_VARARGS}, |
| 2807 | #if defined(USE_EXPAT) |
| 2808 | {"XMLParser", (PyCFunction) xmlparser, METH_VARARGS|METH_KEYWORDS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2809 | #endif |
| 2810 | {NULL, NULL} |
| 2811 | }; |
| 2812 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2813 | |
| 2814 | static struct PyModuleDef _elementtreemodule = { |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 2815 | PyModuleDef_HEAD_INIT, |
| 2816 | "_elementtree", |
| 2817 | NULL, |
| 2818 | -1, |
| 2819 | _functions, |
| 2820 | NULL, |
| 2821 | NULL, |
| 2822 | NULL, |
| 2823 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2824 | }; |
| 2825 | |
Neal Norwitz | f6657e6 | 2006-12-28 04:47:50 +0000 | [diff] [blame] | 2826 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2827 | PyInit__elementtree(void) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2828 | { |
| 2829 | PyObject* m; |
| 2830 | PyObject* g; |
| 2831 | char* bootstrap; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2832 | |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 2833 | /* Initialize object types */ |
| 2834 | if (PyType_Ready(&TreeBuilder_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 2835 | return NULL; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 2836 | if (PyType_Ready(&Element_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 2837 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2838 | #if defined(USE_EXPAT) |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 2839 | if (PyType_Ready(&XMLParser_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 2840 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2841 | #endif |
| 2842 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2843 | m = PyModule_Create(&_elementtreemodule); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2844 | if (!m) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2845 | return NULL; |
| 2846 | |
| 2847 | /* The code below requires that the module gets already added |
| 2848 | to sys.modules. */ |
| 2849 | PyDict_SetItemString(PyImport_GetModuleDict(), |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 2850 | _elementtreemodule.m_name, |
| 2851 | m); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2852 | |
| 2853 | /* python glue code */ |
| 2854 | |
| 2855 | g = PyDict_New(); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2856 | if (!g) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2857 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2858 | |
| 2859 | PyDict_SetItemString(g, "__builtins__", PyEval_GetBuiltins()); |
| 2860 | |
| 2861 | bootstrap = ( |
| 2862 | |
Florent Xicluna | f4bdf4e | 2012-02-11 11:28:16 +0100 | [diff] [blame^] | 2863 | "from copy import deepcopy\n" |
| 2864 | "from xml.etree import ElementPath\n" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2865 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2866 | "def iter(node, tag=None):\n" /* helper */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2867 | " if tag == '*':\n" |
| 2868 | " tag = None\n" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2869 | " if tag is None or node.tag == tag:\n" |
| 2870 | " yield node\n" |
| 2871 | " for node in node:\n" |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2872 | " for node in iter(node, tag):\n" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2873 | " yield node\n" |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2874 | |
| 2875 | "def itertext(node):\n" /* helper */ |
| 2876 | " if node.text:\n" |
| 2877 | " yield node.text\n" |
| 2878 | " for e in node:\n" |
| 2879 | " for s in e.itertext():\n" |
| 2880 | " yield s\n" |
| 2881 | " if e.tail:\n" |
| 2882 | " yield e.tail\n" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2883 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2884 | ); |
| 2885 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2886 | if (!PyRun_String(bootstrap, Py_file_input, g, NULL)) |
| 2887 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2888 | |
| 2889 | elementpath_obj = PyDict_GetItemString(g, "ElementPath"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2890 | elementtree_deepcopy_obj = PyDict_GetItemString(g, "deepcopy"); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2891 | elementtree_iter_obj = PyDict_GetItemString(g, "iter"); |
| 2892 | elementtree_itertext_obj = PyDict_GetItemString(g, "itertext"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2893 | |
| 2894 | #if defined(USE_PYEXPAT_CAPI) |
| 2895 | /* link against pyexpat, if possible */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2896 | expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0); |
| 2897 | if (expat_capi) { |
| 2898 | /* check that it's usable */ |
| 2899 | if (strcmp(expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 || |
| 2900 | expat_capi->size < sizeof(struct PyExpat_CAPI) || |
| 2901 | expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION || |
| 2902 | expat_capi->MINOR_VERSION != XML_MINOR_VERSION || |
| 2903 | expat_capi->MICRO_VERSION != XML_MICRO_VERSION) |
| 2904 | expat_capi = NULL; |
| 2905 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2906 | #endif |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2907 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2908 | elementtree_parseerror_obj = PyErr_NewException( |
| 2909 | "cElementTree.ParseError", PyExc_SyntaxError, NULL |
| 2910 | ); |
| 2911 | Py_INCREF(elementtree_parseerror_obj); |
| 2912 | PyModule_AddObject(m, "ParseError", elementtree_parseerror_obj); |
| 2913 | |
| 2914 | return m; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2915 | } |