]> granicus.if.org Git - python/commitdiff
Iaaue #25598: Fix memory_hex from #9951 for non-contiguous buffers.
authorStefan Krah <skrah@bytereef.org>
Tue, 10 Nov 2015 17:17:22 +0000 (18:17 +0100)
committerStefan Krah <skrah@bytereef.org>
Tue, 10 Nov 2015 17:17:22 +0000 (18:17 +0100)
Lib/test/test_buffer.py
Lib/test/test_memoryview.py
Objects/memoryobject.c

index a65339041539d72a2e44ec9254ade9c002033060..2eef9fc68590013896644b3ae50a2fa8fd06e02b 100644 (file)
@@ -841,6 +841,11 @@ class TestBufferProtocol(unittest.TestCase):
                 # test tobytes()
                 self.assertEqual(result.tobytes(), b)
 
+                # test hex()
+                m = memoryview(result)
+                h = "".join("%02x" % c for c in b)
+                self.assertEqual(m.hex(), h)
+
                 # lst := expected multi-dimensional logical representation
                 # flatten(lst) := elements in C-order
                 ff = fmt if fmt else 'B'
index da01a84f27432974febbf2b8322693293aebcdb7..d2bab381042efe8ec41c5e361d94f1930438006f 100644 (file)
@@ -512,6 +512,13 @@ class OtherTest(unittest.TestCase):
                 m[2:] = memoryview(p6).cast(format)[2:]
                 self.assertEqual(d.value, 0.6)
 
+    def test_memoryview_hex(self):
+        # Issue #9951: memoryview.hex() segfaults with non-contiguous buffers.
+        x = b'0' * 200000
+        m1 = memoryview(x)
+        m2 = m1[::-1]
+        self.assertEqual(m2.hex(), '30' * 200000)
+
 
 if __name__ == "__main__":
     unittest.main()
index 74cad7dc0ef5d4dc4d0eb2fd8c774fe59e77e94e..fe543662c976968ba471ceea9ef6c2a3d99bbf26 100644 (file)
@@ -2156,8 +2156,23 @@ static PyObject *
 memory_hex(PyMemoryViewObject *self, PyObject *dummy)
 {
     Py_buffer *src = VIEW_ADDR(self);
+    PyObject *bytes;
+    PyObject *ret;
+
     CHECK_RELEASED(self);
-    return _Py_strhex(src->buf, src->len);
+
+    if (MV_C_CONTIGUOUS(self->flags)) {
+        return _Py_strhex(src->buf, src->len);
+    }
+
+    bytes = memory_tobytes(self, dummy);
+    if (bytes == NULL)
+        return NULL;
+
+    ret = _Py_strhex(PyBytes_AS_STRING(bytes), Py_SIZE(bytes));
+    Py_DECREF(bytes);
+
+    return ret;
 }
 
 static PyObject *