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