diff -r 0eeffeadaa1e Lib/test/test_array.py --- a/Lib/test/test_array.py Mon Aug 06 17:53:19 2012 -0700 +++ b/Lib/test/test_array.py Tue Aug 07 11:44:05 2012 +0200 @@ -1015,6 +1015,19 @@ a = array.array('H', b"1234") self.assertEqual(len(a) * a.itemsize, 4) + @support.cpython_only + def test_sizeof_with_buffer(self): + a = array.array(self.typecode, self.example) + basesize = support.calcvobjsize('Pn2Pi') + buffer_size = a.buffer_info()[1] * a.itemsize + support.check_sizeof(self, a, basesize + buffer_size) + + @support.cpython_only + def test_sizeof_without_buffer(self): + a = array.array(self.typecode) + basesize = support.calcvobjsize('Pn2Pi') + support.check_sizeof(self, a, basesize) + class StringTest(BaseTest): diff -r 0eeffeadaa1e Modules/arraymodule.c --- a/Modules/arraymodule.c Mon Aug 06 17:53:19 2012 -0700 +++ b/Modules/arraymodule.c Tue Aug 07 11:44:05 2012 +0200 @@ -1567,6 +1567,19 @@ 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."); + /*********************** Pickling support ************************/ @@ -2143,6 +2156,8 @@ tobytes_doc}, {"tounicode", (PyCFunction)array_tounicode, METH_NOARGS, tounicode_doc}, + {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS, + sizeof_doc}, {NULL, NULL} /* sentinel */ };