diff Modules/arraymodule.c @ 78493:45ef9bc8739f 3.2

Issue #15424: Add a __sizeof__ implementation for array objects. Patch by Ludwig Hähne.
author Meador Inge <meadori@gmail.com>
date Fri, 10 Aug 2012 22:35:45 -0500
parents 1e2880250610
children 88dcccb46298 7d82a60850fd
line wrap: on
line diff
--- a/Modules/arraymodule.c	Thu Aug 09 21:26:34 2012 +0300
+++ b/Modules/arraymodule.c	Fri Aug 10 22:35:45 2012 -0500
@@ -1510,6 +1510,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 ************************/
 
@@ -2077,6 +2090,8 @@
      tobytes_doc},
     {"tounicode",   (PyCFunction)array_tounicode,       METH_NOARGS,
      tounicode_doc},
+    {"__sizeof__",      (PyCFunction)array_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}           /* sentinel */
 };