# xml.etree test for cElementTree
-
+import sys, struct
from test import support
from test.support import import_fresh_module
import unittest
self.assertEqual(cET_alias.SubElement.__module__, '_elementtree')
+@unittest.skipUnless(cET, 'requires _elementtree')
+class SizeofTest(unittest.TestCase):
+ def setUp(self):
+ import _testcapi
+ gc_headsize = _testcapi.SIZEOF_PYGC_HEAD
+ # object header
+ header = 'PP'
+ if hasattr(sys, "gettotalrefcount"):
+ # debug header
+ header = 'PP' + header
+ # fields
+ element = header + '5P'
+ self.elementsize = gc_headsize + struct.calcsize(element)
+ # extra
+ self.extra = struct.calcsize('PiiP4P')
+
+ def test_element(self):
+ e = cET.Element('a')
+ self.assertEqual(sys.getsizeof(e), self.elementsize)
+
+ def test_element_with_attrib(self):
+ e = cET.Element('a', href='about:')
+ self.assertEqual(sys.getsizeof(e),
+ self.elementsize + self.extra)
+
+ def test_element_with_children(self):
+ e = cET.Element('a')
+ for i in range(5):
+ cET.SubElement(e, 'span')
+ # should have space for 8 children now
+ self.assertEqual(sys.getsizeof(e),
+ self.elementsize + self.extra +
+ struct.calcsize('8P'))
+
def test_main():
from test import test_xml_etree, test_xml_etree_c
support.run_unittest(
MiscTests,
TestAliasWorking,
- TestAcceleratorImported
+ TestAcceleratorImported,
+ SizeofTest,
)
# Run the same test suite as the Python module
return NULL;
}
+static PyObject*
+element_sizeof(PyObject* _self, PyObject* args)
+{
+ ElementObject *self = (ElementObject*)_self;
+ Py_ssize_t result = sizeof(ElementObject);
+ if (self->extra) {
+ result += sizeof(ElementObjectExtra);
+ if (self->extra->children != self->extra->_children)
+ result += sizeof(PyObject*) * self->extra->allocated;
+ }
+ return PyLong_FromSsize_t(result);
+}
+
LOCAL(int)
checkpath(PyObject* tag)
{
{"__copy__", (PyCFunction) element_copy, METH_VARARGS},
{"__deepcopy__", (PyCFunction) element_deepcopy, METH_VARARGS},
+ {"__sizeof__", element_sizeof, METH_NOARGS},
{NULL, NULL}
};