diff -r e057a7d18fa2 Lib/test/test_array.py --- a/Lib/test/test_array.py Mon Aug 06 16:09:09 2012 -0400 +++ b/Lib/test/test_array.py Fri Aug 10 14:21:27 2012 +0200 @@ -788,6 +788,19 @@ warnings.filterwarnings("ignore", '', DeprecationWarning) ArraySubclassWithKwargs('b', newarg=1) + @test_support.cpython_only + def test_sizeof_with_buffer(self): + a = array.array(self.typecode, self.example) + basesize = test_support.calcvobjsize('4P') + buffer_size = a.buffer_info()[1] * a.itemsize + test_support.check_sizeof(self, a, basesize + buffer_size) + + @test_support.cpython_only + def test_sizeof_without_buffer(self): + a = array.array(self.typecode) + basesize = test_support.calcvobjsize('4P') + test_support.check_sizeof(self, a, basesize) + class StringTest(BaseTest): diff -r e057a7d18fa2 Modules/arraymodule.c --- a/Modules/arraymodule.c Mon Aug 06 16:09:09 2012 -0400 +++ b/Modules/arraymodule.c Fri Aug 10 14:21:27 2012 +0200 @@ -1477,6 +1477,19 @@ is raised. Use array.fromstring(ustr.decode(...)) to\n\ append Unicode data to an array of some other type."); +static PyObject * +array_sizeof(arrayobject *self, PyObject *unused) +{ + Py_ssize_t res; + 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."); + static PyObject * array_tounicode(arrayobject *self, PyObject *unused) @@ -1606,6 +1619,8 @@ #endif {"write", (PyCFunction)array_tofile_as_write, METH_O, tofile_doc}, + {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS, + sizeof_doc}, {NULL, NULL} /* sentinel */ };