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