| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "jsonslicer.hh" |
|
|
| #include "encoding.hh" |
|
|
| #include <Python.h> |
| #include <yajl/yajl_parse.h> |
|
|
| JsonSlicer* JsonSlicer_iter(JsonSlicer* self) { |
| Py_INCREF(self); |
| return self; |
| } |
|
|
| PyObject* JsonSlicer_iternext(JsonSlicer* self) { |
| |
| if (!self->complete.empty()) { |
| return self->complete.pop_front().release(); |
| } |
|
|
| bool eof = false; |
|
|
| do { |
| |
| PyObjPtr buffer = PyObjPtr::Take(PyObject_CallMethod(self->io.get(), "read", "n", self->read_size)); |
|
|
| |
| if (!buffer) { |
| return nullptr; |
| } |
| if (PyUnicode_Check(buffer.get())) { |
| PyObjPtr encoded = encode(buffer, self->input_encoding, self->input_errors); |
| if (!encoded) { |
| return nullptr; |
| } |
| buffer = encoded; |
| } |
| if (!PyBytes_Check(buffer.get())) { |
| PyErr_Format(PyExc_RuntimeError, "Unexpected read result type %s, expected bytes", buffer.get()->ob_type->tp_name); |
| return nullptr; |
| } |
|
|
| |
| yajl_status status; |
| if (PyBytes_GET_SIZE(buffer.get()) == 0) { |
| eof = true; |
| status = yajl_complete_parse(self->yajl); |
| } else { |
| status = yajl_parse(self->yajl, (const unsigned char*)PyBytes_AS_STRING(buffer.get()), PyBytes_GET_SIZE(buffer.get())); |
| } |
|
|
| |
| if (status != yajl_status_ok) { |
| if (status == yajl_status_error) { |
| unsigned char* error = yajl_get_error(self->yajl, self->yajl_verbose_errors, (const unsigned char*)PyBytes_AS_STRING(buffer.get()), PyBytes_GET_SIZE(buffer.get())); |
| PyErr_Format(PyExc_RuntimeError, "YAJL error: %s", error); |
| yajl_free_error(self->yajl, error); |
| } |
| return nullptr; |
| } |
|
|
| |
| if (!self->complete.empty()) { |
| return self->complete.pop_front().release(); |
| } |
| } while (!eof); |
|
|
| return nullptr; |
| } |
|
|