diff -r b127046831e2 Lib/test/test_array.py --- a/Lib/test/test_array.py Mon Jul 23 00:24:24 2012 -0500 +++ b/Lib/test/test_array.py Wed Jul 25 13:50:35 2012 +0200 @@ -1015,6 +1015,11 @@ a = array.array('H', b"1234") self.assertEqual(len(a) * a.itemsize, 4) + def test_sizeof(self): + a = array.array('i', range(64)) + buffer_size = a.buffer_info()[1] * a.itemsize + self.assertGreater(a.__sizeof__(), buffer_size) + class StringTest(BaseTest): diff -r b127046831e2 Modules/arraymodule.c --- a/Modules/arraymodule.c Mon Jul 23 00:24:24 2012 -0500 +++ b/Modules/arraymodule.c Wed Jul 25 13:50:35 2012 +0200 @@ -1576,6 +1576,20 @@ an array of some other type."); +static PyObject * +array_sizeof(PyObject *_self, PyObject *unused) +{ + Py_ssize_t res; + arrayobject *self = (arrayobject *)_self; + res = sizeof(arrayobject) + (self->allocated * self->ob_descr->itemsize); + return PyLong_FromSsize_t(res); +} + +PyDoc_STRVAR(sizeof_doc, +"__sizeof__() -> int\n\ +\n\ +Size of the array in memory, in bytes."); + /*********************** Pickling support ************************/ @@ -2146,6 +2160,8 @@ tobytes_doc}, {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, tounicode_doc}, + {"__sizeof__", array_sizeof, METH_NOARGS, + sizeof_doc}, {NULL, NULL} /* sentinel */ };