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