| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <Python.h> |
| #include "structmember.h" |
|
|
| #include "expat.h" |
|
|
| |
| |
| |
| #ifdef _MSC_VER |
| #define inline |
| #endif |
|
|
| #undef CLAMP |
| #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) |
|
|
| static Py_ssize_t |
| next_power_of_2(Py_ssize_t n) |
| { |
| |
| n--; |
| n |= n >> 1; |
| n |= n >> 2; |
| n |= n >> 4; |
| n |= n >> 8; |
| n |= n >> 16; |
| n++; |
|
|
| return n; |
| } |
|
|
| |
| |
| |
|
|
| #if BYTEORDER == 1234 |
| # define TD_AS_INT 0x00004454 |
| # define TD_AS_INT_MASK 0x00ffffff |
| #else |
| # define TD_AS_INT 0x54440000 |
| # define TD_AS_INT_MASK 0xffffff00 |
| #endif |
|
|
| |
| #ifdef __clang__ |
| #undef PyTuple_SET_ITEM |
| #define PyTuple_SET_ITEM(a, b, c) PyTuple_SetItem((a), (b), (c)) |
| #endif |
|
|
| |
| |
| |
| typedef struct { |
| PyObject_HEAD |
| XML_Parser parser; |
| int done; |
|
|
| |
| PyObject* fd; |
| int file; |
| PyObject* read; |
| ssize_t buffersize; |
| XML_Char* buffer; |
|
|
| |
| Py_ssize_t text_alloc; |
| Py_ssize_t text_size; |
| XML_Char* text; |
| int keep_text; |
|
|
| |
| PyObject** queue; |
| Py_ssize_t queue_size; |
| Py_ssize_t queue_read_idx; |
| Py_ssize_t queue_write_idx; |
|
|
| |
| |
| PyObject* error_type; |
| PyObject* error_value; |
| PyObject* error_traceback; |
|
|
| |
| |
| unsigned long last_line; |
| unsigned long last_col; |
|
|
| |
| PyObject* dict_singleton; |
| PyObject* td_singleton; |
| PyObject* read_args; |
| } IterParser; |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| static int |
| queue_realloc(IterParser *self, Py_ssize_t req_size) |
| { |
| PyObject** new_queue; |
| Py_ssize_t n = req_size / 2; |
|
|
| if (n <= self->queue_size) |
| return 0; |
|
|
| new_queue = realloc(self->queue, sizeof(PyObject*) * (size_t)n); |
|
|
| if (new_queue == NULL) { |
| PyErr_SetString(PyExc_MemoryError, "Out of memory for XML parsing queue."); |
| |
| |
| |
| |
| |
| |
| |
| |
| goto fail; |
| } |
|
|
| self->queue = new_queue; |
| self->queue_size = n; |
| return 0; |
|
|
| fail: |
| free(self->queue); |
| self->queue = NULL; |
| self->queue_size = 0; |
| return -1; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| static int |
| text_realloc(IterParser *self, Py_ssize_t req_size) |
| { |
| Py_ssize_t n = req_size; |
| char *new_mem = NULL; |
|
|
| if (req_size < self->text_alloc) { |
| return 0; |
| } |
|
|
| |
| n = next_power_of_2(n); |
|
|
| if (n < req_size) { |
| PyErr_SetString(PyExc_MemoryError, "Out of memory for XML text."); |
| return -1; |
| } |
|
|
| new_mem = malloc(n * sizeof(XML_Char)); |
| if (new_mem == NULL) { |
| PyErr_SetString(PyExc_MemoryError, "Out of memory for XML text."); |
| return -1; |
| } |
|
|
| memcpy(new_mem, self->text, (size_t)(self->text_size + 1) * sizeof(XML_Char)); |
|
|
| free(self->text); |
| self->text = new_mem; |
| self->text_alloc = n; |
|
|
| return 0; |
| } |
|
|
| #define IS_WHITESPACE(c) ((c) == (XML_Char)0x20 || \ |
| (c) == (XML_Char)0x0d || \ |
| (c) == (XML_Char)0x0a || \ |
| (c) == (XML_Char)0x09) |
|
|
| |
| |
| |
| |
| |
| |
| |
| static int |
| text_append(IterParser *self, const XML_Char *data, Py_ssize_t len) |
| { |
| Py_ssize_t new_size; |
|
|
| if (len == 0) { |
| return 0; |
| } |
|
|
| |
| if (self->text_size == 0) { |
| while (len && IS_WHITESPACE(*data)) { |
| ++data; |
| --len; |
| } |
| } |
|
|
| |
| new_size = self->text_size + len; |
| if (text_realloc(self, new_size + 1)) { |
| return -1; |
| } |
|
|
| memcpy(self->text + self->text_size, |
| data, |
| (size_t)len * sizeof(XML_Char)); |
|
|
| self->text_size = new_size; |
| self->text[self->text_size] = (XML_Char)0x0; |
|
|
| return 0; |
| } |
|
|
| |
| |
| |
| static void |
| text_clear(IterParser *self) |
| { |
| self->text[0] = (XML_Char)0; |
| self->text_size = 0; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| static inline PyObject* |
| make_pos(const IterParser *self) |
| { |
| return Py_BuildValue( |
| "(nn)", |
| (size_t)self->last_line, |
| (size_t)self->last_col); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| static const XML_Char * |
| remove_namespace(const XML_Char *name) |
| { |
| const XML_Char* name_start = NULL; |
|
|
| |
| for (name_start = name; *name_start != '\0'; ++name_start) { |
| if (*name_start == ':') { |
| break; |
| } |
| } |
|
|
| if (*name_start == ':') { |
| ++name_start; |
| } else { |
| name_start = name; |
| } |
|
|
| return name_start; |
| } |
|
|
| |
| |
| |
| static void |
| startElement(IterParser *self, const XML_Char *name, const XML_Char **atts) |
| { |
| PyObject* pyname = NULL; |
| PyObject* pyatts = NULL; |
| const XML_Char** att_ptr = atts; |
| const XML_Char* name_start = NULL; |
| PyObject* tuple = NULL; |
| PyObject* key = NULL; |
| PyObject* val = NULL; |
| PyObject* pos = NULL; |
|
|
| |
| |
| if (PyErr_Occurred() != NULL) { |
| XML_StopParser(self->parser, 0); |
| return; |
| } |
|
|
| |
| if (self->queue_write_idx < self->queue_size) { |
| tuple = PyTuple_New(4); |
| if (tuple == NULL) { |
| goto fail; |
| } |
|
|
| Py_INCREF(Py_True); |
| PyTuple_SET_ITEM(tuple, 0, Py_True); |
|
|
| |
| |
| |
| |
| |
| |
| if ((*(int*)name & TD_AS_INT_MASK) == TD_AS_INT) { |
| Py_INCREF(self->td_singleton); |
| PyTuple_SetItem(tuple, 1, self->td_singleton); |
| } else { |
| name_start = remove_namespace(name); |
|
|
| pyname = PyUnicode_FromString(name_start); |
| if (pyname == NULL) { |
| goto fail; |
| } |
| PyTuple_SetItem(tuple, 1, pyname); |
| pyname = NULL; |
| } |
|
|
| if (*att_ptr) { |
| pyatts = PyDict_New(); |
| if (pyatts == NULL) { |
| goto fail; |
| } |
| do { |
| if (*(*(att_ptr + 1)) != 0) { |
| key = PyUnicode_FromString(*att_ptr); |
| if (key == NULL) { |
| goto fail; |
| } |
| val = PyUnicode_FromString(*(att_ptr + 1)); |
| if (val == NULL) { |
| Py_DECREF(key); |
| goto fail; |
| } |
| if (PyDict_SetItem(pyatts, key, val)) { |
| Py_DECREF(key); |
| Py_DECREF(val); |
| goto fail; |
| } |
| Py_DECREF(key); |
| Py_DECREF(val); |
| key = val = NULL; |
| } |
| att_ptr += 2; |
| } while (*att_ptr); |
| } else { |
| Py_INCREF(self->dict_singleton); |
| pyatts = self->dict_singleton; |
| } |
|
|
| PyTuple_SetItem(tuple, 2, pyatts); |
| pyatts = NULL; |
|
|
| self->last_line = (unsigned long)XML_GetCurrentLineNumber( |
| self->parser); |
| self->last_col = (unsigned long)XML_GetCurrentColumnNumber( |
| self->parser); |
|
|
| pos = make_pos(self); |
| if (pos == NULL) { |
| goto fail; |
| } |
| PyTuple_SetItem(tuple, 3, pos); |
| pos = NULL; |
|
|
| text_clear(self); |
|
|
| self->keep_text = 1; |
|
|
| self->queue[self->queue_write_idx++] = tuple; |
| } else { |
| PyErr_SetString( |
| PyExc_RuntimeError, |
| "XML queue overflow in startElement. This most likely indicates an internal bug."); |
| goto fail; |
| } |
|
|
| return; |
|
|
| fail: |
| Py_XDECREF(tuple); |
| Py_XDECREF(pyatts); |
| XML_StopParser(self->parser, 0); |
| } |
|
|
| |
| |
| |
| static void |
| endElement(IterParser *self, const XML_Char *name) |
| { |
| PyObject* pyname = NULL; |
| PyObject* tuple = NULL; |
| PyObject* pytext = NULL; |
| const XML_Char* name_start = NULL; |
| XML_Char* end; |
| PyObject* pos = NULL; |
|
|
| |
| |
| if (PyErr_Occurred() != NULL) { |
| XML_StopParser(self->parser, 0); |
| return; |
| } |
|
|
| |
| if (self->queue_write_idx < self->queue_size) { |
| tuple = PyTuple_New(4); |
| if (tuple == NULL) { |
| goto fail; |
| } |
|
|
| Py_INCREF(Py_False); |
| PyTuple_SET_ITEM(tuple, 0, Py_False); |
|
|
| |
| |
| |
| |
| |
| |
| if ((*(int*)name & TD_AS_INT_MASK) == TD_AS_INT) { |
| Py_INCREF(self->td_singleton); |
| PyTuple_SetItem(tuple, 1, self->td_singleton); |
| } else { |
| name_start = remove_namespace(name); |
|
|
| pyname = PyUnicode_FromString(name_start); |
| if (pyname == NULL) { |
| goto fail; |
| } |
| PyTuple_SetItem(tuple, 1, pyname); |
| pyname = NULL; |
| } |
|
|
| |
| end = self->text + self->text_size - 1; |
| while (end >= self->text && IS_WHITESPACE(*end)) { |
| --end; |
| --self->text_size; |
| } |
|
|
| pytext = PyUnicode_FromStringAndSize(self->text, self->text_size); |
| if (pytext == NULL) { |
| goto fail; |
| } |
| PyTuple_SetItem(tuple, 2, pytext); |
| pytext = NULL; |
|
|
| pos = make_pos(self); |
| if (pos == NULL) { |
| goto fail; |
| } |
| PyTuple_SetItem(tuple, 3, pos); |
| pos = NULL; |
|
|
| self->keep_text = 0; |
|
|
| self->queue[self->queue_write_idx++] = tuple; |
| } else { |
| PyErr_SetString( |
| PyExc_RuntimeError, |
| "XML queue overflow in endElement. This most likely indicates an internal bug."); |
| goto fail; |
| } |
|
|
| return; |
|
|
| fail: |
| Py_XDECREF(tuple); |
| XML_StopParser(self->parser, 0); |
| } |
|
|
| |
| |
| |
| static void |
| characterData(IterParser *self, const XML_Char *text, int len) |
| { |
| |
| |
| if (PyErr_Occurred() != NULL) { |
| XML_StopParser(self->parser, 0); |
| return; |
| } |
|
|
| if (self->text_size == 0) { |
| self->last_line = (unsigned long)XML_GetCurrentLineNumber( |
| self->parser); |
| self->last_col = (unsigned long)XML_GetCurrentColumnNumber( |
| self->parser); |
| } |
|
|
| if (self->keep_text) { |
| (void)text_append(self, text, (Py_ssize_t)len); |
| } |
| } |
|
|
| |
| |
| |
| static void |
| xmlDecl(IterParser *self, const XML_Char *version, |
| const XML_Char *encoding, int standalone) |
| { |
| PyObject* tuple = NULL; |
| PyObject* xml_str = NULL; |
| PyObject* attrs = NULL; |
| PyObject* encoding_str = NULL; |
| PyObject* version_str = NULL; |
| PyObject* pos = NULL; |
|
|
| if (self->queue_write_idx < self->queue_size) { |
| tuple = PyTuple_New(4); |
| if (tuple == NULL) { |
| goto fail; |
| } |
|
|
| Py_INCREF(Py_True); |
| PyTuple_SET_ITEM(tuple, 0, Py_True); |
|
|
| xml_str = PyUnicode_FromString("xml"); |
| if (xml_str == NULL) { |
| goto fail; |
| } |
| PyTuple_SET_ITEM(tuple, 1, xml_str); |
| xml_str = NULL; |
|
|
| attrs = PyDict_New(); |
| if (attrs == NULL) { |
| goto fail; |
| } |
|
|
| if (encoding) { |
| encoding_str = PyUnicode_FromString(encoding); |
| } else { |
| encoding_str = PyUnicode_FromString(""); |
| } |
| if (encoding_str == NULL) { |
| goto fail; |
| } |
| if (PyDict_SetItemString(attrs, "encoding", encoding_str)) { |
| Py_DECREF(encoding_str); |
| goto fail; |
| } |
| Py_DECREF(encoding_str); |
| encoding_str = NULL; |
|
|
| if (version) { |
| version_str = PyUnicode_FromString(version); |
| } else { |
| version_str = PyUnicode_FromString(""); |
| } |
| if (version_str == NULL) { |
| goto fail; |
| } |
| if (PyDict_SetItemString(attrs, "version", version_str)) { |
| Py_DECREF(version_str); |
| goto fail; |
| } |
| Py_DECREF(version_str); |
| version_str = NULL; |
|
|
| PyTuple_SET_ITEM(tuple, 2, attrs); |
| attrs = NULL; |
|
|
| self->last_line = (unsigned long)XML_GetCurrentLineNumber( |
| self->parser); |
| self->last_col = (unsigned long)XML_GetCurrentColumnNumber( |
| self->parser); |
|
|
| pos = make_pos(self); |
| if (pos == NULL) { |
| goto fail; |
| } |
| PyTuple_SetItem(tuple, 3, pos); |
| pos = NULL; |
|
|
| self->queue[self->queue_write_idx++] = tuple; |
| } else { |
| PyErr_SetString( |
| PyExc_RuntimeError, |
| "XML queue overflow in xmlDecl. This most likely indicates an internal bug."); |
| goto fail; |
| } |
|
|
| return; |
|
|
| fail: |
| Py_XDECREF(tuple); |
| Py_XDECREF(attrs); |
| XML_StopParser(self->parser, 0); |
| } |
|
|
| |
| |
| |
| |
| static PyObject * |
| IterParser_iter(IterParser* self) |
| { |
| Py_INCREF(self); |
| return (PyObject*) self; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static PyObject * |
| IterParser_next(IterParser* self) |
| { |
| PyObject* data = NULL; |
| XML_Char* buf; |
| Py_ssize_t buflen; |
|
|
| |
| if (self->queue_read_idx < self->queue_write_idx) { |
| return self->queue[self->queue_read_idx++]; |
| } |
|
|
| |
| if (self->error_type) { |
| PyErr_Restore(self->error_type, self->error_value, self->error_traceback); |
| self->error_type = NULL; |
| self->error_value = NULL; |
| self->error_traceback = NULL; |
| return NULL; |
| } |
|
|
| |
| |
| |
| if (self->done) { |
| return NULL; |
| } |
|
|
| self->queue_read_idx = 0; |
| self->queue_write_idx = 0; |
|
|
| do { |
| |
| if (self->read) { |
| data = PyObject_CallObject(self->read, self->read_args); |
| if (data == NULL) { |
| goto fail; |
| } |
|
|
| if (PyBytes_AsStringAndSize(data, &buf, &buflen) == -1) { |
| Py_DECREF(data); |
| goto fail; |
| } |
|
|
| if (buflen < self->buffersize) { |
| |
| self->done = 1; |
| } |
| |
| |
| } else { |
| buflen = (Py_ssize_t)read( |
| self->file, self->buffer, (size_t)self->buffersize); |
| if (buflen == -1) { |
| PyErr_SetFromErrno(PyExc_OSError); |
| goto fail; |
| } else if (buflen < self->buffersize) { |
| |
| self->done = 1; |
| } |
|
|
| buf = self->buffer; |
| } |
|
|
| if(queue_realloc(self, buflen)) { |
| Py_XDECREF(data); |
| goto fail; |
| } |
|
|
| |
| if (XML_Parse(self->parser, buf, (int)buflen, self->done) == XML_STATUS_ERROR) { |
| |
| |
| |
| if (PyErr_Occurred() != NULL) { |
| goto fail; |
| } |
|
|
| |
| |
| Py_XDECREF(data); |
| PyErr_Format( |
| PyExc_ValueError, "%lu:%lu: %s", |
| XML_GetCurrentLineNumber(self->parser), |
| XML_GetCurrentColumnNumber(self->parser), |
| XML_ErrorString(XML_GetErrorCode(self->parser))); |
| goto fail; |
| } |
| Py_XDECREF(data); |
|
|
| if (PyErr_Occurred() != NULL) { |
| goto fail; |
| } |
| } while (self->queue_write_idx == 0 && self->done == 0); |
|
|
| if (self->queue_write_idx == 0) { |
| return NULL; |
| } |
|
|
| if (self->queue_write_idx >= self->queue_size) { |
| PyErr_SetString( |
| PyExc_RuntimeError, |
| "XML queue overflow. This most likely indicates an internal bug."); |
| return NULL; |
| } |
|
|
| return self->queue[self->queue_read_idx++]; |
|
|
| fail: |
| |
| |
| |
| PyErr_Fetch(&self->error_type, &self->error_value, &self->error_traceback); |
| PyErr_Clear(); |
|
|
| if (self->queue_read_idx < self->queue_write_idx) { |
| return self->queue[self->queue_read_idx++]; |
| } |
|
|
| PyErr_Restore(self->error_type, self->error_value, self->error_traceback); |
| self->error_type = NULL; |
| self->error_value = NULL; |
| self->error_traceback = NULL; |
| return NULL; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| static int |
| IterParser_traverse(IterParser *self, visitproc visit, void *arg) |
| { |
| int vret; |
| Py_ssize_t read_index; |
|
|
| read_index = self->queue_read_idx; |
| while (read_index < self->queue_write_idx) { |
| vret = visit(self->queue[read_index++], arg); |
| if (vret != 0) return vret; |
| } |
|
|
| if (self->fd) { |
| vret = visit(self->fd, arg); |
| if (vret != 0) return vret; |
| } |
|
|
| if (self->read) { |
| vret = visit(self->read, arg); |
| if (vret != 0) return vret; |
| } |
|
|
| if (self->read_args) { |
| vret = visit(self->read_args, arg); |
| if (vret != 0) return vret; |
| } |
|
|
| if (self->dict_singleton) { |
| vret = visit(self->dict_singleton, arg); |
| if (vret != 0) return vret; |
| } |
|
|
| if (self->td_singleton) { |
| vret = visit(self->td_singleton, arg); |
| if (vret != 0) return vret; |
| } |
|
|
| if (self->error_type) { |
| vret = visit(self->error_type, arg); |
| if (vret != 0) return vret; |
| } |
|
|
| if (self->error_value) { |
| vret = visit(self->error_value, arg); |
| if (vret != 0) return vret; |
| } |
|
|
| if (self->error_traceback) { |
| vret = visit(self->error_traceback, arg); |
| if (vret != 0) return vret; |
| } |
|
|
| return 0; |
| } |
|
|
| |
| static int |
| IterParser_clear(IterParser *self) |
| { |
| PyObject *tmp; |
|
|
| while (self->queue_read_idx < self->queue_write_idx) { |
| tmp = self->queue[self->queue_read_idx]; |
| self->queue[self->queue_read_idx] = NULL; |
| Py_XDECREF(tmp); |
| self->queue_read_idx++; |
| } |
|
|
| tmp = self->fd; |
| self->fd = NULL; |
| Py_XDECREF(tmp); |
|
|
| tmp = self->read; |
| self->read = NULL; |
| Py_XDECREF(tmp); |
|
|
| tmp = self->read_args; |
| self->read_args = NULL; |
| Py_XDECREF(tmp); |
|
|
| tmp = self->dict_singleton; |
| self->dict_singleton = NULL; |
| Py_XDECREF(tmp); |
|
|
| tmp = self->td_singleton; |
| self->td_singleton = NULL; |
| Py_XDECREF(tmp); |
|
|
| tmp = self->error_type; |
| self->error_type = NULL; |
| Py_XDECREF(tmp); |
|
|
| tmp = self->error_value; |
| self->error_value = NULL; |
| Py_XDECREF(tmp); |
|
|
| tmp = self->error_traceback; |
| self->error_traceback = NULL; |
| Py_XDECREF(tmp); |
|
|
| return 0; |
| } |
|
|
| |
| |
| |
| |
| static void |
| IterParser_dealloc(IterParser* self) |
| { |
| IterParser_clear(self); |
|
|
| free(self->buffer); self->buffer = NULL; |
| free(self->queue); self->queue = NULL; |
| free(self->text); self->text = NULL; |
| if (self->parser != NULL) { |
| XML_ParserFree(self->parser); |
| self->parser = NULL; |
| } |
|
|
| Py_TYPE(self)->tp_free((PyObject*)self); |
| } |
|
|
| |
| |
| |
|
|
| static PyObject * |
| IterParser_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| { |
| IterParser *self = NULL; |
|
|
| self = (IterParser *)type->tp_alloc(type, 0); |
| if (self != NULL) { |
| self->parser = NULL; |
| self->fd = NULL; |
| self->file = -1; |
| self->read = NULL; |
| self->read_args = NULL; |
| self->dict_singleton = NULL; |
| self->td_singleton = NULL; |
| self->buffersize = 0; |
| self->buffer = NULL; |
| self->queue_read_idx = 0; |
| self->queue_write_idx = 0; |
| self->text_alloc = 0; |
| self->text_size = 0; |
| self->text = NULL; |
| self->keep_text = 0; |
| self->done = 0; |
| self->queue_size = 0; |
| self->queue = NULL; |
| self->error_type = NULL; |
| self->error_value = NULL; |
| self->error_traceback = NULL; |
| } |
|
|
| return (PyObject *)self; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| static int |
| IterParser_init(IterParser *self, PyObject *args, PyObject *kwds) |
| { |
| PyObject* fd = NULL; |
| PyObject* read = NULL; |
| ssize_t buffersize = 1 << 14; |
|
|
| static char *kwlist[] = {"fd", "buffersize", NULL}; |
| if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:IterParser.__init__", kwlist, |
| &fd, &buffersize)) { |
| return -1; |
| } |
|
|
| |
| self->buffersize = CLAMP(buffersize, (ssize_t)(1 << 10), (ssize_t)(1 << 24)); |
| #ifdef __clang__ |
| |
| |
| read = PyObject_GetAttrString(fd, "read"); |
| if (read != NULL) { |
| fd = read; |
| } |
| #else |
| self->file = PyObject_AsFileDescriptor(fd); |
| if (self->file != -1) { |
| |
| |
| |
| self->buffer = malloc((size_t)self->buffersize); |
| if (self->buffer == NULL) { |
| PyErr_SetString(PyExc_MemoryError, "Out of memory"); |
| goto fail; |
| } |
| self->fd = fd; Py_INCREF(self->fd); |
| lseek(self->file, 0, SEEK_SET); |
| } else |
| #endif |
| if (PyCallable_Check(fd)) { |
| |
| self->fd = fd; Py_INCREF(self->fd); |
| self->read = fd; Py_INCREF(self->read); |
| } else { |
| PyErr_SetString( |
| PyExc_TypeError, |
| "Arg 1 to iterparser must be a file object or callable object"); |
| goto fail; |
| } |
|
|
| PyErr_Clear(); |
|
|
| self->queue_read_idx = 0; |
| self->queue_write_idx = 0; |
| self->done = 0; |
|
|
| self->text = malloc((size_t)buffersize * sizeof(XML_Char)); |
| self->text_alloc = buffersize; |
| if (self->text == NULL) { |
| PyErr_SetString(PyExc_MemoryError, "Out of memory"); |
| goto fail; |
| } |
| text_clear(self); |
|
|
| self->read_args = Py_BuildValue("(n)", buffersize); |
| if (self->read_args == NULL) { |
| goto fail; |
| } |
|
|
| self->dict_singleton = PyDict_New(); |
| if (self->dict_singleton == NULL) { |
| goto fail; |
| } |
|
|
| self->td_singleton = PyUnicode_FromString("TD"); |
| if (self->td_singleton == NULL) { |
| goto fail; |
| } |
|
|
| if (queue_realloc(self, buffersize)) { |
| goto fail; |
| } |
|
|
| |
| self->parser = XML_ParserCreate(NULL); |
| if (self->parser == NULL) { |
| PyErr_SetString(PyExc_MemoryError, "Out of memory"); |
| goto fail; |
| } |
| XML_SetUserData(self->parser, self); |
| XML_SetElementHandler( |
| self->parser, |
| (XML_StartElementHandler)startElement, |
| (XML_EndElementHandler)endElement); |
| XML_SetCharacterDataHandler( |
| self->parser, |
| (XML_CharacterDataHandler)characterData); |
| XML_SetXmlDeclHandler( |
| self->parser, |
| (XML_XmlDeclHandler)xmlDecl); |
|
|
| Py_XDECREF(read); |
|
|
| return 0; |
|
|
| fail: |
| Py_XDECREF(read); |
| Py_XDECREF(self->fd); |
| Py_XDECREF(self->read); |
| free(self->text); |
| Py_XDECREF(self->dict_singleton); |
| Py_XDECREF(self->td_singleton); |
| Py_XDECREF(self->read_args); |
| free(self->queue); |
|
|
| return -1; |
| } |
|
|
| static PyMemberDef IterParser_members[] = |
| { |
| {NULL} |
| }; |
|
|
| static PyMethodDef IterParser_methods[] = |
| { |
| {NULL} |
| }; |
|
|
| static PyTypeObject IterParserType = |
| { |
| PyVarObject_HEAD_INIT(NULL, 0) |
| "astropy.utils.xml._iterparser.IterParser", |
| sizeof(IterParser), |
| 0, |
| (destructor)IterParser_dealloc, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| "IterParser objects", |
| (traverseproc)IterParser_traverse, |
| (inquiry)IterParser_clear, |
| 0, |
| 0, |
| (getiterfunc)IterParser_iter, |
| (iternextfunc)IterParser_next, |
| IterParser_methods, |
| IterParser_members, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| 0, |
| (initproc)IterParser_init, |
| 0, |
| IterParser_new, |
| }; |
|
|
| |
| |
| |
|
|
| |
| static const char* escapes_cdata[] = { |
| ">", ">", |
| "<", "<", |
| "&", "&", |
| "\0", "\0", |
| }; |
|
|
| |
| static const char* escapes[] = { |
| ">", ">", |
| "<", "<", |
| "'", "'", |
| "&", "&", |
| "\"", """, |
| "\0", "\0" |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| static PyObject* |
| _escape_xml(PyObject* self, PyObject *args, const char** escapes) |
| { |
| PyObject* input_obj; |
| PyObject* input_coerce = NULL; |
| PyObject* output_obj; |
| int count = 0; |
| Py_UNICODE* uinput = NULL; |
| char* input = NULL; |
| Py_ssize_t input_len; |
| Py_UNICODE* uoutput = NULL; |
| char* output = NULL; |
| Py_UNICODE* up = NULL; |
| char* p = NULL; |
| Py_ssize_t i; |
| const char** esc; |
| const char* ent; |
|
|
| if (!PyArg_ParseTuple(args, "O:escape_xml", &input_obj)) { |
| return NULL; |
| } |
|
|
| |
| if (!PyBytes_Check(input_obj)) { |
| input_coerce = PyObject_Str(input_obj); |
| } |
| if (input_coerce) { |
| uinput = PyUnicode_AsUnicode(input_coerce); |
| if (uinput == NULL) { |
| Py_DECREF(input_coerce); |
| return NULL; |
| } |
|
|
| input_len = PyUnicode_GetLength(input_coerce); |
|
|
| for (i = 0; i < input_len; ++i) { |
| for (esc = escapes; ; esc += 2) { |
| if (uinput[i] > (Py_UNICODE)**esc) { |
| break; |
| } else if (uinput[i] == (Py_UNICODE)**esc) { |
| ++count; |
| break; |
| } |
| } |
| } |
|
|
| if (count) { |
| uoutput = malloc((input_len + 1 + count * 5) * sizeof(Py_UNICODE)); |
| if (uoutput == NULL) { |
| Py_DECREF(input_coerce); |
| PyErr_SetString(PyExc_MemoryError, "Out of memory"); |
| return NULL; |
| } |
|
|
| up = uoutput; |
| for (i = 0; i < input_len; ++i) { |
| for (esc = escapes; ; esc += 2) { |
| if (uinput[i] > (Py_UNICODE)**esc) { |
| *(up++) = uinput[i]; |
| break; |
| } else if (uinput[i] == (Py_UNICODE)**esc) { |
| for (ent = *(esc + 1); *ent != '\0'; ++ent) { |
| *(up++) = (Py_UNICODE)*ent; |
| } |
| break; |
| } |
| } |
| } |
|
|
| *up = 0; |
|
|
| Py_DECREF(input_coerce); |
| output_obj = PyUnicode_FromUnicode(uoutput, up - uoutput); |
| free(uoutput); |
| return output_obj; |
| } else { |
| return input_coerce; |
| } |
| } |
|
|
| |
| input_coerce = PyObject_Bytes(input_obj); |
| if (input_coerce) { |
| if (PyBytes_AsStringAndSize(input_coerce, &input, &input_len) == -1) { |
| Py_DECREF(input_coerce); |
| return NULL; |
| } |
|
|
| for (i = 0; i < input_len; ++i) { |
| for (esc = escapes; ; esc += 2) { |
| if (input[i] > **esc) { |
| break; |
| } else if (input[i] == **esc) { |
| ++count; |
| break; |
| } |
| } |
| } |
|
|
| if (count) { |
| output = malloc((input_len + 1 + count * 5) * sizeof(char)); |
| if (output == NULL) { |
| Py_DECREF(input_coerce); |
| PyErr_SetString(PyExc_MemoryError, "Out of memory"); |
| return NULL; |
| } |
|
|
| p = output; |
| for (i = 0; i < input_len; ++i) { |
| for (esc = escapes; ; esc += 2) { |
| if (input[i] > **esc) { |
| *(p++) = input[i]; |
| break; |
| } else if (input[i] == **esc) { |
| for (ent = *(esc + 1); *ent != '\0'; ++ent) { |
| *(p++) = *ent; |
| } |
| break; |
| } |
| } |
| } |
|
|
| *p = 0; |
|
|
| Py_DECREF(input_coerce); |
| output_obj = PyBytes_FromStringAndSize(output, p - output); |
| free(output); |
| return output_obj; |
| } else { |
| return input_coerce; |
| } |
| } |
|
|
| PyErr_SetString(PyExc_TypeError, "must be convertible to str or bytes"); |
| return NULL; |
| } |
|
|
| static PyObject* |
| escape_xml(PyObject* self, PyObject *args) |
| { |
| return _escape_xml(self, args, escapes); |
| } |
|
|
| static PyObject* |
| escape_xml_cdata(PyObject* self, PyObject *args) |
| { |
| return _escape_xml(self, args, escapes_cdata); |
| } |
|
|
| |
| |
| |
|
|
| static PyMethodDef module_methods[] = |
| { |
| {"escape_xml", (PyCFunction)escape_xml, METH_VARARGS, |
| "Fast method to escape XML strings"}, |
| {"escape_xml_cdata", (PyCFunction)escape_xml_cdata, METH_VARARGS, |
| "Fast method to escape XML strings"}, |
| {NULL} |
| }; |
|
|
| struct module_state { |
| void* none; |
| }; |
|
|
| static int module_traverse(PyObject* m, visitproc visit, void* arg) |
| { |
| return 0; |
| } |
|
|
| static int module_clear(PyObject* m) |
| { |
| return 0; |
| } |
|
|
| static struct PyModuleDef moduledef = { |
| PyModuleDef_HEAD_INIT, |
| "_iterparser", |
| "Fast XML parser", |
| sizeof(struct module_state), |
| module_methods, |
| NULL, |
| module_traverse, |
| module_clear, |
| NULL |
| }; |
|
|
| PyMODINIT_FUNC |
| PyInit__iterparser(void) |
| { |
| PyObject* m; |
| m = PyModule_Create(&moduledef); |
|
|
| if (m == NULL) |
| return NULL; |
|
|
| if (PyType_Ready(&IterParserType) < 0) |
| return NULL; |
|
|
| Py_INCREF(&IterParserType); |
| PyModule_AddObject(m, "IterParser", (PyObject *)&IterParserType); |
|
|
| return m; |
| } |
|
|