@@ -1338,16 +1338,17 @@ zero_in_shape(PyMemoryViewObject *mv)
13381338static PyObject *
13391339memory_cast (PyMemoryViewObject * self , PyObject * args , PyObject * kwds )
13401340{
1341- static char * kwlist [] = {"format" , "shape" , NULL };
1341+ static char * kwlist [] = {"format" , "shape" , "readonly" , NULL };
13421342 PyMemoryViewObject * mv = NULL ;
13431343 PyObject * shape = NULL ;
1344+ int readonly = self -> view .readonly ;
13441345 PyObject * format ;
13451346 Py_ssize_t ndim = 1 ;
13461347
13471348 CHECK_RELEASED (self );
13481349
1349- if (!PyArg_ParseTupleAndKeywords (args , kwds , "O|O" , kwlist ,
1350- & format , & shape )) {
1350+ if (!PyArg_ParseTupleAndKeywords (args , kwds , "O|O$p " , kwlist ,
1351+ & format , & shape , & readonly )) {
13511352 return NULL ;
13521353 }
13531354 if (!PyUnicode_Check (format )) {
@@ -1365,6 +1366,13 @@ memory_cast(PyMemoryViewObject *self, PyObject *args, PyObject *kwds)
13651366 "memoryview: cannot cast view with zeros in shape or strides" );
13661367 return NULL ;
13671368 }
1369+
1370+ if (self -> view .readonly && !readonly ){
1371+ PyErr_SetString (PyExc_TypeError ,
1372+ "memoryview: cannot cast readonly buffer to a writable buffer." );
1373+ return NULL ;
1374+ }
1375+
13681376 if (shape ) {
13691377 CHECK_LIST_OR_TUPLE (shape )
13701378 ndim = PySequence_Fast_GET_SIZE (shape );
@@ -1391,6 +1399,8 @@ memory_cast(PyMemoryViewObject *self, PyObject *args, PyObject *kwds)
13911399 if (shape && cast_to_ND (mv , shape , (int )ndim ) < 0 )
13921400 goto error ;
13931401
1402+ mv -> view .readonly = readonly ;
1403+
13941404 return (PyObject * )mv ;
13951405
13961406error :
0 commit comments