From c40627c96ce1d2daff6c5ee462cd76d1fcd18a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 27 Jul 2019 20:13:23 +0200 Subject: [PATCH] python: better name [Strided]ArrayView memory owner property. Mirroring Python's obj is not good, as the name is absolutely not describing what it is. --- doc/python/corrade.containers.rst | 4 +-- src/python/corrade/containers.cpp | 4 +-- src/python/corrade/test/test_containers.py | 40 +++++++++++----------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/doc/python/corrade.containers.rst b/doc/python/corrade.containers.rst index 02f4e74..35fe203 100644 --- a/doc/python/corrade.containers.rst +++ b/doc/python/corrade.containers.rst @@ -56,9 +56,9 @@ .. code:: pycon - >>> b.obj is a + >>> b.owner is a True - >>> b[1:4][:-1].obj is a + >>> b[1:4][:-1].owner is a True `Comparison to Python's memoryview`_ diff --git a/src/python/corrade/containers.cpp b/src/python/corrade/containers.cpp index 6892b39..3d90c13 100644 --- a/src/python/corrade/containers.cpp +++ b/src/python/corrade/containers.cpp @@ -131,7 +131,7 @@ template void arrayView(py::class_, Containers /* Length and memory owning object */ .def("__len__", &Containers::ArrayView::size, "View size") - .def_property_readonly("obj", [](const Containers::ArrayView& self) { + .def_property_readonly("owner", [](const Containers::ArrayView& self) { return pyObjectHolderFor(self).owner; }, "Memory owner object") @@ -352,7 +352,7 @@ template void stridedArrayView(py::class_(self.stride()); }, "View stride in each dimension") .def_property_readonly("dimensions", [](const Containers::StridedArrayView&) { return dimensions; }, "Dimension count") - .def_property_readonly("obj", [](const Containers::StridedArrayView& self) { + .def_property_readonly("owner", [](const Containers::StridedArrayView& self) { return pyObjectHolderFor(self).owner; }, "Memory owner object") diff --git a/src/python/corrade/test/test_containers.py b/src/python/corrade/test/test_containers.py index 843167a..507113d 100644 --- a/src/python/corrade/test/test_containers.py +++ b/src/python/corrade/test/test_containers.py @@ -33,8 +33,8 @@ class ArrayView(unittest.TestCase): def test_init(self): a = containers.ArrayView() b = containers.MutableArrayView() - self.assertIs(a.obj, None) - self.assertIs(b.obj, None) + self.assertIs(a.owner, None) + self.assertIs(b.owner, None) self.assertEqual(len(a), 0) self.assertEqual(len(b), 0) self.assertEqual(bytes(a), b'') @@ -45,7 +45,7 @@ class ArrayView(unittest.TestCase): a_refcount = sys.getrefcount(a) b = containers.ArrayView(a) - self.assertIs(b.obj, a) + self.assertIs(b.owner, a) self.assertEqual(len(b), 5) self.assertEqual(bytes(b), b'hello') self.assertEqual(b[2], 'l') @@ -58,11 +58,11 @@ class ArrayView(unittest.TestCase): # b should keep a reference to a, so deleting the local reference # shouldn't affect it del a - self.assertTrue(sys.getrefcount(b.obj), a_refcount) + self.assertTrue(sys.getrefcount(b.owner), a_refcount) self.assertEqual(b[2], 'l') # Now, if we delete b, a should not be referenced by anything anymore - a = b.obj + a = b.owner del b self.assertTrue(sys.getrefcount(a), a_refcount) @@ -72,7 +72,7 @@ class ArrayView(unittest.TestCase): b = containers.ArrayView(v) # memoryview's buffer protocol returns itself, not the underlying # bytes, as it manages the Py_buffer instance. So this is expected. - self.assertIs(b.obj, v) + self.assertIs(b.owner, v) def test_init_buffer_mutable(self): a = bytearray(b'hello') @@ -84,7 +84,7 @@ class ArrayView(unittest.TestCase): def test_init_array(self): a = array.array('f', [1.0, 4.5, 7.9]) b = containers.ArrayView(a) - self.assertIs(b.obj, a) + self.assertIs(b.owner, a) self.assertEqual(len(b), 3*4) def test_init_buffer_unexpected_stride(self): @@ -209,8 +209,8 @@ class StridedArrayView1D(unittest.TestCase): def test_init(self): a = containers.StridedArrayView1D() b = containers.MutableStridedArrayView1D() - self.assertIs(a.obj, None) - self.assertIs(b.obj, None) + self.assertIs(a.owner, None) + self.assertIs(b.owner, None) self.assertEqual(len(a), 0) self.assertEqual(len(b), 0) self.assertEqual(bytes(a), b'') @@ -227,7 +227,7 @@ class StridedArrayView1D(unittest.TestCase): a_refcount = sys.getrefcount(a) b = containers.StridedArrayView1D(a) - self.assertIs(b.obj, a) + self.assertIs(b.owner, a) self.assertEqual(len(b), 5) self.assertEqual(bytes(b), b'hello') self.assertEqual(b.size, (5, )) @@ -242,11 +242,11 @@ class StridedArrayView1D(unittest.TestCase): # b should keep a reference to a, so deleting the local reference # shouldn't affect it del a - self.assertTrue(sys.getrefcount(b.obj), a_refcount) + self.assertTrue(sys.getrefcount(b.owner), a_refcount) self.assertEqual(b[2], 'l') # Now, if we delete b, a should not be referenced by anything anymore - a = b.obj + a = b.owner del b self.assertTrue(sys.getrefcount(a), a_refcount) @@ -256,7 +256,7 @@ class StridedArrayView1D(unittest.TestCase): b = containers.StridedArrayView1D(v) # memoryview's buffer protocol returns itself, not the underlying # bytes, as it manages the Py_buffer instance. So this is expected. - self.assertIs(b.obj, v) + self.assertIs(b.owner, v) def test_init_buffer_mutable(self): a = bytearray(b'hello') @@ -377,8 +377,8 @@ class StridedArrayView2D(unittest.TestCase): def test_init(self): a = containers.StridedArrayView2D() b = containers.MutableStridedArrayView2D() - self.assertIs(a.obj, None) - self.assertIs(b.obj, None) + self.assertIs(a.owner, None) + self.assertIs(b.owner, None) self.assertEqual(len(a), 0) self.assertEqual(len(b), 0) self.assertEqual(bytes(a), b'') @@ -417,11 +417,11 @@ class StridedArrayView2D(unittest.TestCase): # b should keep a reference to a, so deleting the local reference # shouldn't affect it del a - self.assertTrue(sys.getrefcount(b.obj), a_refcount) + self.assertTrue(sys.getrefcount(b.owner), a_refcount) self.assertEqual(b[1][2], '6') # Now, if we delete b, a should not be referenced by anything anymore - a = b.obj + a = b.owner del b self.assertTrue(sys.getrefcount(a), a_refcount) @@ -473,7 +473,7 @@ class StridedArrayView2D(unittest.TestCase): b_refcount = sys.getrefcount(b) # memoryview's buffer protocol returns itself, not the underlying # bytes, as it manages the Py_buffer instance. So this is expected. - self.assertEqual(b.obj, a) + self.assertEqual(b.owner, a) self.assertEqual(sys.getrefcount(a), a_refcount + 1) # When slicing, b's refcount should not change but a's refcount should @@ -501,7 +501,7 @@ class StridedArrayView2D(unittest.TestCase): b_refcount = sys.getrefcount(b) # memoryview's buffer protocol returns itself, not the underlying # bytes, as it manages the Py_buffer instance. So this is expected. - self.assertEqual(b.obj, a) + self.assertEqual(b.owner, a) self.assertEqual(sys.getrefcount(a), a_refcount + 1) # When slicing, b's refcount should not change but a's refcount should @@ -624,7 +624,7 @@ class StridedArrayView2D(unittest.TestCase): b_refcount = sys.getrefcount(b) # memoryview's buffer protocol returns itself, not the underlying # bytes, as it manages the Py_buffer instance. So this is expected. - self.assertEqual(b.obj, a) + self.assertEqual(b.owner, a) self.assertEqual(sys.getrefcount(a), a_refcount + 1) c = memoryview(b)