Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_memoryview.BytesMemorySliceSliceTest Class Reference
Inheritance diagram for test.test_memoryview.BytesMemorySliceSliceTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_memoryview.BytesMemorySliceSliceTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def check_getitem_with_type
def test_getitem
def test_iter
def test_setitem_readonly
def test_setitem_writable
def test_delitem
def test_tobytes
def test_tolist
def test_compare
def check_attributes_with_type
def test_attributes_readonly
def test_attributes_writable
def test_getbuffer
def test_gc
def test_contextmanager
def test_release
def test_writable_readonly

Static Public Attributes

string source_bytes = "XabcdefY"
 ro_type = bytes
 rw_type = bytearray
 getitem_type = bytes
int itemsize = 1
string format = 'B'
string source_bytes = "abcdef"

Detailed Description

Definition at line 388 of file test_memoryview.py.


Member Function Documentation

Definition at line 170 of file test_memoryview.py.

00170 
00171     def check_attributes_with_type(self, tp):
00172         m = self._view(tp(self._source))
00173         self.assertEqual(m.format, self.format)
00174         self.assertEqual(m.itemsize, self.itemsize)
00175         self.assertEqual(m.ndim, 1)
00176         self.assertEqual(m.shape, (6,))
00177         self.assertEqual(len(m), 6)
00178         self.assertEqual(m.strides, (self.itemsize,))
00179         self.assertEqual(m.suboffsets, None)
00180         return m

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 26 of file test_memoryview.py.

00026 
00027     def check_getitem_with_type(self, tp):
00028         item = self.getitem_type
00029         b = tp(self._source)
00030         oldrefcount = sys.getrefcount(b)
00031         m = self._view(b)
00032         self.assertEqual(m[0], item(b"a"))
00033         self.assertIsInstance(m[0], bytes)
00034         self.assertEqual(m[5], item(b"f"))
00035         self.assertEqual(m[-1], item(b"f"))
00036         self.assertEqual(m[-6], item(b"a"))
00037         # Bounds checking
00038         self.assertRaises(IndexError, lambda: m[6])
00039         self.assertRaises(IndexError, lambda: m[-7])
00040         self.assertRaises(IndexError, lambda: m[sys.maxsize])
00041         self.assertRaises(IndexError, lambda: m[-sys.maxsize])
00042         # Type checking
00043         self.assertRaises(TypeError, lambda: m[None])
00044         self.assertRaises(TypeError, lambda: m[0.0])
00045         self.assertRaises(TypeError, lambda: m["a"])
00046         m = None
00047         self.assertEqual(sys.getrefcount(b), oldrefcount)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 181 of file test_memoryview.py.

00181 
00182     def test_attributes_readonly(self):
00183         if not self.ro_type:
00184             return
00185         m = self.check_attributes_with_type(self.ro_type)
00186         self.assertEqual(m.readonly, True)

Here is the call graph for this function:

Definition at line 187 of file test_memoryview.py.

00187 
00188     def test_attributes_writable(self):
00189         if not self.rw_type:
00190             return
00191         m = self.check_attributes_with_type(self.rw_type)
00192         self.assertEqual(m.readonly, False)

Here is the call graph for this function:

Definition at line 140 of file test_memoryview.py.

00140 
00141     def test_compare(self):
00142         # memoryviews can compare for equality with other objects
00143         # having the buffer interface.
00144         for tp in self._types:
00145             m = self._view(tp(self._source))
00146             for tp_comp in self._types:
00147                 self.assertTrue(m == tp_comp(b"abcdef"))
00148                 self.assertFalse(m != tp_comp(b"abcdef"))
00149                 self.assertFalse(m == tp_comp(b"abcde"))
00150                 self.assertTrue(m != tp_comp(b"abcde"))
00151                 self.assertFalse(m == tp_comp(b"abcde1"))
00152                 self.assertTrue(m != tp_comp(b"abcde1"))
00153             self.assertTrue(m == m)
00154             self.assertTrue(m == m[:])
00155             self.assertTrue(m[0:6] == m[:])
00156             self.assertFalse(m[0:5] == m)
00157 
00158             # Comparison with objects which don't support the buffer API
00159             self.assertFalse(m == "abcdef")
00160             self.assertTrue(m != "abcdef")
00161             self.assertFalse("abcdef" == m)
00162             self.assertTrue("abcdef" != m)
00163 
00164             # Unordered comparisons
00165             for c in (m, b"abcdef"):
00166                 self.assertRaises(TypeError, lambda: m < c)
00167                 self.assertRaises(TypeError, lambda: c <= m)
00168                 self.assertRaises(TypeError, lambda: m >= c)
00169                 self.assertRaises(TypeError, lambda: c > m)

