6
6
static int
7
7
memory_getbuf (PyMemoryViewObject * self , Py_buffer * view , int flags )
8
8
{
9
- if (view != NULL )
9
+ if (view != NULL )
10
10
* view = self -> view ;
11
- return self -> base -> ob_type -> tp_as_buffer -> bf_getbuffer (self -> base ,
12
- NULL , PyBUF_FULL );
11
+ return self -> base -> ob_type -> tp_as_buffer -> bf_getbuffer (self -> base , NULL ,
12
+ PyBUF_FULL );
13
13
}
14
14
15
15
static void
16
- memory_releasebuf (PyMemoryViewObject * self , Py_buffer * view )
16
+ memory_releasebuf (PyMemoryViewObject * self , Py_buffer * view )
17
17
{
18
18
PyObject_ReleaseBuffer (self -> base , NULL );
19
19
}
@@ -42,16 +42,16 @@ PyMemoryView_FromObject(PyObject *base)
42
42
PyMemoryViewObject * mview ;
43
43
44
44
if (!PyObject_CheckBuffer (base )) {
45
- PyErr_SetString (PyExc_TypeError ,
46
- "cannot make memory view because object does " \
8000
45
+ PyErr_SetString (PyExc_TypeError ,
46
+ "cannot make memory view because object does "
47
47
"not have the buffer interface" );
48
- return NULL ;
48
+ return NULL ;
49
49
}
50
-
51
- mview = (PyMemoryViewObject * )PyObject_New (PyMemoryViewObject ,
50
+
51
+ mview = (PyMemoryViewObject * )PyObject_New (PyMemoryViewObject ,
52
52
& PyMemoryView_Type );
53
53
if (mview == NULL ) return NULL ;
54
-
54
+
55
55
mview -> base = NULL ;
56
56
if (PyObject_GetBuffer (base , & (mview -> view ), PyBUF_FULL ) < 0 ) {
57
57
Py_DECREF (mview );
@@ -69,12 +69,12 @@ memory_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)
69
69
PyObject * obj ;
70
70
if (!PyArg_UnpackTuple (args , "memoryview" , 1 , 1 , & obj )) return NULL ;
71
71
72
- return PyMemoryView_FromObject (obj );
72
+ return PyMemoryView_FromObject (obj );
73
73
}
74
74
75
75
76
76
static void
77
- _strided_copy_nd (char * dest , char * src , int nd , Py_ssize_t * shape ,
77
+ _strided_copy_nd (char * dest , char * src , int nd , Py_ssize_t * shape ,
78
78
Py_ssize_t * strides , int itemsize , char fort )
79
79
{
80
80
int k ;
@@ -92,23 +92,23 @@ _strided_copy_nd(char *dest, char *src, int nd, Py_ssize_t *shape,
92
92
}
93
93
else {
94
94
if (fort == 'F' ) {
95
- /* Copy first dimension first,
95
+ /* Copy first dimension first,
96
96
second dimension second, etc...
97
97
Set up the recursive loop backwards so that final
98
- dimension is actually copied last.
98
+ dimension is actually copied last.
99
99
*/
100
100
outstride = itemsize ;
101
101
for (k = 1 ; k < nd - 1 ;k ++ ) {
102
102
outstride *= shape [k ];
103
103
}
104
104
for (k = 0 ; k < shape [nd - 1 ]; k ++ ) {
105
- _strided_copy_nd (dest , src , nd - 1 , shape ,
105
+ _strided_copy_nd (dest , src , nd - 1 , shape ,
106
106
strides , itemsize , fort );
107
107
dest += outstride ;
108
108
src += strides [nd - 1 ];
109
109
}
110
110
}
111
-
111
+
112
112
else {
113
113
/* Copy last dimension first,
114
114
second-to-last dimension second, etc.
@@ -121,7 +121,7 @@ _strided_copy_nd(char *dest, char *src, int nd, Py_ssize_t *shape,
121
121
}
122
122
for (k = 0 ; k < shape [0 ]; k ++ ) {
123
123
_strided_copy_nd (dest , src , nd - 1 , shape + 1 ,
124
- strides + 1 , itemsize ,
124
+ strides + 1 , itemsize ,
125
125
fort );
126
126
dest += outstride ;
127
127
src += strides [0 ];
@@ -142,8 +142,8 @@ _indirect_copy_nd(char *dest, Py_buffer *view, char fort)
142
142
Py_ssize_t elements ;
143
143
char * ptr ;
144
144
void (* func )(int , Py_ssize_t * , Py_ssize_t * );
145
-
146
-
145
+
146
+
147
147
/* XXX(nnorwitz): need to check for overflow! */
148
148
indices = (Py_ssize_t * )PyMem_Malloc (sizeof (Py_ssize_t )* view -> ndim );
149
149
if (indices == NULL ) {
@@ -153,7 +153,7 @@ _indirect_copy_nd(char *dest, Py_buffer *view, char fort)
153
153
for (k = 0 ; k < view -> ndim ;k ++ ) {
154
154
indices [k ] = 0 ;
155
155
}
156
-
156
+
157
157
elements = 1 ;
158
158
for (k = 0 ; k < view -> ndim ; k ++ ) {
159
159
elements *= view -> shape [k ];
@@ -170,26 +170,26 @@ _indirect_copy_nd(char *dest, Py_buffer *view, char fort)
170
170
memcpy (dest , ptr , view -> itemsize );
171
171
dest += view -> itemsize ;
172
172
}
173
-
173
+
174
174
PyMem_Free (indices );
175
175
return 0 ;
176
176
}
177
177
178
- /*
178
+ /*
179
179
Get a the data from an object as a contiguous chunk of memory (in
180
180
either 'C' or 'F'ortran order) even if it means copying it into a
181
181
separate memory area.
182
182
183
183
Returns a new reference to a Memory view object. If no copy is needed,
184
- the memory view object points to the original memory and holds a
184
+ the memory view object points to the original memory and holds a
185
185
lock on the original. If a copy is needed, then the memory view object
186
- points to a brand-new Bytes object (and holds a memory lock on it).
186
+ points to a brand-new Bytes object (and holds a memory lock on it).
187
187
188
188
buffertype
189
189
190
190
PyBUF_READ buffer only needs to be read-only
191
191
PyBUF_WRITE buffer needs to be writable (give error if not contiguous)
192
- PyBUF_SHADOW buffer needs to be writable so shadow it with
192
+ PyBUF_SHADOW buffer needs to be writable so shadow it with
193
193
a contiguous buffer if it is not. The view will point to
194
194
the shadow buffer which can be written to and then
195
195
will be copied back into the other buffer when the memory
@@ -210,7 +210,7 @@ PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort)
210
210
"object does not have the buffer interface" );
211
211
return NULL ;
212
212
}
213
-
213
+
214
214
mem = PyObject_New (PyMemoryViewObject , & PyMemoryView_Type );
215
215
if (mem == NULL ) return NULL ;
216
216
@@ -240,8 +240,8 @@ PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort)
240
240
if (buffertype == PyBUF_WRITE ) {
241
241
PyObject_DEL (mem );
242
242
PyErr_SetString (PyExc_BufferError ,
243
- "writable contiguous buffer requested for a non-contiguous" \
244
- "object ." );
243
+ "writable contiguous buffer requested "
244
+ "for a non-contiguousobject ." );
245
245
return NULL ;
246
246
}
247
247
bytes = PyBytes_FromStringAndSize (NULL , view -> len );
@@ -255,15 +255,15 @@ PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort)
255
255
*/
256
256
/* strided or in-direct copy */
257
257
if (view -> suboffsets == NULL ) {
258
- _strided_copy_nd (dest , view -> buf , view -> ndim , view -> shape ,
259
- view -> strides , view -> itemsize , fort );
258
+ _strided_copy_nd (dest , view -> buf , view -> ndim , view -> shape ,
259
+ view -> strides , view -> itemsize , fort );
260
260
}
261
261
else {
262
262
if (_indirect_copy_nd (dest , view , fort ) < 0 ) {
263
263
Py_DECREF (bytes );
264
264
PyObject_ReleaseBuffer (obj , view );
265
265
return NULL ;
266
- }
266
+ }
267
267
}
268
268
if (buffertype == PyBUF_SHADOW ) {
269
269
/* return a shadowed memory-view object */
@@ -356,7 +356,7 @@ memory_ndim_get(PyMemoryViewObject *self)
356
356
return PyInt_FromLong (self -> view .ndim );
357
357
}
358
358
359
- static PyGetSetDef memory_getsetlist [] = {
359
+ static PyGetSetDef memory_getsetlist [] = {
360
360
{"format" , (getter )memory_format_get , NULL , NULL },
361
361
{"itemsize" , (getter )memory_itemsize_get , NULL , NULL },
362
362
{"shape" , (getter )memory_shape_get , NULL , NULL },
@@ -379,7 +379,7 @@ static PyObject *
379
379
memory_tolist (PyMemoryViewObject * mem , PyObject * noargs )
380
380
{
381
381
/* This should construct a (nested) list of unpacked objects
382
- possibly using the struct module.
382
+ possibly using the struct module.
383
383
*/
384
384
Py_INCREF (Py_NotImplemented );
385
385
return Py_NotImplemented ;
@@ -403,16 +403,16 @@ memory_dealloc(PyMemoryViewObject *self)
403
403
with buffer interface and the second element is a
404
404
contiguous "shadow" that must be copied back into
405
405
the data areay of the first tuple element before
406
- releasing the buffer on the first element.
406
+ releasing the buffer on the first element.
407
407
*/
408
-
408
+
409
409
PyObject_CopyData (PyTuple_GET_ITEM (self -> base ,0 ),
410
410
PyTuple_GET_ITEM (self -> base ,1 ));
411
411
412
412
/* The view member should have readonly == -1 in
413
413
this instance indicating that the memory can
414
414
be "locked" and was locked and will be unlocked
415
- again after this call.
415
+ again after this call.
416
416
*/
417
417
PyObject_ReleaseBuffer (PyTuple_GET_ITEM (self -> base ,0 ),
418
418
& (self -> view ));
@@ -444,7 +444,7 @@ memory_str(PyMemoryViewObject *self)
444
444
445
445
if (PyObject_GetBuffer ((PyObject * )self , & view , PyBUF_FULL ) < 0 )
446
446
return NULL ;
447
-
447
+
448
448
res = PyBytes_FromStringAndSize (NULL , view .len );
449
449
PyBuffer_ToContiguous (PyBytes_AS_STRING (res ), & view , view .len , 'C' );
450
450
PyObject_ReleaseBuffer ((PyObject * )self , & view );
@@ -464,13 +464,13 @@ memory_length(PyMemoryViewObject *self)
464
464
return view .len ;
465
465
}
466
466
467
- /*
467
+ /*
468
468
mem[obj] returns a bytes object holding the data for one element if
469
469
obj fully indexes the memory view or another memory-view object
470
470
if it does not.
471
-
471
+
472
472
0-d memory-view objects can be referenced using ... or () but
473
- not with anything else.
473
+ not with anything else.
474
474
*/
475
475
static PyObject *
476
476
memory_subscript (PyMemoryViewObject * self , PyObject * key )
@@ -485,7 +485,8 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
485
485
return (PyObject * )self ;
486
486
}
487
487
else {
488
- PyErr_SetString (PyExc_IndexError , "invalid indexing of 0-dim memory" );
488
+ PyErr_SetString (PyExc_IndexError ,
489
+ "invalid indexing of 0-dim memory" );
489
490
return NULL ;
490
491
}
491
492
}
@@ -498,23 +499,25 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
498
499
/* Return a bytes object */
499
500
char * ptr ;
500
501
ptr = (char * )view -> buf ;
501
- if (view -> strides == NULL )
502
+ if (view -> strides == NULL )
502
503
ptr += view -> itemsize * result ;
503
504
else
504
505
ptr += view -> strides [0 ] * result ;
505
- if (view -> suboffsets != NULL && view -> suboffsets [0 ] >= 0 ) {
506
+ if (view -> suboffsets != NULL &&
507
+ view -> suboffsets [0 ] >= 0 )
508
+ {
506
509
ptr = * ((char * * )ptr ) + view -> suboffsets [0 ];
507
510
}
508
511
return PyBytes_FromStringAndSize (ptr , view -> itemsize );
509
512
}
510
513
else {
511
- /* Return a new memory-view object */
514
+ /* Return a new memory-view object */
512
515
Py_buffer newview ;
513
516
PyMemoryView_FromMemory (& newview );
514
517
}
515
518
}
516
-
517
-
519
+
520
+
518
521
519
522
Py_INCREF (Py_NotImplemented );
520
523
return Py_NotImplemented ;
@@ -570,7 +573,7 @@ PyTypeObject PyMemoryView_Type = {
570
573
0 , /* tp_weaklistoffset */
571
574
0 , /* tp_iter */
572
575
0 , /* tp_iternext */
573
- memory_methods , /* tp_methods */
576
+ memory_methods , /* tp_methods */
574
577
0 , /* tp_members */
575
578
memory_getsetlist , /* tp_getset */
576
579
0 , /* tp_base */
0 commit comments