]> granicus.if.org Git - python/commitdiff
Issue #15424: Add a __sizeof__ implementation for array objects.
authorMeador Inge <meadori@gmail.com>
Sat, 11 Aug 2012 03:35:45 +0000 (22:35 -0500)
committerMeador Inge <meadori@gmail.com>
Sat, 11 Aug 2012 03:35:45 +0000 (22:35 -0500)
Patch by Ludwig Hähne.

Lib/test/test_array.py
Misc/ACKS
Misc/NEWS
Modules/arraymodule.c

index 5190c357ea949c803144df10be91289a254d0bfa..e26e9add0559340636133b1546da3a0cff484214 100755 (executable)
@@ -988,6 +988,19 @@ class BaseTest(unittest.TestCase):
         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):
 
index 4d8f13c70ef1d8e5184e0e6fb942bd57caa6eb6f..36dc7fe0a6e8912ff8be2923745a8c81d7b4d61b 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -434,6 +434,7 @@ Jim Hugunin
 Greg Humphreys
 Eric Huss
 Jeremy Hylton
+Ludwig Hähne
 Gerhard Häring
 Fredrik Håård
 Mihai Ibanescu
index 99812d29accef86efefc5d48f764c76c0bfe8599..5a4d71bc358fa2d0c373f81a8f710e3e97095971 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -101,6 +101,9 @@ Core and Builtins
 Library
 -------
 
+- 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.
 
index 641e283ed125aee6ece8fd908b8c9792c134137f..2d4ee727090b990ea568f373025c4981627a59b5 100644 (file)
@@ -1510,6 +1510,19 @@ array.tobytes().decode() to obtain a unicode string from\n\
 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 @@ static PyMethodDef array_methods[] = {
      tobytes_doc},
     {"tounicode",   (PyCFunction)array_tounicode,       METH_NOARGS,
      tounicode_doc},
+    {"__sizeof__",      (PyCFunction)array_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}           /* sentinel */
 };