Here is the call graph for this function:

Definition at line 253 of file test_memoryview.py.

00253 
00254     def test_contextmanager(self):
00255         for tp in self._types:
00256             b = tp(self._source)
00257             m = self._view(b)
00258             with m as cm:
00259                 self.assertIs(cm, m)
00260             self._check_released(m, tp)
00261             m = self._view(b)
00262             # Can release explicitly inside the context manager
00263             with m:
00264                 m.release()

Here is the call graph for this function:

Definition at line 115 of file test_memoryview.py.

00115 
00116     def test_delitem(self):
00117         for tp in self._types:
00118             b = tp(self._source)
00119             m = self._view(b)
00120             with self.assertRaises(TypeError):
00121                 del m[1]
00122             with self.assertRaises(TypeError):
00123                 del m[1:4]

Here is the call graph for this function:

Definition at line 206 of file test_memoryview.py.

00206 
00207     def test_gc(self):
00208         for tp in self._types:
00209             if not isinstance(tp, type):
00210                 # If tp is a factory rather than a plain type, skip
00211                 continue
00212 
00213             class MySource(tp):
00214                 pass
00215             class MyObject:
00216                 pass
00217 
00218             # Create a reference cycle through a memoryview object
00219             b = MySource(tp(b'abc'))
00220             m = self._view(b)
00221             o = MyObject()
00222             b.m = m
00223             b.o = o
00224             wr = weakref.ref(o)
00225             b = m = o = None
00226             # The cycle must be broken
00227             gc.collect()
00228             self.assertTrue(wr() is None, wr())

Here is the call graph for this function:

Reimplemented in test.test_memoryview.BaseArrayMemoryTests.

Definition at line 193 of file test_memoryview.py.

00193 
00194     def test_getbuffer(self):
00195         # Test PyObject_GetBuffer() on a memoryview object.
00196         for tp in self._types:
00197             b = tp(self._source)
00198             oldrefcount = sys.getrefcount(b)
00199             m = self._view(b)
00200             oldviewrefcount = sys.getrefcount(m)
00201             s = str(m, "utf-8")
00202             self._check_contents(tp, b, s.encode("utf-8"))
00203             self.assertEqual(sys.getrefcount(m), oldviewrefcount)
00204             m = None
00205             self.assertEqual(sys.getrefcount(b), oldrefcount)

Here is the call graph for this function:

Definition at line 48 of file test_memoryview.py.

00048 
00049     def test_getitem(self):
00050         for tp in self._types:
00051             self.check_getitem_with_type(tp)

Here is the call graph for this function:

Definition at line 52 of file test_memoryview.py.

00052 
00053     def test_iter(self):
00054         for tp in self._types:
00055             b = tp(self._source)
00056             m = self._view(b)
00057             self.assertEqual(list(m), [m[i] for i in range(len(m))])

Here is the call graph for this function:

Definition at line 265 of file test_memoryview.py.

00265 
00266     def test_release(self):
00267         for tp in self._types:
00268             b = tp(self._source)
00269             m = self._view(b)
00270             m.release()
00271             self._check_released(m, tp)
00272             # Can be called a second time (it's a no-op)
00273             m.release()
00274             self._check_released(m, tp)

Here is the call graph for this function:

Definition at line 58 of file test_memoryview.py.

00058 
00059     def test_setitem_readonly(self):
00060         if not self.ro_type:
00061             return
00062         b = self.ro_type(self._source)
00063         oldrefcount = sys.getrefcount(b)
00064         m = self._view(b)
00065         def setitem(value):
00066             m[0] = value
00067         self.assertRaises(TypeError, setitem, b"a")
00068         self.assertRaises(TypeError, setitem, 65)
00069         self.assertRaises(TypeError, setitem, memoryview(b"a"))
00070         m = None
00071         self.assertEqual(sys.getrefcount(b), oldrefcount)

