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