[Python-checkins] cpython (3.3): Issue #19014: memoryview.cast() is now allowed on zero-length views.

antoine.pitrou python-checkins at python.org
Thu Oct 3 19:59:02 CEST 2013


http://hg.python.org/cpython/rev/b08e092df155
changeset:   85939:b08e092df155
branch:      3.3
parent:      85937:5950dd4cd9ef
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Thu Oct 03 19:55:41 2013 +0200
summary:
  Issue #19014: memoryview.cast() is now allowed on zero-length views.

files:
  Lib/test/test_buffer.py |  13 ++++++++++---
  Misc/NEWS               |   2 ++
  Objects/memoryobject.c  |   2 +-
  3 files changed, 13 insertions(+), 4 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
@@ -2432,15 +2432,22 @@
         self.assertRaises(ValueError, get_contiguous, nd[::-1], PyBUF_READ, 'C')
 
     def test_memoryview_cast_zero_shape(self):
-        # Casts are undefined if shape contains zeros. These arrays are
-        # regarded as C-contiguous by Numpy and PyBuffer_GetContiguous(),
-        # so they are not caught by the test for C-contiguity in memory_cast().
+        # Casts are undefined if buffer is multidimensional and shape
+        # contains zeros. These arrays are regarded as C-contiguous by
+        # Numpy and PyBuffer_GetContiguous(), so they are not caught by
+        # the test for C-contiguity in memory_cast().
         items = [1,2,3]
         for shape in ([0,3,3], [3,0,3], [0,3,3]):
             ex = ndarray(items, shape=shape)
             self.assertTrue(ex.c_contiguous)
             msrc = memoryview(ex)
             self.assertRaises(TypeError, msrc.cast, 'c')
+        # Monodimensional empty view can be cast (issue #19014).
+        for fmt, _, _ in iter_format(1, 'memoryview'):
+            msrc = memoryview(b'')
+            m = msrc.cast(fmt)
+            self.assertEqual(m.tobytes(), b'')
+            self.assertEqual(m.tolist(), [])
 
     def test_memoryview_struct_module(self):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------
 
+- Issue #19014: memoryview.cast() is now allowed on zero-length views.
+
 - Issue #19098: Prevent overflow in the compiler when the recursion limit is set
   absurdly high.
 
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -1330,7 +1330,7 @@
             "memoryview: casts are restricted to C-contiguous views");
         return NULL;
     }
-    if (zero_in_shape(self)) {
+    if ((shape || self->view.ndim != 1) && zero_in_shape(self)) {
         PyErr_SetString(PyExc_TypeError,
             "memoryview: cannot cast view with zeros in shape or strides");
         return NULL;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list