Browse Source

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.
pull/2/head
Vladimír Vondruš 7 years ago
parent
commit
c40627c96c
  1. 4
      doc/python/corrade.containers.rst
  2. 4
      src/python/corrade/containers.cpp
  3. 40
      src/python/corrade/test/test_containers.py

4
doc/python/corrade.containers.rst

@ -56,9 +56,9 @@
.. code:: pycon .. code:: pycon
>>> b.obj is a >>> b.owner is a
True True
>>> b[1:4][:-1].obj is a >>> b[1:4][:-1].owner is a
True True
`Comparison to Python's memoryview`_ `Comparison to Python's memoryview`_

4
src/python/corrade/containers.cpp

@ -131,7 +131,7 @@ template<class T> void arrayView(py::class_<Containers::ArrayView<T>, Containers
/* Length and memory owning object */ /* Length and memory owning object */
.def("__len__", &Containers::ArrayView<T>::size, "View size") .def("__len__", &Containers::ArrayView<T>::size, "View size")
.def_property_readonly("obj", [](const Containers::ArrayView<T>& self) { .def_property_readonly("owner", [](const Containers::ArrayView<T>& self) {
return pyObjectHolderFor<Containers::PyArrayViewHolder>(self).owner; return pyObjectHolderFor<Containers::PyArrayViewHolder>(self).owner;
}, "Memory owner object") }, "Memory owner object")
@ -352,7 +352,7 @@ template<unsigned dimensions, class T> void stridedArrayView(py::class_<Containe
return stride<dimensions>(self.stride()); return stride<dimensions>(self.stride());
}, "View stride in each dimension") }, "View stride in each dimension")
.def_property_readonly("dimensions", [](const Containers::StridedArrayView<dimensions, T>&) { return dimensions; }, "Dimension count") .def_property_readonly("dimensions", [](const Containers::StridedArrayView<dimensions, T>&) { return dimensions; }, "Dimension count")
.def_property_readonly("obj", [](const Containers::StridedArrayView<dimensions, T>& self) { .def_property_readonly("owner", [](const Containers::StridedArrayView<dimensions, T>& self) {
return pyObjectHolderFor<Containers::PyArrayViewHolder>(self).owner; return pyObjectHolderFor<Containers::PyArrayViewHolder>(self).owner;
}, "Memory owner object") }, "Memory owner object")

40
src/python/corrade/test/test_containers.py

