diff options
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 15 | ||||
-rw-r--r-- | numpy/core/src/multiarray/scalartypes.c.src | 5 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 26 | ||||
-rw-r--r-- | numpy/core/tests/test_scalarmath.py | 9 |
4 files changed, 55 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index a791c2c22..cf298faa6 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -1901,6 +1901,16 @@ array_dumps(PyArrayObject *self, PyObject *args) static PyObject * +array_sizeof(PyArrayObject *self, PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) { + return NULL; + } + return PyObject_GetAttrString(self, "nbytes"); +} + + +static PyObject * array_transpose(PyArrayObject *self, PyObject *args) { PyObject *shape = Py_None; @@ -2308,6 +2318,11 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { (PyCFunction)array_wraparray, METH_VARARGS, NULL}, + /* for the sys module */ + {"__sizeof__", + (PyCFunction) array_sizeof, + METH_VARARGS, NULL}, + /* for the copy module */ {"__copy__", (PyCFunction)array_copy_keeporder, diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index 4fa634098..8a9569bdb 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -1921,6 +1921,11 @@ static PyMethodDef gentype_methods[] = { (PyCFunction)gentype_wraparray, METH_VARARGS, doc_sc_wraparray}, + /* for the sys module */ + {"__sizeof__", + (PyCFunction)gentype_itemsize_get, + METH_VARARGS, NULL}, + /* for the copy module */ {"__copy__", (PyCFunction)gentype_copy, diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 70398ee84..8eab5800e 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -4566,5 +4566,31 @@ class TestWhere(TestCase): assert_equal(np.where(False, b, a), "abcd") +class TestSizeOf(TestCase): + + def test_empty_array(self): + x = np.array([]) + assert_equal(0, sys.getsizeof(x)) + + def check_array(self, dtype): + elem_size = sys.getsizeof(dtype(0)) + + for length in [10, 50, 100, 500]: + x = np.arange(length, dtype=dtype) + assert_equal(length * elem_size, sys.getsizeof(x)) + + def test_array_int32(self): + self.check_array(np.int32) + + def test_array_int64(self): + self.check_array(np.int64) + + def test_array_float32(self): + self.check_array(np.float32) + + def test_array_float64(self): + self.check_array(np.float64) + + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index afdc06c03..0e07e6cff 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -271,5 +271,14 @@ class TestRepr(object): for t in [np.float32, np.float64]: yield self._test_type_repr, t + +class TestSizeOf(TestCase): + + def test_equal_nbytes(self): + for type in types: + x = type(0) + assert_equal(sys.getsizeof(x), x.nbytes) + + if __name__ == "__main__": run_module_suite() |