diff -r 657673a37dd3 Lib/test/test_array.py --- a/Lib/test/test_array.py Sun Jul 22 13:56:54 2012 +0300 +++ b/Lib/test/test_array.py Sun Jul 22 18:09:32 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.assertGreaterEqual(a.__sizeof__(), buffer_size) + class StringTest(BaseTest): diff -r 657673a37dd3 Modules/arraymodule.c --- a/Modules/arraymodule.c Sun Jul 22 13:56:54 2012 +0300 +++ b/Modules/arraymodule.c Sun Jul 22 18:09:32 2012 +0200 @@ -1576,6 +1576,19 @@ an array of some other type."); +static PyObject * +array_sizeof(arrayobject *self, PyObject *unused) +{ + Py_ssize_t res; + res = sizeof(arrayobject) + (Py_SIZE(self) * 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 +2159,8 @@ tobytes_doc}, {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, tounicode_doc}, + {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS, + sizeof_doc}, {NULL, NULL} /* sentinel */ };