summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMeador Inge <meadori@gmail.com>2012-08-11 04:21:39 (GMT)
committerMeador Inge <meadori@gmail.com>2012-08-11 04:21:39 (GMT)
commit80dd1af4e0ee07b2f65833835754f0a27bf48975 (patch)
tree1d7f04a3c9fd2268459b864ea89abdb83ed7e8ea
parent688a551ca075ed28126e11b5935ed0649d1e3b83 (diff)
parent03b4d5072aa044d8c6827e387b647dcc6c9d0ff6 (diff)
downloadcpython-80dd1af4e0ee07b2f65833835754f0a27bf48975.zip
cpython-80dd1af4e0ee07b2f65833835754f0a27bf48975.tar.gz
cpython-80dd1af4e0ee07b2f65833835754f0a27bf48975.tar.bz2
Issue #15424: Add a __sizeof__ implementation for array objects.
Patch by Ludwig Hähne.
-rwxr-xr-xLib/test/test_array.py13
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/arraymodule.c15
4 files changed, 32 insertions, 0 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index eb6d77f..544c2ce 100755
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -1015,6 +1015,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('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
index 4dce609..03708967 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -479,6 +479,7 @@ Greg Humphreys
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
index 972e769..3de6805 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -80,6 +80,9 @@ Core and Builtins
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
index f0615c9..04eb67c 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1567,6 +1567,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 ************************/
@@ -2143,6 +2156,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 */
};