[Python-checkins] cpython (3.5): Iaaue #25598: Fix memory_hex from #9951 for non-contiguous buffers.

stefan.krah python-checkins at python.org
Tue Nov 10 12:19:08 EST 2015


https://hg.python.org/cpython/rev/f3d8bb3ffa98
changeset: 99046:f3d8bb3ffa98
branch: 3.5
parent: 99043:db168611c6c8
user: Stefan Krah <skrah at bytereef.org>
date: Tue Nov 10 18:17:22 2015 +0100
summary:
 Iaaue #25598: Fix memory_hex from #9951 for non-contiguous buffers.
files:
 Lib/test/test_buffer.py | 5 +++++
 Lib/test/test_memoryview.py | 7 +++++++
 Objects/memoryobject.c | 17 ++++++++++++++++-
 3 files changed, 28 insertions(+), 1 deletions(-)
diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py
--- a/Lib/test/test_buffer.py
+++ b/Lib/test/test_buffer.py
@@ -841,6 +841,11 @@
 # 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'
diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py
--- a/Lib/test/test_memoryview.py
+++ b/Lib/test/test_memoryview.py
@@ -512,6 +512,13 @@
 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()
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -2156,8 +2156,23 @@
 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 *
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /