| 1 |
""" |
| 2 |
Arrays. |
| 3 |
""" |
| 4 |
|
| 5 |
from pypy.interpreter.baseobjspace import W_Root |
| 6 |
from pypy.interpreter.error import OperationError, oefmt |
| 7 |
from pypy.interpreter.gateway import interp2app |
| 8 |
from pypy.interpreter.typedef import TypeDef |
| 9 |
|
| 10 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 11 |
from rpython.rlib.rarithmetic import ovfcheck |
| 12 |
|
| 13 |
from pypy.module._cffi_backend import cdataobj |
| 14 |
from pypy.module._cffi_backend.ctypeptr import W_CTypePtrOrArray, W_CTypePointer |
| 15 |
from pypy.module._cffi_backend import ctypeprim |
| 16 |
|
| 17 |
|
| 18 |
class W_CTypeArray(W_CTypePtrOrArray): |
| 19 |
_attrs_ = ['ctptr'] |
| 20 |
_immutable_fields_ = ['ctptr'] |
| 21 |
kind = "array" |
| 22 |
is_nonfunc_pointer_or_array = True |
| 23 |
|
| 24 |
def __init__(self, space, ctptr, length, arraysize, extra): |
| 25 |
assert isinstance(ctptr, W_CTypePointer) |
| 26 |
W_CTypePtrOrArray.__init__(self, space, arraysize, extra, 0, |
| 27 |
ctptr.ctitem) |
| 28 |
self.length = length |
| 29 |
self.ctptr = ctptr |
| 30 |
|
| 31 |
def _alignof(self): |
| 32 |
return self.ctitem.alignof() |
| 33 |
|
| 34 |
def newp(self, w_init, allocator): |
| 35 |
space = self.space |
| 36 |
datasize = self.size |
| 37 |
|
| 38 |
if datasize < 0: |
| 39 |
w_init, length = self.get_new_array_length(w_init) |
| 40 |
try: |
| 41 |
datasize = ovfcheck(length * self.ctitem.size) |
| 42 |
except OverflowError: |
| 43 |
raise oefmt(space.w_OverflowError, |
| 44 |
"array size would overflow a ssize_t") |
| 45 |
else: |
| 46 |
length = self.length |
| 47 |
|
| 48 |
cdata = allocator.allocate(space, datasize, self, length) |
| 49 |
|
| 50 |
if not space.is_w(w_init, space.w_None): |
| 51 |
with cdata as ptr: |
| 52 |
self.convert_from_object(ptr, w_init) |
| 53 |
return cdata |
| 54 |
|
| 55 |
def get_new_array_length(self, w_value): |
| 56 |
space = self.space |
| 57 |
if (space.isinstance_w(w_value, space.w_list) or |
| 58 |
space.isinstance_w(w_value, space.w_tuple)): |
| 59 |
return (w_value, space.int_w(space.len(w_value))) |
| 60 |
elif space.isinstance_w(w_value, space.w_bytes): |
| 61 |
|
| 62 |
s = space.bytes_w(w_value) |
| 63 |
return (w_value, len(s) + 1) |
| 64 |
elif space.isinstance_w(w_value, space.w_unicode): |
| 65 |
from pypy.module._cffi_backend import wchar_helper |
| 66 |
w_u = space.convert_arg_to_w_unicode(w_value) |
| 67 |
if self.ctitem.size == 2: |
| 68 |
length = wchar_helper.utf8_size_as_char16(w_u._utf8) |
| 69 |
else: |
| 70 |
length = w_u._len() |
| 71 |
return (w_value, length + 1) |
| 72 |
else: |
| 73 |
try: |
| 74 |
explicitlength = space.getindex_w(w_value, |
| 75 |
space.w_OverflowError) |
| 76 |
except OperationError as e: |
| 77 |
if e.match(space, space.w_TypeError): |
| 78 |
raise oefmt(space.w_TypeError, |
| 79 |
"expected new array length or list/tuple/str, " |
| 80 |
"not %T", w_value) |
| 81 |
raise |
| 82 |
if explicitlength < 0: |
| 83 |
raise oefmt(space.w_ValueError, "negative array length") |
| 84 |
return (space.w_None, explicitlength) |
| 85 |
|
| 86 |
def _check_subscript_index(self, w_cdata, i): |
| 87 |
space = self.space |
| 88 |
if i < 0: |
| 89 |
raise oefmt(space.w_IndexError, "negative index") |
| 90 |
if i >= w_cdata.get_array_length(): |
| 91 |
raise oefmt(space.w_IndexError, |
| 92 |
"index too large for cdata '%s' (expected %d < %d)", |
| 93 |
self.name, i, w_cdata.get_array_length()) |
| 94 |
return self |
| 95 |
|
| 96 |
def _check_slice_index(self, w_cdata, start, stop): |
| 97 |
space = self.space |
| 98 |
if start < 0: |
| 99 |
raise oefmt(space.w_IndexError, "negative index") |
| 100 |
if stop > w_cdata.get_array_length(): |
| 101 |
raise oefmt(space.w_IndexError, |
| 102 |
"index too large (expected %d <= %d)", |
| 103 |
stop, w_cdata.get_array_length()) |
| 104 |
return self.ctptr |
| 105 |
|
| 106 |
def convert_from_object(self, cdata, w_ob): |
| 107 |
if isinstance(w_ob, cdataobj.W_CData) and w_ob.ctype is self: |
| 108 |
length = w_ob.get_array_length() |
| 109 |
with w_ob as source: |
| 110 |
source = rffi.cast(rffi.CONST_VOIDP, source) |
| 111 |
target = rffi.cast(rffi.VOIDP, cdata) |
| 112 |
size = rffi.cast(rffi.SIZE_T, self.ctitem.size * length) |
| 113 |
rffi.c_memcpy(target, source, size) |
| 114 |
else: |
| 115 |
self.convert_array_from_object(cdata, w_ob) |
| 116 |
|
| 117 |
def convert_to_object(self, cdata): |
| 118 |
if self.length < 0: |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
self = self.ctptr |
| 123 |
|
| 124 |
return cdataobj.W_CData(self.space, cdata, self) |
| 125 |
|
| 126 |
def add(self, cdata, i): |
| 127 |
p = rffi.ptradd(cdata, i * self.ctitem.size) |
| 128 |
return cdataobj.W_CData(self.space, p, self.ctptr) |
| 129 |
|
| 130 |
def iter(self, cdata): |
| 131 |
return W_CDataIter(self.space, self.ctitem, cdata) |
| 132 |
|
| 133 |
def get_vararg_type(self): |
| 134 |
return self.ctptr |
| 135 |
|
| 136 |
def _fget(self, attrchar): |
| 137 |
if attrchar == 'i': |
| 138 |
return self.ctitem |
| 139 |
if attrchar == 'l': |
| 140 |
if self.length >= 0: |
| 141 |
return self.space.newint(self.length) |
| 142 |
else: |
| 143 |
return self.space.w_None |
| 144 |
return W_CTypePtrOrArray._fget(self, attrchar) |
| 145 |
|
| 146 |
def typeoffsetof_index(self, index): |
| 147 |
return self.ctptr.typeoffsetof_index(index) |
| 148 |
|
| 149 |
|
| 150 |
class W_CDataIter(W_Root): |
| 151 |
_immutable_fields_ = ['ctitem', 'cdata', '_stop'] |
| 152 |
|
| 153 |
def __init__(self, space, ctitem, cdata): |
| 154 |
self.space = space |
| 155 |
self.ctitem = ctitem |
| 156 |
self.cdata = cdata |
| 157 |
length = cdata.get_array_length() |
| 158 |
self._next = cdata.unsafe_escaping_ptr() |
| 159 |
self._stop = rffi.ptradd(self._next, length * ctitem.size) |
| 160 |
|
| 161 |
def iter_w(self): |
| 162 |
return self |
| 163 |
|
| 164 |
def next_w(self): |
| 165 |
result = self._next |
| 166 |
if result == self._stop: |
| 167 |
raise OperationError(self.space.w_StopIteration, self.space.w_None) |
| 168 |
self._next = rffi.ptradd(result, self.ctitem.size) |
| 169 |
return self.ctitem.convert_to_object(result) |
| 170 |
|
| 171 |
W_CDataIter.typedef = TypeDef( |
| 172 |
'_cffi_backend.__CData_iterator', |
| 173 |
__iter__ = interp2app(W_CDataIter.iter_w), |
| 174 |
__next__ = interp2app(W_CDataIter.next_w), |
| 175 |
) |
| 176 |
W_CDataIter.typedef.acceptable_as_base_class = False |