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