@ -33,8 +33,8 @@ class ArrayView(unittest.TestCase):
def test_init(self): def test_init(self):
a = containers.ArrayView() a = containers.ArrayView()
b = containers.MutableArrayView() b = containers.MutableArrayView()
self.assertIs(a.obj, None) self.assertIs(a.owner, None)
self.assertIs(b.obj, None) self.assertIs(b.owner, None)
self.assertEqual(len(a), 0) self.assertEqual(len(a), 0)
self.assertEqual(len(b), 0) self.assertEqual(len(b), 0)
self.assertEqual(bytes(a), b'') self.assertEqual(bytes(a), b'')
@ -45,7 +45,7 @@ class ArrayView(unittest.TestCase):
a_refcount = sys.getrefcount(a) a_refcount = sys.getrefcount(a)
b = containers.ArrayView(a) b = containers.ArrayView(a)
self.assertIs(b.obj, a) self.assertIs(b.owner, a)
self.assertEqual(len(b), 5) self.assertEqual(len(b), 5)
self.assertEqual(bytes(b), b'hello') self.assertEqual(bytes(b), b'hello')
self.assertEqual(b[2], 'l') 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 # b should keep a reference to a, so deleting the local reference
# shouldn't affect it # shouldn't affect it
del a del a
self.assertTrue(sys.getrefcount(b.obj), a_refcount) self.assertTrue(sys.getrefcount(b.owner), a_refcount)
self.assertEqual(b[2], 'l') self.assertEqual(b[2], 'l')
# Now, if we delete b, a should not be referenced by anything anymore # Now, if we delete b, a should not be referenced by anything anymore
a = b.obj a = b.owner
del b del b
self.assertTrue(sys.getrefcount(a), a_refcount) self.assertTrue(sys.getrefcount(a), a_refcount)
@ -72,7 +72,7 @@ class ArrayView(unittest.TestCase):
b = containers.ArrayView(v) b = containers.ArrayView(v)
# memoryview's buffer protocol returns itself, not the underlying # memoryview's buffer protocol returns itself, not the underlying
# bytes, as it manages the Py_buffer instance. So this is expected. # 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): def test_init_buffer_mutable(self):
a = bytearray(b'hello') a = bytearray(b'hello')
@ -84,7 +84,7 @@ class ArrayView(unittest.TestCase):
def test_init_array(self): def test_init_array(self):
a = array.array('f', [1.0, 4.5, 7.9]) a = array.array('f', [1.0, 4.5, 7.9])
b = containers.ArrayView(a) b = containers.ArrayView(a)
self.assertIs(b.obj, a) self.assertIs(b.owner, a)
self.assertEqual(len(b), 3*4) self.assertEqual(len(b), 3*4)
def test_init_buffer_unexpected_stride(self): def test_init_buffer_unexpected_stride(self):
@ -209,8 +209,8 @@ class StridedArrayView1D(unittest.TestCase):
def test_init(self): def test_init(self):
a = containers.StridedArrayView1D() a = containers.StridedArrayView1D()
b = containers.MutableStridedArrayView1D() b = containers.MutableStridedArrayView1D()
self.assertIs(a.obj, None) self.assertIs(a.owner, None)
self.assertIs(b.obj, None) self.assertIs(b.owner, None)
self.assertEqual(len(a), 0) self.assertEqual(len(a), 0)
self.assertEqual(len(b), 0) self.assertEqual(len(b), 0)
self.assertEqual(bytes(a), b'') self.assertEqual(bytes(a), b'')
@ -227,7 +227,7 @@ class StridedArrayView1D(unittest.TestCase):
a_refcount = sys.getrefcount(a) a_refcount = sys.getrefcount(a)
b = containers.StridedArrayView1D(a) b = containers.StridedArrayView1D(a)
self.assertIs(b.obj, a) self.assertIs(b.owner, a)
self.assertEqual(len(b), 5) self.assertEqual(len(b), 5)
self.assertEqual(bytes(b), b'hello') self.assertEqual(bytes(b), b'hello')
self.assertEqual(b.size, (5, )) 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 # b should keep a reference to a, so deleting the local reference
# shouldn't affect it # shouldn't affect it
del a del a
self.assertTrue(sys.getrefcount(b.obj), a_refcount) self.assertTrue(sys.getrefcount(b.owner), a_refcount)
self.assertEqual(b[2], 'l') self.assertEqual(b[2], 'l')
# Now, if we delete b, a should not be referenced by anything anymore # Now, if we delete b, a should not be referenced by anything anymore
a = b.obj a = b.owner
del b del b
self.assertTrue(sys.getrefcount(a), a_refcount) self.assertTrue(sys.getrefcount(a), a_refcount)
@ -256,7 +256,7 @@ class StridedArrayView1D(unittest.TestCase):
b = containers.StridedArrayView1D(v) b = containers.StridedArrayView1D(v)
# memoryview's buffer protocol returns itself, not the underlying # memoryview's buffer protocol returns itself, not the underlying
# bytes, as it manages the Py_buffer instance. So this is expected. # 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): def test_init_buffer_mutable(self):
a = bytearray(b'hello') a = bytearray(b'hello')
@ -377,8 +377,8 @@ class StridedArrayView2D(unittest.TestCase):
def test_init(self): def test_init(self):
a = containers.StridedArrayView2D() a = containers.StridedArrayView2D()
b = containers.MutableStridedArrayView2D() b = containers.MutableStridedArrayView2D()
self.assertIs(a.obj, None) self.assertIs(a.owner, None)
self.assertIs(b.obj, None) self.assertIs(b.owner, None)
self.assertEqual(len(a), 0) self.assertEqual(len(a), 0)
self.assertEqual(len(b), 0) self.assertEqual(len(b), 0)
self.assertEqual(bytes(a), b'') 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 # b should keep a reference to a, so deleting the local reference
# shouldn't affect it # shouldn't affect it
del a del a
self.assertTrue(sys.getrefcount(b.obj), a_refcount) self.assertTrue(sys.getrefcount(b.owner), a_refcount)
self.assertEqual(b[1][2], '6') self.assertEqual(b[1][2], '6')
# Now, if we delete b, a should not be referenced by anything anymore # Now, if we delete b, a should not be referenced by anything anymore
a = b.obj a = b.owner
del b del b
self.assertTrue(sys.getrefcount(a), a_refcount) self.assertTrue(sys.getrefcount(a), a_refcount)
@ -473,7 +473,7 @@ class StridedArrayView2D(unittest.TestCase):
b_refcount = sys.getrefcount(b) b_refcount = sys.getrefcount(b)
# memoryview's buffer protocol returns itself, not the underlying # memoryview's buffer protocol returns itself, not the underlying
# bytes, as it manages the Py_buffer instance. So this is expected. # 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) self.assertEqual(sys.getrefcount(a), a_refcount + 1)
# When slicing, b's refcount should not change but a's refcount should # 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) b_refcount = sys.getrefcount(b)
# memoryview's buffer protocol returns itself, not the underlying # memoryview's buffer protocol returns itself, not the underlying
# bytes, as it manages the Py_buffer instance. So this is expected. # 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) self.assertEqual(sys.getrefcount(a), a_refcount + 1)
# When slicing, b's refcount should not change but a's refcount should # 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) b_refcount = sys.getrefcount(b)
# memoryview's buffer protocol returns itself, not the underlying # memoryview's buffer protocol returns itself, not the underlying
# bytes, as it manages the Py_buffer instance. So this is expected. # 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) self.assertEqual(sys.getrefcount(a), a_refcount + 1)
c = memoryview(b) c = memoryview(b)

Loading…
Cancel
Save