Eli Bendersky | bf05df2 | 2013-04-20 05:44:01 -0700 | [diff] [blame] | 1 | /*-------------------------------------------------------------------- |
| 2 | * Licensed to PSF under a Contributor Agreement. |
| 3 | * See http://www.python.org/psf/license for licensing details. |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 4 | * |
Eli Bendersky | bf05df2 | 2013-04-20 05:44:01 -0700 | [diff] [blame] | 5 | * _elementtree - C accelerator for xml.etree.ElementTree |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 6 | * Copyright (c) 1999-2009 by Secret Labs AB. All rights reserved. |
| 7 | * Copyright (c) 1999-2009 by Fredrik Lundh. |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 8 | * |
| 9 | * info@pythonware.com |
| 10 | * http://www.pythonware.com |
Eli Bendersky | bf05df2 | 2013-04-20 05:44:01 -0700 | [diff] [blame] | 11 | *-------------------------------------------------------------------- |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "Python.h" |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 15 | #include "structmember.h" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 16 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 17 | /* -------------------------------------------------------------------- */ |
| 18 | /* configuration */ |
| 19 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 20 | /* An element can hold this many children without extra memory |
| 21 | allocations. */ |
| 22 | #define STATIC_CHILDREN 4 |
| 23 | |
| 24 | /* For best performance, chose a value so that 80-90% of all nodes |
| 25 | have no more than the given number of children. Set this to zero |
| 26 | to minimize the size of the element structure itself (this only |
| 27 | helps if you have lots of leaf nodes with attributes). */ |
| 28 | |
| 29 | /* Also note that pymalloc always allocates blocks in multiples of |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 30 | eight bytes. For the current C version of ElementTree, this means |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 31 | that the number of children should be an even number, at least on |
| 32 | 32-bit platforms. */ |
| 33 | |
| 34 | /* -------------------------------------------------------------------- */ |
| 35 | |
| 36 | #if 0 |
| 37 | static int memory = 0; |
| 38 | #define ALLOC(size, comment)\ |
| 39 | do { memory += size; printf("%8d - %s\n", memory, comment); } while (0) |
| 40 | #define RELEASE(size, comment)\ |
| 41 | do { memory -= size; printf("%8d - %s\n", memory, comment); } while (0) |
| 42 | #else |
| 43 | #define ALLOC(size, comment) |
| 44 | #define RELEASE(size, comment) |
| 45 | #endif |
| 46 | |
| 47 | /* compiler tweaks */ |
| 48 | #if defined(_MSC_VER) |
| 49 | #define LOCAL(type) static __inline type __fastcall |
| 50 | #else |
| 51 | #define LOCAL(type) static type |
| 52 | #endif |
| 53 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 54 | /* macros used to store 'join' flags in string object pointers. note |
| 55 | that all use of text and tail as object pointers must be wrapped in |
| 56 | JOIN_OBJ. see comments in the ElementObject definition for more |
| 57 | info. */ |
| 58 | #define JOIN_GET(p) ((Py_uintptr_t) (p) & 1) |
| 59 | #define JOIN_SET(p, flag) ((void*) ((Py_uintptr_t) (JOIN_OBJ(p)) | (flag))) |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 60 | #define JOIN_OBJ(p) ((PyObject*) ((Py_uintptr_t) (p) & ~(Py_uintptr_t)1)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 61 | |
Eli Bendersky | dd3661e | 2013-09-13 06:24:25 -0700 | [diff] [blame] | 62 | /* Py_CLEAR for a PyObject* that uses a join flag. Pass the pointer by |
| 63 | * reference since this function sets it to NULL. |
| 64 | */ |
doko@ubuntu.com | 0648bf7 | 2013-09-18 12:12:28 +0200 | [diff] [blame] | 65 | static void _clear_joined_ptr(PyObject **p) |
Eli Bendersky | dd3661e | 2013-09-13 06:24:25 -0700 | [diff] [blame] | 66 | { |
| 67 | if (*p) { |
| 68 | PyObject *tmp = JOIN_OBJ(*p); |
| 69 | *p = NULL; |
| 70 | Py_DECREF(tmp); |
| 71 | } |
| 72 | } |
| 73 | |
Ronald Oussoren | 138d080 | 2013-07-19 11:11:25 +0200 | [diff] [blame] | 74 | /* Types defined by this extension */ |
| 75 | static PyTypeObject Element_Type; |
| 76 | static PyTypeObject ElementIter_Type; |
| 77 | static PyTypeObject TreeBuilder_Type; |
| 78 | static PyTypeObject XMLParser_Type; |
| 79 | |
| 80 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 81 | /* Per-module state; PEP 3121 */ |
| 82 | typedef struct { |
| 83 | PyObject *parseerror_obj; |
| 84 | PyObject *deepcopy_obj; |
| 85 | PyObject *elementpath_obj; |
| 86 | } elementtreestate; |
| 87 | |
| 88 | static struct PyModuleDef elementtreemodule; |
| 89 | |
| 90 | /* Given a module object (assumed to be _elementtree), get its per-module |
| 91 | * state. |
| 92 | */ |
| 93 | #define ET_STATE(mod) ((elementtreestate *) PyModule_GetState(mod)) |
| 94 | |
| 95 | /* Find the module instance imported in the currently running sub-interpreter |
| 96 | * and get its state. |
| 97 | */ |
| 98 | #define ET_STATE_GLOBAL \ |
| 99 | ((elementtreestate *) PyModule_GetState(PyState_FindModule(&elementtreemodule))) |
| 100 | |
| 101 | static int |
| 102 | elementtree_clear(PyObject *m) |
| 103 | { |
| 104 | elementtreestate *st = ET_STATE(m); |
| 105 | Py_CLEAR(st->parseerror_obj); |
| 106 | Py_CLEAR(st->deepcopy_obj); |
| 107 | Py_CLEAR(st->elementpath_obj); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int |
| 112 | elementtree_traverse(PyObject *m, visitproc visit, void *arg) |
| 113 | { |
| 114 | elementtreestate *st = ET_STATE(m); |
| 115 | Py_VISIT(st->parseerror_obj); |
| 116 | Py_VISIT(st->deepcopy_obj); |
| 117 | Py_VISIT(st->elementpath_obj); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | elementtree_free(void *m) |
| 123 | { |
| 124 | elementtree_clear((PyObject *)m); |
| 125 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 126 | |
| 127 | /* helpers */ |
| 128 | |
| 129 | LOCAL(PyObject*) |
| 130 | deepcopy(PyObject* object, PyObject* memo) |
| 131 | { |
| 132 | /* do a deep copy of the given object */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 133 | PyObject* args; |
| 134 | PyObject* result; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 135 | elementtreestate *st = ET_STATE_GLOBAL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 136 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 137 | if (!st->deepcopy_obj) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 138 | PyErr_SetString( |
| 139 | PyExc_RuntimeError, |
| 140 | "deepcopy helper not found" |
| 141 | ); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 145 | args = PyTuple_Pack(2, object, memo); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 146 | if (!args) |
| 147 | return NULL; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 148 | result = PyObject_CallObject(st->deepcopy_obj, args); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 149 | Py_DECREF(args); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 150 | return result; |
| 151 | } |
| 152 | |
| 153 | LOCAL(PyObject*) |
| 154 | list_join(PyObject* list) |
| 155 | { |
| 156 | /* join list elements (destroying the list in the process) */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 157 | PyObject* joiner; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 158 | PyObject* result; |
| 159 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 160 | joiner = PyUnicode_FromStringAndSize("", 0); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 161 | if (!joiner) |
| 162 | return NULL; |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 163 | result = PyUnicode_Join(joiner, list); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 164 | Py_DECREF(joiner); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 165 | if (result) |
| 166 | Py_DECREF(list); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 167 | return result; |
| 168 | } |
| 169 | |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 170 | /* Is the given object an empty dictionary? |
| 171 | */ |
| 172 | static int |
| 173 | is_empty_dict(PyObject *obj) |
| 174 | { |
| 175 | return PyDict_CheckExact(obj) && PyDict_Size(obj) == 0; |
| 176 | } |
| 177 | |
| 178 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 179 | /* -------------------------------------------------------------------- */ |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 180 | /* the Element type */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 181 | |
| 182 | typedef struct { |
| 183 | |
| 184 | /* attributes (a dictionary object), or None if no attributes */ |
| 185 | PyObject* attrib; |
| 186 | |
| 187 | /* child elements */ |
| 188 | int length; /* actual number of items */ |
| 189 | int allocated; /* allocated items */ |
| 190 | |
| 191 | /* this either points to _children or to a malloced buffer */ |
| 192 | PyObject* *children; |
| 193 | |
| 194 | PyObject* _children[STATIC_CHILDREN]; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 195 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 196 | } ElementObjectExtra; |
| 197 | |
| 198 | typedef struct { |
| 199 | PyObject_HEAD |
| 200 | |
| 201 | /* element tag (a string). */ |
| 202 | PyObject* tag; |
| 203 | |
| 204 | /* text before first child. note that this is a tagged pointer; |
| 205 | use JOIN_OBJ to get the object pointer. the join flag is used |
| 206 | to distinguish lists created by the tree builder from lists |
| 207 | assigned to the attribute by application code; the former |
| 208 | should be joined before being returned to the user, the latter |
| 209 | should be left intact. */ |
| 210 | PyObject* text; |
| 211 | |
| 212 | /* text after this element, in parent. note that this is a tagged |
| 213 | pointer; use JOIN_OBJ to get the object pointer. */ |
| 214 | PyObject* tail; |
| 215 | |
| 216 | ElementObjectExtra* extra; |
| 217 | |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 218 | PyObject *weakreflist; /* For tp_weaklistoffset */ |
| 219 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 220 | } ElementObject; |
| 221 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 222 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 223 | #define Element_CheckExact(op) (Py_TYPE(op) == &Element_Type) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 224 | |
| 225 | /* -------------------------------------------------------------------- */ |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 226 | /* Element constructors and destructor */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 227 | |
| 228 | LOCAL(int) |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 229 | create_extra(ElementObject* self, PyObject* attrib) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 230 | { |
| 231 | self->extra = PyObject_Malloc(sizeof(ElementObjectExtra)); |
Victor Stinner | 81aac73 | 2013-07-12 02:03:34 +0200 | [diff] [blame] | 232 | if (!self->extra) { |
| 233 | PyErr_NoMemory(); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 234 | return -1; |
Victor Stinner | 81aac73 | 2013-07-12 02:03:34 +0200 | [diff] [blame] | 235 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 236 | |
| 237 | if (!attrib) |
| 238 | attrib = Py_None; |
| 239 | |
| 240 | Py_INCREF(attrib); |
| 241 | self->extra->attrib = attrib; |
| 242 | |
| 243 | self->extra->length = 0; |
| 244 | self->extra->allocated = STATIC_CHILDREN; |
| 245 | self->extra->children = self->extra->_children; |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | LOCAL(void) |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 251 | dealloc_extra(ElementObject* self) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 252 | { |
Eli Bendersky | 08b8529 | 2012-04-04 15:55:07 +0300 | [diff] [blame] | 253 | ElementObjectExtra *myextra; |
| 254 | int i; |
| 255 | |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 256 | if (!self->extra) |
| 257 | return; |
| 258 | |
| 259 | /* Avoid DECREFs calling into this code again (cycles, etc.) |
| 260 | */ |
Eli Bendersky | 08b8529 | 2012-04-04 15:55:07 +0300 | [diff] [blame] | 261 | myextra = self->extra; |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 262 | self->extra = NULL; |
| 263 | |
| 264 | Py_DECREF(myextra->attrib); |
| 265 | |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 266 | for (i = 0; i < myextra->length; i++) |
| 267 | Py_DECREF(myextra->children[i]); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 268 | |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 269 | if (myextra->children != myextra->_children) |
| 270 | PyObject_Free(myextra->children); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 271 | |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 272 | PyObject_Free(myextra); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 275 | /* Convenience internal function to create new Element objects with the given |
| 276 | * tag and attributes. |
| 277 | */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 278 | LOCAL(PyObject*) |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 279 | create_new_element(PyObject* tag, PyObject* attrib) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 280 | { |
| 281 | ElementObject* self; |
| 282 | |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 283 | self = PyObject_GC_New(ElementObject, &Element_Type); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 284 | if (self == NULL) |
| 285 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 286 | self->extra = NULL; |
| 287 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 288 | Py_INCREF(tag); |
| 289 | self->tag = tag; |
| 290 | |
| 291 | Py_INCREF(Py_None); |
| 292 | self->text = Py_None; |
| 293 | |
| 294 | Py_INCREF(Py_None); |
| 295 | self->tail = Py_None; |
| 296 | |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 297 | self->weakreflist = NULL; |
| 298 | |
Victor Stinner | d917dcb | 2013-07-12 02:05:17 +0200 | [diff] [blame] | 299 | ALLOC(sizeof(ElementObject), "create element"); |
| 300 | PyObject_GC_Track(self); |
| 301 | |
Victor Stinner | 71c8b7e | 2013-07-11 23:08:39 +0200 | [diff] [blame] | 302 | if (attrib != Py_None && !is_empty_dict(attrib)) { |
| 303 | if (create_extra(self, attrib) < 0) { |
Victor Stinner | d917dcb | 2013-07-12 02:05:17 +0200 | [diff] [blame] | 304 | Py_DECREF(self); |
Victor Stinner | 71c8b7e | 2013-07-11 23:08:39 +0200 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
| 307 | } |
| 308 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 309 | return (PyObject*) self; |
| 310 | } |
| 311 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 312 | static PyObject * |
| 313 | element_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 314 | { |
| 315 | ElementObject *e = (ElementObject *)type->tp_alloc(type, 0); |
| 316 | if (e != NULL) { |
| 317 | Py_INCREF(Py_None); |
| 318 | e->tag = Py_None; |
| 319 | |
| 320 | Py_INCREF(Py_None); |
| 321 | e->text = Py_None; |
| 322 | |
| 323 | Py_INCREF(Py_None); |
| 324 | e->tail = Py_None; |
| 325 | |
| 326 | e->extra = NULL; |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 327 | e->weakreflist = NULL; |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 328 | } |
| 329 | return (PyObject *)e; |
| 330 | } |
| 331 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 332 | /* Helper function for extracting the attrib dictionary from a keywords dict. |
| 333 | * This is required by some constructors/functions in this module that can |
Eli Bendersky | 4583990 | 2013-01-13 05:14:47 -0800 | [diff] [blame] | 334 | * either accept attrib as a keyword argument or all attributes splashed |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 335 | * directly into *kwds. |
Eli Bendersky | d4cb4b7 | 2013-04-22 05:25:25 -0700 | [diff] [blame] | 336 | * |
| 337 | * Return a dictionary with the content of kwds merged into the content of |
| 338 | * attrib. If there is no attrib keyword, return a copy of kwds. |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 339 | */ |
| 340 | static PyObject* |
| 341 | get_attrib_from_keywords(PyObject *kwds) |
| 342 | { |
Eli Bendersky | 45f3d2f | 2013-04-24 05:34:07 -0700 | [diff] [blame] | 343 | PyObject *attrib_str = PyUnicode_FromString("attrib"); |
| 344 | PyObject *attrib = PyDict_GetItem(kwds, attrib_str); |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 345 | |
| 346 | if (attrib) { |
| 347 | /* If attrib was found in kwds, copy its value and remove it from |
| 348 | * kwds |
| 349 | */ |
| 350 | if (!PyDict_Check(attrib)) { |
Eli Bendersky | 45f3d2f | 2013-04-24 05:34:07 -0700 | [diff] [blame] | 351 | Py_DECREF(attrib_str); |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 352 | PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s", |
| 353 | Py_TYPE(attrib)->tp_name); |
| 354 | return NULL; |
| 355 | } |
| 356 | attrib = PyDict_Copy(attrib); |
Eli Bendersky | 45f3d2f | 2013-04-24 05:34:07 -0700 | [diff] [blame] | 357 | PyDict_DelItem(kwds, attrib_str); |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 358 | } else { |
| 359 | attrib = PyDict_New(); |
| 360 | } |
Eli Bendersky | 45f3d2f | 2013-04-24 05:34:07 -0700 | [diff] [blame] | 361 | |
| 362 | Py_DECREF(attrib_str); |
| 363 | |
| 364 | /* attrib can be NULL if PyDict_New failed */ |
| 365 | if (attrib) |
Christian Heimes | 7ed4294 | 2013-07-20 15:12:09 +0200 | [diff] [blame] | 366 | if (PyDict_Update(attrib, kwds) < 0) |
| 367 | return NULL; |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 368 | return attrib; |
| 369 | } |
| 370 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 371 | static int |
| 372 | element_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 373 | { |
| 374 | PyObject *tag; |
| 375 | PyObject *tmp; |
| 376 | PyObject *attrib = NULL; |
| 377 | ElementObject *self_elem; |
| 378 | |
| 379 | if (!PyArg_ParseTuple(args, "O|O!:Element", &tag, &PyDict_Type, &attrib)) |
| 380 | return -1; |
| 381 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 382 | if (attrib) { |
| 383 | /* attrib passed as positional arg */ |
| 384 | attrib = PyDict_Copy(attrib); |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 385 | if (!attrib) |
| 386 | return -1; |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 387 | if (kwds) { |
| 388 | if (PyDict_Update(attrib, kwds) < 0) { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 389 | Py_DECREF(attrib); |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 390 | return -1; |
| 391 | } |
| 392 | } |
| 393 | } else if (kwds) { |
| 394 | /* have keywords args */ |
| 395 | attrib = get_attrib_from_keywords(kwds); |
| 396 | if (!attrib) |
| 397 | return -1; |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | self_elem = (ElementObject *)self; |
| 401 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 402 | if (attrib != NULL && !is_empty_dict(attrib)) { |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 403 | if (create_extra(self_elem, attrib) < 0) { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 404 | Py_DECREF(attrib); |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 405 | return -1; |
| 406 | } |
| 407 | } |
| 408 | |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 409 | /* We own a reference to attrib here and it's no longer needed. */ |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 410 | Py_XDECREF(attrib); |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 411 | |
| 412 | /* Replace the objects already pointed to by tag, text and tail. */ |
| 413 | tmp = self_elem->tag; |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 414 | Py_INCREF(tag); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 415 | self_elem->tag = tag; |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 416 | Py_DECREF(tmp); |
| 417 | |
| 418 | tmp = self_elem->text; |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 419 | Py_INCREF(Py_None); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 420 | self_elem->text = Py_None; |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 421 | Py_DECREF(JOIN_OBJ(tmp)); |
| 422 | |
| 423 | tmp = self_elem->tail; |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 424 | Py_INCREF(Py_None); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 425 | self_elem->tail = Py_None; |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 426 | Py_DECREF(JOIN_OBJ(tmp)); |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 431 | LOCAL(int) |
| 432 | element_resize(ElementObject* self, int extra) |
| 433 | { |
| 434 | int size; |
| 435 | PyObject* *children; |
| 436 | |
| 437 | /* make sure self->children can hold the given number of extra |
| 438 | elements. set an exception and return -1 if allocation failed */ |
| 439 | |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 440 | if (!self->extra) { |
| 441 | if (create_extra(self, NULL) < 0) |
| 442 | return -1; |
| 443 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 444 | |
| 445 | size = self->extra->length + extra; |
| 446 | |
| 447 | if (size > self->extra->allocated) { |
| 448 | /* use Python 2.4's list growth strategy */ |
| 449 | size = (size >> 3) + (size < 9 ? 3 : 6) + size; |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 450 | /* Coverity CID #182 size_error: Allocating 1 bytes to pointer "children" |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 451 | * which needs at least 4 bytes. |
| 452 | * Although it's a false alarm always assume at least one child to |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 453 | * be safe. |
| 454 | */ |
| 455 | size = size ? size : 1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 456 | if (self->extra->children != self->extra->_children) { |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 457 | /* Coverity CID #182 size_error: Allocating 1 bytes to pointer |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 458 | * "children", which needs at least 4 bytes. Although it's a |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 459 | * false alarm always assume at least one child to be safe. |
| 460 | */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 461 | children = PyObject_Realloc(self->extra->children, |
| 462 | size * sizeof(PyObject*)); |
| 463 | if (!children) |
| 464 | goto nomemory; |
| 465 | } else { |
| 466 | children = PyObject_Malloc(size * sizeof(PyObject*)); |
| 467 | if (!children) |
| 468 | goto nomemory; |
| 469 | /* copy existing children from static area to malloc buffer */ |
| 470 | memcpy(children, self->extra->children, |
| 471 | self->extra->length * sizeof(PyObject*)); |
| 472 | } |
| 473 | self->extra->children = children; |
| 474 | self->extra->allocated = size; |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | |
| 479 | nomemory: |
| 480 | PyErr_NoMemory(); |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | LOCAL(int) |
| 485 | element_add_subelement(ElementObject* self, PyObject* element) |
| 486 | { |
| 487 | /* add a child element to a parent */ |
| 488 | |
| 489 | if (element_resize(self, 1) < 0) |
| 490 | return -1; |
| 491 | |
| 492 | Py_INCREF(element); |
| 493 | self->extra->children[self->extra->length] = element; |
| 494 | |
| 495 | self->extra->length++; |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | LOCAL(PyObject*) |
| 501 | element_get_attrib(ElementObject* self) |
| 502 | { |
| 503 | /* return borrowed reference to attrib dictionary */ |
| 504 | /* note: this function assumes that the extra section exists */ |
| 505 | |
| 506 | PyObject* res = self->extra->attrib; |
| 507 | |
| 508 | if (res == Py_None) { |
| 509 | /* create missing dictionary */ |
| 510 | res = PyDict_New(); |
| 511 | if (!res) |
| 512 | return NULL; |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 513 | Py_DECREF(Py_None); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 514 | self->extra->attrib = res; |
| 515 | } |
| 516 | |
| 517 | return res; |
| 518 | } |
| 519 | |
| 520 | LOCAL(PyObject*) |
| 521 | element_get_text(ElementObject* self) |
| 522 | { |
| 523 | /* return borrowed reference to text attribute */ |
| 524 | |
| 525 | PyObject* res = self->text; |
| 526 | |
| 527 | if (JOIN_GET(res)) { |
| 528 | res = JOIN_OBJ(res); |
| 529 | if (PyList_CheckExact(res)) { |
| 530 | res = list_join(res); |
| 531 | if (!res) |
| 532 | return NULL; |
| 533 | self->text = res; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return res; |
| 538 | } |
| 539 | |
| 540 | LOCAL(PyObject*) |
| 541 | element_get_tail(ElementObject* self) |
| 542 | { |
| 543 | /* return borrowed reference to text attribute */ |
| 544 | |
| 545 | PyObject* res = self->tail; |
| 546 | |
| 547 | if (JOIN_GET(res)) { |
| 548 | res = JOIN_OBJ(res); |
| 549 | if (PyList_CheckExact(res)) { |
| 550 | res = list_join(res); |
| 551 | if (!res) |
| 552 | return NULL; |
| 553 | self->tail = res; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return res; |
| 558 | } |
| 559 | |
| 560 | static PyObject* |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 561 | subelement(PyObject *self, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 562 | { |
| 563 | PyObject* elem; |
| 564 | |
| 565 | ElementObject* parent; |
| 566 | PyObject* tag; |
| 567 | PyObject* attrib = NULL; |
| 568 | if (!PyArg_ParseTuple(args, "O!O|O!:SubElement", |
| 569 | &Element_Type, &parent, &tag, |
| 570 | &PyDict_Type, &attrib)) |
| 571 | return NULL; |
| 572 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 573 | if (attrib) { |
| 574 | /* attrib passed as positional arg */ |
| 575 | attrib = PyDict_Copy(attrib); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 576 | if (!attrib) |
| 577 | return NULL; |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 578 | if (kwds) { |
| 579 | if (PyDict_Update(attrib, kwds) < 0) { |
| 580 | return NULL; |
| 581 | } |
| 582 | } |
| 583 | } else if (kwds) { |
| 584 | /* have keyword args */ |
| 585 | attrib = get_attrib_from_keywords(kwds); |
| 586 | if (!attrib) |
| 587 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 588 | } else { |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 589 | /* no attrib arg, no kwds, so no attribute */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 590 | Py_INCREF(Py_None); |
| 591 | attrib = Py_None; |
| 592 | } |
| 593 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 594 | elem = create_new_element(tag, attrib); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 595 | Py_DECREF(attrib); |
Victor Stinner | 71c8b7e | 2013-07-11 23:08:39 +0200 | [diff] [blame] | 596 | if (elem == NULL) |
| 597 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 598 | |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 599 | if (element_add_subelement(parent, elem) < 0) { |
| 600 | Py_DECREF(elem); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 601 | return NULL; |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 602 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 603 | |
| 604 | return elem; |
| 605 | } |
| 606 | |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 607 | static int |
| 608 | element_gc_traverse(ElementObject *self, visitproc visit, void *arg) |
| 609 | { |
| 610 | Py_VISIT(self->tag); |
| 611 | Py_VISIT(JOIN_OBJ(self->text)); |
| 612 | Py_VISIT(JOIN_OBJ(self->tail)); |
| 613 | |
| 614 | if (self->extra) { |
| 615 | int i; |
| 616 | Py_VISIT(self->extra->attrib); |
| 617 | |
| 618 | for (i = 0; i < self->extra->length; ++i) |
| 619 | Py_VISIT(self->extra->children[i]); |
| 620 | } |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int |
| 625 | element_gc_clear(ElementObject *self) |
| 626 | { |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 627 | Py_CLEAR(self->tag); |
Eli Bendersky | dd3661e | 2013-09-13 06:24:25 -0700 | [diff] [blame] | 628 | _clear_joined_ptr(&self->text); |
| 629 | _clear_joined_ptr(&self->tail); |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 630 | |
| 631 | /* After dropping all references from extra, it's no longer valid anyway, |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 632 | * so fully deallocate it. |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 633 | */ |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 634 | dealloc_extra(self); |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 638 | static void |
| 639 | element_dealloc(ElementObject* self) |
| 640 | { |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 641 | PyObject_GC_UnTrack(self); |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 642 | |
| 643 | if (self->weakreflist != NULL) |
| 644 | PyObject_ClearWeakRefs((PyObject *) self); |
| 645 | |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 646 | /* element_gc_clear clears all references and deallocates extra |
| 647 | */ |
| 648 | element_gc_clear(self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 649 | |
| 650 | RELEASE(sizeof(ElementObject), "destroy element"); |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 651 | Py_TYPE(self)->tp_free((PyObject *)self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /* -------------------------------------------------------------------- */ |
| 655 | /* methods (in alphabetical order) */ |
| 656 | |
| 657 | static PyObject* |
| 658 | element_append(ElementObject* self, PyObject* args) |
| 659 | { |
| 660 | PyObject* element; |
| 661 | if (!PyArg_ParseTuple(args, "O!:append", &Element_Type, &element)) |
| 662 | return NULL; |
| 663 | |
| 664 | if (element_add_subelement(self, element) < 0) |
| 665 | return NULL; |
| 666 | |
| 667 | Py_RETURN_NONE; |
| 668 | } |
| 669 | |
| 670 | static PyObject* |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 671 | element_clearmethod(ElementObject* self, PyObject* args) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 672 | { |
| 673 | if (!PyArg_ParseTuple(args, ":clear")) |
| 674 | return NULL; |
| 675 | |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 676 | dealloc_extra(self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 677 | |
| 678 | Py_INCREF(Py_None); |
| 679 | Py_DECREF(JOIN_OBJ(self->text)); |
| 680 | self->text = Py_None; |
| 681 | |
| 682 | Py_INCREF(Py_None); |
| 683 | Py_DECREF(JOIN_OBJ(self->tail)); |
| 684 | self->tail = Py_None; |
| 685 | |
| 686 | Py_RETURN_NONE; |
| 687 | } |
| 688 | |
| 689 | static PyObject* |
| 690 | element_copy(ElementObject* self, PyObject* args) |
| 691 | { |
| 692 | int i; |
| 693 | ElementObject* element; |
| 694 | |
| 695 | if (!PyArg_ParseTuple(args, ":__copy__")) |
| 696 | return NULL; |
| 697 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 698 | element = (ElementObject*) create_new_element( |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 699 | self->tag, (self->extra) ? self->extra->attrib : Py_None |
| 700 | ); |
| 701 | if (!element) |
| 702 | return NULL; |
| 703 | |
| 704 | Py_DECREF(JOIN_OBJ(element->text)); |
| 705 | element->text = self->text; |
| 706 | Py_INCREF(JOIN_OBJ(element->text)); |
| 707 | |
| 708 | Py_DECREF(JOIN_OBJ(element->tail)); |
| 709 | element->tail = self->tail; |
| 710 | Py_INCREF(JOIN_OBJ(element->tail)); |
| 711 | |
| 712 | if (self->extra) { |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 713 | |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 714 | if (element_resize(element, self->extra->length) < 0) { |
| 715 | Py_DECREF(element); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 716 | return NULL; |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 717 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 718 | |
| 719 | for (i = 0; i < self->extra->length; i++) { |
| 720 | Py_INCREF(self->extra->children[i]); |
| 721 | element->extra->children[i] = self->extra->children[i]; |
| 722 | } |
| 723 | |
| 724 | element->extra->length = self->extra->length; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 725 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | return (PyObject*) element; |
| 729 | } |
| 730 | |
| 731 | static PyObject* |
| 732 | element_deepcopy(ElementObject* self, PyObject* args) |
| 733 | { |
| 734 | int i; |
| 735 | ElementObject* element; |
| 736 | PyObject* tag; |
| 737 | PyObject* attrib; |
| 738 | PyObject* text; |
| 739 | PyObject* tail; |
| 740 | PyObject* id; |
| 741 | |
| 742 | PyObject* memo; |
| 743 | if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo)) |
| 744 | return NULL; |
| 745 | |
| 746 | tag = deepcopy(self->tag, memo); |
| 747 | if (!tag) |
| 748 | return NULL; |
| 749 | |
| 750 | if (self->extra) { |
| 751 | attrib = deepcopy(self->extra->attrib, memo); |
| 752 | if (!attrib) { |
| 753 | Py_DECREF(tag); |
| 754 | return NULL; |
| 755 | } |
| 756 | } else { |
| 757 | Py_INCREF(Py_None); |
| 758 | attrib = Py_None; |
| 759 | } |
| 760 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 761 | element = (ElementObject*) create_new_element(tag, attrib); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 762 | |
| 763 | Py_DECREF(tag); |
| 764 | Py_DECREF(attrib); |
| 765 | |
| 766 | if (!element) |
| 767 | return NULL; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 768 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 769 | text = deepcopy(JOIN_OBJ(self->text), memo); |
| 770 | if (!text) |
| 771 | goto error; |
| 772 | Py_DECREF(element->text); |
| 773 | element->text = JOIN_SET(text, JOIN_GET(self->text)); |
| 774 | |
| 775 | tail = deepcopy(JOIN_OBJ(self->tail), memo); |
| 776 | if (!tail) |
| 777 | goto error; |
| 778 | Py_DECREF(element->tail); |
| 779 | element->tail = JOIN_SET(tail, JOIN_GET(self->tail)); |
| 780 | |
| 781 | if (self->extra) { |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 782 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 783 | if (element_resize(element, self->extra->length) < 0) |
| 784 | goto error; |
| 785 | |
| 786 | for (i = 0; i < self->extra->length; i++) { |
| 787 | PyObject* child = deepcopy(self->extra->children[i], memo); |
| 788 | if (!child) { |
| 789 | element->extra->length = i; |
| 790 | goto error; |
| 791 | } |
| 792 | element->extra->children[i] = child; |
| 793 | } |
| 794 | |
| 795 | element->extra->length = self->extra->length; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 796 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* add object to memo dictionary (so deepcopy won't visit it again) */ |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 800 | id = PyLong_FromSsize_t((Py_uintptr_t) self); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 801 | if (!id) |
| 802 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 803 | |
| 804 | i = PyDict_SetItem(memo, id, (PyObject*) element); |
| 805 | |
| 806 | Py_DECREF(id); |
| 807 | |
| 808 | if (i < 0) |
| 809 | goto error; |
| 810 | |
| 811 | return (PyObject*) element; |
| 812 | |
| 813 | error: |
| 814 | Py_DECREF(element); |
| 815 | return NULL; |
| 816 | } |
| 817 | |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 818 | static PyObject* |
Antoine Pitrou | 09fcb72 | 2013-10-23 19:20:21 +0200 | [diff] [blame] | 819 | element_sizeof(PyObject* myself, PyObject* args) |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 820 | { |
Antoine Pitrou | 09fcb72 | 2013-10-23 19:20:21 +0200 | [diff] [blame] | 821 | ElementObject *self = (ElementObject*)myself; |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 822 | Py_ssize_t result = sizeof(ElementObject); |
| 823 | if (self->extra) { |
| 824 | result += sizeof(ElementObjectExtra); |
| 825 | if (self->extra->children != self->extra->_children) |
| 826 | result += sizeof(PyObject*) * self->extra->allocated; |
| 827 | } |
| 828 | return PyLong_FromSsize_t(result); |
| 829 | } |
| 830 | |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 831 | /* dict keys for getstate/setstate. */ |
| 832 | #define PICKLED_TAG "tag" |
| 833 | #define PICKLED_CHILDREN "_children" |
| 834 | #define PICKLED_ATTRIB "attrib" |
| 835 | #define PICKLED_TAIL "tail" |
| 836 | #define PICKLED_TEXT "text" |
| 837 | |
| 838 | /* __getstate__ returns a fabricated instance dict as in the pure-Python |
| 839 | * Element implementation, for interoperability/interchangeability. This |
| 840 | * makes the pure-Python implementation details an API, but (a) there aren't |
| 841 | * any unnecessary structures there; and (b) it buys compatibility with 3.2 |
| 842 | * pickles. See issue #16076. |
| 843 | */ |
| 844 | static PyObject * |
| 845 | element_getstate(ElementObject *self) |
| 846 | { |
| 847 | int i, noattrib; |
| 848 | PyObject *instancedict = NULL, *children; |
| 849 | |
| 850 | /* Build a list of children. */ |
| 851 | children = PyList_New(self->extra ? self->extra->length : 0); |
| 852 | if (!children) |
| 853 | return NULL; |
| 854 | for (i = 0; i < PyList_GET_SIZE(children); i++) { |
| 855 | PyObject *child = self->extra->children[i]; |
| 856 | Py_INCREF(child); |
| 857 | PyList_SET_ITEM(children, i, child); |
| 858 | } |
| 859 | |
| 860 | /* Construct the state object. */ |
| 861 | noattrib = (self->extra == NULL || self->extra->attrib == Py_None); |
| 862 | if (noattrib) |
| 863 | instancedict = Py_BuildValue("{sOsOs{}sOsO}", |
| 864 | PICKLED_TAG, self->tag, |
| 865 | PICKLED_CHILDREN, children, |
| 866 | PICKLED_ATTRIB, |
Eli Bendersky | dd3661e | 2013-09-13 06:24:25 -0700 | [diff] [blame] | 867 | PICKLED_TEXT, JOIN_OBJ(self->text), |
| 868 | PICKLED_TAIL, JOIN_OBJ(self->tail)); |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 869 | else |
| 870 | instancedict = Py_BuildValue("{sOsOsOsOsO}", |
| 871 | PICKLED_TAG, self->tag, |
| 872 | PICKLED_CHILDREN, children, |
| 873 | PICKLED_ATTRIB, self->extra->attrib, |
Eli Bendersky | dd3661e | 2013-09-13 06:24:25 -0700 | [diff] [blame] | 874 | PICKLED_TEXT, JOIN_OBJ(self->text), |
| 875 | PICKLED_TAIL, JOIN_OBJ(self->tail)); |
Eli Bendersky | b8f6dc8 | 2013-01-12 05:20:16 -0800 | [diff] [blame] | 876 | if (instancedict) { |
| 877 | Py_DECREF(children); |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 878 | return instancedict; |
Eli Bendersky | b8f6dc8 | 2013-01-12 05:20:16 -0800 | [diff] [blame] | 879 | } |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 880 | else { |
| 881 | for (i = 0; i < PyList_GET_SIZE(children); i++) |
| 882 | Py_DECREF(PyList_GET_ITEM(children, i)); |
| 883 | Py_DECREF(children); |
| 884 | |
| 885 | return NULL; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | static PyObject * |
| 890 | element_setstate_from_attributes(ElementObject *self, |
| 891 | PyObject *tag, |
| 892 | PyObject *attrib, |
| 893 | PyObject *text, |
| 894 | PyObject *tail, |
| 895 | PyObject *children) |
| 896 | { |
| 897 | Py_ssize_t i, nchildren; |
| 898 | |
| 899 | if (!tag) { |
| 900 | PyErr_SetString(PyExc_TypeError, "tag may not be NULL"); |
| 901 | return NULL; |
| 902 | } |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 903 | |
| 904 | Py_CLEAR(self->tag); |
| 905 | self->tag = tag; |
| 906 | Py_INCREF(self->tag); |
| 907 | |
Eli Bendersky | dd3661e | 2013-09-13 06:24:25 -0700 | [diff] [blame] | 908 | _clear_joined_ptr(&self->text); |
| 909 | self->text = text ? JOIN_SET(text, PyList_CheckExact(text)) : Py_None; |
| 910 | Py_INCREF(JOIN_OBJ(self->text)); |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 911 | |
Eli Bendersky | dd3661e | 2013-09-13 06:24:25 -0700 | [diff] [blame] | 912 | _clear_joined_ptr(&self->tail); |
| 913 | self->tail = tail ? JOIN_SET(tail, PyList_CheckExact(tail)) : Py_None; |
| 914 | Py_INCREF(JOIN_OBJ(self->tail)); |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 915 | |
| 916 | /* Handle ATTRIB and CHILDREN. */ |
| 917 | if (!children && !attrib) |
| 918 | Py_RETURN_NONE; |
| 919 | |
| 920 | /* Compute 'nchildren'. */ |
| 921 | if (children) { |
| 922 | if (!PyList_Check(children)) { |
| 923 | PyErr_SetString(PyExc_TypeError, "'_children' is not a list"); |
| 924 | return NULL; |
| 925 | } |
| 926 | nchildren = PyList_Size(children); |
| 927 | } |
| 928 | else { |
| 929 | nchildren = 0; |
| 930 | } |
| 931 | |
| 932 | /* Allocate 'extra'. */ |
| 933 | if (element_resize(self, nchildren)) { |
| 934 | return NULL; |
| 935 | } |
| 936 | assert(self->extra && self->extra->allocated >= nchildren); |
| 937 | |
| 938 | /* Copy children */ |
| 939 | for (i = 0; i < nchildren; i++) { |
| 940 | self->extra->children[i] = PyList_GET_ITEM(children, i); |
| 941 | Py_INCREF(self->extra->children[i]); |
| 942 | } |
| 943 | |
| 944 | self->extra->length = nchildren; |
| 945 | self->extra->allocated = nchildren; |
| 946 | |
| 947 | /* Stash attrib. */ |
| 948 | if (attrib) { |
| 949 | Py_CLEAR(self->extra->attrib); |
| 950 | self->extra->attrib = attrib; |
| 951 | Py_INCREF(attrib); |
| 952 | } |
| 953 | |
| 954 | Py_RETURN_NONE; |
| 955 | } |
| 956 | |
| 957 | /* __setstate__ for Element instance from the Python implementation. |
| 958 | * 'state' should be the instance dict. |
| 959 | */ |
| 960 | static PyObject * |
| 961 | element_setstate_from_Python(ElementObject *self, PyObject *state) |
| 962 | { |
| 963 | static char *kwlist[] = {PICKLED_TAG, PICKLED_ATTRIB, PICKLED_TEXT, |
| 964 | PICKLED_TAIL, PICKLED_CHILDREN, 0}; |
| 965 | PyObject *args; |
| 966 | PyObject *tag, *attrib, *text, *tail, *children; |
Eli Bendersky | 799e3ed | 2013-01-12 05:42:38 -0800 | [diff] [blame] | 967 | PyObject *retval; |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 968 | |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 969 | tag = attrib = text = tail = children = NULL; |
| 970 | args = PyTuple_New(0); |
Eli Bendersky | 799e3ed | 2013-01-12 05:42:38 -0800 | [diff] [blame] | 971 | if (!args) |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 972 | return NULL; |
Eli Bendersky | 799e3ed | 2013-01-12 05:42:38 -0800 | [diff] [blame] | 973 | |
| 974 | if (PyArg_ParseTupleAndKeywords(args, state, "|$OOOOO", kwlist, &tag, |
| 975 | &attrib, &text, &tail, &children)) |
| 976 | retval = element_setstate_from_attributes(self, tag, attrib, text, |
| 977 | tail, children); |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 978 | else |
Eli Bendersky | 799e3ed | 2013-01-12 05:42:38 -0800 | [diff] [blame] | 979 | retval = NULL; |
| 980 | |
| 981 | Py_DECREF(args); |
| 982 | return retval; |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | static PyObject * |
| 986 | element_setstate(ElementObject *self, PyObject *state) |
| 987 | { |
| 988 | if (!PyDict_CheckExact(state)) { |
| 989 | PyErr_Format(PyExc_TypeError, |
| 990 | "Don't know how to unpickle \"%.200R\" as an Element", |
| 991 | state); |
| 992 | return NULL; |
| 993 | } |
| 994 | else |
| 995 | return element_setstate_from_Python(self, state); |
| 996 | } |
| 997 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 998 | LOCAL(int) |
| 999 | checkpath(PyObject* tag) |
| 1000 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1001 | Py_ssize_t i; |
| 1002 | int check = 1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1003 | |
| 1004 | /* check if a tag contains an xpath character */ |
| 1005 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1006 | #define PATHCHAR(ch) \ |
| 1007 | (ch == '/' || ch == '*' || ch == '[' || ch == '@' || ch == '.') |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1008 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1009 | if (PyUnicode_Check(tag)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1010 | const Py_ssize_t len = PyUnicode_GET_LENGTH(tag); |
| 1011 | void *data = PyUnicode_DATA(tag); |
| 1012 | unsigned int kind = PyUnicode_KIND(tag); |
| 1013 | for (i = 0; i < len; i++) { |
| 1014 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 1015 | if (ch == '{') |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1016 | check = 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1017 | else if (ch == '}') |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1018 | check = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1019 | else if (check && PATHCHAR(ch)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1020 | return 1; |
| 1021 | } |
| 1022 | return 0; |
| 1023 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 1024 | if (PyBytes_Check(tag)) { |
| 1025 | char *p = PyBytes_AS_STRING(tag); |
| 1026 | for (i = 0; i < PyBytes_GET_SIZE(tag); i++) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1027 | if (p[i] == '{') |
| 1028 | check = 0; |
| 1029 | else if (p[i] == '}') |
| 1030 | check = 1; |
| 1031 | else if (check && PATHCHAR(p[i])) |
| 1032 | return 1; |
| 1033 | } |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | return 1; /* unknown type; might be path expression */ |
| 1038 | } |
| 1039 | |
| 1040 | static PyObject* |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1041 | element_extend(ElementObject* self, PyObject* args) |
| 1042 | { |
| 1043 | PyObject* seq; |
| 1044 | Py_ssize_t i, seqlen = 0; |
| 1045 | |
| 1046 | PyObject* seq_in; |
| 1047 | if (!PyArg_ParseTuple(args, "O:extend", &seq_in)) |
| 1048 | return NULL; |
| 1049 | |
| 1050 | seq = PySequence_Fast(seq_in, ""); |
| 1051 | if (!seq) { |
| 1052 | PyErr_Format( |
| 1053 | PyExc_TypeError, |
| 1054 | "expected sequence, not \"%.200s\"", Py_TYPE(seq_in)->tp_name |
| 1055 | ); |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | |
| 1059 | seqlen = PySequence_Size(seq); |
| 1060 | for (i = 0; i < seqlen; i++) { |
| 1061 | PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
Eli Bendersky | 396e8fc | 2012-03-23 14:24:20 +0200 | [diff] [blame] | 1062 | if (!PyObject_IsInstance(element, (PyObject *)&Element_Type)) { |
| 1063 | Py_DECREF(seq); |
| 1064 | PyErr_Format( |
| 1065 | PyExc_TypeError, |
| 1066 | "expected an Element, not \"%.200s\"", |
| 1067 | Py_TYPE(element)->tp_name); |
| 1068 | return NULL; |
| 1069 | } |
| 1070 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1071 | if (element_add_subelement(self, element) < 0) { |
| 1072 | Py_DECREF(seq); |
| 1073 | return NULL; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | Py_DECREF(seq); |
| 1078 | |
| 1079 | Py_RETURN_NONE; |
| 1080 | } |
| 1081 | |
| 1082 | static PyObject* |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1083 | element_find(ElementObject *self, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1084 | { |
| 1085 | int i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1086 | PyObject* tag; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1087 | PyObject* namespaces = Py_None; |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1088 | static char *kwlist[] = {"path", "namespaces", 0}; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1089 | elementtreestate *st = ET_STATE_GLOBAL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1090 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1091 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:find", kwlist, |
| 1092 | &tag, &namespaces)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1093 | return NULL; |
| 1094 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1095 | if (checkpath(tag) || namespaces != Py_None) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1096 | _Py_IDENTIFIER(find); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1097 | return _PyObject_CallMethodId( |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1098 | st->elementpath_obj, &PyId_find, "OOO", self, tag, namespaces |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1099 | ); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1100 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (!self->extra) |
| 1103 | Py_RETURN_NONE; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 1104 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1105 | for (i = 0; i < self->extra->length; i++) { |
| 1106 | PyObject* item = self->extra->children[i]; |
| 1107 | if (Element_CheckExact(item) && |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 1108 | PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ) == 1) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1109 | Py_INCREF(item); |
| 1110 | return item; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | Py_RETURN_NONE; |
| 1115 | } |
| 1116 | |
| 1117 | static PyObject* |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1118 | element_findtext(ElementObject *self, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1119 | { |
| 1120 | int i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1121 | PyObject* tag; |
| 1122 | PyObject* default_value = Py_None; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1123 | PyObject* namespaces = Py_None; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1124 | _Py_IDENTIFIER(findtext); |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1125 | static char *kwlist[] = {"path", "default", "namespaces", 0}; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1126 | elementtreestate *st = ET_STATE_GLOBAL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1127 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1128 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:findtext", kwlist, |
| 1129 | &tag, &default_value, &namespaces)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1130 | return NULL; |
| 1131 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1132 | if (checkpath(tag) || namespaces != Py_None) |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1133 | return _PyObject_CallMethodId( |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1134 | st->elementpath_obj, &PyId_findtext, "OOOO", self, tag, default_value, namespaces |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1135 | ); |
| 1136 | |
| 1137 | if (!self->extra) { |
| 1138 | Py_INCREF(default_value); |
| 1139 | return default_value; |
| 1140 | } |
| 1141 | |
| 1142 | for (i = 0; i < self->extra->length; i++) { |
| 1143 | ElementObject* item = (ElementObject*) self->extra->children[i]; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 1144 | if (Element_CheckExact(item) && (PyObject_RichCompareBool(item->tag, tag, Py_EQ) == 1)) { |
| 1145 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1146 | PyObject* text = element_get_text(item); |
| 1147 | if (text == Py_None) |
Eli Bendersky | 25771b3 | 2013-01-13 05:26:07 -0800 | [diff] [blame] | 1148 | return PyUnicode_New(0, 0); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1149 | Py_XINCREF(text); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1150 | return text; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | Py_INCREF(default_value); |
| 1155 | return default_value; |
| 1156 | } |
| 1157 | |
| 1158 | static PyObject* |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1159 | element_findall(ElementObject *self, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1160 | { |
| 1161 | int i; |
| 1162 | PyObject* out; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1163 | PyObject* tag; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1164 | PyObject* namespaces = Py_None; |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1165 | static char *kwlist[] = {"path", "namespaces", 0}; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1166 | elementtreestate *st = ET_STATE_GLOBAL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1167 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1168 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:findall", kwlist, |
| 1169 | &tag, &namespaces)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1170 | return NULL; |
| 1171 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1172 | if (checkpath(tag) || namespaces != Py_None) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1173 | _Py_IDENTIFIER(findall); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1174 | return _PyObject_CallMethodId( |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1175 | st->elementpath_obj, &PyId_findall, "OOO", self, tag, namespaces |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1176 | ); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1177 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1178 | |
| 1179 | out = PyList_New(0); |
| 1180 | if (!out) |
| 1181 | return NULL; |
| 1182 | |
| 1183 | if (!self->extra) |
| 1184 | return out; |
| 1185 | |
| 1186 | for (i = 0; i < self->extra->length; i++) { |
| 1187 | PyObject* item = self->extra->children[i]; |
| 1188 | if (Element_CheckExact(item) && |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 1189 | PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ) == 1) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1190 | if (PyList_Append(out, item) < 0) { |
| 1191 | Py_DECREF(out); |
| 1192 | return NULL; |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | return out; |
| 1198 | } |
| 1199 | |
| 1200 | static PyObject* |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1201 | element_iterfind(ElementObject *self, PyObject *args, PyObject *kwds) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1202 | { |
| 1203 | PyObject* tag; |
| 1204 | PyObject* namespaces = Py_None; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1205 | _Py_IDENTIFIER(iterfind); |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1206 | static char *kwlist[] = {"path", "namespaces", 0}; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1207 | elementtreestate *st = ET_STATE_GLOBAL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1208 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1209 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:iterfind", kwlist, |
| 1210 | &tag, &namespaces)) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1211 | return NULL; |
| 1212 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1213 | return _PyObject_CallMethodId( |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 1214 | st->elementpath_obj, &PyId_iterfind, "OOO", self, tag, namespaces |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1215 | ); |
| 1216 | } |
| 1217 | |
| 1218 | static PyObject* |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 1219 | element_get(ElementObject* self, PyObject* args, PyObject* kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1220 | { |
| 1221 | PyObject* value; |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 1222 | static char* kwlist[] = {"key", "default", 0}; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1223 | |
| 1224 | PyObject* key; |
| 1225 | PyObject* default_value = Py_None; |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 1226 | |
| 1227 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:get", kwlist, &key, |
| 1228 | &default_value)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1229 | return NULL; |
| 1230 | |
| 1231 | if (!self->extra || self->extra->attrib == Py_None) |
| 1232 | value = default_value; |
| 1233 | else { |
| 1234 | value = PyDict_GetItem(self->extra->attrib, key); |
| 1235 | if (!value) |
| 1236 | value = default_value; |
| 1237 | } |
| 1238 | |
| 1239 | Py_INCREF(value); |
| 1240 | return value; |
| 1241 | } |
| 1242 | |
| 1243 | static PyObject* |
| 1244 | element_getchildren(ElementObject* self, PyObject* args) |
| 1245 | { |
| 1246 | int i; |
| 1247 | PyObject* list; |
| 1248 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1249 | /* FIXME: report as deprecated? */ |
| 1250 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1251 | if (!PyArg_ParseTuple(args, ":getchildren")) |
| 1252 | return NULL; |
| 1253 | |
| 1254 | if (!self->extra) |
| 1255 | return PyList_New(0); |
| 1256 | |
| 1257 | list = PyList_New(self->extra->length); |
| 1258 | if (!list) |
| 1259 | return NULL; |
| 1260 | |
| 1261 | for (i = 0; i < self->extra->length; i++) { |
| 1262 | PyObject* item = self->extra->children[i]; |
| 1263 | Py_INCREF(item); |
| 1264 | PyList_SET_ITEM(list, i, item); |
| 1265 | } |
| 1266 | |
| 1267 | return list; |
| 1268 | } |
| 1269 | |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 1270 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1271 | static PyObject * |
| 1272 | create_elementiter(ElementObject *self, PyObject *tag, int gettext); |
| 1273 | |
| 1274 | |
| 1275 | static PyObject * |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 1276 | element_iter(ElementObject *self, PyObject *args, PyObject *kwds) |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1277 | { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1278 | PyObject* tag = Py_None; |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 1279 | static char* kwlist[] = {"tag", 0}; |
| 1280 | |
| 1281 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:iter", kwlist, &tag)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1282 | return NULL; |
| 1283 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1284 | return create_elementiter(self, tag, 0); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | |
| 1288 | static PyObject* |
| 1289 | element_itertext(ElementObject* self, PyObject* args) |
| 1290 | { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1291 | if (!PyArg_ParseTuple(args, ":itertext")) |
| 1292 | return NULL; |
| 1293 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1294 | return create_elementiter(self, Py_None, 1); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1297 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1298 | static PyObject* |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1299 | element_getitem(PyObject* self_, Py_ssize_t index) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1300 | { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1301 | ElementObject* self = (ElementObject*) self_; |
| 1302 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1303 | if (!self->extra || index < 0 || index >= self->extra->length) { |
| 1304 | PyErr_SetString( |
| 1305 | PyExc_IndexError, |
| 1306 | "child index out of range" |
| 1307 | ); |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | Py_INCREF(self->extra->children[index]); |
| 1312 | return self->extra->children[index]; |
| 1313 | } |
| 1314 | |
| 1315 | static PyObject* |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1316 | element_insert(ElementObject* self, PyObject* args) |
| 1317 | { |
| 1318 | int i; |
| 1319 | |
| 1320 | int index; |
| 1321 | PyObject* element; |
| 1322 | if (!PyArg_ParseTuple(args, "iO!:insert", &index, |
| 1323 | &Element_Type, &element)) |
| 1324 | return NULL; |
| 1325 | |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 1326 | if (!self->extra) { |
| 1327 | if (create_extra(self, NULL) < 0) |
| 1328 | return NULL; |
| 1329 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1330 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1331 | if (index < 0) { |
| 1332 | index += self->extra->length; |
| 1333 | if (index < 0) |
| 1334 | index = 0; |
| 1335 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1336 | if (index > self->extra->length) |
| 1337 | index = self->extra->length; |
| 1338 | |
| 1339 | if (element_resize(self, 1) < 0) |
| 1340 | return NULL; |
| 1341 | |
| 1342 | for (i = self->extra->length; i > index; i--) |
| 1343 | self->extra->children[i] = self->extra->children[i-1]; |
| 1344 | |
| 1345 | Py_INCREF(element); |
| 1346 | self->extra->children[index] = element; |
| 1347 | |
| 1348 | self->extra->length++; |
| 1349 | |
| 1350 | Py_RETURN_NONE; |
| 1351 | } |
| 1352 | |
| 1353 | static PyObject* |
| 1354 | element_items(ElementObject* self, PyObject* args) |
| 1355 | { |
| 1356 | if (!PyArg_ParseTuple(args, ":items")) |
| 1357 | return NULL; |
| 1358 | |
| 1359 | if (!self->extra || self->extra->attrib == Py_None) |
| 1360 | return PyList_New(0); |
| 1361 | |
| 1362 | return PyDict_Items(self->extra->attrib); |
| 1363 | } |
| 1364 | |
| 1365 | static PyObject* |
| 1366 | element_keys(ElementObject* self, PyObject* args) |
| 1367 | { |
| 1368 | if (!PyArg_ParseTuple(args, ":keys")) |
| 1369 | return NULL; |
| 1370 | |
| 1371 | if (!self->extra || self->extra->attrib == Py_None) |
| 1372 | return PyList_New(0); |
| 1373 | |
| 1374 | return PyDict_Keys(self->extra->attrib); |
| 1375 | } |
| 1376 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1377 | static Py_ssize_t |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1378 | element_length(ElementObject* self) |
| 1379 | { |
| 1380 | if (!self->extra) |
| 1381 | return 0; |
| 1382 | |
| 1383 | return self->extra->length; |
| 1384 | } |
| 1385 | |
| 1386 | static PyObject* |
| 1387 | element_makeelement(PyObject* self, PyObject* args, PyObject* kw) |
| 1388 | { |
| 1389 | PyObject* elem; |
| 1390 | |
| 1391 | PyObject* tag; |
| 1392 | PyObject* attrib; |
| 1393 | if (!PyArg_ParseTuple(args, "OO:makeelement", &tag, &attrib)) |
| 1394 | return NULL; |
| 1395 | |
| 1396 | attrib = PyDict_Copy(attrib); |
| 1397 | if (!attrib) |
| 1398 | return NULL; |
| 1399 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1400 | elem = create_new_element(tag, attrib); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1401 | |
| 1402 | Py_DECREF(attrib); |
| 1403 | |
| 1404 | return elem; |
| 1405 | } |
| 1406 | |
| 1407 | static PyObject* |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1408 | element_remove(ElementObject* self, PyObject* args) |
| 1409 | { |
| 1410 | int i; |
| 1411 | |
| 1412 | PyObject* element; |
| 1413 | if (!PyArg_ParseTuple(args, "O!:remove", &Element_Type, &element)) |
| 1414 | return NULL; |
| 1415 | |
| 1416 | if (!self->extra) { |
| 1417 | /* element has no children, so raise exception */ |
| 1418 | PyErr_SetString( |
| 1419 | PyExc_ValueError, |
| 1420 | "list.remove(x): x not in list" |
| 1421 | ); |
| 1422 | return NULL; |
| 1423 | } |
| 1424 | |
| 1425 | for (i = 0; i < self->extra->length; i++) { |
| 1426 | if (self->extra->children[i] == element) |
| 1427 | break; |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 1428 | if (PyObject_RichCompareBool(self->extra->children[i], element, Py_EQ) == 1) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1429 | break; |
| 1430 | } |
| 1431 | |
| 1432 | if (i == self->extra->length) { |
| 1433 | /* element is not in children, so raise exception */ |
| 1434 | PyErr_SetString( |
| 1435 | PyExc_ValueError, |
| 1436 | "list.remove(x): x not in list" |
| 1437 | ); |
| 1438 | return NULL; |
| 1439 | } |
| 1440 | |
| 1441 | Py_DECREF(self->extra->children[i]); |
| 1442 | |
| 1443 | self->extra->length--; |
| 1444 | |
| 1445 | for (; i < self->extra->length; i++) |
| 1446 | self->extra->children[i] = self->extra->children[i+1]; |
| 1447 | |
| 1448 | Py_RETURN_NONE; |
| 1449 | } |
| 1450 | |
| 1451 | static PyObject* |
| 1452 | element_repr(ElementObject* self) |
| 1453 | { |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1454 | if (self->tag) |
| 1455 | return PyUnicode_FromFormat("<Element %R at %p>", self->tag, self); |
| 1456 | else |
| 1457 | return PyUnicode_FromFormat("<Element at %p>", self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | static PyObject* |
| 1461 | element_set(ElementObject* self, PyObject* args) |
| 1462 | { |
| 1463 | PyObject* attrib; |
| 1464 | |
| 1465 | PyObject* key; |
| 1466 | PyObject* value; |
| 1467 | if (!PyArg_ParseTuple(args, "OO:set", &key, &value)) |
| 1468 | return NULL; |
| 1469 | |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 1470 | if (!self->extra) { |
| 1471 | if (create_extra(self, NULL) < 0) |
| 1472 | return NULL; |
| 1473 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1474 | |
| 1475 | attrib = element_get_attrib(self); |
| 1476 | if (!attrib) |
| 1477 | return NULL; |
| 1478 | |
| 1479 | if (PyDict_SetItem(attrib, key, value) < 0) |
| 1480 | return NULL; |
| 1481 | |
| 1482 | Py_RETURN_NONE; |
| 1483 | } |
| 1484 | |
| 1485 | static int |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1486 | element_setitem(PyObject* self_, Py_ssize_t index, PyObject* item) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1487 | { |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 1488 | ElementObject* self = (ElementObject*) self_; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1489 | int i; |
| 1490 | PyObject* old; |
| 1491 | |
| 1492 | if (!self->extra || index < 0 || index >= self->extra->length) { |
| 1493 | PyErr_SetString( |
| 1494 | PyExc_IndexError, |
| 1495 | "child assignment index out of range"); |
| 1496 | return -1; |
| 1497 | } |
| 1498 | |
| 1499 | old = self->extra->children[index]; |
| 1500 | |
| 1501 | if (item) { |
| 1502 | Py_INCREF(item); |
| 1503 | self->extra->children[index] = item; |
| 1504 | } else { |
| 1505 | self->extra->length--; |
| 1506 | for (i = index; i < self->extra->length; i++) |
| 1507 | self->extra->children[i] = self->extra->children[i+1]; |
| 1508 | } |
| 1509 | |
| 1510 | Py_DECREF(old); |
| 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1515 | static PyObject* |
| 1516 | element_subscr(PyObject* self_, PyObject* item) |
| 1517 | { |
| 1518 | ElementObject* self = (ElementObject*) self_; |
| 1519 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1520 | if (PyIndex_Check(item)) { |
| 1521 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1522 | |
| 1523 | if (i == -1 && PyErr_Occurred()) { |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | if (i < 0 && self->extra) |
| 1527 | i += self->extra->length; |
| 1528 | return element_getitem(self_, i); |
| 1529 | } |
| 1530 | else if (PySlice_Check(item)) { |
| 1531 | Py_ssize_t start, stop, step, slicelen, cur, i; |
| 1532 | PyObject* list; |
| 1533 | |
| 1534 | if (!self->extra) |
| 1535 | return PyList_New(0); |
| 1536 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1537 | if (PySlice_GetIndicesEx(item, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1538 | self->extra->length, |
| 1539 | &start, &stop, &step, &slicelen) < 0) { |
| 1540 | return NULL; |
| 1541 | } |
| 1542 | |
| 1543 | if (slicelen <= 0) |
| 1544 | return PyList_New(0); |
| 1545 | else { |
| 1546 | list = PyList_New(slicelen); |
| 1547 | if (!list) |
| 1548 | return NULL; |
| 1549 | |
| 1550 | for (cur = start, i = 0; i < slicelen; |
| 1551 | cur += step, i++) { |
| 1552 | PyObject* item = self->extra->children[cur]; |
| 1553 | Py_INCREF(item); |
| 1554 | PyList_SET_ITEM(list, i, item); |
| 1555 | } |
| 1556 | |
| 1557 | return list; |
| 1558 | } |
| 1559 | } |
| 1560 | else { |
| 1561 | PyErr_SetString(PyExc_TypeError, |
| 1562 | "element indices must be integers"); |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | static int |
| 1568 | element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) |
| 1569 | { |
| 1570 | ElementObject* self = (ElementObject*) self_; |
| 1571 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1572 | if (PyIndex_Check(item)) { |
| 1573 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1574 | |
| 1575 | if (i == -1 && PyErr_Occurred()) { |
| 1576 | return -1; |
| 1577 | } |
| 1578 | if (i < 0 && self->extra) |
| 1579 | i += self->extra->length; |
| 1580 | return element_setitem(self_, i, value); |
| 1581 | } |
| 1582 | else if (PySlice_Check(item)) { |
| 1583 | Py_ssize_t start, stop, step, slicelen, newlen, cur, i; |
| 1584 | |
| 1585 | PyObject* recycle = NULL; |
| 1586 | PyObject* seq = NULL; |
| 1587 | |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 1588 | if (!self->extra) { |
| 1589 | if (create_extra(self, NULL) < 0) |
| 1590 | return -1; |
| 1591 | } |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1592 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1593 | if (PySlice_GetIndicesEx(item, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1594 | self->extra->length, |
| 1595 | &start, &stop, &step, &slicelen) < 0) { |
| 1596 | return -1; |
| 1597 | } |
| 1598 | |
Eli Bendersky | 865756a | 2012-03-09 13:38:15 +0200 | [diff] [blame] | 1599 | if (value == NULL) { |
| 1600 | /* Delete slice */ |
| 1601 | size_t cur; |
| 1602 | Py_ssize_t i; |
| 1603 | |
| 1604 | if (slicelen <= 0) |
| 1605 | return 0; |
| 1606 | |
| 1607 | /* Since we're deleting, the direction of the range doesn't matter, |
| 1608 | * so for simplicity make it always ascending. |
| 1609 | */ |
| 1610 | if (step < 0) { |
| 1611 | stop = start + 1; |
| 1612 | start = stop + step * (slicelen - 1) - 1; |
| 1613 | step = -step; |
| 1614 | } |
| 1615 | |
| 1616 | assert((size_t)slicelen <= PY_SIZE_MAX / sizeof(PyObject *)); |
| 1617 | |
| 1618 | /* recycle is a list that will contain all the children |
| 1619 | * scheduled for removal. |
| 1620 | */ |
| 1621 | if (!(recycle = PyList_New(slicelen))) { |
| 1622 | PyErr_NoMemory(); |
| 1623 | return -1; |
| 1624 | } |
| 1625 | |
| 1626 | /* This loop walks over all the children that have to be deleted, |
| 1627 | * with cur pointing at them. num_moved is the amount of children |
| 1628 | * until the next deleted child that have to be "shifted down" to |
| 1629 | * occupy the deleted's places. |
| 1630 | * Note that in the ith iteration, shifting is done i+i places down |
| 1631 | * because i children were already removed. |
| 1632 | */ |
| 1633 | for (cur = start, i = 0; cur < (size_t)stop; cur += step, ++i) { |
| 1634 | /* Compute how many children have to be moved, clipping at the |
| 1635 | * list end. |
| 1636 | */ |
| 1637 | Py_ssize_t num_moved = step - 1; |
| 1638 | if (cur + step >= (size_t)self->extra->length) { |
| 1639 | num_moved = self->extra->length - cur - 1; |
| 1640 | } |
| 1641 | |
| 1642 | PyList_SET_ITEM(recycle, i, self->extra->children[cur]); |
| 1643 | |
| 1644 | memmove( |
| 1645 | self->extra->children + cur - i, |
| 1646 | self->extra->children + cur + 1, |
| 1647 | num_moved * sizeof(PyObject *)); |
| 1648 | } |
| 1649 | |
| 1650 | /* Leftover "tail" after the last removed child */ |
| 1651 | cur = start + (size_t)slicelen * step; |
| 1652 | if (cur < (size_t)self->extra->length) { |
| 1653 | memmove( |
| 1654 | self->extra->children + cur - slicelen, |
| 1655 | self->extra->children + cur, |
| 1656 | (self->extra->length - cur) * sizeof(PyObject *)); |
| 1657 | } |
| 1658 | |
| 1659 | self->extra->length -= slicelen; |
| 1660 | |
| 1661 | /* Discard the recycle list with all the deleted sub-elements */ |
| 1662 | Py_XDECREF(recycle); |
| 1663 | return 0; |
| 1664 | } |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1665 | else { |
Eli Bendersky | 865756a | 2012-03-09 13:38:15 +0200 | [diff] [blame] | 1666 | /* A new slice is actually being assigned */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1667 | seq = PySequence_Fast(value, ""); |
| 1668 | if (!seq) { |
| 1669 | PyErr_Format( |
| 1670 | PyExc_TypeError, |
| 1671 | "expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name |
| 1672 | ); |
| 1673 | return -1; |
| 1674 | } |
| 1675 | newlen = PySequence_Size(seq); |
| 1676 | } |
| 1677 | |
| 1678 | if (step != 1 && newlen != slicelen) |
| 1679 | { |
| 1680 | PyErr_Format(PyExc_ValueError, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1681 | "attempt to assign sequence of size %zd " |
| 1682 | "to extended slice of size %zd", |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1683 | newlen, slicelen |
| 1684 | ); |
| 1685 | return -1; |
| 1686 | } |
| 1687 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1688 | /* Resize before creating the recycle bin, to prevent refleaks. */ |
| 1689 | if (newlen > slicelen) { |
| 1690 | if (element_resize(self, newlen - slicelen) < 0) { |
| 1691 | if (seq) { |
| 1692 | Py_DECREF(seq); |
| 1693 | } |
| 1694 | return -1; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | if (slicelen > 0) { |
| 1699 | /* to avoid recursive calls to this method (via decref), move |
| 1700 | old items to the recycle bin here, and get rid of them when |
| 1701 | we're done modifying the element */ |
| 1702 | recycle = PyList_New(slicelen); |
| 1703 | if (!recycle) { |
| 1704 | if (seq) { |
| 1705 | Py_DECREF(seq); |
| 1706 | } |
| 1707 | return -1; |
| 1708 | } |
| 1709 | for (cur = start, i = 0; i < slicelen; |
| 1710 | cur += step, i++) |
| 1711 | PyList_SET_ITEM(recycle, i, self->extra->children[cur]); |
| 1712 | } |
| 1713 | |
| 1714 | if (newlen < slicelen) { |
| 1715 | /* delete slice */ |
| 1716 | for (i = stop; i < self->extra->length; i++) |
| 1717 | self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
| 1718 | } else if (newlen > slicelen) { |
| 1719 | /* insert slice */ |
| 1720 | for (i = self->extra->length-1; i >= stop; i--) |
| 1721 | self->extra->children[i + newlen - slicelen] = self->extra->children[i]; |
| 1722 | } |
| 1723 | |
| 1724 | /* replace the slice */ |
| 1725 | for (cur = start, i = 0; i < newlen; |
| 1726 | cur += step, i++) { |
| 1727 | PyObject* element = PySequence_Fast_GET_ITEM(seq, i); |
| 1728 | Py_INCREF(element); |
| 1729 | self->extra->children[cur] = element; |
| 1730 | } |
| 1731 | |
| 1732 | self->extra->length += newlen - slicelen; |
| 1733 | |
| 1734 | if (seq) { |
| 1735 | Py_DECREF(seq); |
| 1736 | } |
| 1737 | |
| 1738 | /* discard the recycle bin, and everything in it */ |
| 1739 | Py_XDECREF(recycle); |
| 1740 | |
| 1741 | return 0; |
| 1742 | } |
| 1743 | else { |
| 1744 | PyErr_SetString(PyExc_TypeError, |
| 1745 | "element indices must be integers"); |
| 1746 | return -1; |
| 1747 | } |
| 1748 | } |
| 1749 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1750 | static PyMethodDef element_methods[] = { |
| 1751 | |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 1752 | {"clear", (PyCFunction) element_clearmethod, METH_VARARGS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1753 | |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 1754 | {"get", (PyCFunction) element_get, METH_VARARGS | METH_KEYWORDS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1755 | {"set", (PyCFunction) element_set, METH_VARARGS}, |
| 1756 | |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1757 | {"find", (PyCFunction) element_find, METH_VARARGS | METH_KEYWORDS}, |
| 1758 | {"findtext", (PyCFunction) element_findtext, METH_VARARGS | METH_KEYWORDS}, |
| 1759 | {"findall", (PyCFunction) element_findall, METH_VARARGS | METH_KEYWORDS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1760 | |
| 1761 | {"append", (PyCFunction) element_append, METH_VARARGS}, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1762 | {"extend", (PyCFunction) element_extend, METH_VARARGS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1763 | {"insert", (PyCFunction) element_insert, METH_VARARGS}, |
| 1764 | {"remove", (PyCFunction) element_remove, METH_VARARGS}, |
| 1765 | |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 1766 | {"iter", (PyCFunction) element_iter, METH_VARARGS | METH_KEYWORDS}, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1767 | {"itertext", (PyCFunction) element_itertext, METH_VARARGS}, |
Eli Bendersky | 737b173 | 2012-05-29 06:02:56 +0300 | [diff] [blame] | 1768 | {"iterfind", (PyCFunction) element_iterfind, METH_VARARGS | METH_KEYWORDS}, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1769 | |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 1770 | {"getiterator", (PyCFunction) element_iter, METH_VARARGS | METH_KEYWORDS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1771 | {"getchildren", (PyCFunction) element_getchildren, METH_VARARGS}, |
| 1772 | |
| 1773 | {"items", (PyCFunction) element_items, METH_VARARGS}, |
| 1774 | {"keys", (PyCFunction) element_keys, METH_VARARGS}, |
| 1775 | |
| 1776 | {"makeelement", (PyCFunction) element_makeelement, METH_VARARGS}, |
| 1777 | |
| 1778 | {"__copy__", (PyCFunction) element_copy, METH_VARARGS}, |
| 1779 | {"__deepcopy__", (PyCFunction) element_deepcopy, METH_VARARGS}, |
Martin v. Löwis | bce1666 | 2012-06-17 10:41:22 +0200 | [diff] [blame] | 1780 | {"__sizeof__", element_sizeof, METH_NOARGS}, |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 1781 | {"__getstate__", (PyCFunction)element_getstate, METH_NOARGS}, |
| 1782 | {"__setstate__", (PyCFunction)element_setstate, METH_O}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1783 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1784 | {NULL, NULL} |
| 1785 | }; |
| 1786 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1787 | static PyObject* |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1788 | element_getattro(ElementObject* self, PyObject* nameobj) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1789 | { |
| 1790 | PyObject* res; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1791 | char *name = ""; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1792 | |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 1793 | if (PyUnicode_Check(nameobj)) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1794 | name = _PyUnicode_AsString(nameobj); |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 1795 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 1796 | if (name == NULL) |
| 1797 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1798 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1799 | /* handle common attributes first */ |
| 1800 | if (strcmp(name, "tag") == 0) { |
| 1801 | res = self->tag; |
| 1802 | Py_INCREF(res); |
| 1803 | return res; |
| 1804 | } else if (strcmp(name, "text") == 0) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1805 | res = element_get_text(self); |
Victor Stinner | 71c8b7e | 2013-07-11 23:08:39 +0200 | [diff] [blame] | 1806 | Py_XINCREF(res); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1807 | return res; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1810 | /* methods */ |
| 1811 | res = PyObject_GenericGetAttr((PyObject*) self, nameobj); |
| 1812 | if (res) |
| 1813 | return res; |
| 1814 | |
| 1815 | /* less common attributes */ |
| 1816 | if (strcmp(name, "tail") == 0) { |
| 1817 | PyErr_Clear(); |
| 1818 | res = element_get_tail(self); |
| 1819 | } else if (strcmp(name, "attrib") == 0) { |
| 1820 | PyErr_Clear(); |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 1821 | if (!self->extra) { |
| 1822 | if (create_extra(self, NULL) < 0) |
| 1823 | return NULL; |
| 1824 | } |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1825 | res = element_get_attrib(self); |
| 1826 | } |
| 1827 | |
| 1828 | if (!res) |
| 1829 | return NULL; |
| 1830 | |
| 1831 | Py_INCREF(res); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1832 | return res; |
| 1833 | } |
| 1834 | |
Eli Bendersky | ef9683b | 2013-05-18 07:52:34 -0700 | [diff] [blame] | 1835 | static int |
Eli Bendersky | b20df95 | 2012-05-20 06:33:29 +0300 | [diff] [blame] | 1836 | element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1837 | { |
Eli Bendersky | b20df95 | 2012-05-20 06:33:29 +0300 | [diff] [blame] | 1838 | char *name = ""; |
| 1839 | if (PyUnicode_Check(nameobj)) |
| 1840 | name = _PyUnicode_AsString(nameobj); |
Victor Stinner | 4d46343 | 2013-07-11 23:05:03 +0200 | [diff] [blame] | 1841 | if (name == NULL) |
Eli Bendersky | ef9683b | 2013-05-18 07:52:34 -0700 | [diff] [blame] | 1842 | return -1; |
Victor Stinner | 4d46343 | 2013-07-11 23:05:03 +0200 | [diff] [blame] | 1843 | |
| 1844 | if (strcmp(name, "tag") == 0) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1845 | Py_DECREF(self->tag); |
| 1846 | self->tag = value; |
| 1847 | Py_INCREF(self->tag); |
| 1848 | } else if (strcmp(name, "text") == 0) { |
| 1849 | Py_DECREF(JOIN_OBJ(self->text)); |
| 1850 | self->text = value; |
| 1851 | Py_INCREF(self->text); |
| 1852 | } else if (strcmp(name, "tail") == 0) { |
| 1853 | Py_DECREF(JOIN_OBJ(self->tail)); |
| 1854 | self->tail = value; |
| 1855 | Py_INCREF(self->tail); |
| 1856 | } else if (strcmp(name, "attrib") == 0) { |
Victor Stinner | 5f0af23 | 2013-07-11 23:01:36 +0200 | [diff] [blame] | 1857 | if (!self->extra) { |
| 1858 | if (create_extra(self, NULL) < 0) |
| 1859 | return -1; |
| 1860 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1861 | Py_DECREF(self->extra->attrib); |
| 1862 | self->extra->attrib = value; |
| 1863 | Py_INCREF(self->extra->attrib); |
| 1864 | } else { |
Eli Bendersky | ef9683b | 2013-05-18 07:52:34 -0700 | [diff] [blame] | 1865 | PyErr_SetString(PyExc_AttributeError, |
Eli Bendersky | 6a55dc3 | 2013-05-19 16:59:59 -0700 | [diff] [blame] | 1866 | "Can't set arbitrary attributes on Element"); |
Eli Bendersky | ef9683b | 2013-05-18 07:52:34 -0700 | [diff] [blame] | 1867 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Eli Bendersky | ef9683b | 2013-05-18 07:52:34 -0700 | [diff] [blame] | 1870 | return 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | static PySequenceMethods element_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1874 | (lenfunc) element_length, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1875 | 0, /* sq_concat */ |
| 1876 | 0, /* sq_repeat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1877 | element_getitem, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1878 | 0, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1879 | element_setitem, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 1880 | 0, |
| 1881 | }; |
| 1882 | |
| 1883 | static PyMappingMethods element_as_mapping = { |
| 1884 | (lenfunc) element_length, |
| 1885 | (binaryfunc) element_subscr, |
| 1886 | (objobjargproc) element_ass_subscr, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1887 | }; |
| 1888 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 1889 | static PyTypeObject Element_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1890 | PyVarObject_HEAD_INIT(NULL, 0) |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 1891 | "xml.etree.ElementTree.Element", sizeof(ElementObject), 0, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1892 | /* methods */ |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1893 | (destructor)element_dealloc, /* tp_dealloc */ |
| 1894 | 0, /* tp_print */ |
| 1895 | 0, /* tp_getattr */ |
Eli Bendersky | b20df95 | 2012-05-20 06:33:29 +0300 | [diff] [blame] | 1896 | 0, /* tp_setattr */ |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1897 | 0, /* tp_reserved */ |
| 1898 | (reprfunc)element_repr, /* tp_repr */ |
| 1899 | 0, /* tp_as_number */ |
| 1900 | &element_as_sequence, /* tp_as_sequence */ |
| 1901 | &element_as_mapping, /* tp_as_mapping */ |
| 1902 | 0, /* tp_hash */ |
| 1903 | 0, /* tp_call */ |
| 1904 | 0, /* tp_str */ |
| 1905 | (getattrofunc)element_getattro, /* tp_getattro */ |
Eli Bendersky | b20df95 | 2012-05-20 06:33:29 +0300 | [diff] [blame] | 1906 | (setattrofunc)element_setattro, /* tp_setattro */ |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1907 | 0, /* tp_as_buffer */ |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 1908 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 1909 | /* tp_flags */ |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1910 | 0, /* tp_doc */ |
Eli Bendersky | 0192ba3 | 2012-03-30 16:38:33 +0300 | [diff] [blame] | 1911 | (traverseproc)element_gc_traverse, /* tp_traverse */ |
| 1912 | (inquiry)element_gc_clear, /* tp_clear */ |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1913 | 0, /* tp_richcompare */ |
Eli Bendersky | ebf37a2 | 2012-04-03 22:02:37 +0300 | [diff] [blame] | 1914 | offsetof(ElementObject, weakreflist), /* tp_weaklistoffset */ |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 1915 | 0, /* tp_iter */ |
| 1916 | 0, /* tp_iternext */ |
| 1917 | element_methods, /* tp_methods */ |
| 1918 | 0, /* tp_members */ |
| 1919 | 0, /* tp_getset */ |
| 1920 | 0, /* tp_base */ |
| 1921 | 0, /* tp_dict */ |
| 1922 | 0, /* tp_descr_get */ |
| 1923 | 0, /* tp_descr_set */ |
| 1924 | 0, /* tp_dictoffset */ |
| 1925 | (initproc)element_init, /* tp_init */ |
| 1926 | PyType_GenericAlloc, /* tp_alloc */ |
| 1927 | element_new, /* tp_new */ |
| 1928 | 0, /* tp_free */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 1929 | }; |
| 1930 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 1931 | /******************************* Element iterator ****************************/ |
| 1932 | |
| 1933 | /* ElementIterObject represents the iteration state over an XML element in |
| 1934 | * pre-order traversal. To keep track of which sub-element should be returned |
| 1935 | * next, a stack of parents is maintained. This is a standard stack-based |
| 1936 | * iterative pre-order traversal of a tree. |
| 1937 | * The stack is managed using a single-linked list starting at parent_stack. |
| 1938 | * Each stack node contains the saved parent to which we should return after |
| 1939 | * the current one is exhausted, and the next child to examine in that parent. |
| 1940 | */ |
| 1941 | typedef struct ParentLocator_t { |
| 1942 | ElementObject *parent; |
| 1943 | Py_ssize_t child_index; |
| 1944 | struct ParentLocator_t *next; |
| 1945 | } ParentLocator; |
| 1946 | |
| 1947 | typedef struct { |
| 1948 | PyObject_HEAD |
| 1949 | ParentLocator *parent_stack; |
| 1950 | ElementObject *root_element; |
| 1951 | PyObject *sought_tag; |
| 1952 | int root_done; |
| 1953 | int gettext; |
| 1954 | } ElementIterObject; |
| 1955 | |
| 1956 | |
| 1957 | static void |
| 1958 | elementiter_dealloc(ElementIterObject *it) |
| 1959 | { |
| 1960 | ParentLocator *p = it->parent_stack; |
| 1961 | while (p) { |
| 1962 | ParentLocator *temp = p; |
| 1963 | Py_XDECREF(p->parent); |
| 1964 | p = p->next; |
| 1965 | PyObject_Free(temp); |
| 1966 | } |
| 1967 | |
| 1968 | Py_XDECREF(it->sought_tag); |
| 1969 | Py_XDECREF(it->root_element); |
| 1970 | |
| 1971 | PyObject_GC_UnTrack(it); |
| 1972 | PyObject_GC_Del(it); |
| 1973 | } |
| 1974 | |
| 1975 | static int |
| 1976 | elementiter_traverse(ElementIterObject *it, visitproc visit, void *arg) |
| 1977 | { |
| 1978 | ParentLocator *p = it->parent_stack; |
| 1979 | while (p) { |
| 1980 | Py_VISIT(p->parent); |
| 1981 | p = p->next; |
| 1982 | } |
| 1983 | |
| 1984 | Py_VISIT(it->root_element); |
| 1985 | Py_VISIT(it->sought_tag); |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
| 1989 | /* Helper function for elementiter_next. Add a new parent to the parent stack. |
| 1990 | */ |
| 1991 | static ParentLocator * |
| 1992 | parent_stack_push_new(ParentLocator *stack, ElementObject *parent) |
| 1993 | { |
| 1994 | ParentLocator *new_node = PyObject_Malloc(sizeof(ParentLocator)); |
| 1995 | if (new_node) { |
| 1996 | new_node->parent = parent; |
| 1997 | Py_INCREF(parent); |
| 1998 | new_node->child_index = 0; |
| 1999 | new_node->next = stack; |
| 2000 | } |
| 2001 | return new_node; |
| 2002 | } |
| 2003 | |
| 2004 | static PyObject * |
| 2005 | elementiter_next(ElementIterObject *it) |
| 2006 | { |
| 2007 | /* Sub-element iterator. |
Eli Bendersky | 4583990 | 2013-01-13 05:14:47 -0800 | [diff] [blame] | 2008 | * |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2009 | * A short note on gettext: this function serves both the iter() and |
| 2010 | * itertext() methods to avoid code duplication. However, there are a few |
| 2011 | * small differences in the way these iterations work. Namely: |
| 2012 | * - itertext() only yields text from nodes that have it, and continues |
| 2013 | * iterating when a node doesn't have text (so it doesn't return any |
| 2014 | * node like iter()) |
| 2015 | * - itertext() also has to handle tail, after finishing with all the |
| 2016 | * children of a node. |
| 2017 | */ |
Eli Bendersky | 113da64 | 2012-06-15 07:52:49 +0300 | [diff] [blame] | 2018 | ElementObject *cur_parent; |
| 2019 | Py_ssize_t child_index; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2020 | |
| 2021 | while (1) { |
| 2022 | /* Handle the case reached in the beginning and end of iteration, where |
| 2023 | * the parent stack is empty. The root_done flag gives us indication |
| 2024 | * whether we've just started iterating (so root_done is 0), in which |
| 2025 | * case the root is returned. If root_done is 1 and we're here, the |
| 2026 | * iterator is exhausted. |
| 2027 | */ |
| 2028 | if (!it->parent_stack->parent) { |
| 2029 | if (it->root_done) { |
| 2030 | PyErr_SetNone(PyExc_StopIteration); |
| 2031 | return NULL; |
| 2032 | } else { |
| 2033 | it->parent_stack = parent_stack_push_new(it->parent_stack, |
| 2034 | it->root_element); |
| 2035 | if (!it->parent_stack) { |
| 2036 | PyErr_NoMemory(); |
| 2037 | return NULL; |
| 2038 | } |
| 2039 | |
| 2040 | it->root_done = 1; |
| 2041 | if (it->sought_tag == Py_None || |
| 2042 | PyObject_RichCompareBool(it->root_element->tag, |
| 2043 | it->sought_tag, Py_EQ) == 1) { |
| 2044 | if (it->gettext) { |
Eli Bendersky | e6174ca | 2013-01-10 06:27:53 -0800 | [diff] [blame] | 2045 | PyObject *text = element_get_text(it->root_element); |
| 2046 | if (!text) |
| 2047 | return NULL; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2048 | if (PyObject_IsTrue(text)) { |
| 2049 | Py_INCREF(text); |
| 2050 | return text; |
| 2051 | } |
| 2052 | } else { |
| 2053 | Py_INCREF(it->root_element); |
| 2054 | return (PyObject *)it->root_element; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | /* See if there are children left to traverse in the current parent. If |
| 2061 | * yes, visit the next child. If not, pop the stack and try again. |
| 2062 | */ |
Eli Bendersky | 113da64 | 2012-06-15 07:52:49 +0300 | [diff] [blame] | 2063 | cur_parent = it->parent_stack->parent; |
| 2064 | child_index = it->parent_stack->child_index; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2065 | if (cur_parent->extra && child_index < cur_parent->extra->length) { |
| 2066 | ElementObject *child = (ElementObject *) |
| 2067 | cur_parent->extra->children[child_index]; |
| 2068 | it->parent_stack->child_index++; |
| 2069 | it->parent_stack = parent_stack_push_new(it->parent_stack, |
| 2070 | child); |
| 2071 | if (!it->parent_stack) { |
| 2072 | PyErr_NoMemory(); |
| 2073 | return NULL; |
| 2074 | } |
| 2075 | |
| 2076 | if (it->gettext) { |
Eli Bendersky | e6174ca | 2013-01-10 06:27:53 -0800 | [diff] [blame] | 2077 | PyObject *text = element_get_text(child); |
| 2078 | if (!text) |
| 2079 | return NULL; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2080 | if (PyObject_IsTrue(text)) { |
| 2081 | Py_INCREF(text); |
| 2082 | return text; |
| 2083 | } |
| 2084 | } else if (it->sought_tag == Py_None || |
| 2085 | PyObject_RichCompareBool(child->tag, |
| 2086 | it->sought_tag, Py_EQ) == 1) { |
| 2087 | Py_INCREF(child); |
| 2088 | return (PyObject *)child; |
| 2089 | } |
| 2090 | else |
| 2091 | continue; |
| 2092 | } |
| 2093 | else { |
Eli Bendersky | e6174ca | 2013-01-10 06:27:53 -0800 | [diff] [blame] | 2094 | PyObject *tail; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2095 | ParentLocator *next = it->parent_stack->next; |
Eli Bendersky | e6174ca | 2013-01-10 06:27:53 -0800 | [diff] [blame] | 2096 | if (it->gettext) { |
| 2097 | tail = element_get_tail(cur_parent); |
| 2098 | if (!tail) |
| 2099 | return NULL; |
| 2100 | } |
| 2101 | else |
| 2102 | tail = Py_None; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2103 | Py_XDECREF(it->parent_stack->parent); |
| 2104 | PyObject_Free(it->parent_stack); |
| 2105 | it->parent_stack = next; |
| 2106 | |
| 2107 | /* Note that extra condition on it->parent_stack->parent here; |
| 2108 | * this is because itertext() is supposed to only return *inner* |
| 2109 | * text, not text following the element it began iteration with. |
| 2110 | */ |
| 2111 | if (it->parent_stack->parent && PyObject_IsTrue(tail)) { |
| 2112 | Py_INCREF(tail); |
| 2113 | return tail; |
| 2114 | } |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | return NULL; |
| 2119 | } |
| 2120 | |
| 2121 | |
| 2122 | static PyTypeObject ElementIter_Type = { |
| 2123 | PyVarObject_HEAD_INIT(NULL, 0) |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 2124 | /* Using the module's name since the pure-Python implementation does not |
| 2125 | have such a type. */ |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2126 | "_elementtree._element_iterator", /* tp_name */ |
| 2127 | sizeof(ElementIterObject), /* tp_basicsize */ |
| 2128 | 0, /* tp_itemsize */ |
| 2129 | /* methods */ |
| 2130 | (destructor)elementiter_dealloc, /* tp_dealloc */ |
| 2131 | 0, /* tp_print */ |
| 2132 | 0, /* tp_getattr */ |
| 2133 | 0, /* tp_setattr */ |
| 2134 | 0, /* tp_reserved */ |
| 2135 | 0, /* tp_repr */ |
| 2136 | 0, /* tp_as_number */ |
| 2137 | 0, /* tp_as_sequence */ |
| 2138 | 0, /* tp_as_mapping */ |
| 2139 | 0, /* tp_hash */ |
| 2140 | 0, /* tp_call */ |
| 2141 | 0, /* tp_str */ |
| 2142 | 0, /* tp_getattro */ |
| 2143 | 0, /* tp_setattro */ |
| 2144 | 0, /* tp_as_buffer */ |
| 2145 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2146 | 0, /* tp_doc */ |
| 2147 | (traverseproc)elementiter_traverse, /* tp_traverse */ |
| 2148 | 0, /* tp_clear */ |
| 2149 | 0, /* tp_richcompare */ |
| 2150 | 0, /* tp_weaklistoffset */ |
| 2151 | PyObject_SelfIter, /* tp_iter */ |
| 2152 | (iternextfunc)elementiter_next, /* tp_iternext */ |
| 2153 | 0, /* tp_methods */ |
| 2154 | 0, /* tp_members */ |
| 2155 | 0, /* tp_getset */ |
| 2156 | 0, /* tp_base */ |
| 2157 | 0, /* tp_dict */ |
| 2158 | 0, /* tp_descr_get */ |
| 2159 | 0, /* tp_descr_set */ |
| 2160 | 0, /* tp_dictoffset */ |
| 2161 | 0, /* tp_init */ |
| 2162 | 0, /* tp_alloc */ |
| 2163 | 0, /* tp_new */ |
| 2164 | }; |
| 2165 | |
| 2166 | |
| 2167 | static PyObject * |
| 2168 | create_elementiter(ElementObject *self, PyObject *tag, int gettext) |
| 2169 | { |
| 2170 | ElementIterObject *it; |
| 2171 | PyObject *star = NULL; |
| 2172 | |
| 2173 | it = PyObject_GC_New(ElementIterObject, &ElementIter_Type); |
| 2174 | if (!it) |
| 2175 | return NULL; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2176 | |
| 2177 | if (PyUnicode_Check(tag)) |
| 2178 | star = PyUnicode_FromString("*"); |
| 2179 | else if (PyBytes_Check(tag)) |
| 2180 | star = PyBytes_FromString("*"); |
| 2181 | |
| 2182 | if (star && PyObject_RichCompareBool(tag, star, Py_EQ) == 1) |
| 2183 | tag = Py_None; |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2184 | Py_XDECREF(star); |
Victor Stinner | 4d46343 | 2013-07-11 23:05:03 +0200 | [diff] [blame] | 2185 | |
| 2186 | Py_INCREF(tag); |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2187 | it->sought_tag = tag; |
| 2188 | it->root_done = 0; |
| 2189 | it->gettext = gettext; |
Victor Stinner | 4d46343 | 2013-07-11 23:05:03 +0200 | [diff] [blame] | 2190 | Py_INCREF(self); |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2191 | it->root_element = self; |
| 2192 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2193 | PyObject_GC_Track(it); |
Victor Stinner | d917dcb | 2013-07-12 02:05:17 +0200 | [diff] [blame] | 2194 | |
| 2195 | it->parent_stack = PyObject_Malloc(sizeof(ParentLocator)); |
| 2196 | if (it->parent_stack == NULL) { |
| 2197 | Py_DECREF(it); |
| 2198 | PyErr_NoMemory(); |
| 2199 | return NULL; |
| 2200 | } |
| 2201 | it->parent_stack->parent = NULL; |
| 2202 | it->parent_stack->child_index = 0; |
| 2203 | it->parent_stack->next = NULL; |
| 2204 | |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 2205 | return (PyObject *)it; |
| 2206 | } |
| 2207 | |
| 2208 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2209 | /* ==================================================================== */ |
| 2210 | /* the tree builder type */ |
| 2211 | |
| 2212 | typedef struct { |
| 2213 | PyObject_HEAD |
| 2214 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2215 | PyObject *root; /* root node (first created node) */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2216 | |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2217 | PyObject *this; /* current node */ |
| 2218 | PyObject *last; /* most recently created node */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2219 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2220 | PyObject *data; /* data collector (string or list), or NULL */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2221 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2222 | PyObject *stack; /* element stack */ |
| 2223 | Py_ssize_t index; /* current stack size (0 means empty) */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2224 | |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2225 | PyObject *element_factory; |
| 2226 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2227 | /* element tracing */ |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2228 | PyObject *events; /* list of events, or NULL if not collecting */ |
| 2229 | PyObject *start_event_obj; /* event objects (NULL to ignore) */ |
| 2230 | PyObject *end_event_obj; |
| 2231 | PyObject *start_ns_event_obj; |
| 2232 | PyObject *end_ns_event_obj; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2233 | } TreeBuilderObject; |
| 2234 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 2235 | #define TreeBuilder_CheckExact(op) (Py_TYPE(op) == &TreeBuilder_Type) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2236 | |
| 2237 | /* -------------------------------------------------------------------- */ |
| 2238 | /* constructor and destructor */ |
| 2239 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2240 | static PyObject * |
| 2241 | treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2242 | { |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2243 | TreeBuilderObject *t = (TreeBuilderObject *)type->tp_alloc(type, 0); |
| 2244 | if (t != NULL) { |
| 2245 | t->root = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2246 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2247 | Py_INCREF(Py_None); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2248 | t->this = Py_None; |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2249 | Py_INCREF(Py_None); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2250 | t->last = Py_None; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2251 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2252 | t->data = NULL; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2253 | t->element_factory = NULL; |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2254 | t->stack = PyList_New(20); |
| 2255 | if (!t->stack) { |
| 2256 | Py_DECREF(t->this); |
| 2257 | Py_DECREF(t->last); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2258 | Py_DECREF((PyObject *) t); |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2259 | return NULL; |
| 2260 | } |
| 2261 | t->index = 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2262 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2263 | t->events = NULL; |
| 2264 | t->start_event_obj = t->end_event_obj = NULL; |
| 2265 | t->start_ns_event_obj = t->end_ns_event_obj = NULL; |
| 2266 | } |
| 2267 | return (PyObject *)t; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2270 | static int |
| 2271 | treebuilder_init(PyObject *self, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2272 | { |
Eli Bendersky | c68e136 | 2012-06-03 06:09:42 +0300 | [diff] [blame] | 2273 | static char *kwlist[] = {"element_factory", 0}; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2274 | PyObject *element_factory = NULL; |
| 2275 | TreeBuilderObject *self_tb = (TreeBuilderObject *)self; |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2276 | PyObject *tmp; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2277 | |
| 2278 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:TreeBuilder", kwlist, |
| 2279 | &element_factory)) { |
| 2280 | return -1; |
| 2281 | } |
| 2282 | |
| 2283 | if (element_factory) { |
| 2284 | Py_INCREF(element_factory); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2285 | tmp = self_tb->element_factory; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2286 | self_tb->element_factory = element_factory; |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2287 | Py_XDECREF(tmp); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2288 | } |
| 2289 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2290 | return 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2293 | static int |
| 2294 | treebuilder_gc_traverse(TreeBuilderObject *self, visitproc visit, void *arg) |
| 2295 | { |
| 2296 | Py_VISIT(self->root); |
| 2297 | Py_VISIT(self->this); |
| 2298 | Py_VISIT(self->last); |
| 2299 | Py_VISIT(self->data); |
| 2300 | Py_VISIT(self->stack); |
| 2301 | Py_VISIT(self->element_factory); |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
| 2305 | static int |
| 2306 | treebuilder_gc_clear(TreeBuilderObject *self) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2307 | { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2308 | Py_CLEAR(self->end_ns_event_obj); |
| 2309 | Py_CLEAR(self->start_ns_event_obj); |
| 2310 | Py_CLEAR(self->end_event_obj); |
| 2311 | Py_CLEAR(self->start_event_obj); |
| 2312 | Py_CLEAR(self->events); |
| 2313 | Py_CLEAR(self->stack); |
| 2314 | Py_CLEAR(self->data); |
| 2315 | Py_CLEAR(self->last); |
| 2316 | Py_CLEAR(self->this); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2317 | Py_CLEAR(self->element_factory); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2318 | Py_CLEAR(self->root); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2319 | return 0; |
| 2320 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2321 | |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2322 | static void |
| 2323 | treebuilder_dealloc(TreeBuilderObject *self) |
| 2324 | { |
| 2325 | PyObject_GC_UnTrack(self); |
| 2326 | treebuilder_gc_clear(self); |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2327 | Py_TYPE(self)->tp_free((PyObject *)self); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | /* -------------------------------------------------------------------- */ |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2331 | /* helpers for handling of arbitrary element-like objects */ |
| 2332 | |
| 2333 | static int |
| 2334 | treebuilder_set_element_text_or_tail(PyObject *element, PyObject *data, |
| 2335 | PyObject **dest, _Py_Identifier *name) |
| 2336 | { |
| 2337 | if (Element_CheckExact(element)) { |
| 2338 | Py_DECREF(JOIN_OBJ(*dest)); |
| 2339 | *dest = JOIN_SET(data, PyList_CheckExact(data)); |
| 2340 | return 0; |
| 2341 | } |
| 2342 | else { |
| 2343 | PyObject *joined = list_join(data); |
| 2344 | int r; |
| 2345 | if (joined == NULL) |
| 2346 | return -1; |
| 2347 | r = _PyObject_SetAttrId(element, name, joined); |
| 2348 | Py_DECREF(joined); |
| 2349 | return r; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | /* These two functions steal a reference to data */ |
| 2354 | static int |
| 2355 | treebuilder_set_element_text(PyObject *element, PyObject *data) |
| 2356 | { |
| 2357 | _Py_IDENTIFIER(text); |
| 2358 | return treebuilder_set_element_text_or_tail( |
| 2359 | element, data, &((ElementObject *) element)->text, &PyId_text); |
| 2360 | } |
| 2361 | |
| 2362 | static int |
| 2363 | treebuilder_set_element_tail(PyObject *element, PyObject *data) |
| 2364 | { |
| 2365 | _Py_IDENTIFIER(tail); |
| 2366 | return treebuilder_set_element_text_or_tail( |
| 2367 | element, data, &((ElementObject *) element)->tail, &PyId_tail); |
| 2368 | } |
| 2369 | |
| 2370 | static int |
| 2371 | treebuilder_add_subelement(PyObject *element, PyObject *child) |
| 2372 | { |
| 2373 | _Py_IDENTIFIER(append); |
| 2374 | if (Element_CheckExact(element)) { |
| 2375 | ElementObject *elem = (ElementObject *) element; |
| 2376 | return element_add_subelement(elem, child); |
| 2377 | } |
| 2378 | else { |
| 2379 | PyObject *res; |
| 2380 | res = _PyObject_CallMethodId(element, &PyId_append, "O", child); |
| 2381 | if (res == NULL) |
| 2382 | return -1; |
| 2383 | Py_DECREF(res); |
| 2384 | return 0; |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2389 | /* handlers */ |
| 2390 | |
| 2391 | LOCAL(PyObject*) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2392 | treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag, |
| 2393 | PyObject* attrib) |
| 2394 | { |
| 2395 | PyObject* node; |
| 2396 | PyObject* this; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 2397 | elementtreestate *st = ET_STATE_GLOBAL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2398 | |
| 2399 | if (self->data) { |
| 2400 | if (self->this == self->last) { |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2401 | if (treebuilder_set_element_text(self->last, self->data)) |
| 2402 | return NULL; |
| 2403 | } |
| 2404 | else { |
| 2405 | if (treebuilder_set_element_tail(self->last, self->data)) |
| 2406 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2407 | } |
| 2408 | self->data = NULL; |
| 2409 | } |
| 2410 | |
Eli Bendersky | 08231a9 | 2013-05-18 15:47:16 -0700 | [diff] [blame] | 2411 | if (self->element_factory && self->element_factory != Py_None) { |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2412 | node = PyObject_CallFunction(self->element_factory, "OO", tag, attrib); |
| 2413 | } else { |
| 2414 | node = create_new_element(tag, attrib); |
| 2415 | } |
| 2416 | if (!node) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2417 | return NULL; |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2418 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2419 | |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2420 | this = self->this; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2421 | |
| 2422 | if (this != Py_None) { |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2423 | if (treebuilder_add_subelement(this, node) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2424 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2425 | } else { |
| 2426 | if (self->root) { |
| 2427 | PyErr_SetString( |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 2428 | st->parseerror_obj, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2429 | "multiple elements on top level" |
| 2430 | ); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2431 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2432 | } |
| 2433 | Py_INCREF(node); |
| 2434 | self->root = node; |
| 2435 | } |
| 2436 | |
| 2437 | if (self->index < PyList_GET_SIZE(self->stack)) { |
| 2438 | if (PyList_SetItem(self->stack, self->index, this) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2439 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2440 | Py_INCREF(this); |
| 2441 | } else { |
| 2442 | if (PyList_Append(self->stack, this) < 0) |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2443 | goto error; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2444 | } |
| 2445 | self->index++; |
| 2446 | |
| 2447 | Py_DECREF(this); |
| 2448 | Py_INCREF(node); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2449 | self->this = node; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2450 | |
| 2451 | Py_DECREF(self->last); |
| 2452 | Py_INCREF(node); |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2453 | self->last = node; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2454 | |
| 2455 | if (self->start_event_obj) { |
| 2456 | PyObject* res; |
| 2457 | PyObject* action = self->start_event_obj; |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2458 | res = PyTuple_Pack(2, action, node); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2459 | if (res) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2460 | PyList_Append(self->events, res); |
| 2461 | Py_DECREF(res); |
| 2462 | } else |
| 2463 | PyErr_Clear(); /* FIXME: propagate error */ |
| 2464 | } |
| 2465 | |
| 2466 | return node; |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2467 | |
| 2468 | error: |
| 2469 | Py_DECREF(node); |
| 2470 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | LOCAL(PyObject*) |
| 2474 | treebuilder_handle_data(TreeBuilderObject* self, PyObject* data) |
| 2475 | { |
| 2476 | if (!self->data) { |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2477 | if (self->last == Py_None) { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2478 | /* ignore calls to data before the first call to start */ |
| 2479 | Py_RETURN_NONE; |
| 2480 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2481 | /* store the first item as is */ |
| 2482 | Py_INCREF(data); self->data = data; |
| 2483 | } else { |
| 2484 | /* more than one item; use a list to collect items */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2485 | if (PyBytes_CheckExact(self->data) && Py_REFCNT(self->data) == 1 && |
| 2486 | PyBytes_CheckExact(data) && PyBytes_GET_SIZE(data) == 1) { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2487 | /* XXX this code path unused in Python 3? */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2488 | /* expat often generates single character data sections; handle |
| 2489 | the most common case by resizing the existing string... */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2490 | Py_ssize_t size = PyBytes_GET_SIZE(self->data); |
| 2491 | if (_PyBytes_Resize(&self->data, size + 1) < 0) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2492 | return NULL; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2493 | PyBytes_AS_STRING(self->data)[size] = PyBytes_AS_STRING(data)[0]; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2494 | } else if (PyList_CheckExact(self->data)) { |
| 2495 | if (PyList_Append(self->data, data) < 0) |
| 2496 | return NULL; |
| 2497 | } else { |
| 2498 | PyObject* list = PyList_New(2); |
| 2499 | if (!list) |
| 2500 | return NULL; |
| 2501 | PyList_SET_ITEM(list, 0, self->data); |
| 2502 | Py_INCREF(data); PyList_SET_ITEM(list, 1, data); |
| 2503 | self->data = list; |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | Py_RETURN_NONE; |
| 2508 | } |
| 2509 | |
| 2510 | LOCAL(PyObject*) |
| 2511 | treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag) |
| 2512 | { |
| 2513 | PyObject* item; |
| 2514 | |
| 2515 | if (self->data) { |
| 2516 | if (self->this == self->last) { |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2517 | if (treebuilder_set_element_text(self->last, self->data)) |
| 2518 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2519 | } else { |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2520 | if (treebuilder_set_element_tail(self->last, self->data)) |
| 2521 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2522 | } |
| 2523 | self->data = NULL; |
| 2524 | } |
| 2525 | |
| 2526 | if (self->index == 0) { |
| 2527 | PyErr_SetString( |
| 2528 | PyExc_IndexError, |
| 2529 | "pop from empty stack" |
| 2530 | ); |
| 2531 | return NULL; |
| 2532 | } |
| 2533 | |
| 2534 | self->index--; |
| 2535 | |
| 2536 | item = PyList_GET_ITEM(self->stack, self->index); |
| 2537 | Py_INCREF(item); |
| 2538 | |
| 2539 | Py_DECREF(self->last); |
| 2540 | |
Antoine Pitrou | ee32931 | 2012-10-04 19:53:29 +0200 | [diff] [blame] | 2541 | self->last = self->this; |
| 2542 | self->this = item; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2543 | |
| 2544 | if (self->end_event_obj) { |
| 2545 | PyObject* res; |
| 2546 | PyObject* action = self->end_event_obj; |
| 2547 | PyObject* node = (PyObject*) self->last; |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2548 | res = PyTuple_Pack(2, action, node); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2549 | if (res) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2550 | PyList_Append(self->events, res); |
| 2551 | Py_DECREF(res); |
| 2552 | } else |
| 2553 | PyErr_Clear(); /* FIXME: propagate error */ |
| 2554 | } |
| 2555 | |
| 2556 | Py_INCREF(self->last); |
| 2557 | return (PyObject*) self->last; |
| 2558 | } |
| 2559 | |
| 2560 | LOCAL(void) |
| 2561 | treebuilder_handle_namespace(TreeBuilderObject* self, int start, |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2562 | PyObject *prefix, PyObject *uri) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2563 | { |
| 2564 | PyObject* res; |
| 2565 | PyObject* action; |
| 2566 | PyObject* parcel; |
| 2567 | |
| 2568 | if (!self->events) |
| 2569 | return; |
| 2570 | |
| 2571 | if (start) { |
| 2572 | if (!self->start_ns_event_obj) |
| 2573 | return; |
| 2574 | action = self->start_ns_event_obj; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2575 | parcel = Py_BuildValue("OO", prefix, uri); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2576 | if (!parcel) |
| 2577 | return; |
| 2578 | Py_INCREF(action); |
| 2579 | } else { |
| 2580 | if (!self->end_ns_event_obj) |
| 2581 | return; |
| 2582 | action = self->end_ns_event_obj; |
| 2583 | Py_INCREF(action); |
| 2584 | parcel = Py_None; |
| 2585 | Py_INCREF(parcel); |
| 2586 | } |
| 2587 | |
| 2588 | res = PyTuple_New(2); |
| 2589 | |
| 2590 | if (res) { |
| 2591 | PyTuple_SET_ITEM(res, 0, action); |
| 2592 | PyTuple_SET_ITEM(res, 1, parcel); |
| 2593 | PyList_Append(self->events, res); |
| 2594 | Py_DECREF(res); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2595 | } |
| 2596 | else { |
| 2597 | Py_DECREF(action); |
| 2598 | Py_DECREF(parcel); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2599 | PyErr_Clear(); /* FIXME: propagate error */ |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2600 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | /* -------------------------------------------------------------------- */ |
| 2604 | /* methods (in alphabetical order) */ |
| 2605 | |
| 2606 | static PyObject* |
| 2607 | treebuilder_data(TreeBuilderObject* self, PyObject* args) |
| 2608 | { |
| 2609 | PyObject* data; |
| 2610 | if (!PyArg_ParseTuple(args, "O:data", &data)) |
| 2611 | return NULL; |
| 2612 | |
| 2613 | return treebuilder_handle_data(self, data); |
| 2614 | } |
| 2615 | |
| 2616 | static PyObject* |
| 2617 | treebuilder_end(TreeBuilderObject* self, PyObject* args) |
| 2618 | { |
| 2619 | PyObject* tag; |
| 2620 | if (!PyArg_ParseTuple(args, "O:end", &tag)) |
| 2621 | return NULL; |
| 2622 | |
| 2623 | return treebuilder_handle_end(self, tag); |
| 2624 | } |
| 2625 | |
| 2626 | LOCAL(PyObject*) |
| 2627 | treebuilder_done(TreeBuilderObject* self) |
| 2628 | { |
| 2629 | PyObject* res; |
| 2630 | |
| 2631 | /* FIXME: check stack size? */ |
| 2632 | |
| 2633 | if (self->root) |
| 2634 | res = self->root; |
| 2635 | else |
| 2636 | res = Py_None; |
| 2637 | |
| 2638 | Py_INCREF(res); |
| 2639 | return res; |
| 2640 | } |
| 2641 | |
| 2642 | static PyObject* |
| 2643 | treebuilder_close(TreeBuilderObject* self, PyObject* args) |
| 2644 | { |
| 2645 | if (!PyArg_ParseTuple(args, ":close")) |
| 2646 | return NULL; |
| 2647 | |
| 2648 | return treebuilder_done(self); |
| 2649 | } |
| 2650 | |
| 2651 | static PyObject* |
| 2652 | treebuilder_start(TreeBuilderObject* self, PyObject* args) |
| 2653 | { |
| 2654 | PyObject* tag; |
| 2655 | PyObject* attrib = Py_None; |
| 2656 | if (!PyArg_ParseTuple(args, "O|O:start", &tag, &attrib)) |
| 2657 | return NULL; |
| 2658 | |
| 2659 | return treebuilder_handle_start(self, tag, attrib); |
| 2660 | } |
| 2661 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2662 | static PyMethodDef treebuilder_methods[] = { |
| 2663 | {"data", (PyCFunction) treebuilder_data, METH_VARARGS}, |
| 2664 | {"start", (PyCFunction) treebuilder_start, METH_VARARGS}, |
| 2665 | {"end", (PyCFunction) treebuilder_end, METH_VARARGS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2666 | {"close", (PyCFunction) treebuilder_close, METH_VARARGS}, |
| 2667 | {NULL, NULL} |
| 2668 | }; |
| 2669 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 2670 | static PyTypeObject TreeBuilder_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2671 | PyVarObject_HEAD_INIT(NULL, 0) |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 2672 | "xml.etree.ElementTree.TreeBuilder", sizeof(TreeBuilderObject), 0, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2673 | /* methods */ |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2674 | (destructor)treebuilder_dealloc, /* tp_dealloc */ |
| 2675 | 0, /* tp_print */ |
| 2676 | 0, /* tp_getattr */ |
| 2677 | 0, /* tp_setattr */ |
| 2678 | 0, /* tp_reserved */ |
| 2679 | 0, /* tp_repr */ |
| 2680 | 0, /* tp_as_number */ |
| 2681 | 0, /* tp_as_sequence */ |
| 2682 | 0, /* tp_as_mapping */ |
| 2683 | 0, /* tp_hash */ |
| 2684 | 0, /* tp_call */ |
| 2685 | 0, /* tp_str */ |
| 2686 | 0, /* tp_getattro */ |
| 2687 | 0, /* tp_setattro */ |
| 2688 | 0, /* tp_as_buffer */ |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2689 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 2690 | /* tp_flags */ |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2691 | 0, /* tp_doc */ |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2692 | (traverseproc)treebuilder_gc_traverse, /* tp_traverse */ |
| 2693 | (inquiry)treebuilder_gc_clear, /* tp_clear */ |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 2694 | 0, /* tp_richcompare */ |
| 2695 | 0, /* tp_weaklistoffset */ |
| 2696 | 0, /* tp_iter */ |
| 2697 | 0, /* tp_iternext */ |
| 2698 | treebuilder_methods, /* tp_methods */ |
| 2699 | 0, /* tp_members */ |
| 2700 | 0, /* tp_getset */ |
| 2701 | 0, /* tp_base */ |
| 2702 | 0, /* tp_dict */ |
| 2703 | 0, /* tp_descr_get */ |
| 2704 | 0, /* tp_descr_set */ |
| 2705 | 0, /* tp_dictoffset */ |
| 2706 | (initproc)treebuilder_init, /* tp_init */ |
| 2707 | PyType_GenericAlloc, /* tp_alloc */ |
| 2708 | treebuilder_new, /* tp_new */ |
| 2709 | 0, /* tp_free */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2710 | }; |
| 2711 | |
| 2712 | /* ==================================================================== */ |
| 2713 | /* the expat interface */ |
| 2714 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2715 | #include "expat.h" |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2716 | #include "pyexpat.h" |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 2717 | |
| 2718 | /* The PyExpat_CAPI structure is an immutable dispatch table, so it can be |
| 2719 | * cached globally without being in per-module state. |
| 2720 | */ |
Eli Bendersky | 20d4174 | 2012-06-01 09:48:37 +0300 | [diff] [blame] | 2721 | static struct PyExpat_CAPI *expat_capi; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2722 | #define EXPAT(func) (expat_capi->func) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2723 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 2724 | static XML_Memory_Handling_Suite ExpatMemoryHandler = { |
| 2725 | PyObject_Malloc, PyObject_Realloc, PyObject_Free}; |
| 2726 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2727 | typedef struct { |
| 2728 | PyObject_HEAD |
| 2729 | |
| 2730 | XML_Parser parser; |
| 2731 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 2732 | PyObject *target; |
| 2733 | PyObject *entity; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2734 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 2735 | PyObject *names; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2736 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 2737 | PyObject *handle_start; |
| 2738 | PyObject *handle_data; |
| 2739 | PyObject *handle_end; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2740 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 2741 | PyObject *handle_comment; |
| 2742 | PyObject *handle_pi; |
| 2743 | PyObject *handle_doctype; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2744 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 2745 | PyObject *handle_close; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2746 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2747 | } XMLParserObject; |
| 2748 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 2749 | #define XMLParser_CheckExact(op) (Py_TYPE(op) == &XMLParser_Type) |
| 2750 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2751 | /* helpers */ |
| 2752 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2753 | LOCAL(PyObject*) |
| 2754 | makeuniversal(XMLParserObject* self, const char* string) |
| 2755 | { |
| 2756 | /* convert a UTF-8 tag/attribute name from the expat parser |
| 2757 | to a universal name string */ |
| 2758 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2759 | Py_ssize_t size = (Py_ssize_t) strlen(string); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2760 | PyObject* key; |
| 2761 | PyObject* value; |
| 2762 | |
| 2763 | /* look the 'raw' name up in the names dictionary */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2764 | key = PyBytes_FromStringAndSize(string, size); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2765 | if (!key) |
| 2766 | return NULL; |
| 2767 | |
| 2768 | value = PyDict_GetItem(self->names, key); |
| 2769 | |
| 2770 | if (value) { |
| 2771 | Py_INCREF(value); |
| 2772 | } else { |
| 2773 | /* new name. convert to universal name, and decode as |
| 2774 | necessary */ |
| 2775 | |
| 2776 | PyObject* tag; |
| 2777 | char* p; |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2778 | Py_ssize_t i; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2779 | |
| 2780 | /* look for namespace separator */ |
| 2781 | for (i = 0; i < size; i++) |
| 2782 | if (string[i] == '}') |
| 2783 | break; |
| 2784 | if (i != size) { |
| 2785 | /* convert to universal name */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2786 | tag = PyBytes_FromStringAndSize(NULL, size+1); |
Victor Stinner | 71c8b7e | 2013-07-11 23:08:39 +0200 | [diff] [blame] | 2787 | if (tag == NULL) { |
| 2788 | Py_DECREF(key); |
| 2789 | return NULL; |
| 2790 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2791 | p = PyBytes_AS_STRING(tag); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2792 | p[0] = '{'; |
| 2793 | memcpy(p+1, string, size); |
| 2794 | size++; |
| 2795 | } else { |
| 2796 | /* plain name; use key as tag */ |
| 2797 | Py_INCREF(key); |
| 2798 | tag = key; |
| 2799 | } |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 2800 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2801 | /* decode universal name */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 2802 | p = PyBytes_AS_STRING(tag); |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2803 | value = PyUnicode_DecodeUTF8(p, size, "strict"); |
| 2804 | Py_DECREF(tag); |
| 2805 | if (!value) { |
| 2806 | Py_DECREF(key); |
| 2807 | return NULL; |
| 2808 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2809 | |
| 2810 | /* add to names dictionary */ |
| 2811 | if (PyDict_SetItem(self->names, key, value) < 0) { |
| 2812 | Py_DECREF(key); |
| 2813 | Py_DECREF(value); |
| 2814 | return NULL; |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | Py_DECREF(key); |
| 2819 | return value; |
| 2820 | } |
| 2821 | |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 2822 | /* Set the ParseError exception with the given parameters. |
| 2823 | * If message is not NULL, it's used as the error string. Otherwise, the |
| 2824 | * message string is the default for the given error_code. |
| 2825 | */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2826 | static void |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 2827 | expat_set_error(enum XML_Error error_code, int line, int column, char *message) |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2828 | { |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 2829 | PyObject *errmsg, *error, *position, *code; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 2830 | elementtreestate *st = ET_STATE_GLOBAL; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2831 | |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 2832 | errmsg = PyUnicode_FromFormat("%s: line %d, column %d", |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 2833 | message ? message : EXPAT(ErrorString)(error_code), |
| 2834 | line, column); |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 2835 | if (errmsg == NULL) |
| 2836 | return; |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2837 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 2838 | error = PyObject_CallFunction(st->parseerror_obj, "O", errmsg); |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 2839 | Py_DECREF(errmsg); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2840 | if (!error) |
| 2841 | return; |
| 2842 | |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 2843 | /* Add code and position attributes */ |
| 2844 | code = PyLong_FromLong((long)error_code); |
| 2845 | if (!code) { |
| 2846 | Py_DECREF(error); |
| 2847 | return; |
| 2848 | } |
| 2849 | if (PyObject_SetAttrString(error, "code", code) == -1) { |
| 2850 | Py_DECREF(error); |
| 2851 | Py_DECREF(code); |
| 2852 | return; |
| 2853 | } |
| 2854 | Py_DECREF(code); |
| 2855 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2856 | position = Py_BuildValue("(ii)", line, column); |
| 2857 | if (!position) { |
| 2858 | Py_DECREF(error); |
| 2859 | return; |
| 2860 | } |
| 2861 | if (PyObject_SetAttrString(error, "position", position) == -1) { |
| 2862 | Py_DECREF(error); |
| 2863 | Py_DECREF(position); |
| 2864 | return; |
| 2865 | } |
| 2866 | Py_DECREF(position); |
| 2867 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 2868 | PyErr_SetObject(st->parseerror_obj, error); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2869 | Py_DECREF(error); |
| 2870 | } |
| 2871 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2872 | /* -------------------------------------------------------------------- */ |
| 2873 | /* handlers */ |
| 2874 | |
| 2875 | static void |
| 2876 | expat_default_handler(XMLParserObject* self, const XML_Char* data_in, |
| 2877 | int data_len) |
| 2878 | { |
| 2879 | PyObject* key; |
| 2880 | PyObject* value; |
| 2881 | PyObject* res; |
| 2882 | |
| 2883 | if (data_len < 2 || data_in[0] != '&') |
| 2884 | return; |
| 2885 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 2886 | if (PyErr_Occurred()) |
| 2887 | return; |
| 2888 | |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2889 | key = PyUnicode_DecodeUTF8(data_in + 1, data_len - 2, "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2890 | if (!key) |
| 2891 | return; |
| 2892 | |
| 2893 | value = PyDict_GetItem(self->entity, key); |
| 2894 | |
| 2895 | if (value) { |
| 2896 | if (TreeBuilder_CheckExact(self->target)) |
| 2897 | res = treebuilder_handle_data( |
| 2898 | (TreeBuilderObject*) self->target, value |
| 2899 | ); |
| 2900 | else if (self->handle_data) |
| 2901 | res = PyObject_CallFunction(self->handle_data, "O", value); |
| 2902 | else |
| 2903 | res = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2904 | Py_XDECREF(res); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2905 | } else if (!PyErr_Occurred()) { |
| 2906 | /* Report the first error, not the last */ |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 2907 | char message[128] = "undefined entity "; |
| 2908 | strncat(message, data_in, data_len < 100?data_len:100); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2909 | expat_set_error( |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 2910 | XML_ERROR_UNDEFINED_ENTITY, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2911 | EXPAT(GetErrorLineNumber)(self->parser), |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 2912 | EXPAT(GetErrorColumnNumber)(self->parser), |
| 2913 | message |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2914 | ); |
| 2915 | } |
| 2916 | |
| 2917 | Py_DECREF(key); |
| 2918 | } |
| 2919 | |
| 2920 | static void |
| 2921 | expat_start_handler(XMLParserObject* self, const XML_Char* tag_in, |
| 2922 | const XML_Char **attrib_in) |
| 2923 | { |
| 2924 | PyObject* res; |
| 2925 | PyObject* tag; |
| 2926 | PyObject* attrib; |
| 2927 | int ok; |
| 2928 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 2929 | if (PyErr_Occurred()) |
| 2930 | return; |
| 2931 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2932 | /* tag name */ |
| 2933 | tag = makeuniversal(self, tag_in); |
| 2934 | if (!tag) |
| 2935 | return; /* parser will look for errors */ |
| 2936 | |
| 2937 | /* attributes */ |
| 2938 | if (attrib_in[0]) { |
| 2939 | attrib = PyDict_New(); |
| 2940 | if (!attrib) |
| 2941 | return; |
| 2942 | while (attrib_in[0] && attrib_in[1]) { |
| 2943 | PyObject* key = makeuniversal(self, attrib_in[0]); |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2944 | PyObject* value = PyUnicode_DecodeUTF8(attrib_in[1], strlen(attrib_in[1]), "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2945 | if (!key || !value) { |
| 2946 | Py_XDECREF(value); |
| 2947 | Py_XDECREF(key); |
| 2948 | Py_DECREF(attrib); |
| 2949 | return; |
| 2950 | } |
| 2951 | ok = PyDict_SetItem(attrib, key, value); |
| 2952 | Py_DECREF(value); |
| 2953 | Py_DECREF(key); |
| 2954 | if (ok < 0) { |
| 2955 | Py_DECREF(attrib); |
| 2956 | return; |
| 2957 | } |
| 2958 | attrib_in += 2; |
| 2959 | } |
| 2960 | } else { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 2961 | /* Pass an empty dictionary on */ |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2962 | attrib = PyDict_New(); |
| 2963 | if (!attrib) |
| 2964 | return; |
| 2965 | } |
| 2966 | |
| 2967 | if (TreeBuilder_CheckExact(self->target)) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2968 | /* shortcut */ |
| 2969 | res = treebuilder_handle_start((TreeBuilderObject*) self->target, |
| 2970 | tag, attrib); |
Eli Bendersky | 48d358b | 2012-05-30 17:57:50 +0300 | [diff] [blame] | 2971 | } |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2972 | else if (self->handle_start) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2973 | res = PyObject_CallFunction(self->handle_start, "OO", tag, attrib); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 2974 | } else |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2975 | res = NULL; |
| 2976 | |
| 2977 | Py_DECREF(tag); |
| 2978 | Py_DECREF(attrib); |
| 2979 | |
| 2980 | Py_XDECREF(res); |
| 2981 | } |
| 2982 | |
| 2983 | static void |
| 2984 | expat_data_handler(XMLParserObject* self, const XML_Char* data_in, |
| 2985 | int data_len) |
| 2986 | { |
| 2987 | PyObject* data; |
| 2988 | PyObject* res; |
| 2989 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 2990 | if (PyErr_Occurred()) |
| 2991 | return; |
| 2992 | |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 2993 | data = PyUnicode_DecodeUTF8(data_in, data_len, "strict"); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 2994 | if (!data) |
| 2995 | return; /* parser will look for errors */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 2996 | |
| 2997 | if (TreeBuilder_CheckExact(self->target)) |
| 2998 | /* shortcut */ |
| 2999 | res = treebuilder_handle_data((TreeBuilderObject*) self->target, data); |
| 3000 | else if (self->handle_data) |
| 3001 | res = PyObject_CallFunction(self->handle_data, "O", data); |
| 3002 | else |
| 3003 | res = NULL; |
| 3004 | |
| 3005 | Py_DECREF(data); |
| 3006 | |
| 3007 | Py_XDECREF(res); |
| 3008 | } |
| 3009 | |
| 3010 | static void |
| 3011 | expat_end_handler(XMLParserObject* self, const XML_Char* tag_in) |
| 3012 | { |
| 3013 | PyObject* tag; |
| 3014 | PyObject* res = NULL; |
| 3015 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3016 | if (PyErr_Occurred()) |
| 3017 | return; |
| 3018 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3019 | if (TreeBuilder_CheckExact(self->target)) |
| 3020 | /* shortcut */ |
| 3021 | /* the standard tree builder doesn't look at the end tag */ |
| 3022 | res = treebuilder_handle_end( |
| 3023 | (TreeBuilderObject*) self->target, Py_None |
| 3024 | ); |
| 3025 | else if (self->handle_end) { |
| 3026 | tag = makeuniversal(self, tag_in); |
| 3027 | if (tag) { |
| 3028 | res = PyObject_CallFunction(self->handle_end, "O", tag); |
| 3029 | Py_DECREF(tag); |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | Py_XDECREF(res); |
| 3034 | } |
| 3035 | |
| 3036 | static void |
| 3037 | expat_start_ns_handler(XMLParserObject* self, const XML_Char* prefix, |
| 3038 | const XML_Char *uri) |
| 3039 | { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3040 | PyObject* sprefix = NULL; |
| 3041 | PyObject* suri = NULL; |
| 3042 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3043 | if (PyErr_Occurred()) |
| 3044 | return; |
| 3045 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3046 | suri = PyUnicode_DecodeUTF8(uri, strlen(uri), "strict"); |
| 3047 | if (!suri) |
| 3048 | return; |
| 3049 | |
| 3050 | if (prefix) |
| 3051 | sprefix = PyUnicode_DecodeUTF8(prefix, strlen(prefix), "strict"); |
| 3052 | else |
| 3053 | sprefix = PyUnicode_FromString(""); |
| 3054 | if (!sprefix) { |
| 3055 | Py_DECREF(suri); |
| 3056 | return; |
| 3057 | } |
| 3058 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3059 | treebuilder_handle_namespace( |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3060 | (TreeBuilderObject*) self->target, 1, sprefix, suri |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3061 | ); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3062 | |
| 3063 | Py_DECREF(sprefix); |
| 3064 | Py_DECREF(suri); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | static void |
| 3068 | expat_end_ns_handler(XMLParserObject* self, const XML_Char* prefix_in) |
| 3069 | { |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3070 | if (PyErr_Occurred()) |
| 3071 | return; |
| 3072 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3073 | treebuilder_handle_namespace( |
| 3074 | (TreeBuilderObject*) self->target, 0, NULL, NULL |
| 3075 | ); |
| 3076 | } |
| 3077 | |
| 3078 | static void |
| 3079 | expat_comment_handler(XMLParserObject* self, const XML_Char* comment_in) |
| 3080 | { |
| 3081 | PyObject* comment; |
| 3082 | PyObject* res; |
| 3083 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3084 | if (PyErr_Occurred()) |
| 3085 | return; |
| 3086 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3087 | if (self->handle_comment) { |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 3088 | comment = PyUnicode_DecodeUTF8(comment_in, strlen(comment_in), "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3089 | if (comment) { |
| 3090 | res = PyObject_CallFunction(self->handle_comment, "O", comment); |
| 3091 | Py_XDECREF(res); |
| 3092 | Py_DECREF(comment); |
| 3093 | } |
| 3094 | } |
| 3095 | } |
| 3096 | |
Eli Bendersky | 4583990 | 2013-01-13 05:14:47 -0800 | [diff] [blame] | 3097 | static void |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3098 | expat_start_doctype_handler(XMLParserObject *self, |
| 3099 | const XML_Char *doctype_name, |
| 3100 | const XML_Char *sysid, |
| 3101 | const XML_Char *pubid, |
| 3102 | int has_internal_subset) |
| 3103 | { |
| 3104 | PyObject *self_pyobj = (PyObject *)self; |
| 3105 | PyObject *doctype_name_obj, *sysid_obj, *pubid_obj; |
| 3106 | PyObject *parser_doctype = NULL; |
| 3107 | PyObject *res = NULL; |
| 3108 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3109 | if (PyErr_Occurred()) |
| 3110 | return; |
| 3111 | |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3112 | doctype_name_obj = makeuniversal(self, doctype_name); |
| 3113 | if (!doctype_name_obj) |
| 3114 | return; |
| 3115 | |
| 3116 | if (sysid) { |
| 3117 | sysid_obj = makeuniversal(self, sysid); |
| 3118 | if (!sysid_obj) { |
| 3119 | Py_DECREF(doctype_name_obj); |
| 3120 | return; |
| 3121 | } |
| 3122 | } else { |
| 3123 | Py_INCREF(Py_None); |
| 3124 | sysid_obj = Py_None; |
| 3125 | } |
| 3126 | |
| 3127 | if (pubid) { |
| 3128 | pubid_obj = makeuniversal(self, pubid); |
| 3129 | if (!pubid_obj) { |
| 3130 | Py_DECREF(doctype_name_obj); |
| 3131 | Py_DECREF(sysid_obj); |
| 3132 | return; |
| 3133 | } |
| 3134 | } else { |
| 3135 | Py_INCREF(Py_None); |
| 3136 | pubid_obj = Py_None; |
| 3137 | } |
| 3138 | |
| 3139 | /* If the target has a handler for doctype, call it. */ |
| 3140 | if (self->handle_doctype) { |
| 3141 | res = PyObject_CallFunction(self->handle_doctype, "OOO", |
| 3142 | doctype_name_obj, pubid_obj, sysid_obj); |
| 3143 | Py_CLEAR(res); |
| 3144 | } |
| 3145 | |
| 3146 | /* Now see if the parser itself has a doctype method. If yes and it's |
| 3147 | * a subclass, call it but warn about deprecation. If it's not a subclass |
| 3148 | * (i.e. vanilla XMLParser), do nothing. |
| 3149 | */ |
| 3150 | parser_doctype = PyObject_GetAttrString(self_pyobj, "doctype"); |
| 3151 | if (parser_doctype) { |
| 3152 | if (!XMLParser_CheckExact(self_pyobj)) { |
| 3153 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 3154 | "This method of XMLParser is deprecated. Define" |
| 3155 | " doctype() method on the TreeBuilder target.", |
| 3156 | 1) < 0) { |
| 3157 | goto clear; |
| 3158 | } |
| 3159 | res = PyObject_CallFunction(parser_doctype, "OOO", |
| 3160 | doctype_name_obj, pubid_obj, sysid_obj); |
| 3161 | Py_CLEAR(res); |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | clear: |
| 3166 | Py_XDECREF(parser_doctype); |
| 3167 | Py_DECREF(doctype_name_obj); |
| 3168 | Py_DECREF(pubid_obj); |
| 3169 | Py_DECREF(sysid_obj); |
| 3170 | } |
| 3171 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3172 | static void |
| 3173 | expat_pi_handler(XMLParserObject* self, const XML_Char* target_in, |
| 3174 | const XML_Char* data_in) |
| 3175 | { |
| 3176 | PyObject* target; |
| 3177 | PyObject* data; |
| 3178 | PyObject* res; |
| 3179 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3180 | if (PyErr_Occurred()) |
| 3181 | return; |
| 3182 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3183 | if (self->handle_pi) { |
Neal Norwitz | 0269b91 | 2007-08-08 06:56:02 +0000 | [diff] [blame] | 3184 | target = PyUnicode_DecodeUTF8(target_in, strlen(target_in), "strict"); |
| 3185 | data = PyUnicode_DecodeUTF8(data_in, strlen(data_in), "strict"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3186 | if (target && data) { |
| 3187 | res = PyObject_CallFunction(self->handle_pi, "OO", target, data); |
| 3188 | Py_XDECREF(res); |
| 3189 | Py_DECREF(data); |
| 3190 | Py_DECREF(target); |
| 3191 | } else { |
| 3192 | Py_XDECREF(data); |
| 3193 | Py_XDECREF(target); |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3198 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3199 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3200 | static PyObject * |
| 3201 | xmlparser_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3202 | { |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3203 | XMLParserObject *self = (XMLParserObject *)type->tp_alloc(type, 0); |
| 3204 | if (self) { |
| 3205 | self->parser = NULL; |
| 3206 | self->target = self->entity = self->names = NULL; |
| 3207 | self->handle_start = self->handle_data = self->handle_end = NULL; |
| 3208 | self->handle_comment = self->handle_pi = self->handle_close = NULL; |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3209 | self->handle_doctype = NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3210 | } |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3211 | return (PyObject *)self; |
| 3212 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3213 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3214 | static int |
| 3215 | xmlparser_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3216 | { |
| 3217 | XMLParserObject *self_xp = (XMLParserObject *)self; |
| 3218 | PyObject *target = NULL, *html = NULL; |
| 3219 | char *encoding = NULL; |
Eli Bendersky | c68e136 | 2012-06-03 06:09:42 +0300 | [diff] [blame] | 3220 | static char *kwlist[] = {"html", "target", "encoding", 0}; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3221 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3222 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOz:XMLParser", kwlist, |
| 3223 | &html, &target, &encoding)) { |
| 3224 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3225 | } |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 3226 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3227 | self_xp->entity = PyDict_New(); |
| 3228 | if (!self_xp->entity) |
| 3229 | return -1; |
| 3230 | |
| 3231 | self_xp->names = PyDict_New(); |
| 3232 | if (!self_xp->names) { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3233 | Py_CLEAR(self_xp->entity); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3234 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3237 | self_xp->parser = EXPAT(ParserCreate_MM)(encoding, &ExpatMemoryHandler, "}"); |
| 3238 | if (!self_xp->parser) { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3239 | Py_CLEAR(self_xp->entity); |
| 3240 | Py_CLEAR(self_xp->names); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3241 | PyErr_NoMemory(); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3242 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3245 | if (target) { |
| 3246 | Py_INCREF(target); |
| 3247 | } else { |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 3248 | target = treebuilder_new(&TreeBuilder_Type, NULL, NULL); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3249 | if (!target) { |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3250 | Py_CLEAR(self_xp->entity); |
| 3251 | Py_CLEAR(self_xp->names); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3252 | EXPAT(ParserFree)(self_xp->parser); |
| 3253 | return -1; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3254 | } |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3255 | } |
| 3256 | self_xp->target = target; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3257 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3258 | self_xp->handle_start = PyObject_GetAttrString(target, "start"); |
| 3259 | self_xp->handle_data = PyObject_GetAttrString(target, "data"); |
| 3260 | self_xp->handle_end = PyObject_GetAttrString(target, "end"); |
| 3261 | self_xp->handle_comment = PyObject_GetAttrString(target, "comment"); |
| 3262 | self_xp->handle_pi = PyObject_GetAttrString(target, "pi"); |
| 3263 | self_xp->handle_close = PyObject_GetAttrString(target, "close"); |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3264 | self_xp->handle_doctype = PyObject_GetAttrString(target, "doctype"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3265 | |
| 3266 | PyErr_Clear(); |
Eli Bendersky | 4583990 | 2013-01-13 05:14:47 -0800 | [diff] [blame] | 3267 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3268 | /* configure parser */ |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3269 | EXPAT(SetUserData)(self_xp->parser, self_xp); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3270 | EXPAT(SetElementHandler)( |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3271 | self_xp->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3272 | (XML_StartElementHandler) expat_start_handler, |
| 3273 | (XML_EndElementHandler) expat_end_handler |
| 3274 | ); |
| 3275 | EXPAT(SetDefaultHandlerExpand)( |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3276 | self_xp->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3277 | (XML_DefaultHandler) expat_default_handler |
| 3278 | ); |
| 3279 | EXPAT(SetCharacterDataHandler)( |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3280 | self_xp->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3281 | (XML_CharacterDataHandler) expat_data_handler |
| 3282 | ); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3283 | if (self_xp->handle_comment) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3284 | EXPAT(SetCommentHandler)( |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3285 | self_xp->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3286 | (XML_CommentHandler) expat_comment_handler |
| 3287 | ); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3288 | if (self_xp->handle_pi) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3289 | EXPAT(SetProcessingInstructionHandler)( |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3290 | self_xp->parser, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3291 | (XML_ProcessingInstructionHandler) expat_pi_handler |
| 3292 | ); |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3293 | EXPAT(SetStartDoctypeDeclHandler)( |
| 3294 | self_xp->parser, |
| 3295 | (XML_StartDoctypeDeclHandler) expat_start_doctype_handler |
| 3296 | ); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3297 | EXPAT(SetUnknownEncodingHandler)( |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3298 | self_xp->parser, |
Eli Bendersky | 6dc32b3 | 2013-05-25 05:25:48 -0700 | [diff] [blame] | 3299 | EXPAT(DefaultUnknownEncodingHandler), NULL |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3300 | ); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3301 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3302 | return 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3305 | static int |
| 3306 | xmlparser_gc_traverse(XMLParserObject *self, visitproc visit, void *arg) |
| 3307 | { |
| 3308 | Py_VISIT(self->handle_close); |
| 3309 | Py_VISIT(self->handle_pi); |
| 3310 | Py_VISIT(self->handle_comment); |
| 3311 | Py_VISIT(self->handle_end); |
| 3312 | Py_VISIT(self->handle_data); |
| 3313 | Py_VISIT(self->handle_start); |
| 3314 | |
| 3315 | Py_VISIT(self->target); |
| 3316 | Py_VISIT(self->entity); |
| 3317 | Py_VISIT(self->names); |
| 3318 | |
| 3319 | return 0; |
| 3320 | } |
| 3321 | |
| 3322 | static int |
| 3323 | xmlparser_gc_clear(XMLParserObject *self) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3324 | { |
| 3325 | EXPAT(ParserFree)(self->parser); |
| 3326 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3327 | Py_CLEAR(self->handle_close); |
| 3328 | Py_CLEAR(self->handle_pi); |
| 3329 | Py_CLEAR(self->handle_comment); |
| 3330 | Py_CLEAR(self->handle_end); |
| 3331 | Py_CLEAR(self->handle_data); |
| 3332 | Py_CLEAR(self->handle_start); |
| 3333 | Py_CLEAR(self->handle_doctype); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3334 | |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3335 | Py_CLEAR(self->target); |
| 3336 | Py_CLEAR(self->entity); |
| 3337 | Py_CLEAR(self->names); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3338 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3339 | return 0; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3342 | static void |
| 3343 | xmlparser_dealloc(XMLParserObject* self) |
| 3344 | { |
| 3345 | PyObject_GC_UnTrack(self); |
| 3346 | xmlparser_gc_clear(self); |
| 3347 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 3348 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3349 | |
| 3350 | LOCAL(PyObject*) |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3351 | expat_parse(XMLParserObject* self, const char* data, int data_len, int final) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3352 | { |
| 3353 | int ok; |
| 3354 | |
Victor Stinner | 3fd8cbd | 2013-07-18 22:46:14 +0200 | [diff] [blame] | 3355 | assert(!PyErr_Occurred()); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3356 | ok = EXPAT(Parse)(self->parser, data, data_len, final); |
| 3357 | |
| 3358 | if (PyErr_Occurred()) |
| 3359 | return NULL; |
| 3360 | |
| 3361 | if (!ok) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3362 | expat_set_error( |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3363 | EXPAT(GetErrorCode)(self->parser), |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3364 | EXPAT(GetErrorLineNumber)(self->parser), |
Eli Bendersky | 5b77d81 | 2012-03-16 08:20:05 +0200 | [diff] [blame] | 3365 | EXPAT(GetErrorColumnNumber)(self->parser), |
| 3366 | NULL |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3367 | ); |
| 3368 | return NULL; |
| 3369 | } |
| 3370 | |
| 3371 | Py_RETURN_NONE; |
| 3372 | } |
| 3373 | |
| 3374 | static PyObject* |
| 3375 | xmlparser_close(XMLParserObject* self, PyObject* args) |
| 3376 | { |
| 3377 | /* end feeding data to parser */ |
| 3378 | |
| 3379 | PyObject* res; |
| 3380 | if (!PyArg_ParseTuple(args, ":close")) |
| 3381 | return NULL; |
| 3382 | |
| 3383 | res = expat_parse(self, "", 0, 1); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3384 | if (!res) |
| 3385 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3386 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3387 | if (TreeBuilder_CheckExact(self->target)) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3388 | Py_DECREF(res); |
| 3389 | return treebuilder_done((TreeBuilderObject*) self->target); |
Eli Bendersky | 6eb50b1 | 2013-08-24 15:17:08 -0700 | [diff] [blame] | 3390 | } |
| 3391 | else if (self->handle_close) { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3392 | Py_DECREF(res); |
| 3393 | return PyObject_CallFunction(self->handle_close, ""); |
Eli Bendersky | 6eb50b1 | 2013-08-24 15:17:08 -0700 | [diff] [blame] | 3394 | } |
| 3395 | else { |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3396 | return res; |
Eli Bendersky | 6eb50b1 | 2013-08-24 15:17:08 -0700 | [diff] [blame] | 3397 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | static PyObject* |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3401 | xmlparser_feed(XMLParserObject* self, PyObject* arg) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3402 | { |
| 3403 | /* feed data to parser */ |
| 3404 | |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3405 | if (PyUnicode_Check(arg)) { |
| 3406 | Py_ssize_t data_len; |
| 3407 | const char *data = PyUnicode_AsUTF8AndSize(arg, &data_len); |
| 3408 | if (data == NULL) |
| 3409 | return NULL; |
| 3410 | if (data_len > INT_MAX) { |
| 3411 | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
| 3412 | return NULL; |
| 3413 | } |
| 3414 | /* Explicitly set UTF-8 encoding. Return code ignored. */ |
| 3415 | (void)EXPAT(SetEncoding)(self->parser, "utf-8"); |
| 3416 | return expat_parse(self, data, (int)data_len, 0); |
| 3417 | } |
| 3418 | else { |
| 3419 | Py_buffer view; |
| 3420 | PyObject *res; |
| 3421 | if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) < 0) |
| 3422 | return NULL; |
| 3423 | if (view.len > INT_MAX) { |
| 3424 | PyBuffer_Release(&view); |
| 3425 | PyErr_SetString(PyExc_OverflowError, "size does not fit in an int"); |
| 3426 | return NULL; |
| 3427 | } |
| 3428 | res = expat_parse(self, view.buf, (int)view.len, 0); |
| 3429 | PyBuffer_Release(&view); |
| 3430 | return res; |
| 3431 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | static PyObject* |
Eli Bendersky | a369923 | 2013-05-19 18:47:23 -0700 | [diff] [blame] | 3435 | xmlparser_parse_whole(XMLParserObject* self, PyObject* args) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3436 | { |
Eli Bendersky | a369923 | 2013-05-19 18:47:23 -0700 | [diff] [blame] | 3437 | /* (internal) parse the whole input, until end of stream */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3438 | PyObject* reader; |
| 3439 | PyObject* buffer; |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3440 | PyObject* temp; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3441 | PyObject* res; |
| 3442 | |
| 3443 | PyObject* fileobj; |
| 3444 | if (!PyArg_ParseTuple(args, "O:_parse", &fileobj)) |
| 3445 | return NULL; |
| 3446 | |
| 3447 | reader = PyObject_GetAttrString(fileobj, "read"); |
| 3448 | if (!reader) |
| 3449 | return NULL; |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 3450 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3451 | /* read from open file object */ |
| 3452 | for (;;) { |
| 3453 | |
| 3454 | buffer = PyObject_CallFunction(reader, "i", 64*1024); |
| 3455 | |
| 3456 | if (!buffer) { |
| 3457 | /* read failed (e.g. due to KeyboardInterrupt) */ |
| 3458 | Py_DECREF(reader); |
| 3459 | return NULL; |
| 3460 | } |
| 3461 | |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3462 | if (PyUnicode_CheckExact(buffer)) { |
| 3463 | /* A unicode object is encoded into bytes using UTF-8 */ |
| 3464 | if (PyUnicode_GET_SIZE(buffer) == 0) { |
| 3465 | Py_DECREF(buffer); |
| 3466 | break; |
| 3467 | } |
| 3468 | temp = PyUnicode_AsEncodedString(buffer, "utf-8", "surrogatepass"); |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3469 | Py_DECREF(buffer); |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3470 | if (!temp) { |
| 3471 | /* Propagate exception from PyUnicode_AsEncodedString */ |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3472 | Py_DECREF(reader); |
| 3473 | return NULL; |
| 3474 | } |
Eli Bendersky | f996e77 | 2012-03-16 05:53:30 +0200 | [diff] [blame] | 3475 | buffer = temp; |
| 3476 | } |
| 3477 | else if (!PyBytes_CheckExact(buffer) || PyBytes_GET_SIZE(buffer) == 0) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3478 | Py_DECREF(buffer); |
| 3479 | break; |
| 3480 | } |
| 3481 | |
| 3482 | res = expat_parse( |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 3483 | self, PyBytes_AS_STRING(buffer), PyBytes_GET_SIZE(buffer), 0 |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3484 | ); |
| 3485 | |
| 3486 | Py_DECREF(buffer); |
| 3487 | |
| 3488 | if (!res) { |
| 3489 | Py_DECREF(reader); |
| 3490 | return NULL; |
| 3491 | } |
| 3492 | Py_DECREF(res); |
| 3493 | |
| 3494 | } |
| 3495 | |
| 3496 | Py_DECREF(reader); |
| 3497 | |
| 3498 | res = expat_parse(self, "", 0, 1); |
| 3499 | |
| 3500 | if (res && TreeBuilder_CheckExact(self->target)) { |
| 3501 | Py_DECREF(res); |
| 3502 | return treebuilder_done((TreeBuilderObject*) self->target); |
| 3503 | } |
| 3504 | |
| 3505 | return res; |
| 3506 | } |
| 3507 | |
| 3508 | static PyObject* |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3509 | xmlparser_doctype(XMLParserObject *self, PyObject *args) |
| 3510 | { |
| 3511 | Py_RETURN_NONE; |
| 3512 | } |
| 3513 | |
| 3514 | static PyObject* |
| 3515 | xmlparser_setevents(XMLParserObject *self, PyObject* args) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3516 | { |
| 3517 | /* activate element event reporting */ |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3518 | Py_ssize_t i, seqlen; |
| 3519 | TreeBuilderObject *target; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3520 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3521 | PyObject *events_queue; |
| 3522 | PyObject *events_to_report = Py_None; |
| 3523 | PyObject *events_seq; |
| 3524 | if (!PyArg_ParseTuple(args, "O!|O:_setevents", &PyList_Type, &events_queue, |
| 3525 | &events_to_report)) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3526 | return NULL; |
| 3527 | |
| 3528 | if (!TreeBuilder_CheckExact(self->target)) { |
| 3529 | PyErr_SetString( |
| 3530 | PyExc_TypeError, |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 3531 | "event handling only supported for ElementTree.TreeBuilder " |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3532 | "targets" |
| 3533 | ); |
| 3534 | return NULL; |
| 3535 | } |
| 3536 | |
| 3537 | target = (TreeBuilderObject*) self->target; |
| 3538 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3539 | Py_INCREF(events_queue); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3540 | Py_XDECREF(target->events); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3541 | target->events = events_queue; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3542 | |
| 3543 | /* clear out existing events */ |
Antoine Pitrou | c194884 | 2012-10-01 23:40:37 +0200 | [diff] [blame] | 3544 | Py_CLEAR(target->start_event_obj); |
| 3545 | Py_CLEAR(target->end_event_obj); |
| 3546 | Py_CLEAR(target->start_ns_event_obj); |
| 3547 | Py_CLEAR(target->end_ns_event_obj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3548 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3549 | if (events_to_report == Py_None) { |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3550 | /* default is "end" only */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3551 | target->end_event_obj = PyUnicode_FromString("end"); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3552 | Py_RETURN_NONE; |
| 3553 | } |
| 3554 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3555 | if (!(events_seq = PySequence_Fast(events_to_report, |
| 3556 | "events must be a sequence"))) { |
| 3557 | return NULL; |
| 3558 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3559 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3560 | seqlen = PySequence_Size(events_seq); |
| 3561 | for (i = 0; i < seqlen; ++i) { |
| 3562 | PyObject *event_name_obj = PySequence_Fast_GET_ITEM(events_seq, i); |
| 3563 | char *event_name = NULL; |
| 3564 | if (PyUnicode_Check(event_name_obj)) { |
| 3565 | event_name = _PyUnicode_AsString(event_name_obj); |
| 3566 | } else if (PyBytes_Check(event_name_obj)) { |
| 3567 | event_name = PyBytes_AS_STRING(event_name_obj); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3568 | } |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3569 | |
| 3570 | if (event_name == NULL) { |
| 3571 | Py_DECREF(events_seq); |
| 3572 | PyErr_Format(PyExc_ValueError, "invalid events sequence"); |
| 3573 | return NULL; |
| 3574 | } else if (strcmp(event_name, "start") == 0) { |
| 3575 | Py_INCREF(event_name_obj); |
| 3576 | target->start_event_obj = event_name_obj; |
| 3577 | } else if (strcmp(event_name, "end") == 0) { |
| 3578 | Py_INCREF(event_name_obj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3579 | Py_XDECREF(target->end_event_obj); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3580 | target->end_event_obj = event_name_obj; |
| 3581 | } else if (strcmp(event_name, "start-ns") == 0) { |
| 3582 | Py_INCREF(event_name_obj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3583 | Py_XDECREF(target->start_ns_event_obj); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3584 | target->start_ns_event_obj = event_name_obj; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3585 | EXPAT(SetNamespaceDeclHandler)( |
| 3586 | self->parser, |
| 3587 | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 3588 | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 3589 | ); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3590 | } else if (strcmp(event_name, "end-ns") == 0) { |
| 3591 | Py_INCREF(event_name_obj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3592 | Py_XDECREF(target->end_ns_event_obj); |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3593 | target->end_ns_event_obj = event_name_obj; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3594 | EXPAT(SetNamespaceDeclHandler)( |
| 3595 | self->parser, |
| 3596 | (XML_StartNamespaceDeclHandler) expat_start_ns_handler, |
| 3597 | (XML_EndNamespaceDeclHandler) expat_end_ns_handler |
| 3598 | ); |
| 3599 | } else { |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3600 | Py_DECREF(events_seq); |
| 3601 | PyErr_Format(PyExc_ValueError, "unknown event '%s'", event_name); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3602 | return NULL; |
| 3603 | } |
| 3604 | } |
| 3605 | |
Eli Bendersky | 3a4fbd8 | 2013-05-19 09:01:49 -0700 | [diff] [blame] | 3606 | Py_DECREF(events_seq); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3607 | Py_RETURN_NONE; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | static PyMethodDef xmlparser_methods[] = { |
Serhiy Storchaka | 66d53fa | 2013-05-22 17:07:51 +0300 | [diff] [blame] | 3611 | {"feed", (PyCFunction) xmlparser_feed, METH_O}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3612 | {"close", (PyCFunction) xmlparser_close, METH_VARARGS}, |
Eli Bendersky | a369923 | 2013-05-19 18:47:23 -0700 | [diff] [blame] | 3613 | {"_parse_whole", (PyCFunction) xmlparser_parse_whole, METH_VARARGS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3614 | {"_setevents", (PyCFunction) xmlparser_setevents, METH_VARARGS}, |
Eli Bendersky | 2b6b73e | 2012-06-01 11:32:34 +0300 | [diff] [blame] | 3615 | {"doctype", (PyCFunction) xmlparser_doctype, METH_VARARGS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3616 | {NULL, NULL} |
| 3617 | }; |
| 3618 | |
Victor Stinner | bfc7bf0 | 2011-03-21 13:23:42 +0100 | [diff] [blame] | 3619 | static PyObject* |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 3620 | xmlparser_getattro(XMLParserObject* self, PyObject* nameobj) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3621 | { |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 3622 | if (PyUnicode_Check(nameobj)) { |
| 3623 | PyObject* res; |
| 3624 | if (PyUnicode_CompareWithASCIIString(nameobj, "entity") == 0) |
| 3625 | res = self->entity; |
| 3626 | else if (PyUnicode_CompareWithASCIIString(nameobj, "target") == 0) |
| 3627 | res = self->target; |
| 3628 | else if (PyUnicode_CompareWithASCIIString(nameobj, "version") == 0) { |
| 3629 | return PyUnicode_FromFormat( |
| 3630 | "Expat %d.%d.%d", XML_MAJOR_VERSION, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3631 | XML_MINOR_VERSION, XML_MICRO_VERSION); |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 3632 | } |
| 3633 | else |
| 3634 | goto generic; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3635 | |
Alexander Belopolsky | e239d23 | 2010-12-08 23:31:48 +0000 | [diff] [blame] | 3636 | Py_INCREF(res); |
| 3637 | return res; |
| 3638 | } |
| 3639 | generic: |
| 3640 | return PyObject_GenericGetAttr((PyObject*) self, nameobj); |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 3643 | static PyTypeObject XMLParser_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3644 | PyVarObject_HEAD_INIT(NULL, 0) |
Eli Bendersky | 698bdb2 | 2013-01-10 06:01:06 -0800 | [diff] [blame] | 3645 | "xml.etree.ElementTree.XMLParser", sizeof(XMLParserObject), 0, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3646 | /* methods */ |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3647 | (destructor)xmlparser_dealloc, /* tp_dealloc */ |
| 3648 | 0, /* tp_print */ |
| 3649 | 0, /* tp_getattr */ |
| 3650 | 0, /* tp_setattr */ |
| 3651 | 0, /* tp_reserved */ |
| 3652 | 0, /* tp_repr */ |
| 3653 | 0, /* tp_as_number */ |
| 3654 | 0, /* tp_as_sequence */ |
| 3655 | 0, /* tp_as_mapping */ |
| 3656 | 0, /* tp_hash */ |
| 3657 | 0, /* tp_call */ |
| 3658 | 0, /* tp_str */ |
| 3659 | (getattrofunc)xmlparser_getattro, /* tp_getattro */ |
| 3660 | 0, /* tp_setattro */ |
| 3661 | 0, /* tp_as_buffer */ |
| 3662 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 3663 | /* tp_flags */ |
| 3664 | 0, /* tp_doc */ |
| 3665 | (traverseproc)xmlparser_gc_traverse, /* tp_traverse */ |
| 3666 | (inquiry)xmlparser_gc_clear, /* tp_clear */ |
| 3667 | 0, /* tp_richcompare */ |
| 3668 | 0, /* tp_weaklistoffset */ |
| 3669 | 0, /* tp_iter */ |
| 3670 | 0, /* tp_iternext */ |
| 3671 | xmlparser_methods, /* tp_methods */ |
| 3672 | 0, /* tp_members */ |
| 3673 | 0, /* tp_getset */ |
| 3674 | 0, /* tp_base */ |
| 3675 | 0, /* tp_dict */ |
| 3676 | 0, /* tp_descr_get */ |
| 3677 | 0, /* tp_descr_set */ |
| 3678 | 0, /* tp_dictoffset */ |
| 3679 | (initproc)xmlparser_init, /* tp_init */ |
| 3680 | PyType_GenericAlloc, /* tp_alloc */ |
| 3681 | xmlparser_new, /* tp_new */ |
| 3682 | 0, /* tp_free */ |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3683 | }; |
| 3684 | |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3685 | /* ==================================================================== */ |
| 3686 | /* python module interface */ |
| 3687 | |
| 3688 | static PyMethodDef _functions[] = { |
Eli Bendersky | a873690 | 2013-01-05 06:26:39 -0800 | [diff] [blame] | 3689 | {"SubElement", (PyCFunction) subelement, METH_VARARGS | METH_KEYWORDS}, |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3690 | {NULL, NULL} |
| 3691 | }; |
| 3692 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3693 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3694 | static struct PyModuleDef elementtreemodule = { |
| 3695 | PyModuleDef_HEAD_INIT, |
| 3696 | "_elementtree", |
| 3697 | NULL, |
| 3698 | sizeof(elementtreestate), |
| 3699 | _functions, |
| 3700 | NULL, |
| 3701 | elementtree_traverse, |
| 3702 | elementtree_clear, |
| 3703 | elementtree_free |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3704 | }; |
| 3705 | |
Neal Norwitz | f6657e6 | 2006-12-28 04:47:50 +0000 | [diff] [blame] | 3706 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3707 | PyInit__elementtree(void) |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3708 | { |
Eli Bendersky | 64d11e6 | 2012-06-15 07:42:50 +0300 | [diff] [blame] | 3709 | PyObject *m, *temp; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3710 | elementtreestate *st; |
| 3711 | |
| 3712 | m = PyState_FindModule(&elementtreemodule); |
| 3713 | if (m) { |
| 3714 | Py_INCREF(m); |
| 3715 | return m; |
| 3716 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3717 | |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 3718 | /* Initialize object types */ |
Ronald Oussoren | 138d080 | 2013-07-19 11:11:25 +0200 | [diff] [blame] | 3719 | if (PyType_Ready(&ElementIter_Type) < 0) |
| 3720 | return NULL; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 3721 | if (PyType_Ready(&TreeBuilder_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 3722 | return NULL; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 3723 | if (PyType_Ready(&Element_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 3724 | return NULL; |
Amaury Forgeot d'Arc | ba4105c | 2008-07-02 21:41:01 +0000 | [diff] [blame] | 3725 | if (PyType_Ready(&XMLParser_Type) < 0) |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 3726 | return NULL; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3727 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3728 | m = PyModule_Create(&elementtreemodule); |
Fredrik Lundh | 44ed4db | 2006-03-12 21:06:35 +0000 | [diff] [blame] | 3729 | if (!m) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3730 | return NULL; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3731 | st = ET_STATE(m); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 3732 | |
Eli Bendersky | 828efde | 2012-04-05 05:40:58 +0300 | [diff] [blame] | 3733 | if (!(temp = PyImport_ImportModule("copy"))) |
| 3734 | return NULL; |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3735 | st->deepcopy_obj = PyObject_GetAttrString(temp, "deepcopy"); |
Eli Bendersky | 828efde | 2012-04-05 05:40:58 +0300 | [diff] [blame] | 3736 | Py_XDECREF(temp); |
| 3737 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3738 | if (!(st->elementpath_obj = PyImport_ImportModule("xml.etree.ElementPath"))) |
Eli Bendersky | 828efde | 2012-04-05 05:40:58 +0300 | [diff] [blame] | 3739 | return NULL; |
| 3740 | |
Eli Bendersky | 20d4174 | 2012-06-01 09:48:37 +0300 | [diff] [blame] | 3741 | /* link against pyexpat */ |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3742 | expat_capi = PyCapsule_Import(PyExpat_CAPSULE_NAME, 0); |
| 3743 | if (expat_capi) { |
| 3744 | /* check that it's usable */ |
| 3745 | if (strcmp(expat_capi->magic, PyExpat_CAPI_MAGIC) != 0 || |
| 3746 | expat_capi->size < sizeof(struct PyExpat_CAPI) || |
| 3747 | expat_capi->MAJOR_VERSION != XML_MAJOR_VERSION || |
| 3748 | expat_capi->MINOR_VERSION != XML_MINOR_VERSION || |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3749 | expat_capi->MICRO_VERSION != XML_MICRO_VERSION) { |
Eli Bendersky | ef391ac | 2012-07-21 20:28:46 +0300 | [diff] [blame] | 3750 | PyErr_SetString(PyExc_ImportError, |
| 3751 | "pyexpat version is incompatible"); |
| 3752 | return NULL; |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3753 | } |
Eli Bendersky | ef391ac | 2012-07-21 20:28:46 +0300 | [diff] [blame] | 3754 | } else { |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3755 | return NULL; |
Eli Bendersky | ef391ac | 2012-07-21 20:28:46 +0300 | [diff] [blame] | 3756 | } |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3757 | |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3758 | st->parseerror_obj = PyErr_NewException( |
Florent Xicluna | a72a98f | 2012-02-13 11:03:30 +0100 | [diff] [blame] | 3759 | "xml.etree.ElementTree.ParseError", PyExc_SyntaxError, NULL |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3760 | ); |
Eli Bendersky | 532d03e | 2013-08-10 08:00:39 -0700 | [diff] [blame] | 3761 | Py_INCREF(st->parseerror_obj); |
| 3762 | PyModule_AddObject(m, "ParseError", st->parseerror_obj); |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3763 | |
Eli Bendersky | 092af1f | 2012-03-04 07:14:03 +0200 | [diff] [blame] | 3764 | Py_INCREF((PyObject *)&Element_Type); |
| 3765 | PyModule_AddObject(m, "Element", (PyObject *)&Element_Type); |
| 3766 | |
Eli Bendersky | 58d548d | 2012-05-29 15:45:16 +0300 | [diff] [blame] | 3767 | Py_INCREF((PyObject *)&TreeBuilder_Type); |
| 3768 | PyModule_AddObject(m, "TreeBuilder", (PyObject *)&TreeBuilder_Type); |
| 3769 | |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3770 | Py_INCREF((PyObject *)&XMLParser_Type); |
| 3771 | PyModule_AddObject(m, "XMLParser", (PyObject *)&XMLParser_Type); |
Eli Bendersky | 52467b1 | 2012-06-01 07:13:08 +0300 | [diff] [blame] | 3772 | |
Florent Xicluna | f15351d | 2010-03-13 23:24:31 +0000 | [diff] [blame] | 3773 | return m; |
Fredrik Lundh | 8c8836b | 2005-12-16 22:06:06 +0000 | [diff] [blame] | 3774 | } |