| 1 |
from rpython.rtyper.annlowlevel import llstr |
| 2 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 3 |
from rpython.rtyper.lltypesystem.rstr import copy_string_to_raw |
| 4 |
from rpython.rlib.objectmodel import keepalive_until_here, we_are_translated |
| 5 |
from rpython.rlib import jit |
| 6 |
|
| 7 |
from pypy.interpreter.baseobjspace import W_Root |
| 8 |
from pypy.interpreter.error import oefmt |
| 9 |
from pypy.interpreter.gateway import unwrap_spec, WrappedDefault |
| 10 |
from pypy.interpreter.typedef import TypeDef, GetSetProperty, ClassAttr |
| 11 |
from pypy.module._cffi_backend import ctypeobj, cdataobj, allocator |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
@unwrap_spec(w_ctype=ctypeobj.W_CType, w_init=WrappedDefault(None)) |
| 17 |
def newp(space, w_ctype, w_init): |
| 18 |
return w_ctype.newp(w_init, allocator.default_allocator) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
@unwrap_spec(w_ctype=ctypeobj.W_CType) |
| 23 |
def cast(space, w_ctype, w_ob): |
| 24 |
return w_ctype.cast(w_ob) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
@unwrap_spec(w_ctype=ctypeobj.W_CType) |
| 29 |
def callback(space, w_ctype, w_callable, w_error=None, w_onerror=None): |
| 30 |
from pypy.module._cffi_backend.ccallback import make_callback |
| 31 |
return make_callback(space, w_ctype, w_callable, w_error, w_onerror) |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
@unwrap_spec(w_cdata=cdataobj.W_CData) |
| 36 |
def typeof(space, w_cdata): |
| 37 |
return w_cdata.ctype |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
def sizeof(space, w_obj): |
| 42 |
if isinstance(w_obj, cdataobj.W_CData): |
| 43 |
size = w_obj._sizeof() |
| 44 |
ctype = w_obj.ctype |
| 45 |
elif isinstance(w_obj, ctypeobj.W_CType): |
| 46 |
size = w_obj.size |
| 47 |
ctype = w_obj |
| 48 |
else: |
| 49 |
raise oefmt(space.w_TypeError, "expected a 'cdata' or 'ctype' object") |
| 50 |
if size < 0: |
| 51 |
raise oefmt(space.w_ValueError, |
| 52 |
"ctype '%s' is of unknown size", ctype.name) |
| 53 |
return space.newint(size) |
| 54 |
|
| 55 |
@unwrap_spec(w_ctype=ctypeobj.W_CType) |
| 56 |
def alignof(space, w_ctype): |
| 57 |
align = w_ctype.alignof() |
| 58 |
return space.newint(align) |
| 59 |
|
| 60 |
@unwrap_spec(w_ctype=ctypeobj.W_CType, following=int) |
| 61 |
def typeoffsetof(space, w_ctype, w_field_or_index, following=0): |
| 62 |
ctype, offset = w_ctype.direct_typeoffsetof(w_field_or_index, following) |
| 63 |
return space.newtuple2(ctype, space.newint(offset)) |
| 64 |
|
| 65 |
@unwrap_spec(w_ctype=ctypeobj.W_CType, w_cdata=cdataobj.W_CData, offset=int) |
| 66 |
def rawaddressof(space, w_ctype, w_cdata, offset): |
| 67 |
return w_ctype.rawaddressof(w_cdata, offset) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
@unwrap_spec(w_ctype=ctypeobj.W_CType, replace_with='text') |
| 72 |
def getcname(space, w_ctype, replace_with): |
| 73 |
p = w_ctype.name_position |
| 74 |
s = '%s%s%s' % (w_ctype.name[:p], replace_with, w_ctype.name[p:]) |
| 75 |
return space.newtext(s) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
@unwrap_spec(w_cdata=cdataobj.W_CData, maxlen=int) |
| 80 |
def string(space, w_cdata, maxlen=-1): |
| 81 |
return w_cdata.ctype.string(w_cdata, maxlen) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@unwrap_spec(w_cdata=cdataobj.W_CData, length=int) |
| 86 |
def unpack(space, w_cdata, length): |
| 87 |
return w_cdata.unpack(length) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
def _get_types(space): |
| 92 |
return space.newtuple2(space.gettypefor(cdataobj.W_CData), |
| 93 |
space.gettypefor(ctypeobj.W_CType)) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
def _get_common_types(space, w_dict): |
| 98 |
from pypy.module._cffi_backend.parse_c_type import ll_enum_common_types |
| 99 |
index = 0 |
| 100 |
while True: |
| 101 |
p = ll_enum_common_types(rffi.cast(rffi.INT, index)) |
| 102 |
if not p: |
| 103 |
break |
| 104 |
key = rffi.charp2str(p) |
| 105 |
value = rffi.charp2str(rffi.ptradd(p, len(key) + 1)) |
| 106 |
space.setitem_str(w_dict, key, space.newtext(value)) |
| 107 |
index += 1 |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
def _fetch_as_read_buffer(space, w_x): |
| 112 |
return space.readbuf_w(w_x) |
| 113 |
|
| 114 |
def _fetch_as_write_buffer(space, w_x): |
| 115 |
return space.writebuf_w(w_x) |
| 116 |
|
| 117 |
@unwrap_spec(w_ctype=ctypeobj.W_CType, require_writable=int) |
| 118 |
def from_buffer(space, w_ctype, w_x, require_writable=0): |
| 119 |
from pypy.module._cffi_backend import ctypeptr, ctypearray |
| 120 |
if not isinstance(w_ctype, ctypeptr.W_CTypePtrOrArray): |
| 121 |
raise oefmt(space.w_TypeError, |
| 122 |
"expected a poiunter or array ctype, got '%s'", |
| 123 |
w_ctype.name) |
| 124 |
if space.isinstance_w(w_x, space.w_unicode): |
| 125 |
raise oefmt(space.w_TypeError, |
| 126 |
"from_buffer() cannot return the address of a unicode object") |
| 127 |
if require_writable: |
| 128 |
buf = _fetch_as_write_buffer(space, w_x) |
| 129 |
else: |
| 130 |
buf = _fetch_as_read_buffer(space, w_x) |
| 131 |
if space.isinstance_w(w_x, space.w_bytes): |
| 132 |
_cdata = get_raw_address_of_string(space, w_x) |
| 133 |
else: |
| 134 |
try: |
| 135 |
_cdata = buf.get_raw_address() |
| 136 |
except ValueError: |
| 137 |
raise oefmt(space.w_TypeError, |
| 138 |
"from_buffer() got a '%T' object, which supports the " |
| 139 |
"buffer interface but cannot be rendered as a plain " |
| 140 |
"raw address on PyPy", w_x) |
| 141 |
|
| 142 |
buffersize = buf.getlength() |
| 143 |
if not isinstance(w_ctype, ctypearray.W_CTypeArray): |
| 144 |
arraylength = buffersize |
| 145 |
else: |
| 146 |
arraylength = w_ctype.length |
| 147 |
if arraylength >= 0: |
| 148 |
|
| 149 |
|
| 150 |
if buffersize < w_ctype.size: |
| 151 |
raise oefmt(space.w_ValueError, |
| 152 |
"buffer is too small (%d bytes) for '%s' (%d bytes)", |
| 153 |
buffersize, w_ctype.name, w_ctype.size) |
| 154 |
else: |
| 155 |
|
| 156 |
itemsize = w_ctype.ctitem.size |
| 157 |
if itemsize == 1: |
| 158 |
|
| 159 |
arraylength = buffersize |
| 160 |
elif itemsize > 0: |
| 161 |
|
| 162 |
|
| 163 |
arraylength = buffersize / itemsize |
| 164 |
else: |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
raise oefmt(space.w_ZeroDivisionError, |
| 171 |
"from_buffer('%s', ..): the actual length of the array " |
| 172 |
"cannot be computed", w_ctype.name) |
| 173 |
|
| 174 |
return cdataobj.W_CDataFromBuffer(space, _cdata, arraylength, |
| 175 |
w_ctype, buf, w_x) |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
class RawBytes(object): |
| 180 |
def __init__(self, string): |
| 181 |
self.ptr = rffi.str2charp(string, track_allocation=False) |
| 182 |
def __del__(self): |
| 183 |
rffi.free_charp(self.ptr, track_allocation=False) |
| 184 |
|
| 185 |
class RawBytesCache(object): |
| 186 |
def __init__(self, space): |
| 187 |
from pypy.interpreter.baseobjspace import W_Root |
| 188 |
from rpython.rlib import rweakref |
| 189 |
self.wdict = rweakref.RWeakKeyDictionary(W_Root, RawBytes) |
| 190 |
|
| 191 |
@jit.dont_look_inside |
| 192 |
def get_raw_address_of_string(space, w_x): |
| 193 |
"""Special case for ffi.from_buffer(string). Returns a 'char *' that |
| 194 |
is valid as long as the string object is alive. Two calls to |
| 195 |
ffi.from_buffer(same_string) are guaranteed to return the same pointer. |
| 196 |
""" |
| 197 |
from rpython.rtyper.annlowlevel import llstr |
| 198 |
from rpython.rtyper.lltypesystem.rstr import STR |
| 199 |
from rpython.rtyper.lltypesystem import llmemory |
| 200 |
from rpython.rlib import rgc |
| 201 |
|
| 202 |
cache = space.fromcache(RawBytesCache) |
| 203 |
rawbytes = cache.wdict.get(w_x) |
| 204 |
if rawbytes is None: |
| 205 |
data = space.bytes_w(w_x) |
| 206 |
if (we_are_translated() and not rgc.can_move(data) |
| 207 |
and not rgc.must_split_gc_address_space()): |
| 208 |
lldata = llstr(data) |
| 209 |
data_start = (llmemory.cast_ptr_to_adr(lldata) + |
| 210 |
rffi.offsetof(STR, 'chars') + |
| 211 |
llmemory.itemoffsetof(STR.chars, 0)) |
| 212 |
data_start = rffi.cast(rffi.CCHARP, data_start) |
| 213 |
data_start[len(data)] = '\x00' |
| 214 |
return data_start |
| 215 |
rawbytes = RawBytes(data) |
| 216 |
cache.wdict.set(w_x, rawbytes) |
| 217 |
return rawbytes.ptr |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
def unsafe_escaping_ptr_for_ptr_or_array(w_cdata): |
| 223 |
if not w_cdata.ctype.is_nonfunc_pointer_or_array: |
| 224 |
raise oefmt(w_cdata.space.w_TypeError, |
| 225 |
"expected a pointer or array ctype, got '%s'", |
| 226 |
w_cdata.ctype.name) |
| 227 |
return w_cdata.unsafe_escaping_ptr() |
| 228 |
|
| 229 |
c_memmove = rffi.llexternal('memmove', [rffi.CCHARP, rffi.CCHARP, |
| 230 |
rffi.SIZE_T], lltype.Void, |
| 231 |
_nowrapper=True) |
| 232 |
|
| 233 |
@unwrap_spec(n=int) |
| 234 |
def memmove(space, w_dest, w_src, n): |
| 235 |
if n < 0: |
| 236 |
raise oefmt(space.w_ValueError, "negative size") |
| 237 |
|
| 238 |
|
| 239 |
src_buf = None |
| 240 |
src_data = lltype.nullptr(rffi.CCHARP.TO) |
| 241 |
if space.isinstance_w(w_src, space.w_bytes): |
| 242 |
src_is_ptr = False |
| 243 |
src_string = space.bytes_w(w_src) |
| 244 |
else: |
| 245 |
if isinstance(w_src, cdataobj.W_CData): |
| 246 |
src_data = unsafe_escaping_ptr_for_ptr_or_array(w_src) |
| 247 |
src_is_ptr = True |
| 248 |
else: |
| 249 |
src_buf = _fetch_as_read_buffer(space, w_src) |
| 250 |
try: |
| 251 |
src_data = src_buf.get_raw_address() |
| 252 |
src_is_ptr = True |
| 253 |
except ValueError: |
| 254 |
src_is_ptr = False |
| 255 |
|
| 256 |
if src_is_ptr: |
| 257 |
src_string = None |
| 258 |
else: |
| 259 |
if n == src_buf.getlength(): |
| 260 |
src_string = src_buf.as_str() |
| 261 |
else: |
| 262 |
src_string = src_buf.getslice(0, 1, n) |
| 263 |
|
| 264 |
dest_buf = None |
| 265 |
dest_data = lltype.nullptr(rffi.CCHARP.TO) |
| 266 |
if isinstance(w_dest, cdataobj.W_CData): |
| 267 |
dest_data = unsafe_escaping_ptr_for_ptr_or_array(w_dest) |
| 268 |
dest_is_ptr = True |
| 269 |
else: |
| 270 |
dest_buf = _fetch_as_write_buffer(space, w_dest) |
| 271 |
try: |
| 272 |
dest_data = dest_buf.get_raw_address() |
| 273 |
dest_is_ptr = True |
| 274 |
except ValueError: |
| 275 |
dest_is_ptr = False |
| 276 |
|
| 277 |
if dest_is_ptr: |
| 278 |
if src_is_ptr: |
| 279 |
c_memmove(dest_data, src_data, rffi.cast(rffi.SIZE_T, n)) |
| 280 |
else: |
| 281 |
copy_string_to_raw(llstr(src_string), dest_data, 0, n) |
| 282 |
else: |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
if src_is_ptr: |
| 287 |
for i in range(n): |
| 288 |
dest_buf.setitem(i, src_data[i]) |
| 289 |
else: |
| 290 |
for i in range(n): |
| 291 |
dest_buf.setitem(i, src_string[i]) |
| 292 |
|
| 293 |
keepalive_until_here(src_buf) |
| 294 |
keepalive_until_here(dest_buf) |
| 295 |
keepalive_until_here(w_src) |
| 296 |
keepalive_until_here(w_dest) |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
@unwrap_spec(w_cdata=cdataobj.W_CData, size=int) |
| 301 |
def gcp(space, w_cdata, w_destructor, size=0): |
| 302 |
return w_cdata.with_gc(w_destructor, size) |
| 303 |
|
| 304 |
@unwrap_spec(w_cdata=cdataobj.W_CData) |
| 305 |
def release(space, w_cdata): |
| 306 |
w_cdata.enter_exit(True) |
| 307 |
|
| 308 |
class OffsetInBytes(W_Root): |
| 309 |
def __init__(self, bytes_w, offset): |
| 310 |
self.bytes = bytes_w |
| 311 |
self.offset = offset |
| 312 |
|
| 313 |
OffsetInBytes.typedef = TypeDef( |
| 314 |
'_cffi_backend._OffsetInBytes', |
| 315 |
) |
| 316 |
|
| 317 |
@unwrap_spec(offset=int) |
| 318 |
def offset_in_bytes(space, w_bytes, offset): |
| 319 |
if not space.isinstance_w(w_bytes, space.w_bytes): |
| 320 |
raise oefmt(space.w_TypeError, "must be bytes, not %T", w_bytes) |
| 321 |
return OffsetInBytes(space.bytes_w(w_bytes), offset) |