| 1 |
|
| 2 |
/* A Lib object is what is in the "lib" attribute of a C extension |
| 3 |
module originally created by recompile(). |
| 4 |
|
| 5 |
A Lib object is special in the sense that it has a custom |
| 6 |
__getattr__ which returns C globals, functions and constants. The |
| 7 |
original idea was to raise AttributeError for anything else, even |
| 8 |
attrs like '__class__', but it breaks various things; now, standard |
| 9 |
attrs are returned, but in the unlikely case where a user cdef()s |
| 10 |
the same name, then the standard attr is hidden (and the various |
| 11 |
things like introspection might break). |
| 12 |
|
| 13 |
A Lib object has got a reference to the _cffi_type_context_s |
| 14 |
structure, which is used to create lazily the objects returned by |
| 15 |
__getattr__. |
| 16 |
*/ |
| 17 |
|
| 18 |
struct CPyExtFunc_s { |
| 19 |
PyMethodDef md; |
| 20 |
void *direct_fn; |
| 21 |
PyObject *direct_fn_cdata; |
| 22 |
int type_index; |
| 23 |
char doc[1]; |
| 24 |
}; |
| 25 |
|
| 26 |
struct LibObject_s { |
| 27 |
PyObject_HEAD |
| 28 |
builder_c_t *l_types_builder; /* same as the one on the ffi object */ |
| 29 |
PyObject *l_dict; /* content, built lazily */ |
| 30 |
PyObject *l_libname; /* some string that gives the name of the lib */ |
| 31 |
FFIObject *l_ffi; /* reference back to the ffi object */ |
| 32 |
void *l_libhandle; /* the dlopen()ed handle, if any */ |
| 33 |
int l_auto_close; /* if we must dlclose() this handle */ |
| 34 |
}; |
| 35 |
|
| 36 |
static struct CPyExtFunc_s *_cpyextfunc_get(PyObject *x) |
| 37 |
{ |
| 38 |
PyObject *y; |
| 39 |
LibObject *lo; |
| 40 |
PyCFunctionObject *fo; |
| 41 |
|
| 42 |
if (!PyCFunction_Check(x)) |
| 43 |
return NULL; |
| 44 |
y = PyCFunction_GET_SELF(x); |
| 45 |
if (!LibObject_Check(y)) |
| 46 |
return NULL; |
| 47 |
|
| 48 |
fo = (PyCFunctionObject *)x; |
| 49 |
lo = (LibObject *)y; |
| 50 |
if (lo->l_libname != fo->m_module) |
| 51 |
return NULL; |
| 52 |
|
| 53 |
return (struct CPyExtFunc_s *)(fo->m_ml); |
| 54 |
} |
| 55 |
|
| 56 |
static PyObject *_cpyextfunc_type(LibObject *lib, struct CPyExtFunc_s *exf) |
| 57 |
{ |
| 58 |
PyObject *tuple, *result; |
| 59 |
tuple = realize_c_type_or_func(lib->l_types_builder, |
| 60 |
lib->l_types_builder->ctx.types, |
| 61 |
exf->type_index); |
| 62 |
if (tuple == NULL) |
| 63 |
return NULL; |
| 64 |
|
| 65 |
/* 'tuple' is a tuple of length 1 containing the real CT_FUNCTIONPTR |
| 66 |
object */ |
| 67 |
result = PyTuple_GetItem(tuple, 0); |
| 68 |
Py_XINCREF(result); |
| 69 |
Py_DECREF(tuple); |
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
static PyObject *_cpyextfunc_type_index(PyObject *x) |
| 74 |
{ |
| 75 |
struct CPyExtFunc_s *exf; |
| 76 |
LibObject *lib; |
| 77 |
|
| 78 |
assert(PyErr_Occurred()); |
| 79 |
exf = _cpyextfunc_get(x); |
| 80 |
if (exf == NULL) |
| 81 |
return NULL; /* still the same exception is set */ |
| 82 |
|
| 83 |
PyErr_Clear(); |
| 84 |
|
| 85 |
lib = (LibObject *)PyCFunction_GET_SELF(x); |
| 86 |
return _cpyextfunc_type(lib, exf); |
| 87 |
} |
| 88 |
|
| 89 |
static void cdlopen_close_ignore_errors(void *libhandle); /* forward */ |
| 90 |
static void *cdlopen_fetch(PyObject *libname, void *libhandle, |
| 91 |
const char *symbol); |
| 92 |
|
| 93 |
static void lib_dealloc(LibObject *lib) |
| 94 |
{ |
| 95 |
PyObject_GC_UnTrack(lib); |
| 96 |
if (lib->l_auto_close) |
| 97 |
cdlopen_close_ignore_errors(lib->l_libhandle); |
| 98 |
Py_DECREF(lib->l_dict); |
| 99 |
Py_DECREF(lib->l_libname); |
| 100 |
Py_DECREF(lib->l_ffi); |
| 101 |
PyObject_GC_Del(lib); |
| 102 |
} |
| 103 |
|
| 104 |
static int lib_traverse(LibObject *lib, visitproc visit, void *arg) |
| 105 |
{ |
| 106 |
Py_VISIT(lib->l_dict); |
| 107 |
Py_VISIT(lib->l_libname); |
| 108 |
Py_VISIT(lib->l_ffi); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
static PyObject *lib_repr(LibObject *lib) |
| 113 |
{ |
| 114 |
return PyUnicode_FromFormat("<Lib object for '%.200s'>", |
| 115 |
PyUnicode_AsUTF8(lib->l_libname)); |
| 116 |
} |
| 117 |
|
| 118 |
static PyObject *lib_build_cpython_func(LibObject *lib, |
| 119 |
const struct _cffi_global_s *g, |
| 120 |
const char *s, int flags) |
| 121 |
{ |
| 122 |
/* First make sure the argument types and return type are really |
| 123 |
built. The C extension code can then assume that they are, |
| 124 |
by calling _cffi_type(). |
| 125 |
*/ |
| 126 |
PyObject *result = NULL; |
| 127 |
CTypeDescrObject **pfargs = NULL; |
| 128 |
CTypeDescrObject *fresult; |
| 129 |
Py_ssize_t nargs = 0; |
| 130 |
struct CPyExtFunc_s *xfunc; |
| 131 |
int i, type_index = _CFFI_GETARG(g->type_op); |
| 132 |
_cffi_opcode_t *opcodes = lib->l_types_builder->ctx.types; |
| 133 |
static const char *const format = ";\n\nCFFI C function from %s.lib"; |
| 134 |
const char *libname = PyUnicode_AsUTF8(lib->l_libname); |
| 135 |
struct funcbuilder_s funcbuilder; |
| 136 |
|
| 137 |
/* return type: */ |
| 138 |
fresult = realize_c_func_return_type(lib->l_types_builder, opcodes, |
| 139 |
type_index); |
| 140 |
if (fresult == NULL) |
| 141 |
goto error; |
| 142 |
|
| 143 |
/* argument types: */ |
| 144 |
/* note that if the arguments are already built, they have a |
| 145 |
pointer in the 'opcodes' array, and GETOP() returns a |
| 146 |
random even value. But OP_FUNCTION_END is odd, so the |
| 147 |
condition below still works correctly. */ |
| 148 |
i = type_index + 1; |
| 149 |
while (_CFFI_GETOP(_CFFI_LOAD_OP(opcodes[i])) != _CFFI_OP_FUNCTION_END) |
| 150 |
i++; |
| 151 |
pfargs = alloca(sizeof(CTypeDescrObject *) * (i - type_index - 1)); |
| 152 |
i = type_index + 1; |
| 153 |
while (_CFFI_GETOP(_CFFI_LOAD_OP(opcodes[i])) != _CFFI_OP_FUNCTION_END) { |
| 154 |
CTypeDescrObject *ct = realize_c_type(lib->l_types_builder, opcodes, i); |
| 155 |
if (ct == NULL) |
| 156 |
goto error; |
| 157 |
pfargs[nargs++] = ct; |
| 158 |
i++; |
| 159 |
} |
| 160 |
|
| 161 |
memset(&funcbuilder, 0, sizeof(funcbuilder)); |
| 162 |
if (fb_build_name(&funcbuilder, g->name, pfargs, nargs, fresult, 0) < 0) |
| 163 |
goto error; |
| 164 |
|
| 165 |
/* The few bytes of memory we allocate here appear to leak, but |
| 166 |
this is not a real leak. Indeed, CPython never unloads its C |
| 167 |
extension modules. There is only one PyMem_Malloc() per real |
| 168 |
C function in a CFFI C extension module. That means that this |
| 169 |
PyMem_Malloc() could also have been written with a static |
| 170 |
global variable generated for each CPYTHON_BLTN defined in the |
| 171 |
C extension, and the effect would be the same (but a bit more |
| 172 |
complicated). |
| 173 |
*/ |
| 174 |
xfunc = PyMem_Malloc(sizeof(struct CPyExtFunc_s) + |
| 175 |
funcbuilder.nb_bytes + |
| 176 |
strlen(format) + strlen(libname)); |
| 177 |
if (xfunc == NULL) { |
| 178 |
PyErr_NoMemory(); |
| 179 |
goto error; |
| 180 |
} |
| 181 |
memset((char *)xfunc, 0, sizeof(struct CPyExtFunc_s)); |
| 182 |
assert(g->address); |
| 183 |
xfunc->md.ml_meth = (PyCFunction)g->address; |
| 184 |
xfunc->md.ml_flags = flags; |
| 185 |
xfunc->md.ml_name = g->name; |
| 186 |
xfunc->md.ml_doc = xfunc->doc; |
| 187 |
xfunc->direct_fn = g->size_or_direct_fn; |
| 188 |
xfunc->type_index = type_index; |
| 189 |
|
| 190 |
/* build the docstring */ |
| 191 |
funcbuilder.bufferp = xfunc->doc; |
| 192 |
if (fb_build_name(&funcbuilder, g->name, pfargs, nargs, fresult, 0) < 0) |
| 193 |
goto error; |
| 194 |
sprintf(funcbuilder.bufferp - 1, format, libname); |
| 195 |
/* done building the docstring */ |
| 196 |
|
| 197 |
result = PyCFunction_NewEx(&xfunc->md, (PyObject *)lib, lib->l_libname); |
| 198 |
/* fall-through */ |
| 199 |
error: |
| 200 |
Py_XDECREF(fresult); |
| 201 |
while (nargs > 0) { |
| 202 |
--nargs; |
| 203 |
Py_DECREF(pfargs[nargs]); |
| 204 |
} |
| 205 |
return result; |
| 206 |
} |
| 207 |
|
| 208 |
static PyObject *lib_build_and_cache_attr(LibObject *lib, PyObject *name, |
| 209 |
int recursion) |
| 210 |
{ |
| 211 |
/* does not return a new reference! */ |
| 212 |
PyObject *x; |
| 213 |
int index; |
| 214 |
const struct _cffi_global_s *g; |
| 215 |
CTypeDescrObject *ct; |
| 216 |
builder_c_t *types_builder = lib->l_types_builder; |
| 217 |
const char *s = PyUnicode_AsUTF8(name); |
| 218 |
if (s == NULL) |
| 219 |
return NULL; |
| 220 |
|
| 221 |
index = search_in_globals(&types_builder->ctx, s, strlen(s)); |
| 222 |
if (index < 0) { |
| 223 |
|
| 224 |
if (types_builder->included_libs != NULL) { |
| 225 |
Py_ssize_t i; |
| 226 |
PyObject *included_ffis = types_builder->included_ffis; |
| 227 |
PyObject *included_libs = types_builder->included_libs; |
| 228 |
|
| 229 |
if (recursion > 100) { |
| 230 |
PyErr_SetString(PyExc_RuntimeError, |
| 231 |
"recursion overflow in ffi.include() delegations"); |
| 232 |
return NULL; |
| 233 |
} |
| 234 |
|
| 235 |
for (i = 0; i < PyTuple_GET_SIZE(included_libs); i++) { |
| 236 |
LibObject *lib1; |
| 237 |
|
| 238 |
lib1 = (LibObject *)PyTuple_GET_ITEM(included_libs, i); |
| 239 |
if (lib1 != NULL) { |
| 240 |
x = PyDict_GetItem(lib1->l_dict, name); |
| 241 |
if (x != NULL) { |
| 242 |
Py_INCREF(x); |
| 243 |
goto found; |
| 244 |
} |
| 245 |
x = lib_build_and_cache_attr(lib1, name, recursion + 1); |
| 246 |
if (x != NULL) { |
| 247 |
Py_INCREF(x); |
| 248 |
goto found; |
| 249 |
} |
| 250 |
} |
| 251 |
else { |
| 252 |
FFIObject *ffi1; |
| 253 |
|
| 254 |
ffi1 = (FFIObject *)PyTuple_GetItem(included_ffis, i); |
| 255 |
if (ffi1 == NULL) |
| 256 |
return NULL; |
| 257 |
x = ffi_fetch_int_constant(ffi1, s, recursion + 1); |
| 258 |
if (x != NULL) |
| 259 |
goto found; |
| 260 |
} |
| 261 |
if (PyErr_Occurred()) |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (recursion > 0) |
| 267 |
return NULL; /* no error set, continue looking elsewhere */ |
| 268 |
|
| 269 |
PyErr_Format(PyExc_AttributeError, |
| 270 |
"cffi library '%.200s' has no function, constant " |
| 271 |
"or global variable named '%.200s'", |
| 272 |
PyUnicode_AsUTF8(lib->l_libname), s); |
| 273 |
return NULL; |
| 274 |
} |
| 275 |
|
| 276 |
g = &types_builder->ctx.globals[index]; |
| 277 |
|
| 278 |
switch (_CFFI_GETOP(g->type_op)) { |
| 279 |
|
| 280 |
case _CFFI_OP_CPYTHON_BLTN_V: |
| 281 |
x = lib_build_cpython_func(lib, g, s, METH_VARARGS); |
| 282 |
break; |
| 283 |
|
| 284 |
case _CFFI_OP_CPYTHON_BLTN_N: |
| 285 |
x = lib_build_cpython_func(lib, g, s, METH_NOARGS); |
| 286 |
break; |
| 287 |
|
| 288 |
case _CFFI_OP_CPYTHON_BLTN_O: |
| 289 |
x = lib_build_cpython_func(lib, g, s, METH_O); |
| 290 |
break; |
| 291 |
|
| 292 |
case _CFFI_OP_CONSTANT_INT: |
| 293 |
case _CFFI_OP_ENUM: |
| 294 |
{ |
| 295 |
/* a constant integer whose value, in an "unsigned long long", |
| 296 |
is obtained by calling the function at g->address */ |
| 297 |
x = realize_global_int(types_builder, index); |
| 298 |
break; |
| 299 |
} |
| 300 |
|
| 301 |
case _CFFI_OP_CONSTANT: |
| 302 |
case _CFFI_OP_DLOPEN_CONST: |
| 303 |
{ |
| 304 |
/* a constant which is not of integer type */ |
| 305 |
char *data; |
| 306 |
ct = realize_c_type(types_builder, types_builder->ctx.types, |
| 307 |
_CFFI_GETARG(g->type_op)); |
| 308 |
if (ct == NULL) |
| 309 |
return NULL; |
| 310 |
|
| 311 |
if (ct->ct_size <= 0) { |
| 312 |
PyErr_Format(FFIError, "constant '%s' is of type '%s', " |
| 313 |
"whose size is not known", s, ct->ct_name); |
| 314 |
return NULL; |
| 315 |
} |
| 316 |
if (g->address == NULL) { |
| 317 |
/* for dlopen() style */ |
| 318 |
assert(_CFFI_GETOP(g->type_op) == _CFFI_OP_DLOPEN_CONST); |
| 319 |
data = cdlopen_fetch(lib->l_libname, lib->l_libhandle, s); |
| 320 |
if (data == NULL) |
| 321 |
return NULL; |
| 322 |
} |
| 323 |
else { |
| 324 |
/* The few bytes of memory we allocate here appear to leak, but |
| 325 |
this is not a real leak. Indeed, CPython never unloads its C |
| 326 |
extension modules. There is only one PyMem_Malloc() per real |
| 327 |
non-integer C constant in a CFFI C extension module. That |
| 328 |
means that this PyMem_Malloc() could also have been written |
| 329 |
with a static global variable generated for each OP_CONSTANT |
| 330 |
defined in the C extension, and the effect would be the same |
| 331 |
(but a bit more complicated). |
| 332 |
|
| 333 |
Note that we used to do alloca(), but see issue #198. We |
| 334 |
could still do alloca(), or explicit PyMem_Free(), in some |
| 335 |
cases; but there is no point and it only makes the remaining |
| 336 |
less-common cases more suspicious. |
| 337 |
*/ |
| 338 |
assert(_CFFI_GETOP(g->type_op) == _CFFI_OP_CONSTANT); |
| 339 |
data = PyMem_Malloc(ct->ct_size); |
| 340 |
if (data == NULL) { |
| 341 |
PyErr_NoMemory(); |
| 342 |
return NULL; |
| 343 |
} |
| 344 |
((void(*)(char*))g->address)(data); |
| 345 |
} |
| 346 |
x = convert_to_object(data, ct); |
| 347 |
Py_DECREF(ct); |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
case _CFFI_OP_GLOBAL_VAR: |
| 352 |
{ |
| 353 |
/* global variable of the exact type specified here |
| 354 |
(nowadays, only used by the ABI mode or backward |
| 355 |
compatibility; see _CFFI_OP_GLOBAL_VAR_F for the API mode) |
| 356 |
*/ |
| 357 |
Py_ssize_t g_size = (Py_ssize_t)g->size_or_direct_fn; |
| 358 |
ct = realize_c_type(types_builder, types_builder->ctx.types, |
| 359 |
_CFFI_GETARG(g->type_op)); |
| 360 |
if (ct == NULL) |
| 361 |
return NULL; |
| 362 |
if (g_size != ct->ct_size && g_size != 0 && ct->ct_size > 0) { |
| 363 |
PyErr_Format(FFIError, |
| 364 |
"global variable '%.200s' should be %zd bytes " |
| 365 |
"according to the cdef, but is actually %zd", |
| 366 |
s, ct->ct_size, g_size); |
| 367 |
x = NULL; |
| 368 |
} |
| 369 |
else { |
| 370 |
void *address = g->address; |
| 371 |
if (address == NULL) { |
| 372 |
/* for dlopen() style */ |
| 373 |
address = cdlopen_fetch(lib->l_libname, lib->l_libhandle, s); |
| 374 |
if (address == NULL) |
| 375 |
return NULL; |
| 376 |
} |
| 377 |
x = make_global_var(name, ct, address, NULL); |
| 378 |
} |
| 379 |
Py_DECREF(ct); |
| 380 |
break; |
| 381 |
} |
| 382 |
|
| 383 |
case _CFFI_OP_GLOBAL_VAR_F: |
| 384 |
ct = realize_c_type(types_builder, types_builder->ctx.types, |
| 385 |
_CFFI_GETARG(g->type_op)); |
| 386 |
if (ct == NULL) |
| 387 |
return NULL; |
| 388 |
x = make_global_var(name, ct, NULL, (gs_fetch_addr_fn)g->address); |
| 389 |
Py_DECREF(ct); |
| 390 |
break; |
| 391 |
|
| 392 |
case _CFFI_OP_DLOPEN_FUNC: |
| 393 |
{ |
| 394 |
/* For dlopen(): the function of the given 'name'. We use |
| 395 |
dlsym() to get the address of something in the dynamic |
| 396 |
library, which we interpret as being exactly a function of |
| 397 |
the specified type. |
| 398 |
*/ |
| 399 |
PyObject *ct1; |
| 400 |
void *address = cdlopen_fetch(lib->l_libname, lib->l_libhandle, s); |
| 401 |
if (address == NULL) |
| 402 |
return NULL; |
| 403 |
|
| 404 |
ct1 = realize_c_type_or_func(types_builder, |
| 405 |
types_builder->ctx.types, |
| 406 |
_CFFI_GETARG(g->type_op)); |
| 407 |
if (ct1 == NULL) |
| 408 |
return NULL; |
| 409 |
|
| 410 |
assert(!CTypeDescr_Check(ct1)); /* must be a function */ |
| 411 |
x = new_simple_cdata(address, unwrap_fn_as_fnptr(ct1)); |
| 412 |
|
| 413 |
Py_DECREF(ct1); |
| 414 |
break; |
| 415 |
} |
| 416 |
|
| 417 |
case _CFFI_OP_EXTERN_PYTHON: |
| 418 |
/* for reading 'lib.bar' where bar is declared with extern "Python" */ |
| 419 |
ct = realize_c_type(types_builder, types_builder->ctx.types, |
| 420 |
_CFFI_GETARG(g->type_op)); |
| 421 |
if (ct == NULL) |
| 422 |
return NULL; |
| 423 |
x = convert_to_object((char *)&g->size_or_direct_fn, ct); |
| 424 |
Py_DECREF(ct); |
| 425 |
break; |
| 426 |
|
| 427 |
default: |
| 428 |
PyErr_Format(PyExc_NotImplementedError, "in lib_build_attr: op=%d", |
| 429 |
(int)_CFFI_GETOP(g->type_op)); |
| 430 |
return NULL; |
| 431 |
} |
| 432 |
|
| 433 |
found: |
| 434 |
if (x != NULL) { |
| 435 |
int err = PyDict_SetItem(lib->l_dict, name, x); |
| 436 |
Py_DECREF(x); |
| 437 |
if (err < 0) /* else there is still one ref left in the dict */ |
| 438 |
return NULL; |
| 439 |
} |
| 440 |
return x; |
| 441 |
} |
| 442 |
|
| 443 |
#define LIB_GET_OR_CACHE_ADDR(x, lib, name, error) \ |
| 444 |
do { \ |
| 445 |
x = PyDict_GetItem(lib->l_dict, name); \ |
| 446 |
if (x == NULL) { \ |
| 447 |
x = lib_build_and_cache_attr(lib, name, 0); \ |
| 448 |
if (x == NULL) { \ |
| 449 |
error; \ |
| 450 |
} \ |
| 451 |
} \ |
| 452 |
} while (0) |
| 453 |
|
| 454 |
static PyObject *_lib_dir1(LibObject *lib, int ignore_global_vars) |
| 455 |
{ |
| 456 |
const struct _cffi_global_s *g = lib->l_types_builder->ctx.globals; |
| 457 |
int i, count = 0, total = lib->l_types_builder->ctx.num_globals; |
| 458 |
PyObject *s, *lst = PyList_New(total); |
| 459 |
if (lst == NULL) |
| 460 |
return NULL; |
| 461 |
|
| 462 |
for (i = 0; i < total; i++) { |
| 463 |
if (ignore_global_vars) { |
| 464 |
int op = _CFFI_GETOP(g[i].type_op); |
| 465 |
if (op == _CFFI_OP_GLOBAL_VAR || op == _CFFI_OP_GLOBAL_VAR_F) |
| 466 |
continue; |
| 467 |
} |
| 468 |
s = PyUnicode_FromString(g[i].name); |
| 469 |
if (s == NULL) |
| 470 |
goto error; |
| 471 |
PyList_SET_ITEM(lst, count, s); |
| 472 |
count++; |
| 473 |
} |
| 474 |
if (PyList_SetSlice(lst, count, total, NULL) < 0) |
| 475 |
goto error; |
| 476 |
return lst; |
| 477 |
|
| 478 |
error: |
| 479 |
Py_DECREF(lst); |
| 480 |
return NULL; |
| 481 |
} |
| 482 |
|
| 483 |
static PyObject *_lib_dict(LibObject *lib) |
| 484 |
{ |
| 485 |
const struct _cffi_global_s *g = lib->l_types_builder->ctx.globals; |
| 486 |
int i, total = lib->l_types_builder->ctx.num_globals; |
| 487 |
PyObject *name, *x, *d = PyDict_New(); |
| 488 |
if (d == NULL) |
| 489 |
return NULL; |
| 490 |
|
| 491 |
for (i = 0; i < total; i++) { |
| 492 |
name = PyUnicode_FromString(g[i].name); |
| 493 |
if (name == NULL) |
| 494 |
goto error; |
| 495 |
|
| 496 |
LIB_GET_OR_CACHE_ADDR(x, lib, name, goto error); |
| 497 |
|
| 498 |
if (PyDict_SetItem(d, name, x) < 0) |
| 499 |
goto error; |
| 500 |
Py_DECREF(name); |
| 501 |
} |
| 502 |
return d; |
| 503 |
|
| 504 |
error: |
| 505 |
Py_XDECREF(name); |
| 506 |
Py_DECREF(d); |
| 507 |
return NULL; |
| 508 |
} |
| 509 |
|
| 510 |
static PyObject *lib_getattr(LibObject *lib, PyObject *name) |
| 511 |
{ |
| 512 |
const char *p; |
| 513 |
PyObject *x; |
| 514 |
LIB_GET_OR_CACHE_ADDR(x, lib, name, goto missing); |
| 515 |
|
| 516 |
if (GlobSupport_Check(x)) { |
| 517 |
return read_global_var((GlobSupportObject *)x); |
| 518 |
} |
| 519 |
Py_INCREF(x); |
| 520 |
return x; |
| 521 |
|
| 522 |
missing: |
| 523 |
/*** ATTRIBUTEERROR IS SET HERE ***/ |
| 524 |
p = PyUnicode_AsUTF8(name); |
| 525 |
if (p == NULL) |
| 526 |
return NULL; |
| 527 |
if (strcmp(p, "__all__") == 0) { |
| 528 |
PyErr_Clear(); |
| 529 |
return _lib_dir1(lib, 1); |
| 530 |
} |
| 531 |
if (strcmp(p, "__dict__") == 0) { |
| 532 |
PyErr_Clear(); |
| 533 |
return _lib_dict(lib); |
| 534 |
} |
| 535 |
if (strcmp(p, "__class__") == 0) { |
| 536 |
PyErr_Clear(); |
| 537 |
x = (PyObject *)&PyModule_Type; |
| 538 |
/* ^^^ used to be Py_TYPE(lib). But HAAAAAACK! That makes |
| 539 |
help() behave correctly. I couldn't find a more reasonable |
| 540 |
way. Urgh. */ |
| 541 |
Py_INCREF(x); |
| 542 |
return x; |
| 543 |
} |
| 544 |
/* this hack is for Python 3.5, and also to give a more |
| 545 |
module-like behavior */ |
| 546 |
if (strcmp(p, "__name__") == 0) { |
| 547 |
PyErr_Clear(); |
| 548 |
return PyUnicode_FromFormat("%s.lib", PyUnicode_AsUTF8(lib->l_libname)); |
| 549 |
} |
| 550 |
if (strcmp(p, "__loader__") == 0 || strcmp(p, "__spec__") == 0) { |
| 551 |
/* some more module-like behavior hacks */ |
| 552 |
PyErr_Clear(); |
| 553 |
Py_INCREF(Py_None); |
| 554 |
return Py_None; |
| 555 |
} |
| 556 |
return NULL; |
| 557 |
} |
| 558 |
|
| 559 |
static int lib_setattr(LibObject *lib, PyObject *name, PyObject *val) |
| 560 |
{ |
| 561 |
PyObject *x; |
| 562 |
LIB_GET_OR_CACHE_ADDR(x, lib, name, return -1); |
| 563 |
|
| 564 |
if (val == NULL) { |
| 565 |
PyErr_SetString(PyExc_AttributeError, "C attribute cannot be deleted"); |
| 566 |
return -1; |
| 567 |
} |
| 568 |
|
| 569 |
if (GlobSupport_Check(x)) { |
| 570 |
return write_global_var((GlobSupportObject *)x, val); |
| 571 |
} |
| 572 |
|
| 573 |
PyErr_Format(PyExc_AttributeError, |
| 574 |
"cannot write to function or constant '%.200s'", |
| 575 |
PyUnicode_Check(name) ? PyUnicode_AsUTF8(name) : "?"); |
| 576 |
return -1; |
| 577 |
} |
| 578 |
|
| 579 |
static PyObject *lib_dir(PyObject *self, PyObject *noarg) |
| 580 |
{ |
| 581 |
return _lib_dir1((LibObject *)self, 0); |
| 582 |
} |
| 583 |
|
| 584 |
static PyMethodDef lib_methods[] = { |
| 585 |
{"__dir__", lib_dir, METH_NOARGS}, |
| 586 |
{NULL, NULL} /* sentinel */ |
| 587 |
}; |
| 588 |
|
| 589 |
static PyTypeObject Lib_Type = { |
| 590 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 591 |
"_cffi_backend.Lib", |
| 592 |
sizeof(LibObject), |
| 593 |
0, |
| 594 |
(destructor)lib_dealloc, /* tp_dealloc */ |
| 595 |
0, /* tp_print */ |
| 596 |
0, /* tp_getattr */ |
| 597 |
0, /* tp_setattr */ |
| 598 |
0, /* tp_compare */ |
| 599 |
(reprfunc)lib_repr, /* tp_repr */ |
| 600 |
0, /* tp_as_number */ |
| 601 |
0, /* tp_as_sequence */ |
| 602 |
0, /* tp_as_mapping */ |
| 603 |
0, /* tp_hash */ |
| 604 |
0, /* tp_call */ |
| 605 |
0, /* tp_str */ |
| 606 |
(getattrofunc)lib_getattr, /* tp_getattro */ |
| 607 |
(setattrofunc)lib_setattr, /* tp_setattro */ |
| 608 |
0, /* tp_as_buffer */ |
| 609 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 610 |
0, /* tp_doc */ |
| 611 |
(traverseproc)lib_traverse, /* tp_traverse */ |
| 612 |
0, /* tp_clear */ |
| 613 |
0, /* tp_richcompare */ |
| 614 |
0, /* tp_weaklistoffset */ |
| 615 |
0, /* tp_iter */ |
| 616 |
0, /* tp_iternext */ |
| 617 |
lib_methods, /* tp_methods */ |
| 618 |
0, /* tp_members */ |
| 619 |
0, /* tp_getset */ |
| 620 |
0, /* tp_base */ |
| 621 |
0, /* tp_dict */ |
| 622 |
0, /* tp_descr_get */ |
| 623 |
0, /* tp_descr_set */ |
| 624 |
offsetof(LibObject, l_dict), /* tp_dictoffset */ |
| 625 |
}; |
| 626 |
|
| 627 |
static LibObject *lib_internal_new(FFIObject *ffi, const char *module_name, |
| 628 |
void *dlopen_libhandle, int auto_close) |
| 629 |
{ |
| 630 |
LibObject *lib; |
| 631 |
PyObject *libname, *dict; |
| 632 |
|
| 633 |
libname = PyUnicode_FromString(module_name); |
| 634 |
if (libname == NULL) |
| 635 |
goto err1; |
| 636 |
|
| 637 |
dict = PyDict_New(); |
| 638 |
if (dict == NULL) |
| 639 |
goto err2; |
| 640 |
|
| 641 |
lib = (LibObject *)PyType_GenericAlloc(&Lib_Type, 0); |
| 642 |
if (lib == NULL) |
| 643 |
goto err3; |
| 644 |
|
| 645 |
lib->l_types_builder = &ffi->types_builder; |
| 646 |
lib->l_dict = dict; |
| 647 |
lib->l_libname = libname; |
| 648 |
Py_INCREF(ffi); |
| 649 |
lib->l_ffi = ffi; |
| 650 |
lib->l_libhandle = dlopen_libhandle; |
| 651 |
lib->l_auto_close = auto_close; |
| 652 |
return lib; |
| 653 |
|
| 654 |
err3: |
| 655 |
Py_DECREF(dict); |
| 656 |
err2: |
| 657 |
Py_DECREF(libname); |
| 658 |
err1: |
| 659 |
if (auto_close) |
| 660 |
cdlopen_close_ignore_errors(dlopen_libhandle); |
| 661 |
return NULL; |
| 662 |
} |
| 663 |
|
| 664 |
static PyObject* try_extract_directfnptr(PyObject *x) |
| 665 |
{ |
| 666 |
/* returns: borrowed ref or NULL */ |
| 667 |
LibObject *lib; |
| 668 |
PyObject *ct; |
| 669 |
struct CPyExtFunc_s *exf = _cpyextfunc_get(x); |
| 670 |
if (exf == NULL) |
| 671 |
return NULL; /* wrong type */ |
| 672 |
if (exf->direct_fn_cdata != NULL) |
| 673 |
return exf->direct_fn_cdata; /* common case: cached */ |
| 674 |
|
| 675 |
if (exf->direct_fn == NULL) |
| 676 |
return x; /* backward compatibility: no direct_fn */ |
| 677 |
|
| 678 |
lib = (LibObject *)PyCFunction_GET_SELF(x); |
| 679 |
ct = _cpyextfunc_type(lib, exf); |
| 680 |
if (ct == NULL) |
| 681 |
return NULL; /* error */ |
| 682 |
|
| 683 |
x = new_simple_cdata(exf->direct_fn, (CTypeDescrObject *)ct); |
| 684 |
Py_DECREF(ct); |
| 685 |
exf->direct_fn_cdata = x; /* caches x, which becomes immortal like exf */ |
| 686 |
return x; |
| 687 |
} |
| 688 |
|
| 689 |
static PyObject *address_of_global_var(PyObject *args) |
| 690 |
{ |
| 691 |
LibObject *lib; |
| 692 |
PyObject *x, *o_varname; |
| 693 |
char *varname; |
| 694 |
|
| 695 |
if (!PyArg_ParseTuple(args, "O!s", &Lib_Type, &lib, &varname)) |
| 696 |
return NULL; |
| 697 |
|
| 698 |
/* rebuild a string from 'varname', to do typechecks and to force |
| 699 |
a unicode back to a plain string (on python 2) */ |
| 700 |
o_varname = PyUnicode_FromString(varname); |
| 701 |
if (o_varname == NULL) |
| 702 |
return NULL; |
| 703 |
|
| 704 |
LIB_GET_OR_CACHE_ADDR(x, lib, o_varname, goto error); |
| 705 |
Py_DECREF(o_varname); |
| 706 |
if (GlobSupport_Check(x)) { |
| 707 |
return cg_addressof_global_var((GlobSupportObject *)x); |
| 708 |
} |
| 709 |
else { |
| 710 |
PyObject *func_cdata = try_extract_directfnptr(x); |
| 711 |
if (func_cdata != NULL) { |
| 712 |
/* an OP_CPYTHON_BLTN: '&func' returns a cdata */ |
| 713 |
Py_INCREF(func_cdata); |
| 714 |
return func_cdata; |
| 715 |
} |
| 716 |
if (PyErr_Occurred()) |
| 717 |
return NULL; |
| 718 |
|
| 719 |
if (CData_Check(x) && /* a constant functionptr cdata: 'f == &f' */ |
| 720 |
(((CDataObject *)x)->c_type->ct_flags & CT_FUNCTIONPTR) != 0) { |
| 721 |
Py_INCREF(x); |
| 722 |
return x; |
| 723 |
} |
| 724 |
else { |
| 725 |
PyErr_Format(PyExc_AttributeError, |
| 726 |
"cannot take the address of the constant '%.200s'", |
| 727 |
varname); |
| 728 |
return NULL; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
error: |
| 733 |
Py_DECREF(o_varname); |
| 734 |
return NULL; |
| 735 |
} |