| 1 |
from rpython.rlib import jit |
| 2 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 3 |
from rpython.rlib.rdynload import DLLHANDLE |
| 4 |
|
| 5 |
from pypy.interpreter.error import OperationError, oefmt |
| 6 |
from pypy.interpreter.baseobjspace import W_Root |
| 7 |
from pypy.interpreter.typedef import TypeDef |
| 8 |
from pypy.interpreter.gateway import interp2app |
| 9 |
|
| 10 |
from pypy.module._cffi_backend import parse_c_type, realize_c_type |
| 11 |
from pypy.module._cffi_backend import cffi_opcode, cglob |
| 12 |
from pypy.module._cffi_backend.realize_c_type import getop, getarg |
| 13 |
from pypy.module._cffi_backend.cdataobj import W_CData |
| 14 |
from pypy.module._cffi_backend.ctypefunc import W_CTypeFunc |
| 15 |
from pypy.module._cffi_backend.wrapper import W_FunctionWrapper |
| 16 |
|
| 17 |
|
| 18 |
class W_LibObject(W_Root): |
| 19 |
libhandle = rffi.cast(DLLHANDLE, 0) |
| 20 |
|
| 21 |
def __init__(self, ffi, libname): |
| 22 |
self.space = ffi.space |
| 23 |
self.ctx = ffi.ctxobj.ctx |
| 24 |
self.ffi = ffi |
| 25 |
self.dict_w = {} |
| 26 |
self.libname = libname |
| 27 |
|
| 28 |
def descr_repr(self): |
| 29 |
return self.space.newtext("<Lib object for '%s'>" % self.libname) |
| 30 |
|
| 31 |
def make_includes_from(self, c_includes): |
| 32 |
space = self.space |
| 33 |
num = 0 |
| 34 |
includes = [] |
| 35 |
while c_includes[num]: |
| 36 |
include_name = rffi.constcharp2str(c_includes[num]) |
| 37 |
try: |
| 38 |
w_lib1 = space.appexec([space.newtext(include_name)], """(modname): |
| 39 |
mod = __import__(modname, None, None, ['ffi', 'lib']) |
| 40 |
return mod.lib""") |
| 41 |
lib1 = space.interp_w(W_LibObject, w_lib1) |
| 42 |
except OperationError as e: |
| 43 |
if e.async(space): |
| 44 |
raise |
| 45 |
raise oefmt(space.w_ImportError, |
| 46 |
"while loading %s: failed to import ffi, lib from %s", |
| 47 |
self.libname, include_name) |
| 48 |
includes.append((lib1.ffi, lib1)) |
| 49 |
num += 1 |
| 50 |
self.ffi.included_ffis_libs = includes[:] |
| 51 |
|
| 52 |
def _build_cpython_func(self, g, fnname): |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
rawfunctype = realize_c_type.realize_c_type_or_func( |
| 60 |
self.ffi, self.ctx.c_types, getarg(g.c_type_op)) |
| 61 |
assert isinstance(rawfunctype, realize_c_type.W_RawFuncType) |
| 62 |
|
| 63 |
rawfunctype.prepare_nostruct_fnptr(self.ffi) |
| 64 |
|
| 65 |
ptr = rffi.cast(rffi.CCHARP, g.c_address) |
| 66 |
assert ptr |
| 67 |
direct_fn = rffi.cast(rffi.CCHARP, g.c_size_or_direct_fn) |
| 68 |
return W_FunctionWrapper(self.space, self.ffi, |
| 69 |
ptr, direct_fn, |
| 70 |
rawfunctype, fnname, self.libname) |
| 71 |
|
| 72 |
@jit.elidable_promote() |
| 73 |
def _get_attr_elidable(self, attr): |
| 74 |
return self.dict_w[attr] |
| 75 |
|
| 76 |
@jit.dont_look_inside |
| 77 |
def _build_attr(self, attr): |
| 78 |
index = parse_c_type.search_in_globals(self.ctx, attr) |
| 79 |
if index < 0: |
| 80 |
for ffi1, lib1 in self.ffi.included_ffis_libs: |
| 81 |
if lib1 is not None: |
| 82 |
try: |
| 83 |
w_result = lib1._get_attr_elidable(attr) |
| 84 |
break |
| 85 |
except KeyError: |
| 86 |
w_result = lib1._build_attr(attr) |
| 87 |
if w_result is not None: |
| 88 |
break |
| 89 |
else: |
| 90 |
w_result = ffi1.fetch_int_constant(attr) |
| 91 |
if w_result is not None: |
| 92 |
break |
| 93 |
else: |
| 94 |
return None |
| 95 |
else: |
| 96 |
space = self.space |
| 97 |
g = rffi.cast(rffi.CArrayPtr(parse_c_type.GLOBAL_S), self.ctx.c_globals)[index] |
| 98 |
op = getop(g.c_type_op) |
| 99 |
if (op == cffi_opcode.OP_CPYTHON_BLTN_V or |
| 100 |
op == cffi_opcode.OP_CPYTHON_BLTN_N or |
| 101 |
op == cffi_opcode.OP_CPYTHON_BLTN_O): |
| 102 |
|
| 103 |
w_result = self._build_cpython_func(g, attr) |
| 104 |
|
| 105 |
elif op == cffi_opcode.OP_GLOBAL_VAR: |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
w_ct = realize_c_type.realize_c_type( |
| 110 |
self.ffi, self.ctx.c_types, getarg(g.c_type_op)) |
| 111 |
g_size = rffi.cast(lltype.Signed, g.c_size_or_direct_fn) |
| 112 |
if g_size != w_ct.size and g_size != 0 and w_ct.size > 0: |
| 113 |
raise oefmt(self.ffi.w_FFIError, |
| 114 |
"global variable '%s' should be %d bytes " |
| 115 |
"according to the cdef, but is actually %d", |
| 116 |
attr, w_ct.size, g_size) |
| 117 |
ptr = rffi.cast(rffi.CCHARP, g.c_address) |
| 118 |
if not ptr: |
| 119 |
ptr = self.cdlopen_fetch(attr) |
| 120 |
w_result = cglob.W_GlobSupport(space, attr, w_ct, ptr=ptr) |
| 121 |
|
| 122 |
elif op == cffi_opcode.OP_GLOBAL_VAR_F: |
| 123 |
w_ct = realize_c_type.realize_c_type( |
| 124 |
self.ffi, self.ctx.c_types, getarg(g.c_type_op)) |
| 125 |
w_result = cglob.W_GlobSupport(space, attr, w_ct, |
| 126 |
fetch_addr=g.c_address) |
| 127 |
|
| 128 |
elif (op == cffi_opcode.OP_CONSTANT_INT or |
| 129 |
op == cffi_opcode.OP_ENUM): |
| 130 |
|
| 131 |
|
| 132 |
w_result = realize_c_type.realize_global_int(self.ffi, g, |
| 133 |
index) |
| 134 |
|
| 135 |
elif (op == cffi_opcode.OP_CONSTANT or |
| 136 |
op == cffi_opcode.OP_DLOPEN_CONST): |
| 137 |
|
| 138 |
w_ct = realize_c_type.realize_c_type( |
| 139 |
self.ffi, self.ctx.c_types, getarg(g.c_type_op)) |
| 140 |
fetch_funcptr = rffi.cast( |
| 141 |
realize_c_type.FUNCPTR_FETCH_CHARP, |
| 142 |
g.c_address) |
| 143 |
if w_ct.size <= 0: |
| 144 |
raise oefmt(self.ffi.w_FFIError, |
| 145 |
"constant '%s' is of type '%s', " |
| 146 |
"whose size is not known", attr, w_ct.name) |
| 147 |
raise oefmt(space.w_SystemError, |
| 148 |
"constant has no known size") |
| 149 |
if not fetch_funcptr: |
| 150 |
assert op == cffi_opcode.OP_DLOPEN_CONST |
| 151 |
ptr = self.cdlopen_fetch(attr) |
| 152 |
else: |
| 153 |
assert op == cffi_opcode.OP_CONSTANT |
| 154 |
ptr = lltype.malloc(rffi.CCHARP.TO, w_ct.size, flavor='raw') |
| 155 |
self.ffi._finalizer.free_mems.append(ptr) |
| 156 |
fetch_funcptr(ptr) |
| 157 |
w_result = w_ct.convert_to_object(ptr) |
| 158 |
|
| 159 |
elif op == cffi_opcode.OP_DLOPEN_FUNC: |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
ptr = self.cdlopen_fetch(attr) |
| 165 |
w_ct = realize_c_type.realize_c_type_or_func( |
| 166 |
self.ffi, self.ctx.c_types, getarg(g.c_type_op)) |
| 167 |
|
| 168 |
assert isinstance(w_ct, realize_c_type.W_RawFuncType) |
| 169 |
w_ctfnptr = w_ct.unwrap_as_fnptr(self.ffi) |
| 170 |
w_result = W_CData(self.space, ptr, w_ctfnptr) |
| 171 |
|
| 172 |
|
| 173 |
elif op == cffi_opcode.OP_EXTERN_PYTHON: |
| 174 |
|
| 175 |
|
| 176 |
w_ct = realize_c_type.realize_c_type( |
| 177 |
self.ffi, self.ctx.c_types, getarg(g.c_type_op)) |
| 178 |
ptr = lltype.direct_fieldptr(g, 'c_size_or_direct_fn') |
| 179 |
w_result = w_ct.convert_to_object(rffi.cast(rffi.CCHARP, ptr)) |
| 180 |
else: |
| 181 |
raise oefmt(space.w_NotImplementedError, |
| 182 |
"in lib_build_attr: op=%d", op) |
| 183 |
|
| 184 |
assert w_result is not None |
| 185 |
self.dict_w[attr] = w_result |
| 186 |
return w_result |
| 187 |
|
| 188 |
def _get_attr(self, w_attr, is_getattr=False): |
| 189 |
attr = self.space.text_w(w_attr) |
| 190 |
try: |
| 191 |
w_value = self._get_attr_elidable(attr) |
| 192 |
except KeyError: |
| 193 |
w_value = self._build_attr(attr) |
| 194 |
if w_value is None: |
| 195 |
if is_getattr and attr == '__all__': |
| 196 |
return self.dir1(ignore_global_vars=True) |
| 197 |
if is_getattr and attr == '__dict__': |
| 198 |
return self.full_dict_copy() |
| 199 |
if is_getattr and attr == '__class__': |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
from pypy.interpreter.module import Module |
| 204 |
return self.space.gettypeobject(Module.typedef) |
| 205 |
if is_getattr and attr == '__name__': |
| 206 |
return self.space.newtext("%s.lib" % self.libname) |
| 207 |
if is_getattr and attr == '__loader__': |
| 208 |
return self.space.w_None |
| 209 |
if is_getattr and attr == '__spec__': |
| 210 |
return self.space.w_None |
| 211 |
raise oefmt(self.space.w_AttributeError, |
| 212 |
"cffi library '%s' has no function, constant " |
| 213 |
"or global variable named '%s'", |
| 214 |
self.libname, attr) |
| 215 |
return w_value |
| 216 |
|
| 217 |
def descr_getattribute(self, w_attr): |
| 218 |
w_value = self._get_attr(w_attr, is_getattr=True) |
| 219 |
if isinstance(w_value, cglob.W_GlobSupport): |
| 220 |
w_value = w_value.read_global_var() |
| 221 |
return w_value |
| 222 |
|
| 223 |
def descr_setattr(self, w_attr, w_newvalue): |
| 224 |
w_value = self._get_attr(w_attr) |
| 225 |
if isinstance(w_value, cglob.W_GlobSupport): |
| 226 |
w_value.write_global_var(w_newvalue) |
| 227 |
else: |
| 228 |
raise oefmt(self.space.w_AttributeError, |
| 229 |
"cannot write to function or constant '%s'", |
| 230 |
self.space.text_w(w_attr)) |
| 231 |
|
| 232 |
def descr_delattr(self, w_attr): |
| 233 |
self._get_attr(w_attr) |
| 234 |
raise oefmt(self.space.w_AttributeError, |
| 235 |
"C attribute cannot be deleted") |
| 236 |
|
| 237 |
def descr_dir(self): |
| 238 |
return self.dir1() |
| 239 |
|
| 240 |
def dir1(self, ignore_global_vars=False): |
| 241 |
space = self.space |
| 242 |
total = rffi.getintfield(self.ctx, 'c_num_globals') |
| 243 |
g = rffi.cast(rffi.CArrayPtr(parse_c_type.GLOBAL_S), self.ctx.c_globals) |
| 244 |
names_w = [] |
| 245 |
for i in range(total): |
| 246 |
if ignore_global_vars: |
| 247 |
op = getop(g[i].c_type_op) |
| 248 |
if (op == cffi_opcode.OP_GLOBAL_VAR or |
| 249 |
op == cffi_opcode.OP_GLOBAL_VAR_F): |
| 250 |
continue |
| 251 |
names_w.append(space.newtext(rffi.constcharp2str(g[i].c_name))) |
| 252 |
return space.newlist(names_w) |
| 253 |
|
| 254 |
def full_dict_copy(self): |
| 255 |
space = self.space |
| 256 |
total = rffi.getintfield(self.ctx, 'c_num_globals') |
| 257 |
g = rffi.cast(rffi.CArrayPtr(parse_c_type.GLOBAL_S), self.ctx.c_globals) |
| 258 |
w_result = space.newdict() |
| 259 |
for i in range(total): |
| 260 |
w_attr = space.newtext(rffi.constcharp2str(g[i].c_name)) |
| 261 |
w_value = self._get_attr(w_attr) |
| 262 |
space.setitem(w_result, w_attr, w_value) |
| 263 |
return w_result |
| 264 |
|
| 265 |
def address_of_func_or_global_var(self, varname): |
| 266 |
|
| 267 |
|
| 268 |
space = self.space |
| 269 |
w_value = self._get_attr(space.newtext(varname)) |
| 270 |
if isinstance(w_value, cglob.W_GlobSupport): |
| 271 |
|
| 272 |
return w_value.address() |
| 273 |
|
| 274 |
if isinstance(w_value, W_FunctionWrapper): |
| 275 |
|
| 276 |
return w_value.try_extract_direct_fnptr_as_cdata(space) |
| 277 |
|
| 278 |
if (isinstance(w_value, W_CData) and |
| 279 |
isinstance(w_value.ctype, W_CTypeFunc)): |
| 280 |
|
| 281 |
return w_value |
| 282 |
|
| 283 |
raise oefmt(space.w_AttributeError, |
| 284 |
"cannot take the address of the constant '%s'", varname) |
| 285 |
|
| 286 |
def cdlopen_fetch(self, name): |
| 287 |
raise NotImplementedError |
| 288 |
|
| 289 |
def cdlopen_close(self): |
| 290 |
raise oefmt(self.ffi.w_FFIError, |
| 291 |
"library '%s' was not created with ffi.dlopen()", |
| 292 |
self.libname) |
| 293 |
|
| 294 |
|
| 295 |
W_LibObject.typedef = TypeDef( |
| 296 |
'_cffi_backend.Lib', |
| 297 |
__repr__ = interp2app(W_LibObject.descr_repr), |
| 298 |
__getattribute__ = interp2app(W_LibObject.descr_getattribute), |
| 299 |
__setattr__ = interp2app(W_LibObject.descr_setattr), |
| 300 |
__delattr__ = interp2app(W_LibObject.descr_delattr), |
| 301 |
__dir__ = interp2app(W_LibObject.descr_dir), |
| 302 |
) |
| 303 |
W_LibObject.typedef.acceptable_as_base_class = False |