diff Modules/arraymodule.c @ 78492:91382d4e2dfb 2.7

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:05:45 -0500
parents 78a7d5f8f054
children
line wrap: on
line diff
--- a/Modules/arraymodule.c	Thu Aug 09 23:04:42 2012 -0500
+++ b/Modules/arraymodule.c	Fri Aug 10 22:05:45 2012 -0500
@@ -1533,6 +1533,19 @@
 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 @@
 #endif
     {"write",           (PyCFunction)array_tofile_as_write,     METH_O,
      tofile_doc},
+    {"__sizeof__",      (PyCFunction)array_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}           /* sentinel */
 };