| 1 |
from pypy.interpreter.error import oefmt |
| 2 |
from pypy.module.cpyext.api import ( |
| 3 |
cpython_api, CANNOT_FAIL, PyBUF_MAX_NDIM, build_type_checkers, |
| 4 |
Py_ssize_tP, cts, parse_dir, bootstrap_function, Py_bufferP, slot_function, |
| 5 |
PyBUF_READ, PyBUF_WRITE) |
| 6 |
from pypy.module.cpyext.pyobject import ( |
| 7 |
PyObject, make_ref, decref, from_ref, make_typedescr, |
| 8 |
get_typedescr, track_reference) |
| 9 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 10 |
from rpython.rlib.rarithmetic import widen |
| 11 |
from pypy.interpreter.error import oefmt |
| 12 |
from pypy.objspace.std.memoryobject import W_MemoryView |
| 13 |
from pypy.module.cpyext.object import _dealloc |
| 14 |
from pypy.module.cpyext.import_ import PyImport_Import |
| 15 |
from pypy.module.cpyext.buffer import CPyBuffer, fq |
| 16 |
|
| 17 |
cts.parse_header(parse_dir / 'cpyext_memoryobject.h') |
| 18 |
PyMemoryViewObject = cts.gettype('PyMemoryViewObject*') |
| 19 |
|
| 20 |
PyMemoryView_Check, PyMemoryView_CheckExact = build_type_checkers("MemoryView") |
| 21 |
|
| 22 |
FORMAT_ALLOCATED = 0x04 |
| 23 |
|
| 24 |
@bootstrap_function |
| 25 |
def init_memoryobject(space): |
| 26 |
"Type description of PyDictObject" |
| 27 |
make_typedescr(W_MemoryView.typedef, |
| 28 |
basestruct=PyMemoryViewObject.TO, |
| 29 |
attach=memory_attach, |
| 30 |
dealloc=memory_dealloc, |
| 31 |
realize=memory_realize, |
| 32 |
) |
| 33 |
|
| 34 |
def memory_attach(space, py_obj, w_obj, w_userdata=None): |
| 35 |
""" |
| 36 |
Fills a newly allocated PyMemoryViewObject with the given W_MemoryView object. |
| 37 |
""" |
| 38 |
assert isinstance(w_obj, W_MemoryView) |
| 39 |
py_obj = rffi.cast(PyMemoryViewObject, py_obj) |
| 40 |
view = py_obj.c_view |
| 41 |
ndim = w_obj.getndim() |
| 42 |
if ndim >= PyBUF_MAX_NDIM: |
| 43 |
raise oefmt(space.w_RuntimeError, |
| 44 |
"cannot create PyMemoryViewObject with ndim (%d) > %d", |
| 45 |
ndim, PyBUF_MAX_NDIM) |
| 46 |
fill_Py_buffer(space, w_obj.view, view) |
| 47 |
try: |
| 48 |
view.c_buf = rffi.cast(rffi.VOIDP, w_obj.view.get_raw_address()) |
| 49 |
|
| 50 |
|
| 51 |
view.c_obj = make_ref(space, w_obj) |
| 52 |
rffi.setintfield(view, 'c_readonly', |
| 53 |
rffi.cast(rffi.INT_real, w_obj.view.readonly)) |
| 54 |
except ValueError: |
| 55 |
w_s = w_obj.descr_tobytes(space) |
| 56 |
view.c_obj = make_ref(space, w_s) |
| 57 |
view.c_buf = rffi.cast(rffi.VOIDP, rffi.str2charp(space.bytes_w(w_s), |
| 58 |
track_allocation=False)) |
| 59 |
rffi.setintfield(view, 'c_readonly', 1) |
| 60 |
|
| 61 |
def memory_realize(space, obj): |
| 62 |
""" |
| 63 |
Creates the memory object in the interpreter |
| 64 |
""" |
| 65 |
py_mem = rffi.cast(PyMemoryViewObject, obj) |
| 66 |
view = py_mem.c_view |
| 67 |
ndim = widen(view.c_ndim) |
| 68 |
shape = None |
| 69 |
if view.c_shape: |
| 70 |
shape = [view.c_shape[i] for i in range(ndim)] |
| 71 |
strides = None |
| 72 |
if view.c_strides: |
| 73 |
strides = [view.c_strides[i] for i in range(ndim)] |
| 74 |
format = 'B' |
| 75 |
if view.c_format: |
| 76 |
format = rffi.charp2str(view.c_format) |
| 77 |
buf = CPyBuffer(space, view.c_buf, view.c_len, from_ref(space, view.c_obj), |
| 78 |
format=format, shape=shape, strides=strides, |
| 79 |
ndim=ndim, itemsize=view.c_itemsize, |
| 80 |
readonly=widen(view.c_readonly)) |
| 81 |
|
| 82 |
fq.register_finalizer(buf) |
| 83 |
|
| 84 |
w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type)) |
| 85 |
w_obj = space.allocate_instance(W_MemoryView, w_type) |
| 86 |
w_obj.__init__(buf) |
| 87 |
track_reference(space, obj, w_obj) |
| 88 |
return w_obj |
| 89 |
|
| 90 |
@slot_function([PyObject], lltype.Void) |
| 91 |
def memory_dealloc(space, py_obj): |
| 92 |
mem_obj = rffi.cast(PyMemoryViewObject, py_obj) |
| 93 |
view = mem_obj.c_view |
| 94 |
if view.c_obj: |
| 95 |
decref(space, view.c_obj) |
| 96 |
view.c_obj = rffi.cast(PyObject, 0) |
| 97 |
flags = widen(view.c_flags) |
| 98 |
if flags & FORMAT_ALLOCATED == FORMAT_ALLOCATED: |
| 99 |
lltype.free(view.c_format, flavor='raw') |
| 100 |
_dealloc(space, py_obj) |
| 101 |
|
| 102 |
def fill_Py_buffer(space, buf, view): |
| 103 |
|
| 104 |
ndim = buf.getndim() |
| 105 |
view.c_len = buf.getlength() |
| 106 |
view.c_itemsize = buf.getitemsize() |
| 107 |
rffi.setintfield(view, 'c_ndim', ndim) |
| 108 |
fmt = buf.getformat() |
| 109 |
n = len(fmt) |
| 110 |
view.c_format = lltype.malloc(rffi.CCHARP.TO, n + 1, flavor='raw', |
| 111 |
add_memory_pressure=True) |
| 112 |
flags = widen(view.c_flags) |
| 113 |
flags |= FORMAT_ALLOCATED |
| 114 |
view.c_flags = rffi.cast(rffi.INT_real, flags) |
| 115 |
for i in range(n): |
| 116 |
view.c_format[i] = fmt[i] |
| 117 |
view.c_format[n] = '\x00' |
| 118 |
if ndim > 0: |
| 119 |
view.c_shape = rffi.cast(Py_ssize_tP, view.c__shape) |
| 120 |
view.c_strides = rffi.cast(Py_ssize_tP, view.c__strides) |
| 121 |
shape = buf.getshape() |
| 122 |
strides = buf.getstrides() |
| 123 |
for i in range(ndim): |
| 124 |
view.c_shape[i] = shape[i] |
| 125 |
view.c_strides[i] = strides[i] |
| 126 |
else: |
| 127 |
view.c_shape = lltype.nullptr(Py_ssize_tP.TO) |
| 128 |
view.c_strides = lltype.nullptr(Py_ssize_tP.TO) |
| 129 |
view.c_suboffsets = lltype.nullptr(Py_ssize_tP.TO) |
| 130 |
view.c_internal = lltype.nullptr(rffi.VOIDP.TO) |
| 131 |
return 0 |
| 132 |
|
| 133 |
def _IsFortranContiguous(view): |
| 134 |
ndim = widen(view.c_ndim) |
| 135 |
if ndim == 0: |
| 136 |
return 1 |
| 137 |
if not view.c_strides: |
| 138 |
return ndim == 1 |
| 139 |
sd = view.c_itemsize |
| 140 |
if ndim == 1: |
| 141 |
return view.c_shape[0] == 1 or sd == view.c_strides[0] |
| 142 |
for i in range(view.c_ndim): |
| 143 |
dim = view.c_shape[i] |
| 144 |
if dim == 0: |
| 145 |
return 1 |
| 146 |
if view.c_strides[i] != sd: |
| 147 |
return 0 |
| 148 |
sd *= dim |
| 149 |
return 1 |
| 150 |
|
| 151 |
def _IsCContiguous(view): |
| 152 |
ndim = widen(view.c_ndim) |
| 153 |
if ndim == 0: |
| 154 |
return 1 |
| 155 |
if not view.c_strides: |
| 156 |
return ndim == 1 |
| 157 |
sd = view.c_itemsize |
| 158 |
if ndim == 1: |
| 159 |
return view.c_shape[0] == 1 or sd == view.c_strides[0] |
| 160 |
for i in range(ndim - 1, -1, -1): |
| 161 |
dim = view.c_shape[i] |
| 162 |
if dim == 0: |
| 163 |
return 1 |
| 164 |
if view.c_strides[i] != sd: |
| 165 |
return 0 |
| 166 |
sd *= dim |
| 167 |
return 1 |
| 168 |
|
| 169 |
@cpython_api([Py_bufferP, lltype.Char], rffi.INT_real, error=CANNOT_FAIL) |
| 170 |
def PyBuffer_IsContiguous(space, view, fort): |
| 171 |
"""Return 1 if the memory defined by the view is C-style (fort is |
| 172 |
'C') or Fortran-style (fort is 'F') contiguous or either one |
| 173 |
(fort is 'A'). Return 0 otherwise.""" |
| 174 |
|
| 175 |
|
| 176 |
if view.c_suboffsets: |
| 177 |
return 0 |
| 178 |
if (fort == 'C'): |
| 179 |
return _IsCContiguous(view) |
| 180 |
elif (fort == 'F'): |
| 181 |
return _IsFortranContiguous(view) |
| 182 |
elif (fort == 'A'): |
| 183 |
return (_IsCContiguous(view) or _IsFortranContiguous(view)) |
| 184 |
return 0 |
| 185 |
|
| 186 |
@cpython_api([PyObject], PyObject) |
| 187 |
def PyMemoryView_FromObject(space, w_obj): |
| 188 |
return space.call_method(space.builtin, "memoryview", w_obj) |
| 189 |
|
| 190 |
@cts.decl("""PyObject * |
| 191 |
PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)""") |
| 192 |
def PyMemoryView_FromMemory(space, mem, size, flags): |
| 193 |
"""Expose a raw memory area as a view of contiguous bytes. flags can be |
| 194 |
PyBUF_READ or PyBUF_WRITE. view->format is set to "B" (unsigned bytes). |
| 195 |
The memoryview has complete buffer information. |
| 196 |
""" |
| 197 |
readonly = int(widen(flags) == PyBUF_READ) |
| 198 |
view = CPyBuffer(space, cts.cast('void*', mem), size, None, |
| 199 |
readonly=readonly) |
| 200 |
w_mview = W_MemoryView(view) |
| 201 |
return w_mview |
| 202 |
|
| 203 |
@cpython_api([Py_bufferP], PyObject, result_is_ll=True) |
| 204 |
def PyMemoryView_FromBuffer(space, view): |
| 205 |
"""Create a memoryview object wrapping the given buffer structure view. |
| 206 |
The memoryview object then owns the buffer represented by view, which |
| 207 |
means you shouldn't try to call PyBuffer_Release() yourself: it |
| 208 |
will be done on deallocation of the memoryview object.""" |
| 209 |
if not view.c_buf: |
| 210 |
raise oefmt(space.w_ValueError, |
| 211 |
"PyMemoryView_FromBuffer(): info->buf must not be NULL") |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
typedescr = get_typedescr(W_MemoryView.typedef) |
| 216 |
py_obj = typedescr.allocate(space, space.w_memoryview) |
| 217 |
|
| 218 |
py_mem = rffi.cast(PyMemoryViewObject, py_obj) |
| 219 |
mview = py_mem.c_view |
| 220 |
mview.c_buf = view.c_buf |
| 221 |
|
| 222 |
mview.c_obj = rffi.cast(PyObject, 0) |
| 223 |
mview.c_len = view.c_len |
| 224 |
mview.c_itemsize = view.c_itemsize |
| 225 |
mview.c_readonly = view.c_readonly |
| 226 |
mview.c_ndim = view.c_ndim |
| 227 |
mview.c_format = view.c_format |
| 228 |
if view.c_strides == rffi.cast(Py_ssize_tP, view.c__strides): |
| 229 |
py_mem.c_view.c_strides = rffi.cast(Py_ssize_tP, py_mem.c_view.c__strides) |
| 230 |
for i in range(view.c_ndim): |
| 231 |
py_mem.c_view.c_strides[i] = view.c_strides[i] |
| 232 |
else: |
| 233 |
|
| 234 |
py_mem.c_view.c_strides = view.c_strides |
| 235 |
if view.c_shape == rffi.cast(Py_ssize_tP, view.c__shape): |
| 236 |
py_mem.c_view.c_shape = rffi.cast(Py_ssize_tP, py_mem.c_view.c__shape) |
| 237 |
for i in range(view.c_ndim): |
| 238 |
py_mem.c_view.c_shape[i] = view.c_shape[i] |
| 239 |
else: |
| 240 |
|
| 241 |
py_mem.c_view.c_shape = view.c_shape |
| 242 |
|
| 243 |
return py_obj |
| 244 |
|
| 245 |
def memory_from_contiguous_copy(space, src, order): |
| 246 |
""" |
| 247 |
Return a memoryview that is based on a contiguous copy of src. |
| 248 |
Assumptions: src has PyBUF_FULL_RO information, src->ndim > 0. |
| 249 |
|
| 250 |
Ownership rules: |
| 251 |
1) As usual, the returned memoryview has a private copy |
| 252 |
of src->shape, src->strides and src->suboffsets. |
| 253 |
2) src->format is copied to the master buffer and released |
| 254 |
in mbuf_dealloc(). The releasebufferproc of the bytes |
| 255 |
object is NULL, so it does not matter that mbuf_release() |
| 256 |
passes the altered format pointer to PyBuffer_Release(). |
| 257 |
""" |
| 258 |
raise oefmt(space.w_NotImplementedError, |
| 259 |
"creating contiguous readonly buffer from non-contiguous " |
| 260 |
"not implemented yet") |
| 261 |
|
| 262 |
|
| 263 |
@cpython_api([PyObject, rffi.INT_real, lltype.Char], PyObject) |
| 264 |
def PyMemoryView_GetContiguous(space, w_obj, buffertype, order): |
| 265 |
""" |
| 266 |
Return a new memoryview object based on a contiguous exporter with |
| 267 |
buffertype={PyBUF_READ, PyBUF_WRITE} and order={'C', 'F'ortran, or 'A'ny}. |
| 268 |
The logical structure of the input and output buffers is the same |
| 269 |
(i.e. tolist(input) == tolist(output)), but the physical layout in |
| 270 |
memory can be explicitly chosen. |
| 271 |
|
| 272 |
As usual, if buffertype=PyBUF_WRITE, the exporter's buffer must be writable, |
| 273 |
otherwise it may be writable or read-only. |
| 274 |
|
| 275 |
If the exporter is already contiguous with the desired target order, |
| 276 |
the memoryview will be directly based on the exporter. |
| 277 |
|
| 278 |
Otherwise, if the buffertype is PyBUF_READ, the memoryview will be |
| 279 |
based on a new bytes object. If order={'C', 'A'ny}, use 'C' order, |
| 280 |
'F'ortran order otherwise. |
| 281 |
""" |
| 282 |
|
| 283 |
buffertype = widen(buffertype) |
| 284 |
if buffertype != PyBUF_READ and buffertype != PyBUF_WRITE: |
| 285 |
raise oefmt(space.w_ValueError, |
| 286 |
"buffertype must be PyBUF_READ or PyBUF_WRITE") |
| 287 |
|
| 288 |
if order != 'C' and order != 'F' and order != 'A': |
| 289 |
raise oefmt(space.w_ValueError, |
| 290 |
"order must be in ('C', 'F', 'A')") |
| 291 |
|
| 292 |
w_mv = space.call_method(space.builtin, "memoryview", w_obj) |
| 293 |
mv = make_ref(space, w_mv) |
| 294 |
mv = rffi.cast(PyMemoryViewObject, mv) |
| 295 |
view = mv.c_view |
| 296 |
if buffertype == PyBUF_WRITE and widen(view.c_readonly): |
| 297 |
raise oefmt(space.w_BufferError, |
| 298 |
"underlying buffer is not writable") |
| 299 |
|
| 300 |
if PyBuffer_IsContiguous(space, view, order): |
| 301 |
return w_mv |
| 302 |
|
| 303 |
if buffertype == PyBUF_WRITE: |
| 304 |
raise oefmt(space.w_BufferError, |
| 305 |
"writable contiguous buffer requested " |
| 306 |
"for a non-contiguous object.") |
| 307 |
|
| 308 |
return memory_from_contiguous_copy(space, view, order) |