static PyObject *ffi_dlopen(PyObject *self, PyObject *args) { char *filename_or_null, *printable_filename; void *handle; int flags = 0; if (PyTuple_GET_SIZE(args) == 0 || PyTuple_GET_ITEM(args, 0) == Py_None) { PyObject *dummy; if (!PyArg_ParseTuple(args, "|Oi:load_library", &dummy, &flags)) return NULL; filename_or_null = NULL; } else if (!PyArg_ParseTuple(args, "et|i:load_library", Py_FileSystemDefaultEncoding, &filename_or_null, &flags)) return NULL; if ((flags & (RTLD_NOW | RTLD_LAZY)) == 0) flags |= RTLD_NOW; printable_filename = filename_or_null ? filename_or_null : "<None>"; handle = dlopen(filename_or_null, flags); if (handle == NULL) { const char *error = dlerror(); PyErr_Format(PyExc_OSError, "cannot load library '%s': %s", printable_filename, error); return NULL; } return (PyObject *)lib_internal_new((FFIObject *)self, printable_filename, handle); }
static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg) { PyObject *m, *modules_dict; FFIObject *ffi; LibObject *lib; Py_ssize_t version; char *module_name, *exports, *module_name_with_lib; void **raw; const struct _cffi_type_context_s *ctx; raw = (void **)PyLong_AsVoidPtr(arg); if (raw == NULL) return NULL; module_name = (char *)raw[0]; version = (Py_ssize_t)raw[1]; exports = (char *)raw[2]; ctx = (const struct _cffi_type_context_s *)raw[3]; if (version < CFFI_VERSION_MIN || version > CFFI_VERSION_MAX) { if (!PyErr_Occurred()) PyErr_Format(PyExc_ImportError, "cffi extension module '%s' has unknown version %p", module_name, (void *)version); return NULL; } /* initialize the exports array */ memcpy(exports, (char *)cffi_exports, sizeof(cffi_exports)); /* make the module object */ m = _my_Py_InitModule(module_name); if (m == NULL) return NULL; /* build the FFI and Lib object inside this new module */ ffi = ffi_internal_new(&FFI_Type, ctx); Py_XINCREF(ffi); /* make the ffi object really immortal */ if (ffi == NULL || PyModule_AddObject(m, "ffi", (PyObject *)ffi) < 0) return NULL; lib = lib_internal_new(ffi, module_name, NULL); if (lib == NULL || PyModule_AddObject(m, "lib", (PyObject *)lib) < 0) return NULL; if (make_included_tuples(module_name, ctx->includes, &ffi->types_builder.included_ffis, &lib->l_types_builder->included_libs) < 0) return NULL; /* add manually 'module_name.lib' in sys.modules: see test_import_from_lib */ modules_dict = PySys_GetObject("modules"); if (!modules_dict) return NULL; module_name_with_lib = alloca(strlen(module_name) + 5); strcpy(module_name_with_lib, module_name); strcat(module_name_with_lib, ".lib"); if (PyDict_SetItemString(modules_dict, module_name_with_lib, (PyObject *)lib) < 0) return NULL; return m; }
static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg) { PyObject *m, *modules_dict; FFIObject *ffi; LibObject *lib; Py_ssize_t version, num_exports; char *module_name, *exports, *module_name_with_lib; void **raw; const struct _cffi_type_context_s *ctx; raw = (void **)PyLong_AsVoidPtr(arg); if (raw == NULL) return NULL; module_name = (char *)raw[0]; version = (Py_ssize_t)raw[1]; exports = (char *)raw[2]; ctx = (const struct _cffi_type_context_s *)raw[3]; if (version < CFFI_VERSION_MIN || version > CFFI_VERSION_MAX) { if (!PyErr_Occurred()) PyErr_Format(PyExc_ImportError, "cffi extension module '%s' uses an unknown version tag %p. " "This module might need a more recent version of cffi " "than the one currently installed, which is %s", module_name, (void *)version, CFFI_VERSION); return NULL; } /* initialize the exports array */ num_exports = 25; if (ctx->flags & 1) /* set to mean that 'extern "Python"' is used */ num_exports = 26; memcpy(exports, (char *)cffi_exports, num_exports * sizeof(void *)); /* make the module object */ m = _my_Py_InitModule(module_name); if (m == NULL) return NULL; /* build the FFI and Lib object inside this new module */ ffi = ffi_internal_new(&FFI_Type, ctx); Py_XINCREF(ffi); /* make the ffi object really immortal */ if (ffi == NULL || PyModule_AddObject(m, "ffi", (PyObject *)ffi) < 0) return NULL; lib = lib_internal_new(ffi, module_name, NULL); if (lib == NULL || PyModule_AddObject(m, "lib", (PyObject *)lib) < 0) return NULL; if (make_included_tuples(module_name, ctx->includes, &ffi->types_builder.included_ffis, &lib->l_types_builder->included_libs) < 0) return NULL; /* add manually 'module_name.lib' in sys.modules: see test_import_from_lib */ modules_dict = PySys_GetObject("modules"); if (!modules_dict) return NULL; module_name_with_lib = alloca(strlen(module_name) + 5); strcpy(module_name_with_lib, module_name); strcat(module_name_with_lib, ".lib"); if (PyDict_SetItemString(modules_dict, module_name_with_lib, (PyObject *)lib) < 0) return NULL; #if PY_MAJOR_VERSION >= 3 /* add manually 'module_name' in sys.modules: it seems that Py_InitModule() is not enough to do that */ if (PyDict_SetItemString(modules_dict, module_name, m) < 0) return NULL; #endif return m; }