diff options
author | Meador Inge <meadori@gmail.com> | 2012-08-11 03:05:45 (GMT) |
---|---|---|
committer | Meador Inge <meadori@gmail.com> | 2012-08-11 03:05:45 (GMT) |
commit | 2d639d5665739886e2044b2c92d2e157ee0161aa (patch) | |
tree | 03cdede58fde2e1cb18a7f6634b8ae4c8097bfe2 /Modules | |
parent | a939105a4018398aa21ee152283e3db5c7acd662 (diff) | |
download | cpython-2d639d5665739886e2044b2c92d2e157ee0161aa.zip cpython-2d639d5665739886e2044b2c92d2e157ee0161aa.tar.gz cpython-2d639d5665739886e2044b2c92d2e157ee0161aa.tar.bz2 |
Issue #15424: Add a __sizeof__ implementation for array objects.
Patch by Ludwig Hähne.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/arraymodule.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index a860f57..5a92862 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1533,6 +1533,19 @@ array_reduce(arrayobject *array) PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); 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_get_typecode(arrayobject *a, void *closure) { char tc = a->ob_descr->typecode; @@ -1606,6 +1619,8 @@ static PyMethodDef array_methods[] = { #endif {"write", (PyCFunction)array_tofile_as_write, METH_O, tofile_doc}, + {"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS, + sizeof_doc}, {NULL, NULL} /* sentinel */ }; |