Here is the call graph for this function:

Definition at line 72 of file test_memoryview.py.

00072 
00073     def test_setitem_writable(self):
00074         if not self.rw_type:
00075             return
00076         tp = self.rw_type
00077         b = self.rw_type(self._source)
00078         oldrefcount = sys.getrefcount(b)
00079         m = self._view(b)
00080         m[0] = tp(b"0")
00081         self._check_contents(tp, b, b"0bcdef")
00082         m[1:3] = tp(b"12")
00083         self._check_contents(tp, b, b"012def")
00084         m[1:1] = tp(b"")
00085         self._check_contents(tp, b, b"012def")
00086         m[:] = tp(b"abcdef")
00087         self._check_contents(tp, b, b"abcdef")
00088 
00089         # Overlapping copies of a view into itself
00090         m[0:3] = m[2:5]
00091         self._check_contents(tp, b, b"cdedef")
00092         m[:] = tp(b"abcdef")
00093         m[2:5] = m[0:3]
00094         self._check_contents(tp, b, b"ababcf")
00095 
00096         def setitem(key, value):
00097             m[key] = tp(value)
00098         # Bounds checking
00099         self.assertRaises(IndexError, setitem, 6, b"a")
00100         self.assertRaises(IndexError, setitem, -7, b"a")
00101         self.assertRaises(IndexError, setitem, sys.maxsize, b"a")
00102         self.assertRaises(IndexError, setitem, -sys.maxsize, b"a")
00103         # Wrong index/slice types
00104         self.assertRaises(TypeError, setitem, 0.0, b"a")
00105         self.assertRaises(TypeError, setitem, (0,), b"a")
00106         self.assertRaises(TypeError, setitem, "a", b"a")
00107         # Trying to resize the memory object
00108         self.assertRaises(ValueError, setitem, 0, b"")
00109         self.assertRaises(ValueError, setitem, 0, b"ab")
00110         self.assertRaises(ValueError, setitem, slice(1,1), b"a")
00111         self.assertRaises(ValueError, setitem, slice(0,2), b"a")
00112 
00113         m = None
00114         self.assertEqual(sys.getrefcount(b), oldrefcount)

Here is the call graph for this function:

Definition at line 124 of file test_memoryview.py.

00124 
00125     def test_tobytes(self):
00126         for tp in self._types:
00127             m = self._view(tp(self._source))
00128             b = m.tobytes()
00129             # This calls self.getitem_type() on each separate byte of b"abcdef"
00130             expected = b"".join(
00131                 self.getitem_type(bytes([c])) for c in b"abcdef")
00132             self.assertEqual(b, expected)
00133             self.assertIsInstance(b, bytes)

Here is the call graph for this function:

Reimplemented in test.test_memoryview.BaseArrayMemoryTests.

Definition at line 134 of file test_memoryview.py.

00134 
00135     def test_tolist(self):
00136         for tp in self._types:
00137             m = self._view(tp(self._source))
00138             l = m.tolist()
00139             self.assertEqual(l, list(b"abcdef"))

Here is the call graph for this function:

Definition at line 275 of file test_memoryview.py.

00275 
00276     def test_writable_readonly(self):
00277         # Issue #10451: memoryview incorrectly exposes a readonly
00278         # buffer as writable causing a segfault if using mmap
00279         tp = self.ro_type
00280         if tp is None:
00281             return
00282         b = tp(self._source)
00283         m = self._view(b)
00284         i = io.BytesIO(b'ZZZZ')
00285         self.assertRaises(TypeError, i.readinto, m)
00286 
00287 # Variations on source objects for the buffer: bytes-like objects, then arrays
00288 # with itemsize > 1.
00289 # NOTE: support for multi-dimensional objects is unimplemented.

Here is the call graph for this function:


Member Data Documentation

Definition at line 295 of file test_memoryview.py.

Definition at line 293 of file test_memoryview.py.

Definition at line 294 of file test_memoryview.py.

Definition at line 291 of file test_memoryview.py.

test.test_memoryview.BaseBytesMemoryTests.rw_type = bytearray [static, inherited]

Definition at line 292 of file test_memoryview.py.

Definition at line 16 of file test_memoryview.py.

Definition at line 342 of file test_memoryview.py.


The documentation for this class was generated from the following file: