2016-08-22 22:53:26 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include "tmplibrary.h"
|
|
|
|
|
|
|
|
#ifdef STANDALONE
|
|
|
|
# include <Python.h>
|
|
|
|
# include "Python-version.h"
|
|
|
|
# error "Shouldn't be here"
|
|
|
|
#else
|
|
|
|
# include "Python-dynload.h"
|
|
|
|
# include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "_memimporter.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
static char module_doc[] =
|
|
|
|
"Importer which can load extension modules from memory";
|
|
|
|
|
|
|
|
bool
|
2016-08-24 17:18:48 +00:00
|
|
|
import_module(const char *initfuncname, char *modname, const char *data, size_t size) {
|
2017-03-16 21:01:13 +00:00
|
|
|
char *oldcontext;
|
2016-08-22 22:53:26 +00:00
|
|
|
|
2017-03-16 21:01:13 +00:00
|
|
|
dprint("import_module: init=%s mod=%s (%p:%lu)\n",
|
|
|
|
initfuncname, modname, data, size);
|
2016-08-22 22:53:26 +00:00
|
|
|
|
2017-03-16 21:01:13 +00:00
|
|
|
void *hmem=memdlopen(modname, data, size);
|
|
|
|
if (!hmem) {
|
|
|
|
dprint("Couldn't load %s: %m\n", modname);
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-22 22:53:26 +00:00
|
|
|
|
2017-03-16 21:01:13 +00:00
|
|
|
void (*do_init)() = dlsym(hmem, initfuncname);
|
|
|
|
if (!do_init) {
|
|
|
|
dprint("Couldn't find sym %s in %s: %m\n", initfuncname, modname);
|
|
|
|
dlclose(hmem);
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-22 22:53:26 +00:00
|
|
|
|
|
|
|
oldcontext = _Py_PackageContext;
|
2017-03-16 21:01:13 +00:00
|
|
|
_Py_PackageContext = modname;
|
|
|
|
dprint("Call %s@%s\n", initfuncname, modname);
|
|
|
|
do_init();
|
|
|
|
_Py_PackageContext = oldcontext;
|
2016-08-22 22:53:26 +00:00
|
|
|
|
2017-03-16 21:01:13 +00:00
|
|
|
dprint("Call %s@%s - complete\n", initfuncname, modname);
|
2016-08-22 22:53:26 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
Py_import_module(PyObject *self, PyObject *args) {
|
2017-03-16 21:01:13 +00:00
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
char *initfuncname;
|
|
|
|
char *modname;
|
|
|
|
char *pathname;
|
|
|
|
|
|
|
|
/* code, initfuncname, fqmodulename, path */
|
|
|
|
if (!PyArg_ParseTuple(args, "s#sss:import_module",
|
|
|
|
&data, &size,
|
|
|
|
&initfuncname, &modname, &pathname)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-08-22 22:53:26 +00:00
|
|
|
|
|
|
|
dprint("DEBUG! %s@%s\n", initfuncname, modname);
|
|
|
|
|
2016-08-24 17:18:48 +00:00
|
|
|
if (!import_module(initfuncname, modname, data, size)) {
|
2017-03-16 21:01:13 +00:00
|
|
|
PyErr_Format(PyExc_ImportError,
|
|
|
|
"Could not find function %s", initfuncname);
|
|
|
|
return NULL;
|
2016-08-22 22:53:26 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:01:13 +00:00
|
|
|
/* Retrieve from sys.modules */
|
|
|
|
return PyImport_ImportModule(modname);
|
2016-08-22 22:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
get_verbose_flag(PyObject *self, PyObject *args)
|
|
|
|
{
|
2017-03-16 21:01:13 +00:00
|
|
|
return PyInt_FromLong(Py_VerboseFlag);
|
2016-08-22 22:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef methods[] = {
|
2017-03-16 21:01:13 +00:00
|
|
|
{ "import_module", Py_import_module, METH_VARARGS,
|
|
|
|
"import_module(data, size, initfuncname, path) -> module" },
|
|
|
|
{ "get_verbose_flag", get_verbose_flag, METH_NOARGS,
|
|
|
|
"Return the Py_Verbose flag" },
|
|
|
|
{ NULL, NULL }, /* Sentinel */
|
2016-08-22 22:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DL_EXPORT(void)
|
|
|
|
init_memimporter(void)
|
|
|
|
{
|
2017-03-16 21:01:13 +00:00
|
|
|
dprint("Importing... %p\n", Py_InitModule4);
|
|
|
|
Py_InitModule3("_memimporter", methods, module_doc);
|
2016-08-22 22:53:26 +00:00
|
|
|
}
|