[Python-checkins] cpython (merge 3.2 -> default): Issue #15424: Add a __sizeof__ implementation for array objects.

meador.inge python-checkins at python.org
Sat Aug 11 06:26:54 CEST 2012


http://hg.python.org/cpython/rev/7d82a60850fd
changeset:   78494:7d82a60850fd
parent:      78491:f7b59e890e30
parent:      78493:45ef9bc8739f
user:        Meador Inge <meadori at gmail.com>
date:        Fri Aug 10 23:21:39 2012 -0500
summary:
  Issue #15424: Add a __sizeof__ implementation for array objects.

Patch by Ludwig Hähne.

files:
  Lib/test/test_array.py |  13 +++++++++++++
  Misc/ACKS              |   1 +
  Misc/NEWS              |   3 +++
  Modules/arraymodule.c  |  15 +++++++++++++++
  4 files changed, 32 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -1015,6 +1015,19 @@
         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('Pn2Pi')
+        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('Pn2Pi')
+        support.check_sizeof(self, a, basesize)
+
 
 class StringTest(BaseTest):
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -479,6 +479,7 @@
 Eric Huss
 Taihyun Hwang
 Jeremy Hylton
+Ludwig Hähne
 Gerhard Häring
 Fredrik Håård
 Catalin Iacob
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -80,6 +80,9 @@
 Library
 -------
 
+- Issue #15424: Add a __sizeof__ implementation for array objects.
+  Patch by Ludwig Hähne.
+
 - Issue #15576: Allow extension modules to act as a package's __init__ module.
 
 - Issue #15502: Have importlib.invalidate_caches() work on sys.meta_path
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1567,6 +1567,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 ************************/
 
@@ -2143,6 +2156,8 @@
      tobytes_doc},
     {"tounicode",   (PyCFunction)array_tounicode,       METH_NOARGS,
      tounicode_doc},
+    {"__sizeof__",      (PyCFunction)array_sizeof,      METH_NOARGS,
+     sizeof_doc},
     {NULL,              NULL}           /* sentinel */
 };
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list