mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
Issue #15424: Add a __sizeof__ implementation for array objects.
Patch by Ludwig Hähne.
This commit is contained in:
commit
80dd1af4e0
4 changed files with 32 additions and 0 deletions
|
@ -1015,6 +1015,19 @@ class BaseTest(unittest.TestCase):
|
||||||
a = array.array('H', b"1234")
|
a = array.array('H', b"1234")
|
||||||
self.assertEqual(len(a) * a.itemsize, 4)
|
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):
|
class StringTest(BaseTest):
|
||||||
|
|
||||||
|
|
|
@ -479,6 +479,7 @@ Greg Humphreys
|
||||||
Eric Huss
|
Eric Huss
|
||||||
Taihyun Hwang
|
Taihyun Hwang
|
||||||
Jeremy Hylton
|
Jeremy Hylton
|
||||||
|
Ludwig Hähne
|
||||||
Gerhard Häring
|
Gerhard Häring
|
||||||
Fredrik Håård
|
Fredrik Håård
|
||||||
Catalin Iacob
|
Catalin Iacob
|
||||||
|
|
|
@ -80,6 +80,9 @@ Core and Builtins
|
||||||
Library
|
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 #15576: Allow extension modules to act as a package's __init__ module.
|
||||||
|
|
||||||
- Issue #15502: Have importlib.invalidate_caches() work on sys.meta_path
|
- Issue #15502: Have importlib.invalidate_caches() work on sys.meta_path
|
||||||
|
|
|
@ -1567,6 +1567,19 @@ array.tobytes().decode() to obtain a unicode string from\n\
|
||||||
an array of some other type.");
|
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 ************************/
|
/*********************** Pickling support ************************/
|
||||||
|
|
||||||
|
@ -2143,6 +2156,8 @@ static PyMethodDef array_methods[] = {
|
||||||
tobytes_doc},
|
tobytes_doc},
|
||||||
{"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
|
{"tounicode", (PyCFunction)array_tounicode, METH_NOARGS,
|
||||||
tounicode_doc},
|
tounicode_doc},
|
||||||
|
{"__sizeof__", (PyCFunction)array_sizeof, METH_NOARGS,
|
||||||
|
sizeof_doc},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue