| 1 |
from pypy.interpreter.baseobjspace import W_Root |
| 2 |
from pypy.interpreter.error import OperationError, oefmt |
| 3 |
from pypy.interpreter.gateway import unwrap_spec, interp2app |
| 4 |
from pypy.interpreter.typedef import TypeDef, make_weakref_descr |
| 5 |
from pypy.module._cffi_backend import cdataobj, ctypeptr, ctypearray |
| 6 |
from pypy.module._cffi_backend import ctypestruct |
| 7 |
from pypy.interpreter.buffer import SimpleView |
| 8 |
|
| 9 |
from rpython.rlib.buffer import LLBuffer |
| 10 |
|
| 11 |
|
| 12 |
class MiniBuffer(W_Root): |
| 13 |
def __init__(self, buffer, keepalive=None): |
| 14 |
self.buffer = buffer |
| 15 |
self.keepalive = keepalive |
| 16 |
|
| 17 |
def buffer_w(self, space, flags): |
| 18 |
return SimpleView(self.buffer, w_obj=self) |
| 19 |
|
| 20 |
def descr_len(self, space): |
| 21 |
return space.newint(self.buffer.getlength()) |
| 22 |
|
| 23 |
def descr_getitem(self, space, w_index): |
| 24 |
start, stop, step, size = space.decode_index4(w_index, |
| 25 |
self) |
| 26 |
if step == 0: |
| 27 |
return space.newbytes(self.buffer.getitem(start)) |
| 28 |
res = self.buffer.getslice(start, step, size) |
| 29 |
return space.newbytes(res) |
| 30 |
|
| 31 |
def descr_setitem(self, space, w_index, w_newstring): |
| 32 |
start, stop, step, size = space.decode_index4(w_index, |
| 33 |
self) |
| 34 |
if step not in (0, 1): |
| 35 |
raise oefmt(space.w_NotImplementedError, "") |
| 36 |
value = space.buffer_w(w_newstring, space.BUF_CONTIG_RO).as_readbuf() |
| 37 |
if value.getlength() != size: |
| 38 |
raise oefmt(space.w_ValueError, |
| 39 |
"cannot modify size of memoryview object") |
| 40 |
if step == 0: |
| 41 |
self.buffer.setitem(start, value.getitem(0)) |
| 42 |
elif step == 1: |
| 43 |
self.buffer.setslice(start, value.as_str()) |
| 44 |
|
| 45 |
|
| 46 |
def _comparison_helper(self, space, w_other, mode): |
| 47 |
if space.isinstance_w(w_other, space.w_unicode): |
| 48 |
return space.w_NotImplemented |
| 49 |
try: |
| 50 |
other_buf = space.readbuf_w(w_other) |
| 51 |
except OperationError as e: |
| 52 |
if e.async(space): |
| 53 |
raise |
| 54 |
return space.w_NotImplemented |
| 55 |
my_buf = self.buffer |
| 56 |
my_len = len(my_buf) |
| 57 |
other_len = len(other_buf) |
| 58 |
if other_len != my_len: |
| 59 |
if mode == 'E': |
| 60 |
return space.w_False |
| 61 |
if mode == 'N': |
| 62 |
return space.w_True |
| 63 |
cmp = _memcmp(my_buf, other_buf, min(my_len, other_len)) |
| 64 |
if cmp == 0: |
| 65 |
if my_len < other_len: |
| 66 |
cmp = -1 |
| 67 |
elif my_len > other_len: |
| 68 |
cmp = 1 |
| 69 |
|
| 70 |
if mode == 'L': res = cmp < 0 |
| 71 |
elif mode == 'l': res = cmp <= 0 |
| 72 |
elif mode == 'E': res = cmp == 0 |
| 73 |
elif mode == 'N': res = cmp != 0 |
| 74 |
elif mode == 'G': res = cmp > 0 |
| 75 |
elif mode == 'g': res = cmp >= 0 |
| 76 |
else: raise AssertionError |
| 77 |
|
| 78 |
return space.newbool(res) |
| 79 |
|
| 80 |
def descr_eq(self, space, w_other): |
| 81 |
return self._comparison_helper(space, w_other, 'E') |
| 82 |
def descr_ne(self, space, w_other): |
| 83 |
return self._comparison_helper(space, w_other, 'N') |
| 84 |
def descr_lt(self, space, w_other): |
| 85 |
return self._comparison_helper(space, w_other, 'L') |
| 86 |
def descr_le(self, space, w_other): |
| 87 |
return self._comparison_helper(space, w_other, 'l') |
| 88 |
def descr_gt(self, space, w_other): |
| 89 |
return self._comparison_helper(space, w_other, 'G') |
| 90 |
def descr_ge(self, space, w_other): |
| 91 |
return self._comparison_helper(space, w_other, 'g') |
| 92 |
|
| 93 |
def _memcmp(buf1, buf2, length): |
| 94 |
|
| 95 |
for i in range(length): |
| 96 |
if buf1[i] < buf2[i]: |
| 97 |
return -1 |
| 98 |
if buf1[i] > buf2[i]: |
| 99 |
return 1 |
| 100 |
return 0 |
| 101 |
|
| 102 |
@unwrap_spec(w_cdata=cdataobj.W_CData, size=int) |
| 103 |
def MiniBuffer___new__(space, w_subtype, w_cdata, size=-1): |
| 104 |
explicit_size = size >= 0 |
| 105 |
ctype = w_cdata.ctype |
| 106 |
if isinstance(ctype, ctypeptr.W_CTypePointer): |
| 107 |
if size < 0: |
| 108 |
structobj = w_cdata.get_structobj() |
| 109 |
if (structobj is not None and |
| 110 |
isinstance(structobj.ctype, ctypestruct.W_CTypeStructOrUnion)): |
| 111 |
size = structobj._sizeof() |
| 112 |
if size < 0: |
| 113 |
size = ctype.ctitem.size |
| 114 |
elif isinstance(ctype, ctypearray.W_CTypeArray): |
| 115 |
if size < 0: |
| 116 |
size = w_cdata._sizeof() |
| 117 |
else: |
| 118 |
raise oefmt(space.w_TypeError, |
| 119 |
"expected a pointer or array cdata, got '%s'", ctype.name) |
| 120 |
if size < 0: |
| 121 |
raise oefmt(space.w_TypeError, |
| 122 |
"don't know the size pointed to by '%s'", ctype.name) |
| 123 |
|
| 124 |
if explicit_size: |
| 125 |
max_size = w_cdata.get_maximum_buffer_size() |
| 126 |
if max_size >= 0 and size > max_size: |
| 127 |
msg = ("ffi.buffer(cdata, bytes): creating a buffer of %d " |
| 128 |
"bytes over a cdata that owns only %d bytes. This " |
| 129 |
"will crash if you access the extra memory") |
| 130 |
msg = msg % (size, max_size) |
| 131 |
space.warn(space.newtext(msg), space.w_UserWarning) |
| 132 |
|
| 133 |
ptr = w_cdata.unsafe_escaping_ptr() |
| 134 |
return MiniBuffer(LLBuffer(ptr, size), w_cdata) |
| 135 |
|
| 136 |
MiniBuffer.typedef = TypeDef( |
| 137 |
"_cffi_backend.buffer", None, None, "read-write", |
| 138 |
__new__ = interp2app(MiniBuffer___new__), |
| 139 |
__len__ = interp2app(MiniBuffer.descr_len), |
| 140 |
__getitem__ = interp2app(MiniBuffer.descr_getitem), |
| 141 |
__setitem__ = interp2app(MiniBuffer.descr_setitem), |
| 142 |
__eq__ = interp2app(MiniBuffer.descr_eq), |
| 143 |
__ne__ = interp2app(MiniBuffer.descr_ne), |
| 144 |
__lt__ = interp2app(MiniBuffer.descr_lt), |
| 145 |
__le__ = interp2app(MiniBuffer.descr_le), |
| 146 |
__gt__ = interp2app(MiniBuffer.descr_gt), |
| 147 |
__ge__ = interp2app(MiniBuffer.descr_ge), |
| 148 |
__weakref__ = make_weakref_descr(MiniBuffer), |
| 149 |
__doc__ = """ffi.buffer(cdata[, byte_size]): |
| 150 |
Return a read-write buffer object that references the raw C data |
| 151 |
pointed to by the given 'cdata'. The 'cdata' must be a pointer or an |
| 152 |
array. Can be passed to functions expecting a buffer, or directly |
| 153 |
manipulated with: |
| 154 |
|
| 155 |
buf[:] get a copy of it in a regular string, or |
| 156 |
buf[idx] as a single character |
| 157 |
buf[:] = ... |
| 158 |
buf[idx] = ... change the content |
| 159 |
""", |
| 160 |
) |
| 161 |
MiniBuffer.typedef.acceptable_as_base_class = False |