[Python-checkins] r63596 - in python/branches/okkoto-sizeof: Lib/test/test_sys.py Objects/listobject.c

robert.schuppenies python-checkins at python.org
Sat May 24 23:01:43 CEST 2008


Author: robert.schuppenies
Date: Sat May 24 23:01:42 2008
New Revision: 63596

Log:
implemented __sizeof__ for listobject


Modified:
   python/branches/okkoto-sizeof/Lib/test/test_sys.py
   python/branches/okkoto-sizeof/Objects/listobject.c

Modified: python/branches/okkoto-sizeof/Lib/test/test_sys.py
==============================================================================
--- python/branches/okkoto-sizeof/Lib/test/test_sys.py	(original)
+++ python/branches/okkoto-sizeof/Lib/test/test_sys.py	Sat May 24 23:01:42 2008
@@ -502,9 +502,6 @@
         l = self.l
         p = self.p
         self.headersize += l
-        # list
-        self.check_sizeof([], p + l)
-        self.check_sizeof([1, 2, 3], p + l)
         # string
         self.check_sizeof('', l + self.align(i + 1))
         self.check_sizeof('abc', l + self.align(i + 1) + 3)
@@ -517,6 +514,9 @@
         self.check_sizeof({}, 3*l + 3*p + 8*(l + 2*p))
         longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
         self.check_sizeof(longdict, 3*l + 3*p + 8*(l + 2*p) + 16*(l + 2*p))
+        # list
+        self.check_sizeof([], l + p + l)
+        self.check_sizeof([1, 2, 3], l + p + l + 3*l)
 
 
 def test_main():

Modified: python/branches/okkoto-sizeof/Objects/listobject.c
==============================================================================
--- python/branches/okkoto-sizeof/Objects/listobject.c	(original)
+++ python/branches/okkoto-sizeof/Objects/listobject.c	Sat May 24 23:01:42 2008
@@ -2420,6 +2420,15 @@
 	return 0;
 }
 
+static PyObject *
+list_sizeof(PyListObject *self)
+{
+	Py_ssize_t res;
+
+	res = sizeof(PyListObject) + self->allocated * sizeof(self->ob_item);
+	return PyLong_FromLong(res);
+}
+
 static PyObject *list_iter(PyObject *seq);
 static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
 
@@ -2427,6 +2436,8 @@
 "x.__getitem__(y) <==> x[y]");
 PyDoc_STRVAR(reversed_doc,
 "L.__reversed__() -- return a reverse iterator over the list");
+PyDoc_STRVAR(sizeof_doc,
+"L.__sizeof__() -- size of L in bytes");
 PyDoc_STRVAR(append_doc,
 "L.append(object) -- append object to end");
 PyDoc_STRVAR(extend_doc,
@@ -2452,6 +2463,7 @@
 static PyMethodDef list_methods[] = {
 	{"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
 	{"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
+	{"__sizeof__",  (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
 	{"append",	(PyCFunction)listappend,  METH_O, append_doc},
 	{"insert",	(PyCFunction)listinsert,  METH_VARARGS, insert_doc},
 	{"extend",      (PyCFunction)listextend,  METH_O, extend_doc},


More information about the Python-checkins mailing list