Meador Inge committed 45ef9bc Draft

Issue #15424: Add a __sizeof__ implementation for array objects.

Patch by Ludwig Hähne.

Comments (0)

Files changed (4)


         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('4Pi')
+        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('4Pi')
+        support.check_sizeof(self, a, basesize)
 class StringTest(BaseTest):
 Greg Humphreys
 Eric Huss
 Jeremy Hylton
+Ludwig Hähne
 Gerhard Häring
 Fredrik Håård
 Mihai Ibanescu
+- Issue #15424: Add a __sizeof__ implementation for array objects.
+  Patch by Ludwig Hähne.
 - Issue #13052: Fix IDLE crashing when replace string in Search/Replace dialog
   ended with '\'. Patch by Roger Serwy.


 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);
+"__sizeof__() -> int\n\
+Size of the array in memory, in bytes.");
 /*********************** Pickling support ************************/
     {"tounicode",   (PyCFunction)array_tounicode,       METH_NOARGS,
+    {"__sizeof__",      (PyCFunction)array_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}           /* sentinel */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.