10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Unit tests for the memoryview
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoXXX We need more tests! Some tests are in test_bytes
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport gc
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport weakref
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport array
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport io
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass AbstractMemoryTests:
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    source_bytes = b"abcdef"
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @property
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _source(self):
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.source_bytes
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @property
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _types(self):
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return filter(None, [self.ro_type, self.rw_type])
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def check_getitem_with_type(self, tp):
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        item = self.getitem_type
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        b = tp(self._source)
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        oldrefcount = sys.getrefcount(b)
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = self._view(b)
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m[0], item(b"a"))
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(m[0], bytes)
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m[5], item(b"f"))
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m[-1], item(b"f"))
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m[-6], item(b"a"))
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Bounds checking
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(IndexError, lambda: m[6])
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(IndexError, lambda: m[-7])
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(IndexError, lambda: m[sys.maxsize])
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(IndexError, lambda: m[-sys.maxsize])
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Type checking
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, lambda: m[None])
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, lambda: m[0.0])
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, lambda: m["a"])
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = None
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(sys.getrefcount(b), oldrefcount)
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_getitem(self):
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.check_getitem_with_type(tp)
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_iter(self):
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            b = tp(self._source)
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m = self._view(b)
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(list(m), [m[i] for i in range(len(m))])
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_repr(self):
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            b = tp(self._source)
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m = self._view(b)
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertIsInstance(m.__repr__(), str)
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_setitem_readonly(self):
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not self.ro_type:
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        b = self.ro_type(self._source)
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        oldrefcount = sys.getrefcount(b)
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = self._view(b)
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def setitem(value):
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m[0] = value
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, setitem, b"a")
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, setitem, 65)
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, setitem, memoryview(b"a"))
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = None
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(sys.getrefcount(b), oldrefcount)
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_setitem_writable(self):
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not self.rw_type:
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tp = self.rw_type
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        b = self.rw_type(self._source)
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        oldrefcount = sys.getrefcount(b)
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = self._view(b)
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m[0] = tp(b"0")
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._check_contents(tp, b, b"0bcdef")
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m[1:3] = tp(b"12")
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._check_contents(tp, b, b"012def")
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m[1:1] = tp(b"")
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._check_contents(tp, b, b"012def")
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m[:] = tp(b"abcdef")
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._check_contents(tp, b, b"abcdef")
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Overlapping copies of a view into itself
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m[0:3] = m[2:5]
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._check_contents(tp, b, b"cdedef")
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m[:] = tp(b"abcdef")
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m[2:5] = m[0:3]
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._check_contents(tp, b, b"ababcf")
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def setitem(key, value):
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m[key] = tp(value)
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Bounds checking
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(IndexError, setitem, 6, b"a")
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(IndexError, setitem, -7, b"a")
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(IndexError, setitem, sys.maxsize, b"a")
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(IndexError, setitem, -sys.maxsize, b"a")
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Wrong index/slice types
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, setitem, 0.0, b"a")
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, setitem, (0,), b"a")
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, setitem, "a", b"a")
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Trying to resize the memory object
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, setitem, 0, b"")
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, setitem, 0, b"ab")
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, setitem, slice(1,1), b"a")
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, setitem, slice(0,2), b"a")
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = None
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(sys.getrefcount(b), oldrefcount)
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_delitem(self):
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            b = tp(self._source)
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m = self._view(b)
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with self.assertRaises(TypeError):
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                del m[1]
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with self.assertRaises(TypeError):
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                del m[1:4]
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_tobytes(self):
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m = self._view(tp(self._source))
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            b = m.tobytes()
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # This calls self.getitem_type() on each separate byte of b"abcdef"
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expected = b"".join(
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.getitem_type(c) for c in b"abcdef")
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(b, expected)
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertIsInstance(b, bytes)
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_tolist(self):
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m = self._view(tp(self._source))
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            l = m.tolist()
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(l, map(ord, b"abcdef"))
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_compare(self):
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # memoryviews can compare for equality with other objects
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # having the buffer interface.
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m = self._view(tp(self._source))
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for tp_comp in self._types:
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertTrue(m == tp_comp(b"abcdef"))
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertFalse(m != tp_comp(b"abcdef"))
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertFalse(m == tp_comp(b"abcde"))
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertTrue(m != tp_comp(b"abcde"))
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertFalse(m == tp_comp(b"abcde1"))
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertTrue(m != tp_comp(b"abcde1"))
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(m == m)
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(m == m[:])
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(m[0:6] == m[:])
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertFalse(m[0:5] == m)
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Comparison with objects which don't support the buffer API
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertFalse(m == u"abcdef")
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(m != u"abcdef")
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertFalse(u"abcdef" == m)
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(u"abcdef" != m)
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Unordered comparisons are unimplemented, and therefore give
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # arbitrary results (they raise a TypeError in py3k)
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def check_attributes_with_type(self, tp):
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = self._view(tp(self._source))
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m.format, self.format)
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(m.format, str)
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m.itemsize, self.itemsize)
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m.ndim, 1)
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m.shape, (6,))
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(len(m), 6)
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m.strides, (self.itemsize,))
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m.suboffsets, None)
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return m
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_attributes_readonly(self):
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not self.ro_type:
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = self.check_attributes_with_type(self.ro_type)
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m.readonly, True)
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_attributes_writable(self):
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not self.rw_type:
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = self.check_attributes_with_type(self.rw_type)
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(m.readonly, False)
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Disabled: unicode uses the old buffer API in 2.x
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #def test_getbuffer(self):
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ## Test PyObject_GetBuffer() on a memoryview object.
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #for tp in self._types:
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #b = tp(self._source)
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #oldrefcount = sys.getrefcount(b)
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #m = self._view(b)
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #oldviewrefcount = sys.getrefcount(m)
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #s = unicode(m, "utf-8")
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #self._check_contents(tp, b, s.encode("utf-8"))
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #self.assertEqual(sys.getrefcount(m), oldviewrefcount)
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #m = None
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #self.assertEqual(sys.getrefcount(b), oldrefcount)
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_gc(self):
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if not isinstance(tp, type):
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # If tp is a factory rather than a plain type, skip
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                continue
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            class MySource(tp):
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            class MyObject:
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Create a reference cycle through a memoryview object
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            b = MySource(tp(b'abc'))
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m = self._view(b)
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            o = MyObject()
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            b.m = m
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            b.o = o
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            wr = weakref.ref(o)
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            b = m = o = None
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # The cycle must be broken
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            gc.collect()
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(wr() is None, wr())
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_writable_readonly(self):
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Issue #10451: memoryview incorrectly exposes a readonly
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # buffer as writable causing a segfault if using mmap
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tp = self.ro_type
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if tp is None:
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        b = tp(self._source)
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = self._view(b)
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        i = io.BytesIO(b'ZZZZ')
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, i.readinto, m)
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Variations on source objects for the buffer: bytes-like objects, then arrays
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# with itemsize > 1.
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# NOTE: support for multi-dimensional objects is unimplemented.
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BaseBytesMemoryTests(AbstractMemoryTests):
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ro_type = bytes
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    rw_type = bytearray
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    getitem_type = bytes
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    itemsize = 1
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    format = 'B'
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Disabled: array.array() does not support the new buffer API in 2.x
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#class BaseArrayMemoryTests(AbstractMemoryTests):
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #ro_type = None
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #rw_type = lambda self, b: array.array('i', map(ord, b))
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #getitem_type = lambda self, b: array.array('i', map(ord, b)).tostring()
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #itemsize = array.array('i').itemsize
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #format = 'i'
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #def test_getbuffer(self):
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ## XXX Test should be adapted for non-byte buffers
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #pass
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #def test_tolist(self):
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ## XXX NotImplementedError: tolist() only supports byte views
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #pass
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Variations on indirection levels: memoryview, slice of memoryview,
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# slice of slice of memoryview.
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# This is important to test allocation subtleties.
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BaseMemoryviewTests:
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _view(self, obj):
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return memoryview(obj)
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _check_contents(self, tp, obj, contents):
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(obj, tp(contents))
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BaseMemorySliceTests:
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    source_bytes = b"XabcdefY"
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _view(self, obj):
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = memoryview(obj)
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return m[1:7]
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _check_contents(self, tp, obj, contents):
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(obj[1:7], tp(contents))
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_refs(self):
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m = memoryview(tp(self._source))
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            oldrefcount = sys.getrefcount(m)
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            m[1:2]
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(sys.getrefcount(m), oldrefcount)
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BaseMemorySliceSliceTests:
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    source_bytes = b"XabcdefY"
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _view(self, obj):
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = memoryview(obj)
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return m[:7][1:]
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _check_contents(self, tp, obj, contents):
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(obj[1:7], tp(contents))
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Concrete test classes
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BytesMemoryviewTest(unittest.TestCase,
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    BaseMemoryviewTests, BaseBytesMemoryTests):
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_constructor(self):
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for tp in self._types:
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            ob = tp(self._source)
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(memoryview(ob))
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(memoryview(object=ob))
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(TypeError, memoryview)
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(TypeError, memoryview, ob, ob)
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(TypeError, memoryview, argument=ob)
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(TypeError, memoryview, ob, argument=True)
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#class ArrayMemoryviewTest(unittest.TestCase,
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #BaseMemoryviewTests, BaseArrayMemoryTests):
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #def test_array_assign(self):
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ## Issue #4569: segfault when mutating a memoryview with itemsize != 1
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #a = array.array('i', range(10))
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #m = memoryview(a)
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #new_a = array.array('i', range(9, -1, -1))
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #m[:] = new_a
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #self.assertEqual(a, new_a)
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BytesMemorySliceTest(unittest.TestCase,
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    BaseMemorySliceTests, BaseBytesMemoryTests):
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pass
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#class ArrayMemorySliceTest(unittest.TestCase,
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #BaseMemorySliceTests, BaseArrayMemoryTests):
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #pass
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass BytesMemorySliceSliceTest(unittest.TestCase,
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    BaseMemorySliceSliceTests, BaseBytesMemoryTests):
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pass
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#class ArrayMemorySliceSliceTest(unittest.TestCase,
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #BaseMemorySliceSliceTests, BaseArrayMemoryTests):
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #pass
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main():
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_support.run_unittest(__name__)
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__":
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_main()
362