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

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


http://hg.python.org/cpython/rev/91382d4e2dfb
changeset: 78492:91382d4e2dfb
branch: 2.7
parent: 78484:260f3ad7af4b
user: Meador Inge <meadori at gmail.com>
date: Fri Aug 10 22:05:45 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
@@ -985,6 +985,19 @@
 upper = long(pow(2, a.itemsize * 8)) - 1L
 self.check_overflow(lower, upper)
 
+ @test_support.cpython_only
+ def test_sizeof_with_buffer(self):
+ a = array.array(self.typecode, self.example)
+ basesize = test_support.calcvobjsize('4P')
+ buffer_size = a.buffer_info()[1] * a.itemsize
+ test_support.check_sizeof(self, a, basesize + buffer_size)
+
+ @test_support.cpython_only
+ def test_sizeof_without_buffer(self):
+ a = array.array(self.typecode)
+ basesize = test_support.calcvobjsize('4P')
+ test_support.check_sizeof(self, a, basesize)
+
 
 class ByteTest(SignedNumberTest):
 typecode = 'b'
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -393,6 +393,7 @@
 Greg Humphreys
 Eric Huss
 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
@@ -94,6 +94,9 @@
 
 - Issue #15567: Fix NameError when running threading._test
 
+- 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.
 
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -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 */
 };
 
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /