elem.tail = X()
elem.__setstate__({'tag': 42}) # shouldn't cause an assertion failure
+ @support.cpython_only
+ def test_uninitialized_parser(self):
+ # The interpreter shouldn't crash in case of calling methods or
+ # accessing attributes of uninitialized XMLParser objects.
+ parser = cET.XMLParser.__new__(cET.XMLParser)
+ self.assertRaises(ValueError, parser.close)
+ self.assertRaises(ValueError, parser.feed, 'foo')
+ class MockFile:
+ def read(*args):
+ return ''
+ self.assertRaises(ValueError, parser._parse_whole, MockFile())
+ self.assertRaises(ValueError, parser._setevents, None)
+ self.assertIsNone(parser.entity)
+ self.assertIsNone(parser.target)
+
def test_setstate_leaks(self):
# Test reference leaks
elem = cET.Element.__new__(cET.Element)
Py_TYPE(self)->tp_free((PyObject *)self);
}
+Py_LOCAL_INLINE(int)
+_check_xmlparser(XMLParserObject* self)
+{
+ if (self->target == NULL) {
+ PyErr_SetString(PyExc_ValueError,
+ "XMLParser.__init__() wasn't called");
+ return 0;
+ }
+ return 1;
+}
+
LOCAL(PyObject*)
expat_parse(XMLParserObject* self, const char* data, int data_len, int final)
{
/* end feeding data to parser */
PyObject* res;
+
+ if (!_check_xmlparser(self)) {
+ return NULL;
+ }
res = expat_parse(self, "", 0, 1);
if (!res)
return NULL;
{
/* feed data to parser */
+ if (!_check_xmlparser(self)) {
+ return NULL;
+ }
if (PyUnicode_Check(data)) {
Py_ssize_t data_len;
const char *data_ptr = PyUnicode_AsUTF8AndSize(data, &data_len);
PyObject* temp;
PyObject* res;
+ if (!_check_xmlparser(self)) {
+ return NULL;
+ }
reader = PyObject_GetAttrString(file, "read");
if (!reader)
return NULL;
TreeBuilderObject *target;
PyObject *events_append, *events_seq;
+ if (!_check_xmlparser(self)) {
+ return NULL;
+ }
if (!TreeBuilder_CheckExact(self->target)) {
PyErr_SetString(
PyExc_TypeError,